@ontrails/testing 1.0.0-beta.13 → 1.0.0-beta.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/.turbo/turbo-lint.log +1 -1
  2. package/CHANGELOG.md +28 -0
  3. package/README.md +11 -11
  4. package/dist/all.d.ts +10 -5
  5. package/dist/all.d.ts.map +1 -1
  6. package/dist/all.js +78 -26
  7. package/dist/all.js.map +1 -1
  8. package/dist/assertions.d.ts +23 -0
  9. package/dist/assertions.d.ts.map +1 -1
  10. package/dist/assertions.js +154 -0
  11. package/dist/assertions.js.map +1 -1
  12. package/dist/context.d.ts +17 -16
  13. package/dist/context.d.ts.map +1 -1
  14. package/dist/context.js +31 -20
  15. package/dist/context.js.map +1 -1
  16. package/dist/contracts.d.ts.map +1 -1
  17. package/dist/contracts.js +9 -5
  18. package/dist/contracts.js.map +1 -1
  19. package/dist/crosses.d.ts +4 -4
  20. package/dist/crosses.d.ts.map +1 -1
  21. package/dist/crosses.js +49 -39
  22. package/dist/crosses.js.map +1 -1
  23. package/dist/detours.d.ts +5 -4
  24. package/dist/detours.d.ts.map +1 -1
  25. package/dist/detours.js +93 -14
  26. package/dist/detours.js.map +1 -1
  27. package/dist/effective-examples.d.ts +30 -0
  28. package/dist/effective-examples.d.ts.map +1 -0
  29. package/dist/effective-examples.js +227 -0
  30. package/dist/effective-examples.js.map +1 -0
  31. package/dist/examples.d.ts +1 -1
  32. package/dist/examples.d.ts.map +1 -1
  33. package/dist/examples.js +79 -41
  34. package/dist/examples.js.map +1 -1
  35. package/dist/harness-cli.d.ts +3 -3
  36. package/dist/harness-cli.d.ts.map +1 -1
  37. package/dist/harness-cli.js +25 -33
  38. package/dist/harness-cli.js.map +1 -1
  39. package/dist/harness-mcp.d.ts +3 -3
  40. package/dist/harness-mcp.d.ts.map +1 -1
  41. package/dist/harness-mcp.js +9 -8
  42. package/dist/harness-mcp.js.map +1 -1
  43. package/dist/index.d.ts +6 -4
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +5 -2
  46. package/dist/index.js.map +1 -1
  47. package/dist/scenario.d.ts +37 -0
  48. package/dist/scenario.d.ts.map +1 -0
  49. package/dist/scenario.js +235 -0
  50. package/dist/scenario.js.map +1 -0
  51. package/dist/types.d.ts +38 -5
  52. package/dist/types.d.ts.map +1 -1
  53. package/package.json +9 -5
  54. package/src/__tests__/all.test.ts +217 -29
  55. package/src/__tests__/context.test.ts +32 -12
  56. package/src/__tests__/contracts.test.ts +72 -18
  57. package/src/__tests__/crosses.test.ts +78 -78
  58. package/src/__tests__/detours.test.ts +176 -19
  59. package/src/__tests__/effective-examples.test.ts +203 -0
  60. package/src/__tests__/examples.test.ts +152 -50
  61. package/src/__tests__/harness-cli.test.ts +90 -0
  62. package/src/__tests__/harness-mcp.test.ts +37 -0
  63. package/src/__tests__/partial-match.test.ts +126 -0
  64. package/src/__tests__/scenario.test.ts +381 -0
  65. package/src/all.ts +149 -12
  66. package/src/assertions.ts +253 -0
  67. package/src/context.ts +64 -38
  68. package/src/contracts.ts +14 -8
  69. package/src/crosses.ts +93 -51
  70. package/src/detours.ts +155 -18
  71. package/src/effective-examples.ts +350 -0
  72. package/src/examples.ts +127 -59
  73. package/src/harness-cli.ts +33 -49
  74. package/src/harness-mcp.ts +9 -8
  75. package/src/index.ts +13 -3
  76. package/src/scenario.ts +370 -0
  77. package/src/types.ts +63 -5
  78. package/tsconfig.tests.json +10 -0
  79. package/tsconfig.tsbuildinfo +1 -1
  80. package/dist/follows.d.ts +0 -38
  81. package/dist/follows.d.ts.map +0 -1
  82. package/dist/follows.js +0 -212
  83. package/dist/follows.js.map +0 -1
