@workflow/world-testing 4.0.1-beta.0
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/CHANGELOG.md +11 -0
- package/LICENSE.md +21 -0
- package/package.json +36 -0
- package/src/addition.mts +27 -0
- package/src/idempotency.mts +30 -0
- package/src/index.mts +7 -0
- package/src/server.mts +104 -0
- package/src/util.mts +99 -0
- package/test/embedded.test.ts +3 -0
- package/tsconfig.json +11 -0
- package/turbo.json +13 -0
- package/workflows/addition.ts +16 -0
- package/workflows/noop.ts +37 -0
package/CHANGELOG.md
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Vercel, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@workflow/world-testing",
|
|
3
|
+
"version": "4.0.1-beta.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/src/index.mjs",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@hono/node-server": "^1.19.5",
|
|
11
|
+
"chalk": "^5.6.2",
|
|
12
|
+
"hono": "^4.9.10",
|
|
13
|
+
"jsonlines": "^0.1.1",
|
|
14
|
+
"zod": "4.1.11",
|
|
15
|
+
"@workflow/cli": "4.0.1-beta.0",
|
|
16
|
+
"workflow": "4.0.1-beta.0",
|
|
17
|
+
"@workflow/world": "4.0.1-beta.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "24.6.2",
|
|
21
|
+
"@types/jsonlines": "^0.1.5",
|
|
22
|
+
"vitest": "^3.2.4",
|
|
23
|
+
"@workflow/tsconfig": "4.0.1-beta.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"vitest": "^3.2.4"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [],
|
|
29
|
+
"author": "",
|
|
30
|
+
"license": "ISC",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "wf build && tsc",
|
|
33
|
+
"start": "node --watch src/server.mts",
|
|
34
|
+
"test": "vitest"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/addition.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { expect, test, vi } from 'vitest';
|
|
2
|
+
import { createFetcher, startServer } from './util.mjs';
|
|
3
|
+
|
|
4
|
+
export function addition(world: string) {
|
|
5
|
+
test('runs an addition', { timeout: 12_000 }, async () => {
|
|
6
|
+
const server = await startServer({ world }).then(createFetcher);
|
|
7
|
+
const result = await server.invoke(
|
|
8
|
+
'workflows/addition.ts',
|
|
9
|
+
'addition',
|
|
10
|
+
[1, 2]
|
|
11
|
+
);
|
|
12
|
+
expect(result.runId).toMatch(/^wrun_.+/);
|
|
13
|
+
await vi.waitFor(
|
|
14
|
+
async () => {
|
|
15
|
+
const run = await server.getRun(result.runId);
|
|
16
|
+
expect(run).toMatchObject<Partial<typeof run>>({
|
|
17
|
+
status: 'completed',
|
|
18
|
+
output: [3],
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
interval: 200,
|
|
23
|
+
timeout: 10_000,
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { expect, test, vi } from 'vitest';
|
|
2
|
+
import { hydrateWorkflowReturnValue } from 'workflow/internal/serialization';
|
|
3
|
+
import { createFetcher, startServer } from './util.mjs';
|
|
4
|
+
|
|
5
|
+
export function idempotency(world: string) {
|
|
6
|
+
test('idempotency', { timeout: 60_000 }, async () => {
|
|
7
|
+
const server = await startServer({ world }).then(createFetcher);
|
|
8
|
+
const result = await server.invoke('workflows/noop.ts', 'brokenWf', [1, 2]);
|
|
9
|
+
expect(result.runId).toMatch(/^wrun_.+/);
|
|
10
|
+
const run = await vi.waitFor(
|
|
11
|
+
async () => {
|
|
12
|
+
const run = await server.getRun(result.runId);
|
|
13
|
+
expect(run).toMatchObject<Partial<typeof run>>({
|
|
14
|
+
status: 'completed',
|
|
15
|
+
});
|
|
16
|
+
return run;
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
interval: 200,
|
|
20
|
+
timeout: 59_000,
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const output = await hydrateWorkflowReturnValue(run.output, [], globalThis);
|
|
25
|
+
|
|
26
|
+
expect(output).toEqual({
|
|
27
|
+
numbers: Array.from({ length: 110 }, () => expect.any(Number)),
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
package/src/index.mts
ADDED
package/src/server.mts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { serve } from '@hono/node-server';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
import { getRun, start } from 'workflow/api';
|
|
5
|
+
import { getWorld } from 'workflow/runtime';
|
|
6
|
+
import * as z from 'zod';
|
|
7
|
+
import flow from '../.well-known/workflow/v1/flow.js';
|
|
8
|
+
import manifest from '../.well-known/workflow/v1/manifest.debug.json' with {
|
|
9
|
+
type: 'json',
|
|
10
|
+
};
|
|
11
|
+
import step from '../.well-known/workflow/v1/step.js';
|
|
12
|
+
|
|
13
|
+
if (!process.env.WORKFLOW_TARGET_WORLD) {
|
|
14
|
+
console.error(
|
|
15
|
+
'Error: WORKFLOW_TARGET_WORLD environment variable is not set.'
|
|
16
|
+
);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type Files = keyof typeof manifest.workflows;
|
|
21
|
+
type Workflows<F extends Files> = keyof (typeof manifest.workflows)[F];
|
|
22
|
+
|
|
23
|
+
const Invoke = z
|
|
24
|
+
.object({
|
|
25
|
+
file: z.literal(Object.keys(manifest.workflows) as Files[]),
|
|
26
|
+
workflow: z.string(),
|
|
27
|
+
args: z.unknown().array().default([]),
|
|
28
|
+
})
|
|
29
|
+
.transform((obj) => {
|
|
30
|
+
const file = obj.file as keyof typeof manifest.workflows;
|
|
31
|
+
const workflow = z
|
|
32
|
+
.literal(
|
|
33
|
+
Object.keys(manifest.workflows[file]) as Workflows<typeof file>[]
|
|
34
|
+
)
|
|
35
|
+
.parse(obj.workflow);
|
|
36
|
+
return {
|
|
37
|
+
args: obj.args,
|
|
38
|
+
workflow: manifest.workflows[file][workflow],
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const app = new Hono()
|
|
43
|
+
.post('/.well-known/workflow/v1/flow', (ctx) => {
|
|
44
|
+
return flow.POST(ctx.req.raw);
|
|
45
|
+
})
|
|
46
|
+
.post('/.well-known/workflow/v1/step', (ctx) => {
|
|
47
|
+
return step.POST(ctx.req.raw);
|
|
48
|
+
})
|
|
49
|
+
.get('/_manifest', (ctx) => ctx.json(manifest))
|
|
50
|
+
.post('/invoke', async (ctx) => {
|
|
51
|
+
const json = await ctx.req.json().then(Invoke.parse);
|
|
52
|
+
const handler = await start(json.workflow, json.args);
|
|
53
|
+
|
|
54
|
+
return ctx.json({ runId: handler.runId });
|
|
55
|
+
})
|
|
56
|
+
.get('/runs/:runId', async (ctx) => {
|
|
57
|
+
return ctx.json(await getWorld().runs.get(ctx.req.param('runId')));
|
|
58
|
+
})
|
|
59
|
+
.get('/runs/:runId/readable', async (ctx) => {
|
|
60
|
+
const runId = ctx.req.param('runId');
|
|
61
|
+
const run = getRun(runId);
|
|
62
|
+
return new Response(run.getReadable());
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
serve(
|
|
66
|
+
{
|
|
67
|
+
fetch: app.fetch,
|
|
68
|
+
port: Number(process.env.PORT) || 0,
|
|
69
|
+
},
|
|
70
|
+
async (info) => {
|
|
71
|
+
console.log(`👂 listening on http://${info.address}:${info.port}`);
|
|
72
|
+
console.log('');
|
|
73
|
+
|
|
74
|
+
process.env.PORT = info.port.toString();
|
|
75
|
+
|
|
76
|
+
for (const [filename, workflows] of Object.entries(manifest.workflows)) {
|
|
77
|
+
for (const workflowName of Object.keys(workflows)) {
|
|
78
|
+
console.log(
|
|
79
|
+
`$ curl -X POST http://localhost:${info.port}/invoke -d '${JSON.stringify(
|
|
80
|
+
{
|
|
81
|
+
file: filename,
|
|
82
|
+
workflow: workflowName,
|
|
83
|
+
}
|
|
84
|
+
)}'`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const world = getWorld();
|
|
90
|
+
if (world.start) {
|
|
91
|
+
console.log(`starting background tasks...`);
|
|
92
|
+
await world.start().then(
|
|
93
|
+
() => console.log('background tasks started.'),
|
|
94
|
+
(err) => console.error('❗ error starting background tasks:', err)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (process.env.CONTROL_FD === '3') {
|
|
99
|
+
const control = fs.createWriteStream('', { fd: 3 });
|
|
100
|
+
control.write(`${JSON.stringify({ state: 'listening', info })}\n`);
|
|
101
|
+
control.end();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
);
|
package/src/util.mts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import cp from 'node:child_process';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { WorkflowRunSchema } from '@workflow/world';
|
|
4
|
+
import chalk, { type ChalkInstance } from 'chalk';
|
|
5
|
+
import jsonlines from 'jsonlines';
|
|
6
|
+
import { assert, onTestFailed, onTestFinished } from 'vitest';
|
|
7
|
+
import * as z from 'zod';
|
|
8
|
+
import type manifest from '../.well-known/workflow/v1/manifest.debug.json';
|
|
9
|
+
|
|
10
|
+
export const Control = z.object({
|
|
11
|
+
state: z.literal('listening'),
|
|
12
|
+
info: z.object({
|
|
13
|
+
port: z.number(),
|
|
14
|
+
}),
|
|
15
|
+
});
|
|
16
|
+
type Control = z.infer<typeof Control>;
|
|
17
|
+
|
|
18
|
+
type Files = keyof typeof manifest.workflows;
|
|
19
|
+
type Workflows<F extends Files> = keyof (typeof manifest.workflows)[F];
|
|
20
|
+
|
|
21
|
+
export const Worlds = {
|
|
22
|
+
embedded: 'embedded',
|
|
23
|
+
postgres: '@workflow/world-postgres',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export async function startServer(opts: { world: string }) {
|
|
27
|
+
let serverPath = new URL('./server.mts', import.meta.url).pathname;
|
|
28
|
+
|
|
29
|
+
if (!existsSync(serverPath)) {
|
|
30
|
+
serverPath = new URL('./server.mjs', import.meta.url).pathname;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const proc = cp.spawn('node', [serverPath], {
|
|
34
|
+
stdio: ['ignore', 'pipe', 'pipe', 'pipe'],
|
|
35
|
+
env: {
|
|
36
|
+
...process.env,
|
|
37
|
+
WORKFLOW_TARGET_WORLD: opts.world,
|
|
38
|
+
CONTROL_FD: '3',
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
onTestFinished(() => {
|
|
42
|
+
proc.kill();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const stdio = [] as { stream: ChalkInstance; chunk: string }[];
|
|
46
|
+
proc.stdout?.on('data', (chunk) => {
|
|
47
|
+
stdio.push({ stream: chalk.white, chunk: chunk.toString() });
|
|
48
|
+
});
|
|
49
|
+
proc.stderr?.on('data', (chunk) => {
|
|
50
|
+
stdio.push({ stream: chalk.red, chunk: chunk.toString() });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
onTestFailed(() => {
|
|
54
|
+
console.log('=== SERVER STDIO ===');
|
|
55
|
+
let buffer = '';
|
|
56
|
+
for (const { stream, chunk } of stdio) {
|
|
57
|
+
buffer += stream.inverse(chunk);
|
|
58
|
+
}
|
|
59
|
+
console.log(buffer);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const fd3 = proc.stdio[3];
|
|
63
|
+
assert(fd3, 'fd3 should be defined');
|
|
64
|
+
|
|
65
|
+
for await (const chunk of fd3.pipe(jsonlines.parse())) {
|
|
66
|
+
return Control.parse(chunk);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
throw new Error('Server did not start correctly');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const Invoke = z.object({ runId: z.coerce.string() });
|
|
73
|
+
|
|
74
|
+
export function createFetcher(control: Control) {
|
|
75
|
+
return {
|
|
76
|
+
async invoke<F extends Files, W extends Workflows<F>>(
|
|
77
|
+
file: F,
|
|
78
|
+
workflow: W,
|
|
79
|
+
args: unknown[]
|
|
80
|
+
) {
|
|
81
|
+
const x = await fetch(`http://localhost:${control.info.port}/invoke`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: {
|
|
84
|
+
'content-type': 'application/json',
|
|
85
|
+
},
|
|
86
|
+
body: JSON.stringify({ file, workflow, args }),
|
|
87
|
+
});
|
|
88
|
+
const data = await x.json().then(Invoke.parse);
|
|
89
|
+
return data;
|
|
90
|
+
},
|
|
91
|
+
async getRun(id: string) {
|
|
92
|
+
const x = await fetch(
|
|
93
|
+
`http://localhost:${control.info.port}/runs/${encodeURIComponent(id)}`
|
|
94
|
+
);
|
|
95
|
+
const data = await x.json();
|
|
96
|
+
return WorkflowRunSchema.parseAsync(data);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@workflow/tsconfig/base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"moduleResolution": "NodeNext",
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"module": "NodeNext",
|
|
7
|
+
"outDir": "dist"
|
|
8
|
+
},
|
|
9
|
+
"include": ["src", ".well-known"],
|
|
10
|
+
"exclude": ["node_modules", "**/*.test.ts"]
|
|
11
|
+
}
|
package/turbo.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RetryableError } from 'workflow';
|
|
2
|
+
|
|
3
|
+
async function add(num: number, num2: number): Promise<number> {
|
|
4
|
+
'use step';
|
|
5
|
+
if (Math.random() < 0.2) {
|
|
6
|
+
throw new RetryableError('Random failure, please retry');
|
|
7
|
+
}
|
|
8
|
+
return num + num2;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function addition(num: number, num2: number): Promise<number> {
|
|
12
|
+
'use workflow';
|
|
13
|
+
const result = await add(num, num2);
|
|
14
|
+
console.log({ result });
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
let count = 0;
|
|
2
|
+
export async function noop(_i: number) {
|
|
3
|
+
'use step';
|
|
4
|
+
|
|
5
|
+
count++;
|
|
6
|
+
return count;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function brokenWf() {
|
|
10
|
+
'use workflow';
|
|
11
|
+
|
|
12
|
+
const numbers = [] as number[];
|
|
13
|
+
|
|
14
|
+
{
|
|
15
|
+
const promises: Promise<number>[] = [];
|
|
16
|
+
for (let i = 0; i < 10; i++) {
|
|
17
|
+
promises.push(noop(i));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
console.log('await 10');
|
|
21
|
+
numbers.push(...(await Promise.all(promises)));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
{
|
|
25
|
+
const promises: Promise<number>[] = [];
|
|
26
|
+
for (let i = 0; i < 100; i++) {
|
|
27
|
+
promises.push(noop(1000 + i));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
console.log('await 100');
|
|
31
|
+
numbers.push(...(await Promise.all(promises)));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log('done.');
|
|
35
|
+
|
|
36
|
+
return { numbers };
|
|
37
|
+
}
|