feat: typed flow core with compile-time context guarantee

This commit is contained in:
chrnx committed 2026-06-26 00:35:44 -05:00
1 parent 96880d055b
commit 11fc746d28
3 files changed
+70

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
import { expect, test } from 'vitest';
import { flow } from '../src/flow';
test('flow accumulates context and runs steps in order', async () => {
const compiled = flow<{ req: { token: string } }>()
.step('auth', (ctx) => ({ user: { id: ctx.req.token } }))
.step('load', (ctx) => ({ greeting: `hi ${ctx.user.id}` }))
.handle((ctx) => ctx.greeting);
const result = await compiled.run({ req: { token: 'abc' } });
expect(result).toBe('hi abc');
});
test('async steps are awaited and accumulate', async () => {
const compiled = flow<{ n: number }>()
.step('double', async (ctx) => ({ doubled: ctx.n * 2 }))
.handle((ctx) => ctx.doubled);
expect(await compiled.run({ n: 21 })).toBe(42);
});