package/src/crosses.ts CHANGED
@@ -10,10 +10,11 @@ import { describe, expect, test } from 'bun:test';
10
10
  import type {
11
11
  AnyTrail,
12
12
  CrossFn,
13
- ProvisionOverrideMap,
13
+ ResourceOverrideMap,
14
14
  TrailContext,
15
15
  } from '@ontrails/core';
16
16
  import {
17
+ buildCrossValidationSchema,
17
18
  executeTrail,
18
19
  InternalError,
19
20
  Result,
@@ -26,7 +27,7 @@ import {
26
27
  assertFullMatch,
27
28
  assertSchemaMatch,
28
29
  } from './assertions.js';
29
- import { mergeProvisionOverrides, mergeTestContext } from './context.js';
30
+ import { mergeResourceOverrides, mergeTestContext } from './context.js';
30
31
  import type { CrossScenario } from './types.js';
31
32
 
32
33
  // ---------------------------------------------------------------------------
@@ -38,21 +39,21 @@ interface CrossRecord {
38
39
  readonly input: unknown;
39
40
  }
40
41
 
41
- const collectDeclaredProvisions = (
42
+ const collectDeclaredResources = (
42
43
  trailDef: AnyTrail,
43
44
  trailsMap: ReadonlyMap<string, AnyTrail> | undefined
44
- ): AnyTrail['provisions'] => {
45
- const seenProvisionIds = new Set<string>();
45
+ ): AnyTrail['resources'] => {
46
+ const seenResourceIds = new Set<string>();
46
47
  const seenTrailIds = new Set<string>();
47
- const provisions: AnyTrail['provisions'][number][] = [];
48
+ const resources: AnyTrail['resources'][number][] = [];
48
49
 
49
50
  const collect = (candidate: AnyTrail): void => {
50
- for (const declaredProvision of candidate.provisions) {
51
- if (seenProvisionIds.has(declaredProvision.id)) {
51
+ for (const declaredResource of candidate.resources) {
52
+ if (seenResourceIds.has(declaredResource.id)) {
52
53
  continue;
53
54
  }
54
- seenProvisionIds.add(declaredProvision.id);
55
- provisions.push(declaredProvision);
55
+ seenResourceIds.add(declaredResource.id);
56
+ resources.push(declaredResource);
56
57
  }
57
58
  };
58
59
 
@@ -71,26 +72,26 @@ const collectDeclaredProvisions = (
71
72
  };
72
73
 
73
74
  visit(trailDef);
74
- return provisions;
75
+ return resources;
75
76
  };
76
77
 
77
- const resolveCrossMockProvisions = async (
78
+ const resolveCrossMockResources = async (
78
79
  trailDef: AnyTrail,
79
80
  trailsMap: ReadonlyMap<string, AnyTrail> | undefined
80
- ): Promise<ProvisionOverrideMap> => {
81
- const provisions: Record<string, unknown> = {};
81
+ ): Promise<ResourceOverrideMap> => {
82
+ const resources: Record<string, unknown> = {};
82
83
 
83
- for (const declaredProvision of collectDeclaredProvisions(
84
+ for (const declaredResource of collectDeclaredResources(
84
85
  trailDef,
85
86
  trailsMap
86
87
  )) {
87
- if (!declaredProvision.mock) {
88
+ if (!declaredResource.mock) {
88
89
  continue;
89
90
  }
90
- provisions[declaredProvision.id] = await declaredProvision.mock();
91
+ resources[declaredResource.id] = await declaredResource.mock();
91
92
  }
92
93
 
93
- return provisions;
94
+ return resources;
94
95
  };
95
96
 
96
97
  // ---------------------------------------------------------------------------
@@ -143,15 +144,12 @@ const tryInjectError = (
143
144
  return Result.err(new Error(errorName));
144
145
  };
145
146
 
146
- /**
147
- * Execute a trail from the map, validating input first.
148
- */
149
147
  const executeFromMap = (
150
148
  id: string,
151
149
  input: unknown,
152
150
  trailsMap: ReadonlyMap<string, AnyTrail> | undefined,
153
151
  ctx: TrailContext,
154
- provisions: ProvisionOverrideMap | undefined,
152
+ resources: ResourceOverrideMap | undefined,
155
153
  cross?: CrossFn
156
154
  ): Result<unknown, Error> | Promise<Result<unknown, Error>> | undefined => {
157
155
  const trailDef = trailsMap?.get(id);
@@ -162,14 +160,36 @@ const executeFromMap = (
162
160
  const nestedCtx = cross ? { ...ctx, cross } : ctx;
163
161
  return executeTrail(trailDef, input, {
164
162
  ctx: nestedCtx,
165
- provisions,
163
+ resources,
164
+ validationSchema: buildCrossValidationSchema(trailDef),
166
165
  });
167
166
  };
168
167
 
168
+ /** Extract trail ID from either a trail object or a string. */
169
+ const resolveCrossId = (idOrTrail: string | { readonly id: string }): string =>
170
+ typeof idOrTrail === 'string' ? idOrTrail : idOrTrail.id;
171
+
169
172
  // ---------------------------------------------------------------------------
170
173
  // Cross factory
171
174
  // ---------------------------------------------------------------------------
172
175
 
176
+ /** Delegate to baseCross, executeFromMap, or fall back to Result.ok(). */
177
+ const delegateCross = (
178
+ id: string,
179
+ input: unknown,
180
+ baseCross: CrossFn | undefined,
181
+ trailsMap: ReadonlyMap<string, AnyTrail> | undefined,
182
+ ctx: TrailContext,
183
+ resources: ResourceOverrideMap | undefined,
184
+ self: CrossFn
185
+ ): Promise<Result<unknown, Error>> => {
186
+ if (baseCross !== undefined) {
187
+ return baseCross(id, input);
188
+ }
189
+ const executed = executeFromMap(id, input, trailsMap, ctx, resources, self);
190
+ return Promise.resolve(executed ?? Result.ok());
191
+ };
192
+
173
193
  /**
174
194
  * Build a recording cross function that optionally injects errors.
175
195
  */
@@ -179,37 +199,59 @@ const createRecordingCross = (
179
199
  trailsMap: ReadonlyMap<string, AnyTrail> | undefined,
180
200
  baseCross: CrossFn | undefined,
181
201
  ctx: TrailContext,
182
- provisions: ProvisionOverrideMap | undefined
202
+ resources: ResourceOverrideMap | undefined
183
203
  ): CrossFn => {
184
204
  // The generic O on CrossFn is erased at runtime; the cast is safe
185
205
  // because callers narrow via isOk/isErr before accessing the value.
186
- const cross = (id: string, input: unknown) => {
206
+ const invokeCross = async (
207
+ idOrTrail: string | { readonly id: string },
208
+ input: unknown,
209
+ self: CrossFn
210
+ ) => {
211
+ const id = resolveCrossId(idOrTrail);
187
212
  trace.push({ id, input });
188
213
 
189
214
  const injected = tryInjectError(id, scenario, trailsMap);
190
215
  if (injected !== undefined) {
191
- return Promise.resolve(injected);
216
+ return injected;
192
217
  }
193
218
 
194
- if (baseCross !== undefined) {
195
- return baseCross(id, input);
196
- }
197
-
198
- const executed = executeFromMap(
219
+ return await delegateCross(
199
220
  id,
200
221
  input,
222
+ baseCross,
201
223
  trailsMap,
202
224
  ctx,
203
- provisions,
204
- cross as CrossFn
225
+ resources,
226
+ self
205
227
  );
206
- if (executed !== undefined) {
207
- return Promise.resolve(executed);
228
+ };
229
+
230
+ // Accepts either a trail object (typed cross), a string id (untyped),
231
+ // or a batch of `[target, input]` tuples.
232
+ const cross = async function cross(
233
+ idOrTrail:
234
+ | string
235
+ | { readonly id: string }
236
+ | readonly (readonly [string | { readonly id: string }, unknown])[],
237
+ input?: unknown
238
+ ) {
239
+ if (Array.isArray(idOrTrail)) {
240
+ return await Promise.all(
241
+ idOrTrail.map(([target, batchInput]) =>
242
+ invokeCross(target, batchInput, cross as CrossFn)
243
+ )
244
+ );
208
245
  }
209
246
 
210
- return Promise.resolve(Result.ok());
211
- };
212
- return cross as CrossFn;
247
+ return await invokeCross(
248
+ idOrTrail as string | { readonly id: string },
249
+ input,
250
+ cross as CrossFn
251
+ );
252
+ } as CrossFn;
253
+
254
+ return cross;
213
255
  };
214
256
 
215
257
  // ---------------------------------------------------------------------------
@@ -280,7 +322,7 @@ const buildTestContext = (
280
322
  scenario: CrossScenario,
281
323
  ctx: Partial<TrailContext> | undefined,
282
324
  trailsMap: ReadonlyMap<string, AnyTrail> | undefined,
283
- provisions: ProvisionOverrideMap | undefined
325
+ resources: ResourceOverrideMap | undefined
284
326
  ): { trace: CrossRecord[]; testCtx: TrailContext } => {
285
327
  const trace: CrossRecord[] = [];
286
328
  const baseCtx = mergeTestContext(ctx);
@@ -290,7 +332,7 @@ const buildTestContext = (
290
332
  trailsMap,
291
333
  baseCtx.cross,
292
334
  baseCtx,
293
- provisions
335
+ resources
294
336
  );
295
337
  return { testCtx: { ...baseCtx, cross }, trace };
296
338
  };
@@ -300,7 +342,7 @@ const runScenario = async (
300
342
  scenario: CrossScenario,
301
343
  ctx: Partial<TrailContext> | undefined,
302
344
  trailsMap: ReadonlyMap<string, AnyTrail> | undefined,
303
- provisions: ProvisionOverrideMap | undefined
345
+ resources: ResourceOverrideMap | undefined
304
346
  ): Promise<void> => {
305
347
  const validated = validateInput(trailDef.input, scenario.input);
306
348
  if (handleValidationError(validated, scenario)) {
@@ -311,11 +353,11 @@ const runScenario = async (
311
353
  scenario,
312
354
  ctx,
313
355
  trailsMap,
314
- provisions
356
+ resources
315
357
  );
316
358
  const result = await executeTrail(trailDef, scenario.input, {
317
359
  ctx: testCtx,
318
- provisions,
360
+ resources,
319
361
  });
320
362
  assertCrossTrace(trace, scenario);
321
363
  assertScenarioResult(result, scenario, trailDef);
@@ -330,11 +372,11 @@ export interface TestCrossOptions {
330
372
  /** Partial context overrides. */
331
373
  readonly ctx?: Partial<TrailContext> | undefined;
332
374
  /**
333
- * Explicit provision overrides merged on top of auto-resolved mocks for every
375
+ * Explicit resource overrides merged on top of auto-resolved mocks for every
334
376
  * scenario. Values are passed by reference — provide immutable objects, or
335
- * use `mock()` on the provision definition to get a fresh instance per run.
377
+ * use `mock()` on the resource definition to get a fresh instance per run.
336
378
  */
337
- readonly provisions?: ProvisionOverrideMap | undefined;
379
+ readonly resources?: ResourceOverrideMap | undefined;
338
380
  /** Map of trail ID to trail definition, used for injectFromExample. */
339
381
  readonly trails?: ReadonlyMap<string, AnyTrail> | undefined;
340
382
  }
@@ -359,23 +401,23 @@ export const testCrosses = (
359
401
  scenarios: readonly CrossScenario[],
360
402
  options?: TestCrossOptions
361
403
  ): void => {
362
- const explicitProvisions = options?.provisions;
404
+ const explicitResources = options?.resources;
363
405
 
364
406
  describe(trailDef.id, () => {
365
407
  test.each([...scenarios])(
366
408
  '$description',
367
409
  async (scenario: CrossScenario) => {
368
- const provisions = mergeProvisionOverrides(
369
- await resolveCrossMockProvisions(trailDef, options?.trails),
410
+ const resources = mergeResourceOverrides(
411
+ await resolveCrossMockResources(trailDef, options?.trails),
370
412
  options?.ctx,
371
- explicitProvisions
413
+ explicitResources
372
414
  );
373
415
  await runScenario(
374
416
  trailDef,
375
417
  scenario,
376
418
  options?.ctx,
377
419
  options?.trails,
378
- provisions
420
+ resources
379
421
  );
380
422
  }
381
423
  );
package/src/detours.ts CHANGED
@@ -1,42 +1,179 @@
1
1
  /**
2
- * testDetours — verify that all detour targets exist in the topo.
2
+ * testDetours — validate the live detour contract for every trail.
3
3
  *
4
- * Pure structural validation. No implementation execution needed.
4
+ * Pure structural validation. No blaze or detour recovery execution needed.
5
5
  */
6
6
 
7
- import { describe, expect, test } from 'bun:test';
7
+ import { describe, test } from 'bun:test';
8
8
 
9
+ import { TrailsError } from '@ontrails/core';
9
10
  import type { Topo, Trail } from '@ontrails/core';
10
11
 
11
- // ---------------------------------------------------------------------------
12
- // testDetours
13
- // ---------------------------------------------------------------------------
12
+ interface RuntimeDetour {
13
+ readonly on?: unknown;
14
+ readonly recover?: unknown;
15
+ }
16
+
17
+ const isErrorConstructor = (
18
+ value: unknown
19
+ ): value is abstract new (...args: never[]) => Error => {
20
+ if (typeof value !== 'function') {
21
+ return false;
22
+ }
23
+
24
+ const { prototype } = value as { prototype?: unknown };
25
+ return prototype instanceof Error;
26
+ };
27
+
28
+ const detourLabel = (trailId: string, index: number, detour: RuntimeDetour) => {
29
+ if (typeof detour.on === 'function') {
30
+ const { name } = detour.on as { name?: unknown };
31
+ if (typeof name === 'string' && name.length > 0) {
32
+ return `${trailId} detour[${index}] on ${name}`;
33
+ }
34
+ }
35
+
36
+ return `${trailId} detour[${index}]`;
37
+ };
38
+
39
+ const assertValidOn = (
40
+ trailId: string,
41
+ index: number,
42
+ detour: RuntimeDetour
43
+ ): void => {
44
+ if (isErrorConstructor(detour.on)) {
45
+ return;
46
+ }
47
+
48
+ throw new Error(
49
+ `${detourLabel(trailId, index, detour)} must declare a real error constructor in on:`
50
+ );
51
+ };
52
+
53
+ const assertCallableRecover = (
54
+ trailId: string,
55
+ index: number,
56
+ detour: RuntimeDetour
57
+ ): void => {
58
+ if (typeof detour.recover === 'function') {
59
+ return;
60
+ }
61
+
62
+ throw new Error(
63
+ `${detourLabel(trailId, index, detour)} must declare a callable recover function`
64
+ );
65
+ };
66
+
67
+ const sameOrSubtype = (
68
+ candidate: abstract new (...args: never[]) => Error,
69
+ ancestor: abstract new (...args: never[]) => Error
70
+ ): boolean => {
71
+ if (candidate === ancestor) {
72
+ return true;
73
+ }
74
+
75
+ let current = Object.getPrototypeOf(candidate.prototype);
76
+ while (current && typeof current === 'object') {
77
+ const ctor = (current as { constructor?: unknown }).constructor;
78
+ if (ctor === ancestor) {
79
+ return true;
80
+ }
81
+ current = Object.getPrototypeOf(current);
82
+ }
83
+
84
+ return false;
85
+ };
86
+
87
+ const getShadowingDetour = (
88
+ detours: readonly RuntimeDetour[],
89
+ index: number
90
+ ):
91
+ | {
92
+ readonly index: number;
93
+ readonly on: abstract new (...args: never[]) => Error;
94
+ }
95
+ | undefined => {
96
+ const detour = detours[index];
97
+ if (!detour || !isErrorConstructor(detour.on)) {
98
+ return undefined;
99
+ }
100
+
101
+ for (let previousIndex = 0; previousIndex < index; previousIndex += 1) {
102
+ const previous = detours[previousIndex];
103
+ if (!previous || !isErrorConstructor(previous.on)) {
104
+ continue;
105
+ }
106
+
107
+ if (sameOrSubtype(detour.on, previous.on)) {
108
+ return { index: previousIndex, on: previous.on };
109
+ }
110
+ }
111
+
112
+ return undefined;
113
+ };
114
+
115
+ const assertNotShadowed = (
116
+ trailId: string,
117
+ detours: readonly RuntimeDetour[],
118
+ index: number
119
+ ): void => {
120
+ const detour = detours[index];
121
+ if (!detour || !isErrorConstructor(detour.on)) {
122
+ return;
123
+ }
124
+
125
+ const shadowing = getShadowingDetour(detours, index);
126
+ if (!shadowing) {
127
+ return;
128
+ }
129
+
130
+ const previousName = shadowing.on.name || TrailsError.name;
131
+ const currentName = detour.on.name || TrailsError.name;
132
+ throw new Error(
133
+ `${trailId} detour[${index}] on ${currentName} is shadowed by earlier detour[${shadowing.index}] on ${previousName}`
134
+ );
135
+ };
14
136
 
15
137
  /**
16
- * Verify that every trail's detour targets reference trails that
17
- * actually exist in the app's topo.
138
+ * Verify that every trail's detours match the live runtime contract:
139
+ * `on` must be an error constructor, `recover` must be callable, and
140
+ * later detours must not be shadowed by earlier broader `on` types.
18
141
  */
19
142
  export const testDetours = (app: Topo): void => {
20
143
  const trailEntries = [...app.trails];
21
144
 
22
145
  describe('detours', () => {
23
146
  describe.each(trailEntries)('%s', (_id, trailDef) => {
24
- const t = trailDef as Trail<unknown, unknown>;
147
+ const trail = trailDef as Trail<unknown, unknown, unknown>;
25
148
 
26
- if (t.detours === undefined) {
149
+ if (trail.detours.length === 0) {
27
150
  return;
28
151
  }
29
152
 
30
- const { detours } = t;
31
- const testCases = Object.entries(detours).flatMap(
32
- ([detourName, targets]) =>
33
- targets.map((targetId) => ({ detourName, targetId }))
153
+ const detourCases = trail.detours.map((detour, index) => ({
154
+ detour,
155
+ index,
156
+ trailId: trail.id,
157
+ }));
158
+
159
+ test.each(detourCases)(
160
+ '$trailId detour[$index] uses an error constructor',
161
+ ({ detour, index, trailId }) => {
162
+ assertValidOn(trailId, index, detour);
163
+ }
164
+ );
165
+
166
+ test.each(detourCases)(
167
+ '$trailId detour[$index] provides a callable recover',
168
+ ({ detour, index, trailId }) => {
169
+ assertCallableRecover(trailId, index, detour);
170
+ }
34
171
  );
35
172
 
36
- test.each(testCases)(
37
- 'detour "$detourName" -> "$targetId" exists',
38
- ({ targetId }) => {
39
- expect(app.has(targetId)).toBe(true);
173
+ test.each(detourCases)(
174
+ '$trailId detour[$index] is not shadowed by an earlier detour',
175
+ ({ index, trailId }) => {
176
+ assertNotShadowed(trailId, trail.detours, index);
40
177
  }
41
178
  );
42
179
  });