@niwa-labs/workflow 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +13 -2
- package/package.json +55 -4
- package/src/actions.ts +408 -0
- package/src/agent.ts +211 -0
- package/src/ask.ts +84 -0
- package/src/bridge.ts +710 -0
- package/src/catalogue.ts +74 -0
- package/src/check.ts +880 -0
- package/src/child.ts +1378 -0
- package/src/cockpit/actions.ts +128 -0
- package/src/cockpit/catalogue-actions.ts +97 -0
- package/src/cockpit/dashboard.tsx +134 -0
- package/src/cockpit/groups.ts +45 -0
- package/src/cockpit/index.tsx +50 -0
- package/src/cockpit/launch.tsx +136 -0
- package/src/cockpit/plan.ts +101 -0
- package/src/cockpit/run-commands.tsx +102 -0
- package/src/cockpit/run-page.tsx +140 -0
- package/src/cockpit/run-summary.ts +16 -0
- package/src/cockpit/runs.tsx +73 -0
- package/src/cockpit/statistics.tsx +176 -0
- package/src/cockpit/track.tsx +159 -0
- package/src/cockpit/workflows.tsx +383 -0
- package/src/describe-cache.ts +122 -0
- package/src/describe.ts +174 -0
- package/src/guide.ts +213 -0
- package/src/index.ts +2813 -0
- package/src/inspect.ts +235 -0
- package/src/installation.ts +90 -0
- package/src/language.ts +217 -0
- package/src/records-entry.ts +63 -0
- package/src/records.ts +574 -0
- package/src/registry.ts +250 -0
- package/src/revise.ts +89 -0
- package/src/runner.ts +339 -0
- package/src/script-types.ts +549 -0
- package/src/stats.ts +460 -0
- package/src/status.ts +512 -0
- package/src/store.ts +259 -0
- package/src/subscriptions.ts +102 -0
- package/src/waiting.ts +61 -0
- package/test/cockpit-actions.test.ts +20 -0
- package/test/cockpit-groups.test.ts +42 -0
- package/test/cockpit-plan.test.ts +118 -0
- package/test/fixtures/branches-host.ts +58 -0
- package/test/fixtures/branches-script.js +23 -0
- package/test/fixtures/bridge-host.ts +23 -0
- package/test/fixtures/bridge-keys-script.js +15 -0
- package/test/fixtures/bridge-wait-script.js +8 -0
- package/test/fixtures/cancel-host.ts +58 -0
- package/test/fixtures/cancel-script.js +12 -0
- package/test/fixtures/crash-host.ts +61 -0
- package/test/fixtures/crash-script.js +14 -0
- package/test/fixtures/ping-records.ts +20 -0
- package/test/fixtures/revision-host.ts +57 -0
- package/test/fixtures/revision-script.js +11 -0
- package/test/fixtures/running-host.ts +61 -0
- package/test/fixtures/running-script.js +12 -0
- package/test/fixtures/with-data/flow.ts +2 -0
- package/test/fixtures/with-data/table.json +1 -0
- package/test/git-repo.ts +45 -0
- package/test/git-worktree.test.ts +413 -0
- package/test/question-format.test.ts +279 -0
- package/test/workflow-agent-caps.test.ts +91 -0
- package/test/workflow-agent-lifetime.test.ts +93 -0
- package/test/workflow-agent-options.test.ts +216 -0
- package/test/workflow-agent-schema.test.ts +386 -0
- package/test/workflow-agent-typings.test.ts +241 -0
- package/test/workflow-ask-resume.test.ts +145 -0
- package/test/workflow-branch-keys.test.ts +133 -0
- package/test/workflow-bridge-inproc.test.ts +230 -0
- package/test/workflow-bridge.test.ts +141 -0
- package/test/workflow-builtin-candidate.test.ts +230 -0
- package/test/workflow-builtin.test.ts +197 -0
- package/test/workflow-cancel-agent.test.ts +144 -0
- package/test/workflow-cancel.test.ts +166 -0
- package/test/workflow-chain.test.ts +84 -0
- package/test/workflow-check-options.test.ts +144 -0
- package/test/workflow-check-uses.test.ts +148 -0
- package/test/workflow-check.test.ts +282 -0
- package/test/workflow-contract.test.ts +295 -0
- package/test/workflow-crash-resume.test.ts +141 -0
- package/test/workflow-deadline.test.ts +102 -0
- package/test/workflow-describe-cache.test.ts +240 -0
- package/test/workflow-directory.test.ts +73 -0
- package/test/workflow-emits.test.ts +261 -0
- package/test/workflow-group-keys.test.ts +72 -0
- package/test/workflow-harnesses.test.ts +262 -0
- package/test/workflow-inspect.test.ts +152 -0
- package/test/workflow-language-types.test.ts +148 -0
- package/test/workflow-manifest.test.ts +93 -0
- package/test/workflow-manual-pause.test.ts +116 -0
- package/test/workflow-owners.test.ts +154 -0
- package/test/workflow-pause.test.ts +200 -0
- package/test/workflow-phases.test.ts +445 -0
- package/test/workflow-plugin-entries.test.ts +59 -0
- package/test/workflow-record-refs.test.ts +310 -0
- package/test/workflow-replay.test.ts +206 -0
- package/test/workflow-resume-once.test.ts +188 -0
- package/test/workflow-revision.test.ts +216 -0
- package/test/workflow-scope.test.ts +505 -0
- package/test/workflow-script-defaults.test.ts +119 -0
- package/test/workflow-script-error.test.ts +228 -0
- package/test/workflow-script-libraries.test.ts +88 -0
- package/test/workflow-script-packages.test.ts +119 -0
- package/test/workflow-stdout.test.ts +89 -0
- package/test/workflow-stop.test.ts +299 -0
- package/test/workflow-store.test.ts +220 -0
- package/test/workflow-visible.test.ts +136 -0
- package/test/workflow-wait-version.test.ts +54 -0
- package/test/workflow-wait.test.ts +224 -0
- package/test/workflow-waiting-agent.test.ts +64 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// The rest of the script API: sleep, wait with a
|
|
2
|
+
// timeout, ask, emit, read, now and random. The host is in this process and its clock is the
|
|
3
|
+
// clock of the test, so a sleep is watched and not waited for.
|
|
4
|
+
|
|
5
|
+
import { afterEach, expect, test } from "bun:test";
|
|
6
|
+
import { writeFile } from "node:fs/promises";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import { seq } from "@niwa-labs/core";
|
|
10
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
11
|
+
import { connect, createNiwa, requests, timers, eventually, tempHome, testClock, ownerTokenOf, WfNote, stub } from "@niwa-labs/testing";
|
|
12
|
+
import type { Host, TestClock } from "@niwa-labs/testing";
|
|
13
|
+
import { workflow } from "../src/index.ts";
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const START = 1_700_000_000_000;
|
|
17
|
+
const MINUTE = 60_000;
|
|
18
|
+
|
|
19
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
20
|
+
const hosts: Host[] = [];
|
|
21
|
+
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
24
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/** A host on a home, with the modules a script uses and a clock of the test's. */
|
|
28
|
+
async function hostOn(path: string, clock: TestClock): Promise<Host> {
|
|
29
|
+
const niwa = createNiwa({ home: path, clock });
|
|
30
|
+
niwa.use(requests());
|
|
31
|
+
niwa.use(timers([]));
|
|
32
|
+
niwa.use(stub(path));
|
|
33
|
+
niwa.use(workflow({ emits: [WfNote] }));
|
|
34
|
+
await niwa.serve();
|
|
35
|
+
hosts.push(niwa);
|
|
36
|
+
return niwa;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const recordsOf = async (niwa: Host): Promise<StoredRecord[]> => niwa.records.read();
|
|
40
|
+
const ofType = (records: readonly StoredRecord[], type: string): StoredRecord[] =>
|
|
41
|
+
records.filter((row) => row.type === type);
|
|
42
|
+
|
|
43
|
+
/** Write a script into the home and start a run of it. */
|
|
44
|
+
async function run(niwa: Host, home: string, source: string): Promise<void> {
|
|
45
|
+
const file = join(home, "script.js");
|
|
46
|
+
await writeFile(file, source);
|
|
47
|
+
await niwa.call("workflow.start" as ActionName, { file } as never);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const finishedOf = async (niwa: Host): Promise<unknown> =>
|
|
51
|
+
(ofType(await recordsOf(niwa), "workflow.run.finished")[0]?.data as { outcome?: { result?: unknown } } | undefined)?.outcome?.result;
|
|
52
|
+
|
|
53
|
+
const hasFinished = async (niwa: Host): Promise<boolean> =>
|
|
54
|
+
ofType(await recordsOf(niwa), "workflow.run.finished").length > 0;
|
|
55
|
+
|
|
56
|
+
const armed = async (niwa: Host, count = 1): Promise<boolean> =>
|
|
57
|
+
ofType(await recordsOf(niwa), "timer.sleep.requested").length >= count;
|
|
58
|
+
|
|
59
|
+
test("W1b sleep records its wake-up moment on the first pass; a restart does not sleep again from the start", async () => {
|
|
60
|
+
const home = await tempHome();
|
|
61
|
+
homes.push(home);
|
|
62
|
+
const first = await hostOn(home.path, testClock(START));
|
|
63
|
+
await run(first, home.path, `export default workflow({ output: z.string() }, async () => { await sleep("5m"); return "up"; });`);
|
|
64
|
+
await eventually(() => armed(first), 10_000, "the sleep should be armed");
|
|
65
|
+
const request = ofType(await recordsOf(first), "timer.sleep.requested")[0]?.data as { at: number };
|
|
66
|
+
expect(request.at).toBe(START + 5 * MINUTE);
|
|
67
|
+
await first.close();
|
|
68
|
+
|
|
69
|
+
// The new host comes up two minutes later: the script is replayed from the top, and the sleep
|
|
70
|
+
// that began at the start of the run ends at its own moment, three minutes from now.
|
|
71
|
+
const clock = testClock(START + 2 * MINUTE);
|
|
72
|
+
const second = await hostOn(home.path, clock);
|
|
73
|
+
await eventually(
|
|
74
|
+
async () => (await recordsOf(second)).some((row) => row.type === "niwa.invocation.state"),
|
|
75
|
+
10_000,
|
|
76
|
+
);
|
|
77
|
+
await clock.advance(3 * MINUTE - 1);
|
|
78
|
+
expect(await hasFinished(second)).toBe(false);
|
|
79
|
+
await clock.advance(1);
|
|
80
|
+
await eventually(() => hasFinished(second), 10_000, "the run should wake");
|
|
81
|
+
expect(await finishedOf(second)).toBe("up");
|
|
82
|
+
const records = await recordsOf(second);
|
|
83
|
+
expect(ofType(records, "timer.sleep.requested")).toHaveLength(1);
|
|
84
|
+
expect(ofType(records, "timer.slept")).toHaveLength(1);
|
|
85
|
+
}, 30_000);
|
|
86
|
+
|
|
87
|
+
test("wait gives the data and the position of the record, or {timedOut: true} at the end of the timeout", async () => {
|
|
88
|
+
const home = await tempHome();
|
|
89
|
+
homes.push(home);
|
|
90
|
+
const clock = testClock(START);
|
|
91
|
+
const niwa = await hostOn(home.path, clock);
|
|
92
|
+
await run(
|
|
93
|
+
niwa,
|
|
94
|
+
home.path,
|
|
95
|
+
`export default workflow({ output: z.json() }, async () => {
|
|
96
|
+
return await wait("stub.finished", { filter: { id: "never" }, timeout: "10m" });
|
|
97
|
+
});`,
|
|
98
|
+
);
|
|
99
|
+
await eventually(() => armed(niwa), 10_000, "the timeout should be armed");
|
|
100
|
+
await clock.advance(10 * MINUTE);
|
|
101
|
+
await eventually(() => hasFinished(niwa), 10_000, "the run should end by the timeout");
|
|
102
|
+
expect(await finishedOf(niwa)).toEqual({ timedOut: true });
|
|
103
|
+
}, 30_000);
|
|
104
|
+
|
|
105
|
+
test("the wake of another sleep does not end a wait; the record does", async () => {
|
|
106
|
+
const home = await tempHome();
|
|
107
|
+
homes.push(home);
|
|
108
|
+
const clock = testClock(START);
|
|
109
|
+
const niwa = await hostOn(home.path, clock);
|
|
110
|
+
await run(
|
|
111
|
+
niwa,
|
|
112
|
+
home.path,
|
|
113
|
+
`export default workflow({ output: z.json() }, async () => {
|
|
114
|
+
return await parallel([
|
|
115
|
+
() => wait("stub.finished", { filter: { id: "x" }, timeout: "10m" }),
|
|
116
|
+
async () => { await sleep("1m"); return "slept"; },
|
|
117
|
+
]);
|
|
118
|
+
});`,
|
|
119
|
+
);
|
|
120
|
+
await eventually(() => armed(niwa, 2), 10_000, "both wakes should be armed");
|
|
121
|
+
await clock.advance(MINUTE);
|
|
122
|
+
await eventually(
|
|
123
|
+
async () => ofType(await recordsOf(niwa), "timer.slept").length === 1,
|
|
124
|
+
10_000,
|
|
125
|
+
"the sleep should wake",
|
|
126
|
+
);
|
|
127
|
+
// Give the invocation the time it would need to end wrongly.
|
|
128
|
+
await Bun.sleep(300);
|
|
129
|
+
expect(await hasFinished(niwa)).toBe(false);
|
|
130
|
+
// A record of the type that is not the one asked for does not end it either.
|
|
131
|
+
await niwa.call("stub.finish" as ActionName, { id: "y" } as never);
|
|
132
|
+
await niwa.call("stub.finish" as ActionName, { id: "x" } as never);
|
|
133
|
+
await eventually(() => hasFinished(niwa), 10_000, "the record should end the wait");
|
|
134
|
+
expect(await finishedOf(niwa)).toEqual([{ data: { id: "x" }, at: expect.any(Number) }, "slept"]);
|
|
135
|
+
}, 30_000);
|
|
136
|
+
|
|
137
|
+
test("ask: no answer within the timeout is an error the script catches, not a false answer", async () => {
|
|
138
|
+
const home = await tempHome();
|
|
139
|
+
homes.push(home);
|
|
140
|
+
const clock = testClock(START);
|
|
141
|
+
const niwa = await hostOn(home.path, clock);
|
|
142
|
+
await run(
|
|
143
|
+
niwa,
|
|
144
|
+
home.path,
|
|
145
|
+
`export default workflow({ output: z.string() }, async () => {
|
|
146
|
+
try { return await ask.confirm("Keep waiting?", { timeout: "10m" }); } catch (error) { return error.name; }
|
|
147
|
+
});`,
|
|
148
|
+
);
|
|
149
|
+
await eventually(
|
|
150
|
+
async () => ofType(await recordsOf(niwa), "request.created").length === 1,
|
|
151
|
+
10_000,
|
|
152
|
+
"the owner should be asked",
|
|
153
|
+
);
|
|
154
|
+
await clock.advance(10 * MINUTE);
|
|
155
|
+
await eventually(() => hasFinished(niwa), 10_000, "the ask should end by the timeout");
|
|
156
|
+
expect(await finishedOf(niwa)).toBe("AskExpired");
|
|
157
|
+
}, 30_000);
|
|
158
|
+
|
|
159
|
+
test("ask: the answer of the owner is the value", async () => {
|
|
160
|
+
const home = await tempHome();
|
|
161
|
+
homes.push(home);
|
|
162
|
+
const niwa = await hostOn(home.path, testClock(START));
|
|
163
|
+
const owner = await connect({ home: home.path, id: "owner:w1", token: ownerTokenOf(home.path) });
|
|
164
|
+
try {
|
|
165
|
+
await run(
|
|
166
|
+
niwa,
|
|
167
|
+
home.path,
|
|
168
|
+
`export default workflow({ output: z.json() }, async () => {
|
|
169
|
+
return await ask.confirm("Keep waiting?", { timeout: "10m" });
|
|
170
|
+
});`,
|
|
171
|
+
);
|
|
172
|
+
await eventually(
|
|
173
|
+
async () => ofType(await recordsOf(niwa), "request.created").length === 1,
|
|
174
|
+
10_000,
|
|
175
|
+
"the owner should be asked",
|
|
176
|
+
);
|
|
177
|
+
const id = (ofType(await recordsOf(niwa), "request.created")[0]?.data as { id: string }).id;
|
|
178
|
+
await owner.call("request.answer" as ActionName, { id, answer: { ids: ["yes"] } } as never);
|
|
179
|
+
await eventually(() => hasFinished(niwa), 10_000, "the answer should end the ask");
|
|
180
|
+
expect(await finishedOf(niwa)).toBe(true);
|
|
181
|
+
} finally {
|
|
182
|
+
await owner.close().catch(() => {});
|
|
183
|
+
}
|
|
184
|
+
}, 30_000);
|
|
185
|
+
|
|
186
|
+
test("call, emit, read, now and random", async () => {
|
|
187
|
+
const home = await tempHome();
|
|
188
|
+
homes.push(home);
|
|
189
|
+
const niwa = await hostOn(home.path, testClock(START));
|
|
190
|
+
await niwa.call("stub.bump" as ActionName, {} as never);
|
|
191
|
+
await run(
|
|
192
|
+
niwa,
|
|
193
|
+
home.path,
|
|
194
|
+
`
|
|
195
|
+
const Note = record("note", z.object({ text: z.string() }));
|
|
196
|
+
export default workflow({ name: "demo", emits: [Note], output: z.json() }, async () => {
|
|
197
|
+
const group = typeof (await niwa.stub);
|
|
198
|
+
const made = await call("stub.create", { name: "x" });
|
|
199
|
+
await niwa.stub.finish({ id: made.id });
|
|
200
|
+
await emit(Note, { text: "hello" });
|
|
201
|
+
const state = await read.state("stub");
|
|
202
|
+
const { records } = await read.records({ types: ["stub.finished"] });
|
|
203
|
+
return { group, made, state, finished: records.map((r) => r.data), at: await now(), dice: await random() };
|
|
204
|
+
});`,
|
|
205
|
+
);
|
|
206
|
+
await eventually(() => hasFinished(niwa), 10_000, "the run should finish");
|
|
207
|
+
const result = (await finishedOf(niwa)) as Record<string, unknown>;
|
|
208
|
+
expect(result.group).toBe("function");
|
|
209
|
+
expect(result.made).toEqual({ id: "t-x" });
|
|
210
|
+
expect(result.state).toEqual({ count: 1 });
|
|
211
|
+
expect(result.finished).toEqual([{ id: "t-x" }]);
|
|
212
|
+
expect(result.at).toBe(START);
|
|
213
|
+
expect(typeof result.dice).toBe("number");
|
|
214
|
+
expect(ofType(await recordsOf(niwa), "wf.demo.note")).toHaveLength(1);
|
|
215
|
+
}, 30_000);
|
|
216
|
+
|
|
217
|
+
test("emit refuses what is not a record the script lists: a type by name is not one", async () => {
|
|
218
|
+
const home = await tempHome();
|
|
219
|
+
homes.push(home);
|
|
220
|
+
const niwa = await hostOn(home.path, testClock(START));
|
|
221
|
+
await run(niwa, home.path, `export default workflow({ name: "demo" }, async () => { const write = emit; await write("stub.finished", { id: "z" }); });`);
|
|
222
|
+
await eventually(
|
|
223
|
+
async () => ofType(await recordsOf(niwa), "workflow.run.paused").length === 1,
|
|
224
|
+
10_000,
|
|
225
|
+
"the run should pause on the refusal",
|
|
226
|
+
);
|
|
227
|
+
const paused = ofType(await recordsOf(niwa), "workflow.run.paused")[0]?.data as { reason: string };
|
|
228
|
+
expect(paused.reason).toContain("lists in workflow({ emits })");
|
|
229
|
+
expect(ofType(await recordsOf(niwa), "stub.finished")).toHaveLength(0);
|
|
230
|
+
}, 30_000);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// The script is a client of niwa: it calls any action, waits for a record,
|
|
2
|
+
// and a restart of the host does not repeat what finished.
|
|
3
|
+
//
|
|
4
|
+
// The scenarios run against real host processes (test/fixtures/bridge-host.ts) that are killed.
|
|
5
|
+
// Only the public API is used: the entries of the packages, and of testing.
|
|
6
|
+
|
|
7
|
+
import { expect, test } from "bun:test";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
|
|
10
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
11
|
+
import { counter, eventually, gate, spawnHost, tempHome, watch, connectAsOwner } from "@niwa-labs/testing";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/bridge-host.ts", import.meta.url));
|
|
16
|
+
|
|
17
|
+
const RunOutput = z.object({ run: z.string(), finished: z.boolean() });
|
|
18
|
+
|
|
19
|
+
function ofType(seen: StoredRecord[], type: string): StoredRecord[] {
|
|
20
|
+
return seen.filter((record) => record.type === type);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The state of the latest invocation of the workflow handler, as the journal holds it. */
|
|
24
|
+
function stateOfRun(seen: StoredRecord[]): string | undefined {
|
|
25
|
+
let state: string | undefined;
|
|
26
|
+
for (const record of ofType(seen, "niwa.invocation.state")) {
|
|
27
|
+
const data = record.data as { invocation: string; state: string };
|
|
28
|
+
if (data.invocation.startsWith("inv:workflow/")) state = data.state;
|
|
29
|
+
}
|
|
30
|
+
return state;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
test("W1a a script calls an action and waits for a record; a restart repeats neither", async () => {
|
|
34
|
+
const home = await tempHome();
|
|
35
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
36
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
37
|
+
let observed: Awaited<ReturnType<typeof watch>> | undefined;
|
|
38
|
+
try {
|
|
39
|
+
first = spawnHost(HOST_SCRIPT, home.path);
|
|
40
|
+
await first.ready();
|
|
41
|
+
const client = await connectAsOwner(home.path);
|
|
42
|
+
observed = await watch(home.path);
|
|
43
|
+
const seen = observed.seen;
|
|
44
|
+
|
|
45
|
+
RunOutput.parse(
|
|
46
|
+
await client.call("workflow.start" as ActionName, { file: fileURLToPath(new URL("./fixtures/bridge-wait-script.js", import.meta.url)) } as never),
|
|
47
|
+
);
|
|
48
|
+
await eventually(() => counter(home.path, "w1.create").count() === 1, 20_000, "the thing should be made");
|
|
49
|
+
await eventually(() => stateOfRun(seen) === "waiting", 20_000, "the run should wait for the record");
|
|
50
|
+
expect(ofType(seen, "timer.sleep.requested")).toHaveLength(1);
|
|
51
|
+
|
|
52
|
+
// The host dies while the script waits.
|
|
53
|
+
await first.kill();
|
|
54
|
+
first = undefined;
|
|
55
|
+
await client.close().catch(() => {});
|
|
56
|
+
|
|
57
|
+
second = spawnHost(HOST_SCRIPT, home.path);
|
|
58
|
+
await second.ready();
|
|
59
|
+
const again = await connectAsOwner(home.path);
|
|
60
|
+
await eventually(() => stateOfRun(seen) === "waiting", 20_000, "the run should keep waiting");
|
|
61
|
+
expect(ofType(seen, "workflow.run.finished")).toHaveLength(0);
|
|
62
|
+
|
|
63
|
+
await again.call("stub.finish" as ActionName, { id: "t-lockfile" } as never);
|
|
64
|
+
await eventually(() => ofType(seen, "workflow.run.finished").length > 0, 30_000, "the run should finish");
|
|
65
|
+
|
|
66
|
+
const finished = ofType(seen, "workflow.run.finished")[0];
|
|
67
|
+
expect((finished?.data as { outcome?: { result?: unknown } }).outcome?.result).toEqual({
|
|
68
|
+
made: { id: "t-lockfile" },
|
|
69
|
+
done: { id: "t-lockfile" },
|
|
70
|
+
});
|
|
71
|
+
// The thing was made once, and the wake of the timeout was asked for once.
|
|
72
|
+
expect(counter(home.path, "w1.create").count()).toBe(1);
|
|
73
|
+
expect(ofType(seen, "timer.sleep.requested")).toHaveLength(1);
|
|
74
|
+
await again.close().catch(() => {});
|
|
75
|
+
} finally {
|
|
76
|
+
await observed?.stop();
|
|
77
|
+
await first?.kill().catch(() => {});
|
|
78
|
+
await second?.kill().catch(() => {});
|
|
79
|
+
await home.cleanup();
|
|
80
|
+
}
|
|
81
|
+
}, 90_000);
|
|
82
|
+
|
|
83
|
+
test("W1c calls that arrive in another order on a replay keep their results", async () => {
|
|
84
|
+
const home = await tempHome();
|
|
85
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
86
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
87
|
+
let observed: Awaited<ReturnType<typeof watch>> | undefined;
|
|
88
|
+
try {
|
|
89
|
+
first = spawnHost(HOST_SCRIPT, home.path);
|
|
90
|
+
await first.ready();
|
|
91
|
+
const client = await connectAsOwner(home.path);
|
|
92
|
+
observed = await watch(home.path);
|
|
93
|
+
const seen = observed.seen;
|
|
94
|
+
|
|
95
|
+
RunOutput.parse(
|
|
96
|
+
await client.call("workflow.start" as ActionName, { file: fileURLToPath(new URL("./fixtures/bridge-keys-script.js", import.meta.url)) } as never),
|
|
97
|
+
);
|
|
98
|
+
await eventually(
|
|
99
|
+
() => counter(home.path, "w1.hold.a").count() === 1 && counter(home.path, "w1.hold.b").count() === 1,
|
|
100
|
+
20_000,
|
|
101
|
+
"both branches should have called",
|
|
102
|
+
);
|
|
103
|
+
// The second branch answers first, and reads a random number. The first is still in flight.
|
|
104
|
+
gate(home.path, "w1.hold.b").open();
|
|
105
|
+
await eventually(
|
|
106
|
+
() => ofType(seen, "niwa.step.outcome").some((record) => (record.data as { key: string }).key === "p1.1.call-2"),
|
|
107
|
+
20_000,
|
|
108
|
+
"the second branch should have read its random number",
|
|
109
|
+
);
|
|
110
|
+
const recorded = ofType(seen, "niwa.step.outcome").find(
|
|
111
|
+
(record) => (record.data as { key: string }).key === "p1.1.call-2",
|
|
112
|
+
);
|
|
113
|
+
const dice = (recorded?.data as { outcome: { value: number } }).outcome.value;
|
|
114
|
+
|
|
115
|
+
await first.kill();
|
|
116
|
+
first = undefined;
|
|
117
|
+
await client.close().catch(() => {});
|
|
118
|
+
|
|
119
|
+
// On the replay the second branch is answered from the journal at once, before the first.
|
|
120
|
+
second = spawnHost(HOST_SCRIPT, home.path);
|
|
121
|
+
await second.ready();
|
|
122
|
+
gate(home.path, "w1.hold.a").open();
|
|
123
|
+
await eventually(() => ofType(seen, "workflow.run.finished").length > 0, 30_000, "the run should finish");
|
|
124
|
+
|
|
125
|
+
const result = (ofType(seen, "workflow.run.finished")[0]?.data as { outcome: { result: Array<Record<string, unknown>> } }).outcome.result;
|
|
126
|
+
expect(result[0]?.held).toEqual({ name: "a" });
|
|
127
|
+
expect(result[1]).toEqual({ held: { name: "b" }, dice });
|
|
128
|
+
// The call that finished was not made again; the one in flight was.
|
|
129
|
+
expect(counter(home.path, "w1.hold.b").count()).toBe(1);
|
|
130
|
+
expect(counter(home.path, "w1.hold.a").count()).toBe(2);
|
|
131
|
+
// The clock and the random number were read once each.
|
|
132
|
+
const keys = ofType(seen, "niwa.step.intent").map((record) => (record.data as { key: string }).key);
|
|
133
|
+
expect(keys.filter((key) => key === "p1.1.call-2")).toHaveLength(1);
|
|
134
|
+
expect(keys.filter((key) => key === "p1.0.call-2")).toHaveLength(1);
|
|
135
|
+
} finally {
|
|
136
|
+
await observed?.stop();
|
|
137
|
+
await first?.kill().catch(() => {});
|
|
138
|
+
await second?.kill().catch(() => {});
|
|
139
|
+
await home.cleanup();
|
|
140
|
+
}
|
|
141
|
+
}, 90_000);
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// A built-in offered with `gated: true` comes in as a candidate: the sync registers each new version of it as one,
|
|
2
|
+
// and neither the latest version nor the installation moves to it. Before any version was accepted the candidate is
|
|
3
|
+
// shown and starts on nothing. A host that starts again with the same file counts the candidate as in place. A suite
|
|
4
|
+
// the same plugin ships starts the candidate by its version and promotes it: then it is latest and installed.
|
|
5
|
+
|
|
6
|
+
import { afterEach, expect, test } from "bun:test";
|
|
7
|
+
import { writeFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
|
|
10
|
+
import { record } from "@niwa-labs/core";
|
|
11
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
12
|
+
import { BUILTIN_WORKFLOW, plugin } from "@niwa-labs/plugin";
|
|
13
|
+
import { createNiwa, eventually, tempDir, tempHome } from "@niwa-labs/testing";
|
|
14
|
+
import type { Host } from "@niwa-labs/testing";
|
|
15
|
+
import { BuiltinsWanted, workflow, workflowBuiltinPromote, workflowGet, workflowInstallationList, workflowList, workflowStart, workflowWait } from "../src/index.ts";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
|
|
18
|
+
import { versionOf } from "../src/registry.ts";
|
|
19
|
+
|
|
20
|
+
const Go = record("gated.go", z.object({ n: z.number() }));
|
|
21
|
+
const Release = record("gated.release", z.object({ n: z.number() }));
|
|
22
|
+
|
|
23
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
24
|
+
const hosts: Host[] = [];
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
28
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/** A plugin that ships the gated built-in `entry`, and the suite that tries it and promotes it. */
|
|
32
|
+
const shipper = (file: string, suite?: string, router?: string) =>
|
|
33
|
+
plugin("gated", (api) => {
|
|
34
|
+
api.declare(Go, Release);
|
|
35
|
+
api.offer(BUILTIN_WORKFLOW, { name: "entry", file, description: "Answer every go", triggers: [Go], gated: true });
|
|
36
|
+
if (suite !== undefined) api.offer(BUILTIN_WORKFLOW, { name: "suite", file: suite, description: "Try the entry and promote it", suite: true });
|
|
37
|
+
if (router !== undefined) api.offer(BUILTIN_WORKFLOW, { name: "router", file: router, description: "Route to the entry" });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** The suite opens a durable trial, checks an answer and optionally waits before promoting. */
|
|
41
|
+
function suiteScript(file: string): void {
|
|
42
|
+
writeFileSync(
|
|
43
|
+
file,
|
|
44
|
+
[
|
|
45
|
+
"export default workflow({",
|
|
46
|
+
' uses: ["workflow.start", "workflow.builtin.trial", "workflow.builtin.promote", "workflow.list", "niwa.records.read"],',
|
|
47
|
+
" input: z.object({ expected: z.string(), router: z.string().optional(), release: z.number().optional() }),",
|
|
48
|
+
" output: z.string(),",
|
|
49
|
+
"}, async ({ expected, router, release }) => {",
|
|
50
|
+
" const { entries } = await niwa.workflow.builtin.trial({});",
|
|
51
|
+
' const entry = entries.find((one) => one.name === "entry");',
|
|
52
|
+
' if (entry === undefined || !entries.some((one) => one.candidate === true)) return "nothing";',
|
|
53
|
+
" const done = await niwa.workflow.start({ id: router ?? entry.workflow, args: router === undefined ? { n: 1 } : { id: entry.workflow }, wait: true, timeoutMs: 20000 });",
|
|
54
|
+
' if (done.outcome?.status !== "done" || done.outcome.result !== expected) return `failed: ${JSON.stringify(done)}`;',
|
|
55
|
+
' if (release !== undefined) await wait("gated.release", { filter: { n: release }, from: 0 });',
|
|
56
|
+
" return (await niwa.workflow.builtin.promote({})).status;",
|
|
57
|
+
"});",
|
|
58
|
+
].join("\n"),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The entry: an installation of it is held to what it uses, which the module's own handler holds. */
|
|
63
|
+
function script(file: string, text: string): void {
|
|
64
|
+
writeFileSync(file, `export default workflow({ on: ["gated.go"], uses: ["workflow.list"], output: z.string() }, async () => ${JSON.stringify(text)});\n`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function served(home: string, file: string, suite?: string, router?: string): Promise<Host> {
|
|
68
|
+
const host = createNiwa({ home });
|
|
69
|
+
host.use(shipper(file, suite, router), workflow());
|
|
70
|
+
await host.serve();
|
|
71
|
+
hosts.push(host);
|
|
72
|
+
return host;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const ofType = async (host: Host, type: string): Promise<StoredRecord[]> => (await host.records.read()).filter((row) => row.type === type);
|
|
76
|
+
const failuresOf = async (host: Host) => (await ofType(host, "niwa.step.outcome")).filter((row) => (row.data as { outcome: { status: string } }).outcome.status === "failed").map((row) => row.data);
|
|
77
|
+
|
|
78
|
+
test("a gated built-in with no accepted version is a candidate: shown, not installed, not started", async () => {
|
|
79
|
+
const home = await tempHome();
|
|
80
|
+
homes.push(home);
|
|
81
|
+
const file = join(await tempDir("gated"), "entry.ts");
|
|
82
|
+
script(file, "v1");
|
|
83
|
+
const host = await served(home.path, file);
|
|
84
|
+
await eventually(async () => (await ofType(host, "workflow.registered")).length === 1, 20_000, "the candidate was not registered");
|
|
85
|
+
await Bun.sleep(300);
|
|
86
|
+
|
|
87
|
+
const [listed] = (await host.call(workflowList, {})).items;
|
|
88
|
+
const got = await host.call(workflowGet, { id: listed?.id ?? "" });
|
|
89
|
+
expect(got.versions.map((one) => one.candidate)).toEqual([true]);
|
|
90
|
+
expect((await host.call(workflowInstallationList, {})).items).toEqual([]);
|
|
91
|
+
await expect(host.call(workflowStart, { id: listed?.id ?? "" })).rejects.toThrow("candidate");
|
|
92
|
+
|
|
93
|
+
await host.emit(Go, { n: 1 });
|
|
94
|
+
await Bun.sleep(500);
|
|
95
|
+
expect(await ofType(host, "workflow.run.requested")).toEqual([]);
|
|
96
|
+
|
|
97
|
+
// The same file again: the candidate is in place, and nothing is asked for.
|
|
98
|
+
await host.close();
|
|
99
|
+
hosts.splice(hosts.indexOf(host), 1);
|
|
100
|
+
const again = await served(home.path, file);
|
|
101
|
+
await Bun.sleep(500);
|
|
102
|
+
expect(await ofType(again, "workflow.builtins.wanted")).toHaveLength(1);
|
|
103
|
+
expect(await ofType(again, "workflow.registered")).toHaveLength(1);
|
|
104
|
+
}, 60_000);
|
|
105
|
+
|
|
106
|
+
const results = async (host: Host, workflowId: string): Promise<unknown[]> => {
|
|
107
|
+
const runs = new Set((await ofType(host, "workflow.run.requested")).filter((row) => (row.data as { workflow?: string }).workflow === workflowId).map((row) => (row.data as { run: string }).run));
|
|
108
|
+
return (await ofType(host, "workflow.run.finished")).filter((row) => runs.has((row.data as { run: string }).run)).map((row) => (row.data as { outcome: { result?: unknown } }).outcome.result);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
test("a suite of the same plugin starts a candidate by its version and promotes it; then a new version waits as a candidate", async () => {
|
|
112
|
+
const home = await tempHome();
|
|
113
|
+
homes.push(home);
|
|
114
|
+
const dir = await tempDir("gated");
|
|
115
|
+
const file = join(dir, "entry.ts");
|
|
116
|
+
const suite = join(dir, "suite.ts");
|
|
117
|
+
script(file, "v1");
|
|
118
|
+
suiteScript(suite);
|
|
119
|
+
const host = await served(home.path, file, suite);
|
|
120
|
+
await eventually(async () => (await host.call(workflowList, {})).items.length === 2, 20_000, "the built-ins were not registered");
|
|
121
|
+
const items = (await host.call(workflowList, {})).items;
|
|
122
|
+
const entry = items.find((one) => one.builtin?.name === "entry");
|
|
123
|
+
const tried = items.find((one) => one.builtin?.name === "suite");
|
|
124
|
+
const v1 = entry?.version ?? "";
|
|
125
|
+
|
|
126
|
+
// Nobody but the suite starts a candidate by its version, or promotes it.
|
|
127
|
+
await expect(host.call(workflowStart, { id: entry?.id ?? "", version: v1 })).rejects.toThrow("candidate");
|
|
128
|
+
await expect(host.call(workflowBuiltinPromote, {})).rejects.toThrow("only a designated built-in suite");
|
|
129
|
+
|
|
130
|
+
const { run } = await host.call(workflowStart, { id: tried?.id ?? "", args: { expected: "v1" } });
|
|
131
|
+
const status = await host.call(workflowWait, { run, timeoutMs: 30_000 });
|
|
132
|
+
if (status.outcome === undefined) throw new Error(JSON.stringify({ status, failed: await failuresOf(host) }));
|
|
133
|
+
expect(status.outcome).toEqual({ status: "done", result: "promoted" });
|
|
134
|
+
const promoted = await ofType(host, "workflow.builtin.promoted");
|
|
135
|
+
expect(promoted.map((row) => row.data)).toEqual([expect.objectContaining({ run, versions: [{ workflow: entry?.id, version: v1 }] })]);
|
|
136
|
+
const got = await host.call(workflowGet, { id: entry?.id ?? "" });
|
|
137
|
+
expect(got.versions.map((one) => one.candidate)).toEqual([undefined]);
|
|
138
|
+
await eventually(async () => (await host.call(workflowInstallationList, {})).items.some((one) => one.workflow === entry?.id), 20_000, "the promoted version was not installed");
|
|
139
|
+
// Installed as the sync installs a built-in: by the module itself, not by the run that promoted it.
|
|
140
|
+
const made = (await ofType(host, "workflow.subscription.installed")).map((row) => row.data as { workflow?: string; installer?: unknown });
|
|
141
|
+
expect(made.filter((one) => one.workflow === entry?.id).map((one) => one.installer)).toEqual([undefined]);
|
|
142
|
+
await host.emit(Go, { n: 1 });
|
|
143
|
+
await eventually(async () => (await results(host, entry?.id ?? "")).includes("v1"), 20_000, "the promoted version did not run");
|
|
144
|
+
await host.close();
|
|
145
|
+
hosts.splice(hosts.indexOf(host), 1);
|
|
146
|
+
|
|
147
|
+
// A new build: the new version is a candidate, and the accepted one stays latest and installed.
|
|
148
|
+
script(file, "v2");
|
|
149
|
+
const next = await served(home.path, file, suite);
|
|
150
|
+
await eventually(async () => (await next.call(workflowGet, { id: entry?.id ?? "" })).versions.length === 2, 20_000, "the candidate was not registered");
|
|
151
|
+
const after = await next.call(workflowGet, { id: entry?.id ?? "" });
|
|
152
|
+
expect(after.versions.map((one) => one.candidate)).toEqual([undefined, true]);
|
|
153
|
+
const listed = (await next.call(workflowList, {})).items.find((one) => one.id === entry?.id);
|
|
154
|
+
expect(listed?.version).toBe(v1);
|
|
155
|
+
const installed = (await next.call(workflowInstallationList, {})).items.filter((one) => one.workflow === entry?.id);
|
|
156
|
+
expect(installed).toHaveLength(1);
|
|
157
|
+
expect((await next.call(workflowStart, { id: entry?.id ?? "", args: { n: 2 }, wait: true, timeoutMs: 20_000 })).outcome).toEqual({ status: "done", result: "v1" });
|
|
158
|
+
}, 90_000);
|
|
159
|
+
|
|
160
|
+
test("trial descendants use the pinned candidate and a late suite cannot roll it back", async () => {
|
|
161
|
+
const home = await tempHome();
|
|
162
|
+
homes.push(home);
|
|
163
|
+
const dir = await tempDir("gated-trial");
|
|
164
|
+
const file = join(dir, "entry.ts");
|
|
165
|
+
const suite = join(dir, "suite.ts");
|
|
166
|
+
const router = join(dir, "router.ts");
|
|
167
|
+
script(file, "v1");
|
|
168
|
+
suiteScript(suite);
|
|
169
|
+
writeFileSync(router, `export default workflow({
|
|
170
|
+
description: "Route to the entry",
|
|
171
|
+
uses: ["workflow.start", "workflow.list"],
|
|
172
|
+
input: z.object({ id: z.string(), version: z.string().optional() }),
|
|
173
|
+
output: z.string(),
|
|
174
|
+
}, async ({ id, version }) => {
|
|
175
|
+
const called = await niwa.workflow.start({ id, version, args: { n: 1 }, wait: true, timeoutMs: 20000 });
|
|
176
|
+
const result = called.outcome?.status === "done" ? called.outcome.result : undefined;
|
|
177
|
+
return typeof result === "string" ? result : "no result";
|
|
178
|
+
});\n`);
|
|
179
|
+
const host = await served(home.path, file, suite, router);
|
|
180
|
+
await eventually(async () => {
|
|
181
|
+
const failures = await failuresOf(host);
|
|
182
|
+
if (failures.length > 0) throw new Error(JSON.stringify(failures));
|
|
183
|
+
return (await host.call(workflowList, {})).items.length === 3;
|
|
184
|
+
}, 20_000);
|
|
185
|
+
const items = (await host.call(workflowList, {})).items;
|
|
186
|
+
const entry = items.find((one) => one.builtin?.name === "entry")!;
|
|
187
|
+
const suiteId = items.find((one) => one.builtin?.name === "suite")!.id;
|
|
188
|
+
const routerId = items.find((one) => one.builtin?.name === "router")!.id;
|
|
189
|
+
const v1 = entry.version;
|
|
190
|
+
const gate = (run: string) => eventually(async () => (await host.call(workflowWait, { run, timeoutMs: 10 })).waiting?.includes("gated.release") === true, 20_000);
|
|
191
|
+
const finished = (run: string) => host.call(workflowWait, { run, timeoutMs: 20_000 });
|
|
192
|
+
|
|
193
|
+
// An ordinary sibling built-in cannot pass an explicit candidate into a child.
|
|
194
|
+
const ordinary = await host.call(workflowStart, { id: routerId, args: { id: entry.id, version: v1 }, wait: true, timeoutMs: 20_000 });
|
|
195
|
+
expect(ordinary.state).toBe("paused");
|
|
196
|
+
expect((await ofType(host, "workflow.run.requested")).some((row) => (row.data as { version?: string }).version === v1)).toBe(false);
|
|
197
|
+
|
|
198
|
+
const older = await host.call(workflowStart, { id: suiteId, args: { expected: "v1", router: routerId, release: 1 } });
|
|
199
|
+
await gate(older.run);
|
|
200
|
+
// Submit the same offer update the host writes when a changed build starts. The sync registers the version.
|
|
201
|
+
script(file, "v2");
|
|
202
|
+
const v2 = versionOf(file)!;
|
|
203
|
+
const prior = BuiltinsWanted.schema.parse((await ofType(host, BuiltinsWanted.name)).at(-1)?.data);
|
|
204
|
+
await host.emit(BuiltinsWanted, { items: prior.items.map((item) => item.name === "entry" ? { ...item, version: v2 } : item) });
|
|
205
|
+
await eventually(async () => (await host.call(workflowGet, { id: entry.id })).versions.length === 2, 20_000);
|
|
206
|
+
const before = (await host.call(workflowGet, { id: entry.id })).versions;
|
|
207
|
+
expect(before.map(({ version, candidate, superseded }) => ({ version, candidate, superseded }))).toEqual([
|
|
208
|
+
{ version: v1, candidate: undefined, superseded: true },
|
|
209
|
+
{ version: v2, candidate: true, superseded: undefined },
|
|
210
|
+
]);
|
|
211
|
+
await expect(host.call(workflowStart, { id: entry.id, version: v1, args: { n: 1 } })).rejects.toThrow("unaccepted candidate");
|
|
212
|
+
|
|
213
|
+
const newer = await host.call(workflowStart, { id: suiteId, args: { expected: "v2", router: routerId, release: 2 } });
|
|
214
|
+
await gate(newer.run);
|
|
215
|
+
const same = await host.call(workflowStart, { id: suiteId, args: { expected: "v2", router: routerId, release: 3 } });
|
|
216
|
+
await gate(same.run);
|
|
217
|
+
await host.emit(Release, { n: 2 });
|
|
218
|
+
expect((await finished(newer.run)).outcome).toEqual({ status: "done", result: "promoted" });
|
|
219
|
+
await host.emit(Release, { n: 1 });
|
|
220
|
+
expect((await finished(older.run)).outcome).toEqual({ status: "done", result: "stale" });
|
|
221
|
+
await host.emit(Release, { n: 3 });
|
|
222
|
+
expect((await finished(same.run)).outcome).toEqual({ status: "done", result: "stale" });
|
|
223
|
+
expect((await ofType(host, "niwa.record.refused")).map((row) => row.data)).toContainEqual(expect.objectContaining({ code: "stale-builtin-trial" }));
|
|
224
|
+
const repeated = await host.call(workflowStart, { id: suiteId, args: { expected: "v2" }, wait: true, timeoutMs: 20_000 });
|
|
225
|
+
expect(repeated.outcome).toEqual({ status: "done", result: "nothing" });
|
|
226
|
+
expect((await ofType(host, "workflow.builtin.promoted")).map((row) => row.data)).toEqual([
|
|
227
|
+
expect.objectContaining({ run: newer.run, versions: [{ workflow: entry.id, version: v2 }] }),
|
|
228
|
+
]);
|
|
229
|
+
expect((await host.call(workflowStart, { id: entry.id, args: { n: 1 }, wait: true, timeoutMs: 20_000 })).outcome).toEqual({ status: "done", result: "v2" });
|
|
230
|
+
}, 90_000);
|