|
| 1 | +import { ExtractFlowOutput, StepOutput, Flow } from '../../src/index.js'; |
| 2 | +import { describe, it, expectTypeOf } from 'vitest'; |
| 3 | + |
| 4 | +describe('ExtractFlowOutput utility type', () => { |
| 5 | + it('should return an empty object for a flow without steps', () => { |
| 6 | + // Use type only instead of creating an unused variable |
| 7 | + type EmptyFlow = Flow<unknown>; |
| 8 | + type FlowOutput = ExtractFlowOutput<EmptyFlow>; |
| 9 | + |
| 10 | + // For an empty flow, output is an empty object |
| 11 | + expectTypeOf<FlowOutput>().toEqualTypeOf<Record<string, never>>(); |
| 12 | + expectTypeOf<keyof FlowOutput>().toEqualTypeOf<never>(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should correctly output for a flow with a single leaf step', () => { |
| 16 | + const singleStepFlow = new Flow<{ input: string }>({ |
| 17 | + slug: 'single_step_flow', |
| 18 | + }).step({ slug: 'process' }, (input) => ({ |
| 19 | + result: input.run.input.toUpperCase(), |
| 20 | + })); |
| 21 | + |
| 22 | + type FlowOutput = ExtractFlowOutput<typeof singleStepFlow>; |
| 23 | + |
| 24 | + // The only leaf step is "process", whose output type is StepOutput<typeof singleStepFlow, 'process'> |
| 25 | + expectTypeOf<FlowOutput>().toMatchTypeOf<{ |
| 26 | + process: StepOutput<typeof singleStepFlow, 'process'>; |
| 27 | + }>(); |
| 28 | + |
| 29 | + // The value for process in output should match its StepOutput type |
| 30 | + expectTypeOf<FlowOutput['process']>().toEqualTypeOf<{ result: string }>(); |
| 31 | + |
| 32 | + // FlowOutput should not have other keys |
| 33 | + expectTypeOf<FlowOutput>().not.toMatchTypeOf<{ |
| 34 | + nonExistentStep: unknown; |
| 35 | + }>(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should correctly output for multiple leaf steps', () => { |
| 39 | + const multiLeafFlow = new Flow<{ data: number }>({ |
| 40 | + slug: 'multi_leaf_flow', |
| 41 | + }) |
| 42 | + .step({ slug: 'intermediate' }, (input) => ({ |
| 43 | + value: input.run.data * 2, |
| 44 | + })) |
| 45 | + .step({ slug: 'leaf1', dependsOn: ['intermediate'] }, (input) => ({ |
| 46 | + squared: input.intermediate.value ** 2, |
| 47 | + })) |
| 48 | + .step({ slug: 'leaf2', dependsOn: ['intermediate'] }, (input) => ({ |
| 49 | + doubled: input.intermediate.value * 2, |
| 50 | + })); |
| 51 | + |
| 52 | + type FlowOutput = ExtractFlowOutput<typeof multiLeafFlow>; |
| 53 | + |
| 54 | + // FlowOutput should have exactly leaf1 and leaf2, each with their respective StepOutputs |
| 55 | + expectTypeOf<FlowOutput>().toMatchTypeOf<{ |
| 56 | + leaf1: StepOutput<typeof multiLeafFlow, 'leaf1'>; |
| 57 | + leaf2: StepOutput<typeof multiLeafFlow, 'leaf2'>; |
| 58 | + }>(); |
| 59 | + |
| 60 | + expectTypeOf<FlowOutput['leaf1']>().toEqualTypeOf<{ squared: number }>(); |
| 61 | + expectTypeOf<FlowOutput['leaf2']>().toEqualTypeOf<{ doubled: number }>(); |
| 62 | + |
| 63 | + // Should NOT have intermediate |
| 64 | + expectTypeOf<FlowOutput>().not.toMatchTypeOf<{ |
| 65 | + intermediate: unknown; |
| 66 | + }>(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should correctly handle a root step that is also a leaf step', () => { |
| 70 | + const rootLeafFlow = new Flow<{ input: string }>({ slug: 'root_leaf_flow' }) |
| 71 | + .step({ slug: 'rootLeaf' }, (input) => ({ |
| 72 | + processed: input.run.input.trim(), |
| 73 | + })) |
| 74 | + .step({ slug: 'intermediate' }, (input) => ({ |
| 75 | + length: input.run.input.length, |
| 76 | + })) |
| 77 | + .step({ slug: 'dependent', dependsOn: ['intermediate'] }, (input) => ({ |
| 78 | + result: input.intermediate.length > 10, |
| 79 | + })); |
| 80 | + |
| 81 | + type FlowOutput = ExtractFlowOutput<typeof rootLeafFlow>; |
| 82 | + |
| 83 | + expectTypeOf<FlowOutput>().toMatchTypeOf<{ |
| 84 | + rootLeaf: StepOutput<typeof rootLeafFlow, 'rootLeaf'>; |
| 85 | + dependent: StepOutput<typeof rootLeafFlow, 'dependent'>; |
| 86 | + }>(); |
| 87 | + |
| 88 | + expectTypeOf<FlowOutput['rootLeaf']>().toEqualTypeOf<{ |
| 89 | + processed: string; |
| 90 | + }>(); |
| 91 | + expectTypeOf<FlowOutput['dependent']>().toEqualTypeOf<{ |
| 92 | + result: boolean; |
| 93 | + }>(); |
| 94 | + |
| 95 | + expectTypeOf<FlowOutput>().not.toMatchTypeOf<{ |
| 96 | + intermediate: unknown; |
| 97 | + }>(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should handle complex dependency chains', () => { |
| 101 | + const complexFlow = new Flow<{ input: number }>({ slug: 'complex_flow' }) |
| 102 | + .step({ slug: 'step1' }, (input) => ({ value: input.run.input + 1 })) |
| 103 | + .step({ slug: 'step2', dependsOn: ['step1'] }, (input) => ({ |
| 104 | + value: input.step1.value * 2, |
| 105 | + })) |
| 106 | + .step({ slug: 'step3', dependsOn: ['step1'] }, (input) => ({ |
| 107 | + value: input.step1.value - 1, |
| 108 | + })) |
| 109 | + .step({ slug: 'step4', dependsOn: ['step2', 'step3'] }, (input) => ({ |
| 110 | + sum: input.step2.value + input.step3.value, |
| 111 | + original: input.run.input, |
| 112 | + })); |
| 113 | + |
| 114 | + type FlowOutput = ExtractFlowOutput<typeof complexFlow>; |
| 115 | + |
| 116 | + expectTypeOf<FlowOutput>().toMatchTypeOf<{ |
| 117 | + step4: StepOutput<typeof complexFlow, 'step4'>; |
| 118 | + }>(); |
| 119 | + |
| 120 | + expectTypeOf<FlowOutput['step4']>().toEqualTypeOf<{ |
| 121 | + sum: number; |
| 122 | + original: number; |
| 123 | + }>(); |
| 124 | + |
| 125 | + // No extras |
| 126 | + expectTypeOf<FlowOutput>().not.toMatchTypeOf<{ |
| 127 | + step1: unknown; |
| 128 | + step2: unknown; |
| 129 | + step3: unknown; |
| 130 | + }>(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments