@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.
- package/.turbo/turbo-lint.log +1 -1
- package/CHANGELOG.md +28 -0
- package/README.md +11 -11
- package/dist/all.d.ts +10 -5
- package/dist/all.d.ts.map +1 -1
- package/dist/all.js +78 -26
- package/dist/all.js.map +1 -1
- package/dist/assertions.d.ts +23 -0
- package/dist/assertions.d.ts.map +1 -1
- package/dist/assertions.js +154 -0
- package/dist/assertions.js.map +1 -1
- package/dist/context.d.ts +17 -16
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +31 -20
- package/dist/context.js.map +1 -1
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js +9 -5
- package/dist/contracts.js.map +1 -1
- package/dist/crosses.d.ts +4 -4
- package/dist/crosses.d.ts.map +1 -1
- package/dist/crosses.js +49 -39
- package/dist/crosses.js.map +1 -1
- package/dist/detours.d.ts +5 -4
- package/dist/detours.d.ts.map +1 -1
- package/dist/detours.js +93 -14
- package/dist/detours.js.map +1 -1
- package/dist/effective-examples.d.ts +30 -0
- package/dist/effective-examples.d.ts.map +1 -0
- package/dist/effective-examples.js +227 -0
- package/dist/effective-examples.js.map +1 -0
- package/dist/examples.d.ts +1 -1
- package/dist/examples.d.ts.map +1 -1
- package/dist/examples.js +79 -41
- package/dist/examples.js.map +1 -1
- package/dist/harness-cli.d.ts +3 -3
- package/dist/harness-cli.d.ts.map +1 -1
- package/dist/harness-cli.js +25 -33
- package/dist/harness-cli.js.map +1 -1
- package/dist/harness-mcp.d.ts +3 -3
- package/dist/harness-mcp.d.ts.map +1 -1
- package/dist/harness-mcp.js +9 -8
- package/dist/harness-mcp.js.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/scenario.d.ts +37 -0
- package/dist/scenario.d.ts.map +1 -0
- package/dist/scenario.js +235 -0
- package/dist/scenario.js.map +1 -0
- package/dist/types.d.ts +38 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -5
- package/src/__tests__/all.test.ts +217 -29
- package/src/__tests__/context.test.ts +32 -12
- package/src/__tests__/contracts.test.ts +72 -18
- package/src/__tests__/crosses.test.ts +78 -78
- package/src/__tests__/detours.test.ts +176 -19
- package/src/__tests__/effective-examples.test.ts +203 -0
- package/src/__tests__/examples.test.ts +152 -50
- package/src/__tests__/harness-cli.test.ts +90 -0
- package/src/__tests__/harness-mcp.test.ts +37 -0
- package/src/__tests__/partial-match.test.ts +126 -0
- package/src/__tests__/scenario.test.ts +381 -0
- package/src/all.ts +149 -12
- package/src/assertions.ts +253 -0
- package/src/context.ts +64 -38
- package/src/contracts.ts +14 -8
- package/src/crosses.ts +93 -51
- package/src/detours.ts +155 -18
- package/src/effective-examples.ts +350 -0
- package/src/examples.ts +127 -59
- package/src/harness-cli.ts +33 -49
- package/src/harness-mcp.ts +9 -8
- package/src/index.ts +13 -3
- package/src/scenario.ts +370 -0
- package/src/types.ts +63 -5
- package/tsconfig.tests.json +10 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/follows.d.ts +0 -38
- package/dist/follows.d.ts.map +0 -1
- package/dist/follows.js +0 -212
- 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
|
-
|
|
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 {
|
|
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
|
|
42
|
+
const collectDeclaredResources = (
|
|
42
43
|
trailDef: AnyTrail,
|
|
43
44
|
trailsMap: ReadonlyMap<string, AnyTrail> | undefined
|
|
44
|
-
): AnyTrail['
|
|
45
|
-
const
|
|
45
|
+
): AnyTrail['resources'] => {
|
|
46
|
+
const seenResourceIds = new Set<string>();
|
|
46
47
|
const seenTrailIds = new Set<string>();
|
|
47
|
-
const
|
|
48
|
+
const resources: AnyTrail['resources'][number][] = [];
|
|
48
49
|
|
|
49
50
|
const collect = (candidate: AnyTrail): void => {
|
|
50
|
-
for (const
|
|
51
|
-
if (
|
|
51
|
+
for (const declaredResource of candidate.resources) {
|
|
52
|
+
if (seenResourceIds.has(declaredResource.id)) {
|
|
52
53
|
continue;
|
|
53
54
|
}
|
|
54
|
-
|
|
55
|
-
|
|
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
|
|
75
|
+
return resources;
|
|
75
76
|
};
|
|
76
77
|
|
|
77
|
-
const
|
|
78
|
+
const resolveCrossMockResources = async (
|
|
78
79
|
trailDef: AnyTrail,
|
|
79
80
|
trailsMap: ReadonlyMap<string, AnyTrail> | undefined
|
|
80
|
-
): Promise<
|
|
81
|
-
const
|
|
81
|
+
): Promise<ResourceOverrideMap> => {
|
|
82
|
+
const resources: Record<string, unknown> = {};
|
|
82
83
|
|
|
83
|
-
for (const
|
|
84
|
+
for (const declaredResource of collectDeclaredResources(
|
|
84
85
|
trailDef,
|
|
85
86
|
trailsMap
|
|
86
87
|
)) {
|
|
87
|
-
if (!
|
|
88
|
+
if (!declaredResource.mock) {
|
|
88
89
|
continue;
|
|
89
90
|
}
|
|
90
|
-
|
|
91
|
+
resources[declaredResource.id] = await declaredResource.mock();
|
|
91
92
|
}
|
|
92
93
|
|
|
93
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
216
|
+
return injected;
|
|
192
217
|
}
|
|
193
218
|
|
|
194
|
-
|
|
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
|
-
|
|
204
|
-
|
|
225
|
+
resources,
|
|
226
|
+
self
|
|
205
227
|
);
|
|
206
|
-
|
|
207
|
-
|
|
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
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
356
|
+
resources
|
|
315
357
|
);
|
|
316
358
|
const result = await executeTrail(trailDef, scenario.input, {
|
|
317
359
|
ctx: testCtx,
|
|
318
|
-
|
|
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
|
|
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
|
|
377
|
+
* use `mock()` on the resource definition to get a fresh instance per run.
|
|
336
378
|
*/
|
|
337
|
-
readonly
|
|
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
|
|
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
|
|
369
|
-
await
|
|
410
|
+
const resources = mergeResourceOverrides(
|
|
411
|
+
await resolveCrossMockResources(trailDef, options?.trails),
|
|
370
412
|
options?.ctx,
|
|
371
|
-
|
|
413
|
+
explicitResources
|
|
372
414
|
);
|
|
373
415
|
await runScenario(
|
|
374
416
|
trailDef,
|
|
375
417
|
scenario,
|
|
376
418
|
options?.ctx,
|
|
377
419
|
options?.trails,
|
|
378
|
-
|
|
420
|
+
resources
|
|
379
421
|
);
|
|
380
422
|
}
|
|
381
423
|
);
|
package/src/detours.ts
CHANGED
|
@@ -1,42 +1,179 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* testDetours —
|
|
2
|
+
* testDetours — validate the live detour contract for every trail.
|
|
3
3
|
*
|
|
4
|
-
* Pure structural validation. No
|
|
4
|
+
* Pure structural validation. No blaze or detour recovery execution needed.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { describe,
|
|
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
|
-
|
|
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
|
|
17
|
-
*
|
|
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
|
|
147
|
+
const trail = trailDef as Trail<unknown, unknown, unknown>;
|
|
25
148
|
|
|
26
|
-
if (
|
|
149
|
+
if (trail.detours.length === 0) {
|
|
27
150
|
return;
|
|
28
151
|
}
|
|
29
152
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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(
|
|
37
|
-
'detour
|
|
38
|
-
({
|
|
39
|
-
|
|
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
|
});
|