@ontrails/testing 1.0.0-beta.10 → 1.0.0-beta.11

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.
@@ -1,7 +1,7 @@
1
1
  import { describe } from 'bun:test';
2
2
 
3
3
  import type { AnyTrail, TrailContext } from '@ontrails/core';
4
- import { Result, trail } from '@ontrails/core';
4
+ import { InternalError, Result, service, trail } from '@ontrails/core';
5
5
  import { z } from 'zod';
6
6
 
7
7
  import { testFollows } from '../follows.js';
@@ -161,3 +161,429 @@ describe('testFollows: expectValue', () => {
161
161
  opts
162
162
  );
163
163
  });
164
+
165
+ // ---------------------------------------------------------------------------
166
+ // Nested follow chain: A → B → C
167
+ // ---------------------------------------------------------------------------
168
+
169
+ const leafTrail = trail('step.leaf', {
170
+ description: 'Leaf trail in a nested chain',
171
+ input: z.object({ value: z.string() }),
172
+ output: z.object({ leaf: z.string() }),
173
+ run: (input: { value: string }) => Result.ok({ leaf: input.value }),
174
+ });
175
+
176
+ const middleTrail = trail('step.middle', {
177
+ description: 'Middle trail that follows the leaf',
178
+ follow: ['step.leaf'],
179
+ input: z.object({ value: z.string() }),
180
+ output: z.object({ middle: z.string() }),
181
+ run: async (input: { value: string }, ctx: TrailContext) => {
182
+ if (!ctx.follow) {
183
+ return Result.err(new Error('follow not available'));
184
+ }
185
+ const leafResult = await ctx.follow<{ leaf: string }>('step.leaf', input);
186
+ if (leafResult.isErr()) {
187
+ return leafResult;
188
+ }
189
+ return Result.ok({ middle: leafResult.value.leaf });
190
+ },
191
+ });
192
+
193
+ const nestedRootTrail = trail('step.root', {
194
+ description: 'Root trail that follows the middle trail',
195
+ follow: ['step.middle'],
196
+ input: z.object({ value: z.string() }),
197
+ output: z.object({ root: z.string() }),
198
+ run: async (input: { value: string }, ctx: TrailContext) => {
199
+ if (!ctx.follow) {
200
+ return Result.err(new Error('follow not available'));
201
+ }
202
+ const midResult = await ctx.follow<{ middle: string }>(
203
+ 'step.middle',
204
+ input
205
+ );
206
+ if (midResult.isErr()) {
207
+ return midResult;
208
+ }
209
+ return Result.ok({ root: midResult.value.middle });
210
+ },
211
+ });
212
+
213
+ const nestedTrailsMap = new Map<string, AnyTrail>([
214
+ ['step.leaf', leafTrail],
215
+ ['step.middle', middleTrail],
216
+ ]);
217
+
218
+ describe('testFollows: nested follow chain (A → B → C)', () => {
219
+ // eslint-disable-next-line jest/require-hook
220
+ testFollows(
221
+ nestedRootTrail,
222
+ [
223
+ {
224
+ description: 'nested ctx.follow works through A → B → C',
225
+ expectOk: true,
226
+ expectValue: { root: 'hello' },
227
+ input: { value: 'hello' },
228
+ },
229
+ ],
230
+ { trails: nestedTrailsMap }
231
+ );
232
+ });
233
+
234
+ const mockDbService = service('db.mock.follows', {
235
+ create: () => Result.ok({ source: 'factory' }),
236
+ mock: () => ({ source: 'mock' }),
237
+ });
238
+
239
+ const serviceLeafTrail = trail('service.leaf', {
240
+ description: 'Leaf trail that reads from a service',
241
+ input: z.object({}),
242
+ output: z.object({ childSource: z.string() }),
243
+ run: (_input, ctx) =>
244
+ Result.ok({ childSource: mockDbService.from(ctx).source }),
245
+ services: [mockDbService],
246
+ });
247
+
248
+ const serviceRootTrail = trail('service.root', {
249
+ description: 'Root trail that reads from a service and follows a child trail',
250
+ follow: ['service.leaf'],
251
+ input: z.object({}),
252
+ output: z.object({ childSource: z.string(), rootSource: z.string() }),
253
+ run: async (_input, ctx: TrailContext) => {
254
+ if (!ctx.follow) {
255
+ return Result.err(new Error('follow not available'));
256
+ }
257
+ const childResult = await ctx.follow<{ childSource: string }>(
258
+ 'service.leaf',
259
+ {}
260
+ );
261
+ if (childResult.isErr()) {
262
+ return childResult;
263
+ }
264
+
265
+ return Result.ok({
266
+ childSource: childResult.value.childSource,
267
+ rootSource: mockDbService.from(ctx).source,
268
+ });
269
+ },
270
+ services: [mockDbService],
271
+ });
272
+
273
+ const serviceTrailsMap = new Map<string, AnyTrail>([
274
+ ['service.leaf', serviceLeafTrail],
275
+ ]);
276
+
277
+ const statefulMockDbService = service('db.mock.follows.stateful', {
278
+ create: () => Result.ok({ count: 0 }),
279
+ mock: () => ({ count: 0 }),
280
+ });
281
+
282
+ const statefulServiceLeafTrail = trail('service.stateful.leaf', {
283
+ description: 'Leaf trail that observes the current mock service state',
284
+ input: z.object({}),
285
+ output: z.object({ childCount: z.number() }),
286
+ run: (_input, ctx) =>
287
+ Result.ok({ childCount: statefulMockDbService.from(ctx).count }),
288
+ services: [statefulMockDbService],
289
+ });
290
+
291
+ const statefulServiceRootTrail = trail('service.stateful.root', {
292
+ description:
293
+ 'Root trail that mutates a mocked service and follows a child trail',
294
+ follow: ['service.stateful.leaf'],
295
+ input: z.object({}),
296
+ output: z.object({ childCount: z.number(), rootCount: z.number() }),
297
+ run: async (_input, ctx: TrailContext) => {
298
+ if (!ctx.follow) {
299
+ return Result.err(new Error('follow not available'));
300
+ }
301
+
302
+ const statefulService = statefulMockDbService.from(ctx);
303
+ statefulService.count += 1;
304
+
305
+ const childResult = await ctx.follow<{ childCount: number }>(
306
+ 'service.stateful.leaf',
307
+ {}
308
+ );
309
+ if (childResult.isErr()) {
310
+ return childResult;
311
+ }
312
+
313
+ return Result.ok({
314
+ childCount: childResult.value.childCount,
315
+ rootCount: statefulService.count,
316
+ });
317
+ },
318
+ services: [statefulMockDbService],
319
+ });
320
+
321
+ const statefulServiceTrailsMap = new Map<string, AnyTrail>([
322
+ ['service.stateful.leaf', statefulServiceLeafTrail],
323
+ ]);
324
+
325
+ const scenarioStateService = service('db.mock.scenarios', {
326
+ create: () => Result.ok({ count: 0 }),
327
+ mock: () => ({ count: 0 }),
328
+ });
329
+
330
+ const scenarioLeafTrail = trail('service.scenario.leaf', {
331
+ description: 'Leaf trail that reads mutable scenario state',
332
+ input: z.object({}),
333
+ output: z.object({ count: z.number() }),
334
+ run: (_input, ctx) =>
335
+ Result.ok({ count: scenarioStateService.from(ctx).count }),
336
+ services: [scenarioStateService],
337
+ });
338
+
339
+ const scenarioRootTrail = trail('service.scenario.root', {
340
+ description: 'Root trail that mutates scenario state and follows a leaf',
341
+ follow: ['service.scenario.leaf'],
342
+ input: z.object({}),
343
+ output: z.object({ count: z.number() }),
344
+ run: async (_input, ctx: TrailContext) => {
345
+ if (!ctx.follow) {
346
+ return Result.err(new Error('follow not available'));
347
+ }
348
+
349
+ const state = scenarioStateService.from(ctx);
350
+ state.count += 1;
351
+
352
+ const leafResult = await ctx.follow<{ count: number }>(
353
+ 'service.scenario.leaf',
354
+ {}
355
+ );
356
+ if (leafResult.isErr()) {
357
+ return leafResult;
358
+ }
359
+
360
+ return Result.ok({ count: leafResult.value.count });
361
+ },
362
+ services: [scenarioStateService],
363
+ });
364
+
365
+ const scenarioTrailsMap = new Map<string, AnyTrail>([
366
+ ['service.scenario.leaf', scenarioLeafTrail],
367
+ ]);
368
+
369
+ const transformedFollowLeafTrail = trail('follow.transformed.leaf', {
370
+ description: 'Leaf trail in a transformed follow chain',
371
+ input: z.object({ value: z.number() }),
372
+ output: z.object({ value: z.number() }),
373
+ run: (input: { value: number }) => Result.ok({ value: input.value }),
374
+ });
375
+
376
+ const transformedFollowRootTrail = trail('follow.transformed.root', {
377
+ description: 'Root trail that transforms input once before following',
378
+ follow: ['follow.transformed.leaf'],
379
+ input: z
380
+ .object({ value: z.string() })
381
+ .transform(({ value }) => ({ value: Number(value) + 1 })),
382
+ output: z.object({ root: z.number() }),
383
+ run: async (input: { value: number }, ctx: TrailContext) => {
384
+ if (!ctx.follow) {
385
+ return Result.err(new Error('follow not available'));
386
+ }
387
+
388
+ const leafResult = await ctx.follow<{ value: number }>(
389
+ 'follow.transformed.leaf',
390
+ { value: input.value }
391
+ );
392
+ if (leafResult.isErr()) {
393
+ return leafResult;
394
+ }
395
+
396
+ return Result.ok({ root: leafResult.value.value });
397
+ },
398
+ });
399
+
400
+ const transformedFollowTrailsMap = new Map<string, AnyTrail>([
401
+ ['follow.transformed.leaf', transformedFollowLeafTrail],
402
+ ]);
403
+
404
+ const undeclaredFollowService = service('db.undeclared.follows', {
405
+ create: () => Result.ok({ source: 'factory' }),
406
+ mock: () => ({ source: 'mock' }),
407
+ });
408
+
409
+ const undeclaredServiceLeafTrail = trail('service.undeclared.leaf', {
410
+ description: 'Leaf trail that declares the shared service',
411
+ input: z.object({}),
412
+ output: z.object({ childSource: z.string() }),
413
+ run: (_input, ctx) =>
414
+ Result.ok({ childSource: undeclaredFollowService.from(ctx).source }),
415
+ services: [undeclaredFollowService],
416
+ });
417
+
418
+ const undeclaredServiceRootTrail = trail('service.undeclared.root', {
419
+ description: 'Root trail that uses a service without declaring it',
420
+ follow: ['service.undeclared.leaf'],
421
+ input: z.object({}),
422
+ output: z.object({ childSource: z.string(), rootSource: z.string() }),
423
+ run: async (_input, ctx: TrailContext) => {
424
+ if (!ctx.follow) {
425
+ return Result.err(new Error('follow not available'));
426
+ }
427
+
428
+ const childResult = await ctx.follow<{ childSource: string }>(
429
+ 'service.undeclared.leaf',
430
+ {}
431
+ );
432
+ if (childResult.isErr()) {
433
+ return childResult;
434
+ }
435
+
436
+ return Result.ok({
437
+ childSource: childResult.value.childSource,
438
+ rootSource: undeclaredFollowService.from(ctx).source,
439
+ });
440
+ },
441
+ });
442
+
443
+ const undeclaredServiceTrailsMap = new Map<string, AnyTrail>([
444
+ ['service.undeclared.leaf', undeclaredServiceLeafTrail],
445
+ ]);
446
+
447
+ const unrelatedFollowService = service('db.unrelated.follows', {
448
+ create: () => Result.ok({ source: 'factory' }),
449
+ mock: () => {
450
+ throw new Error('unrelated mock should not be resolved');
451
+ },
452
+ });
453
+
454
+ const unrelatedServiceTrail = trail('service.unrelated', {
455
+ description: 'Trail that should not be traversed or mocked',
456
+ input: z.object({}),
457
+ output: z.object({ source: z.string() }),
458
+ run: (_input, ctx) =>
459
+ Result.ok({ source: unrelatedFollowService.from(ctx).source }),
460
+ services: [unrelatedFollowService],
461
+ });
462
+
463
+ const unrelatedServiceTrailsMap = new Map<string, AnyTrail>([
464
+ ['service.unrelated', unrelatedServiceTrail],
465
+ ]);
466
+
467
+ describe('testFollows service mocks', () => {
468
+ // eslint-disable-next-line jest/require-hook
469
+ testFollows(
470
+ serviceRootTrail,
471
+ [
472
+ {
473
+ description: 'propagates auto-resolved service mocks through follow',
474
+ expectValue: { childSource: 'mock', rootSource: 'mock' },
475
+ input: {},
476
+ },
477
+ ],
478
+ { trails: serviceTrailsMap }
479
+ );
480
+ });
481
+
482
+ describe('testFollows service mocks are fresh per scenario', () => {
483
+ // eslint-disable-next-line jest/require-hook
484
+ testFollows(
485
+ scenarioRootTrail,
486
+ [
487
+ {
488
+ description: 'first scenario sees a fresh mutable service',
489
+ expectValue: { count: 1 },
490
+ input: {},
491
+ },
492
+ {
493
+ description: 'second scenario also sees a fresh mutable service',
494
+ expectValue: { count: 1 },
495
+ input: {},
496
+ },
497
+ ],
498
+ { trails: scenarioTrailsMap }
499
+ );
500
+ });
501
+
502
+ describe('testFollows explicit service overrides', () => {
503
+ // eslint-disable-next-line jest/require-hook
504
+ testFollows(
505
+ serviceRootTrail,
506
+ [
507
+ {
508
+ description: 'propagates explicit service overrides through follow',
509
+ expectValue: { childSource: 'override', rootSource: 'override' },
510
+ input: {},
511
+ },
512
+ ],
513
+ {
514
+ services: { 'db.mock.follows': { source: 'override' } },
515
+ trails: serviceTrailsMap,
516
+ }
517
+ );
518
+ });
519
+
520
+ describe('testFollows raw transformed input', () => {
521
+ // eslint-disable-next-line jest/require-hook
522
+ testFollows(
523
+ transformedFollowRootTrail,
524
+ [
525
+ {
526
+ description: 'raw scenario input is only transformed once',
527
+ expectValue: { root: 2 },
528
+ input: { value: '1' },
529
+ },
530
+ ],
531
+ { trails: transformedFollowTrailsMap }
532
+ );
533
+ });
534
+
535
+ describe('testFollows service declarations', () => {
536
+ // eslint-disable-next-line jest/require-hook
537
+ testFollows(
538
+ undeclaredServiceRootTrail,
539
+ [
540
+ {
541
+ description: 'fails when the root trail omits a required service',
542
+ expectErr: InternalError,
543
+ expectErrMessage: undeclaredFollowService.id,
544
+ input: {},
545
+ },
546
+ ],
547
+ { trails: undeclaredServiceTrailsMap }
548
+ );
549
+ });
550
+
551
+ describe('testFollows only resolves mocks for trails under test', () => {
552
+ // eslint-disable-next-line jest/require-hook
553
+ testFollows(
554
+ serviceRootTrail,
555
+ [
556
+ {
557
+ description: 'unrelated service mocks are not resolved',
558
+ expectValue: { childSource: 'mock', rootSource: 'mock' },
559
+ input: {},
560
+ },
561
+ ],
562
+ {
563
+ trails: new Map<string, AnyTrail>([
564
+ ...serviceTrailsMap.entries(),
565
+ ...unrelatedServiceTrailsMap.entries(),
566
+ ]),
567
+ }
568
+ );
569
+ });
570
+
571
+ describe('testFollows fresh service mocks per scenario', () => {
572
+ // eslint-disable-next-line jest/require-hook
573
+ testFollows(
574
+ statefulServiceRootTrail,
575
+ [
576
+ {
577
+ description: 'first scenario gets a fresh service mock',
578
+ expectValue: { childCount: 1, rootCount: 1 },
579
+ input: {},
580
+ },
581
+ {
582
+ description: 'second scenario also gets a fresh service mock',
583
+ expectValue: { childCount: 1, rootCount: 1 },
584
+ input: {},
585
+ },
586
+ ],
587
+ { trails: statefulServiceTrailsMap }
588
+ );
589
+ });
package/src/all.ts CHANGED
@@ -11,6 +11,7 @@ import type { Topo, TrailContext } from '@ontrails/core';
11
11
  import { validateTopo } from '@ontrails/core';
12
12
 
13
13
  import { testContracts } from './contracts.js';
14
+ import type { TestExecutionOptions } from './context.js';
14
15
  import { testDetours } from './detours.js';
15
16
  import { testExamples } from './examples.js';
16
17
 
@@ -37,7 +38,10 @@ import { testExamples } from './examples.js';
37
38
  */
38
39
  export const testAll = (
39
40
  topo: Topo,
40
- ctxOrFactory?: Partial<TrailContext> | (() => Partial<TrailContext>)
41
+ ctxOrFactory?:
42
+ | Partial<TrailContext>
43
+ | TestExecutionOptions
44
+ | (() => Partial<TrailContext> | TestExecutionOptions)
41
45
  ): void => {
42
46
  describe('governance', () => {
43
47
  test('topo validates', () => {
package/src/context.ts CHANGED
@@ -2,12 +2,21 @@
2
2
  * Test context factory for creating TrailContext instances suitable for testing.
3
3
  */
4
4
 
5
- import type { FollowFn, TrailContext } from '@ontrails/core';
6
- import { Result } from '@ontrails/core';
5
+ import type {
6
+ FollowFn,
7
+ ServiceOverrideMap,
8
+ Topo,
9
+ TrailContext,
10
+ } from '@ontrails/core';
11
+ import { Result, createServiceLookup } from '@ontrails/core';
7
12
 
8
13
  import { createTestLogger } from './logger.js';
9
14
  import type { TestTrailContextOptions } from './types.js';
10
15
 
16
+ type MutableTrailContext = {
17
+ -readonly [K in keyof TrailContext]: TrailContext[K];
18
+ };
19
+
11
20
  // ---------------------------------------------------------------------------
12
21
  // createTestContext
13
22
  // ---------------------------------------------------------------------------
@@ -21,13 +30,20 @@ import type { TestTrailContextOptions } from './types.js';
21
30
  */
22
31
  export const createTestContext = (
23
32
  overrides?: TestTrailContextOptions
24
- ): TrailContext => ({
25
- env: overrides?.env ?? { TRAILS_ENV: 'test' },
26
- logger: overrides?.logger ?? createTestLogger(),
27
- requestId: overrides?.requestId ?? 'test-request-001',
28
- signal: overrides?.signal ?? new AbortController().signal,
29
- workspaceRoot: overrides?.cwd ?? process.cwd(),
30
- });
33
+ ): TrailContext => {
34
+ const cwd = overrides?.cwd ?? process.cwd();
35
+ const ctx = {
36
+ cwd,
37
+ env: overrides?.env ?? { TRAILS_ENV: 'test' },
38
+ extensions: undefined,
39
+ logger: overrides?.logger ?? createTestLogger(),
40
+ requestId: overrides?.requestId ?? 'test-request-001',
41
+ signal: overrides?.signal ?? new AbortController().signal,
42
+ workspaceRoot: cwd,
43
+ } as MutableTrailContext;
44
+ ctx.service = createServiceLookup(() => ctx);
45
+ return ctx;
46
+ };
31
47
 
32
48
  // ---------------------------------------------------------------------------
33
49
  // createFollowContext
@@ -37,6 +53,11 @@ export interface CreateFollowContextOptions {
37
53
  readonly responses?: Record<string, Result<unknown, Error>> | undefined;
38
54
  }
39
55
 
56
+ export interface TestExecutionOptions {
57
+ readonly ctx?: Partial<TrailContext> | undefined;
58
+ readonly services?: ServiceOverrideMap | undefined;
59
+ }
60
+
40
61
  /**
41
62
  * Create a mock `FollowFn` for testing composite trails.
42
63
  *
@@ -69,15 +90,61 @@ export const createFollowContext = (
69
90
  };
70
91
  };
71
92
 
93
+ const isTestExecutionOptions = (
94
+ input: Partial<TrailContext> | TestExecutionOptions | undefined
95
+ ): input is TestExecutionOptions =>
96
+ input !== undefined &&
97
+ (Object.hasOwn(input, 'ctx') || Object.hasOwn(input, 'services'));
98
+
99
+ export const normalizeTestExecutionOptions = (
100
+ input?: Partial<TrailContext> | TestExecutionOptions
101
+ ): TestExecutionOptions =>
102
+ isTestExecutionOptions(input) ? input : { ctx: input };
103
+
104
+ export const mergeServiceOverrides = (
105
+ autoResolved: ServiceOverrideMap,
106
+ ctx: Partial<TrailContext> | undefined,
107
+ explicit: ServiceOverrideMap | undefined
108
+ ): ServiceOverrideMap => ({
109
+ ...autoResolved,
110
+ ...ctx?.extensions,
111
+ ...explicit,
112
+ });
113
+
114
+ const buildMockServices = async (app: Topo): Promise<ServiceOverrideMap> => {
115
+ const services: Record<string, unknown> = {};
116
+ for (const declaredService of app.listServices()) {
117
+ if (!declaredService.mock) {
118
+ continue;
119
+ }
120
+ services[declaredService.id] = await declaredService.mock();
121
+ }
122
+ return services;
123
+ };
124
+
125
+ export const resolveMockServices = async (
126
+ app: Topo
127
+ ): Promise<ServiceOverrideMap> => await buildMockServices(app);
128
+
72
129
  /**
73
130
  * Merge a Partial<TrailContext> into a test context.
74
131
  * Used internally when the public API accepts Partial<TrailContext>.
75
132
  */
76
- export const mergeTestContext = (ctx?: Partial<TrailContext>): TrailContext => {
77
- if (ctx === undefined) {
78
- return createTestContext();
79
- }
80
-
133
+ export const mergeTestContext = (
134
+ ctx?: Partial<TrailContext>,
135
+ services?: ServiceOverrideMap
136
+ ): TrailContext => {
81
137
  const base = createTestContext();
82
- return { ...base, ...ctx };
138
+ const extensions = {
139
+ ...base.extensions,
140
+ ...ctx?.extensions,
141
+ ...services,
142
+ };
143
+ const merged = {
144
+ ...base,
145
+ ...ctx,
146
+ extensions: Object.keys(extensions).length === 0 ? undefined : extensions,
147
+ } as MutableTrailContext;
148
+ merged.service = createServiceLookup(() => merged);
149
+ return merged;
83
150
  };
package/src/contracts.ts CHANGED
@@ -9,11 +9,17 @@
9
9
  import { describe, test } from 'bun:test';
10
10
 
11
11
  import type { Topo, TrailExample, Trail, TrailContext } from '@ontrails/core';
12
- import { formatZodIssues, validateInput } from '@ontrails/core';
12
+ import { executeTrail, formatZodIssues, validateInput } from '@ontrails/core';
13
13
  import type { z } from 'zod';
14
14
 
15
15
  import { expectOk } from './assertions.js';
16
- import { mergeTestContext } from './context.js';
16
+ import {
17
+ mergeServiceOverrides,
18
+ mergeTestContext,
19
+ normalizeTestExecutionOptions,
20
+ resolveMockServices,
21
+ } from './context.js';
22
+ import type { TestExecutionOptions } from './context.js';
17
23
 
18
24
  // ---------------------------------------------------------------------------
19
25
  // Helpers
@@ -22,13 +28,13 @@ import { mergeTestContext } from './context.js';
22
28
  /** Check if a trail requires follow() but the context doesn't provide it. */
23
29
  const needsFollowContext = (
24
30
  t: unknown,
25
- resolveCtx: () => Partial<TrailContext> | undefined
31
+ resolveCtx: () => Partial<TrailContext> | TestExecutionOptions | undefined
26
32
  ): boolean => {
27
33
  const spec = t as { follow?: readonly string[] };
28
34
  if (!spec.follow || spec.follow.length === 0) {
29
35
  return false;
30
36
  }
31
- return !resolveCtx()?.follow;
37
+ return !normalizeTestExecutionOptions(resolveCtx()).ctx?.follow;
32
38
  };
33
39
 
34
40
  const validateOutputSchema = (
@@ -58,9 +64,12 @@ const validateOutputSchema = (
58
64
  */
59
65
  export const testContracts = (
60
66
  app: Topo,
61
- ctxOrFactory?: Partial<TrailContext> | (() => Partial<TrailContext>)
67
+ ctxOrFactory?:
68
+ | Partial<TrailContext>
69
+ | TestExecutionOptions
70
+ | (() => Partial<TrailContext> | TestExecutionOptions)
62
71
  ): void => {
63
- const resolveCtx =
72
+ const resolveInput =
64
73
  typeof ctxOrFactory === 'function' ? ctxOrFactory : () => ctxOrFactory;
65
74
  const allEntries = app.list() as Trail<unknown, unknown>[];
66
75
 
@@ -72,7 +81,7 @@ export const testContracts = (
72
81
  if (t.examples === undefined || t.examples.length === 0) {
73
82
  return;
74
83
  }
75
- if (needsFollowContext(t, resolveCtx)) {
84
+ if (needsFollowContext(t, resolveInput)) {
76
85
  return;
77
86
  }
78
87
 
@@ -82,12 +91,21 @@ export const testContracts = (
82
91
  test.each(successExamples)(
83
92
  'contract: $name',
84
93
  async (example: TrailExample<unknown, unknown>) => {
85
- const testCtx = mergeTestContext(resolveCtx());
94
+ const resolved = normalizeTestExecutionOptions(resolveInput());
95
+ const services = mergeServiceOverrides(
96
+ await resolveMockServices(app),
97
+ resolved.ctx,
98
+ resolved.services
99
+ );
100
+ const testCtx = mergeTestContext(resolved.ctx);
86
101
 
87
102
  const validated = validateInput(t.input, example.input);
88
- const validatedInput = expectOk(validated);
103
+ expectOk(validated);
89
104
 
90
- const result = await t.run(validatedInput, testCtx);
105
+ const result = await executeTrail(t, example.input, {
106
+ ctx: testCtx,
107
+ services,
108
+ });
91
109
  const resultValue = expectOk(result);
92
110
 
93
111
  validateOutputSchema(outputSchema, resultValue, t.id, example.name);