@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.
Files changed (114) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -2
  3. package/package.json +55 -4
  4. package/src/actions.ts +408 -0
  5. package/src/agent.ts +211 -0
  6. package/src/ask.ts +84 -0
  7. package/src/bridge.ts +710 -0
  8. package/src/catalogue.ts +74 -0
  9. package/src/check.ts +880 -0
  10. package/src/child.ts +1378 -0
  11. package/src/cockpit/actions.ts +128 -0
  12. package/src/cockpit/catalogue-actions.ts +97 -0
  13. package/src/cockpit/dashboard.tsx +134 -0
  14. package/src/cockpit/groups.ts +45 -0
  15. package/src/cockpit/index.tsx +50 -0
  16. package/src/cockpit/launch.tsx +136 -0
  17. package/src/cockpit/plan.ts +101 -0
  18. package/src/cockpit/run-commands.tsx +102 -0
  19. package/src/cockpit/run-page.tsx +140 -0
  20. package/src/cockpit/run-summary.ts +16 -0
  21. package/src/cockpit/runs.tsx +73 -0
  22. package/src/cockpit/statistics.tsx +176 -0
  23. package/src/cockpit/track.tsx +159 -0
  24. package/src/cockpit/workflows.tsx +383 -0
  25. package/src/describe-cache.ts +122 -0
  26. package/src/describe.ts +174 -0
  27. package/src/guide.ts +213 -0
  28. package/src/index.ts +2813 -0
  29. package/src/inspect.ts +235 -0
  30. package/src/installation.ts +90 -0
  31. package/src/language.ts +217 -0
  32. package/src/records-entry.ts +63 -0
  33. package/src/records.ts +574 -0
  34. package/src/registry.ts +250 -0
  35. package/src/revise.ts +89 -0
  36. package/src/runner.ts +339 -0
  37. package/src/script-types.ts +549 -0
  38. package/src/stats.ts +460 -0
  39. package/src/status.ts +512 -0
  40. package/src/store.ts +259 -0
  41. package/src/subscriptions.ts +102 -0
  42. package/src/waiting.ts +61 -0
  43. package/test/cockpit-actions.test.ts +20 -0
  44. package/test/cockpit-groups.test.ts +42 -0
  45. package/test/cockpit-plan.test.ts +118 -0
  46. package/test/fixtures/branches-host.ts +58 -0
  47. package/test/fixtures/branches-script.js +23 -0
  48. package/test/fixtures/bridge-host.ts +23 -0
  49. package/test/fixtures/bridge-keys-script.js +15 -0
  50. package/test/fixtures/bridge-wait-script.js +8 -0
  51. package/test/fixtures/cancel-host.ts +58 -0
  52. package/test/fixtures/cancel-script.js +12 -0
  53. package/test/fixtures/crash-host.ts +61 -0
  54. package/test/fixtures/crash-script.js +14 -0
  55. package/test/fixtures/ping-records.ts +20 -0
  56. package/test/fixtures/revision-host.ts +57 -0
  57. package/test/fixtures/revision-script.js +11 -0
  58. package/test/fixtures/running-host.ts +61 -0
  59. package/test/fixtures/running-script.js +12 -0
  60. package/test/fixtures/with-data/flow.ts +2 -0
  61. package/test/fixtures/with-data/table.json +1 -0
  62. package/test/git-repo.ts +45 -0
  63. package/test/git-worktree.test.ts +413 -0
  64. package/test/question-format.test.ts +279 -0
  65. package/test/workflow-agent-caps.test.ts +91 -0
  66. package/test/workflow-agent-lifetime.test.ts +93 -0
  67. package/test/workflow-agent-options.test.ts +216 -0
  68. package/test/workflow-agent-schema.test.ts +386 -0
  69. package/test/workflow-agent-typings.test.ts +241 -0
  70. package/test/workflow-ask-resume.test.ts +145 -0
  71. package/test/workflow-branch-keys.test.ts +133 -0
  72. package/test/workflow-bridge-inproc.test.ts +230 -0
  73. package/test/workflow-bridge.test.ts +141 -0
  74. package/test/workflow-builtin-candidate.test.ts +230 -0
  75. package/test/workflow-builtin.test.ts +197 -0
  76. package/test/workflow-cancel-agent.test.ts +144 -0
  77. package/test/workflow-cancel.test.ts +166 -0
  78. package/test/workflow-chain.test.ts +84 -0
  79. package/test/workflow-check-options.test.ts +144 -0
  80. package/test/workflow-check-uses.test.ts +148 -0
  81. package/test/workflow-check.test.ts +282 -0
  82. package/test/workflow-contract.test.ts +295 -0
  83. package/test/workflow-crash-resume.test.ts +141 -0
  84. package/test/workflow-deadline.test.ts +102 -0
  85. package/test/workflow-describe-cache.test.ts +240 -0
  86. package/test/workflow-directory.test.ts +73 -0
  87. package/test/workflow-emits.test.ts +261 -0
  88. package/test/workflow-group-keys.test.ts +72 -0
  89. package/test/workflow-harnesses.test.ts +262 -0
  90. package/test/workflow-inspect.test.ts +152 -0
  91. package/test/workflow-language-types.test.ts +148 -0
  92. package/test/workflow-manifest.test.ts +93 -0
  93. package/test/workflow-manual-pause.test.ts +116 -0
  94. package/test/workflow-owners.test.ts +154 -0
  95. package/test/workflow-pause.test.ts +200 -0
  96. package/test/workflow-phases.test.ts +445 -0
  97. package/test/workflow-plugin-entries.test.ts +59 -0
  98. package/test/workflow-record-refs.test.ts +310 -0
  99. package/test/workflow-replay.test.ts +206 -0
  100. package/test/workflow-resume-once.test.ts +188 -0
  101. package/test/workflow-revision.test.ts +216 -0
  102. package/test/workflow-scope.test.ts +505 -0
  103. package/test/workflow-script-defaults.test.ts +119 -0
  104. package/test/workflow-script-error.test.ts +228 -0
  105. package/test/workflow-script-libraries.test.ts +88 -0
  106. package/test/workflow-script-packages.test.ts +119 -0
  107. package/test/workflow-stdout.test.ts +89 -0
  108. package/test/workflow-stop.test.ts +299 -0
  109. package/test/workflow-store.test.ts +220 -0
  110. package/test/workflow-visible.test.ts +136 -0
  111. package/test/workflow-wait-version.test.ts +54 -0
  112. package/test/workflow-wait.test.ts +224 -0
  113. package/test/workflow-waiting-agent.test.ts +64 -0
  114. package/tsconfig.json +15 -0
@@ -0,0 +1,310 @@
1
+ // A script names record types by reference: it imports the type a plugin declares, and wait and
2
+ // read.records give data typed by it. The header stays a literal; references are for the body.
3
+
4
+ import { afterEach, expect, test } from "bun:test";
5
+ import { mkdir, writeFile } from "node:fs/promises";
6
+ import { join } from "node:path";
7
+
8
+ import { plugin } from "@niwa-labs/plugin";
9
+ import { createNiwa, eventually, requests, tempHome, timers } from "@niwa-labs/testing";
10
+ import type { Host } from "@niwa-labs/testing";
11
+
12
+ import { workflow, workflowCheck, workflowStart } from "../src/index.ts";
13
+ import { Ping, PingAhead, Pong } from "./fixtures/ping-records.ts";
14
+
15
+ const homes: Array<{ cleanup(): Promise<void> }> = [];
16
+ const hosts: Host[] = [];
17
+
18
+ afterEach(async () => {
19
+ for (const host of hosts.splice(0)) await host.close().catch(() => {});
20
+ for (const home of homes.splice(0)) await home.cleanup();
21
+ }, 60_000);
22
+
23
+ /** Where the script imports the types from. */
24
+ const RECORDS = join(import.meta.dir, "fixtures", "ping-records.ts");
25
+
26
+ async function served(): Promise<{ host: Host; home: string }> {
27
+ const home = await tempHome();
28
+ homes.push(home);
29
+ const host = createNiwa({ home: home.path });
30
+ host.use(requests(), timers([]), plugin("ping", (api) => api.declare(Ping, Pong)), workflow());
31
+ await host.serve();
32
+ hosts.push(host);
33
+ return { host, home: home.path };
34
+ }
35
+
36
+ async function scriptIn(home: string, name: string, lines: readonly string[]): Promise<string> {
37
+ await mkdir(home, { recursive: true });
38
+ const file = join(home, name);
39
+ await writeFile(file, lines.join("\n"));
40
+ return file;
41
+ }
42
+
43
+ async function errorsOf(host: Host, file: string): Promise<Array<{ line: number; code: string; message: string }>> {
44
+ return (await host.call(workflowCheck, { file })).errors.map(({ line, code, message }) => ({ line, code, message }));
45
+ }
46
+
47
+ async function resultOf(host: Host, run: string): Promise<unknown> {
48
+ await eventually(
49
+ async () => (await host.records.read({ types: ["workflow.run.finished"], filter: { run } })).length === 1,
50
+ 20_000,
51
+ "the run should finish",
52
+ );
53
+ const [finished] = await host.records.read({ types: ["workflow.run.finished"], filter: { run } });
54
+ return (finished?.data as { outcome: unknown }).outcome;
55
+ }
56
+
57
+ const importing = (names: string): string => `import { ${names} } from ${JSON.stringify(RECORDS)};`;
58
+
59
+ test("a script waits for and reads an imported type: the data is typed by it and the run gets it", async () => {
60
+ const { host, home } = await served();
61
+ const file = await scriptIn(home, "refs.ts", [
62
+ importing("Ping, Pong"),
63
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
64
+ ' const { records, next } = await read.records(Ping, { filter: { "to.id": "owner" } });',
65
+ ' await wait(Pong, { filter: { id: "go" } });',
66
+ ' const first = await wait(Ping, { filter: { "to.id": "owner" }, from: next });',
67
+ " const size: number = first.data.size;",
68
+ " const both = await read.records({ types: [Ping, Pong], from: next });",
69
+ ' const kinds = both.records.map((row) => ("size" in row.data ? `ping ${row.data.to.id}` : `pong ${String(row.data.ok)}`));',
70
+ " return { before: records.map((row) => row.data.id), first: first.data.id, size, kinds };",
71
+ "});",
72
+ ]);
73
+ expect(await errorsOf(host, file)).toEqual([]);
74
+ await host.emit(Ping, { id: "old", size: 1, to: { id: "owner" } });
75
+ const { run } = await host.call(workflowStart, { file });
76
+ await eventually(
77
+ async () => (await host.records.read({ types: ["niwa.invocation.state"], filter: { state: "waiting" } })).length > 0,
78
+ 20_000,
79
+ "the run should wait for the pong",
80
+ );
81
+ await host.emit(Ping, { id: "one", size: 3, to: { id: "owner" } });
82
+ await host.emit(Pong, { id: "go", ok: true });
83
+ expect(await resultOf(host, run)).toEqual({
84
+ status: "done",
85
+ result: { before: ["old"], first: "one", size: 3, kinds: ["ping owner", "pong true"] },
86
+ });
87
+ }, 60_000);
88
+
89
+ test("the filter and the data of a wait or a read by reference are held to the type", async () => {
90
+ const { host, home } = await served();
91
+ const wrong = await scriptIn(home, "wrong.ts", [
92
+ importing("Ping"),
93
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
94
+ ' await wait(Ping, { filter: { idd: "a" } });',
95
+ " const ping = await wait(Ping);",
96
+ " const { records } = await read.records(Ping);",
97
+ " return [ping.data.nothing, records[0]?.data.nothing];",
98
+ "});",
99
+ ]);
100
+ expect((await errorsOf(host, wrong)).map(({ line, code }) => ({ line, code }))).toEqual([
101
+ { line: 3, code: "wait" },
102
+ { line: 3, code: expect.stringMatching(/^TS/) },
103
+ { line: 6, code: "TS2339" },
104
+ { line: 6, code: "TS2339" },
105
+ ]);
106
+ }, 60_000);
107
+
108
+ test("the host refuses a reference to a type it does not know or to a version it does not write, and a record the script writes", async () => {
109
+ const { host, home } = await served();
110
+ const file = await scriptIn(home, "refused.js", [
111
+ 'const Ahead = { kind: "record", name: "ping.sent", version: 2, schema: z.object({ id: z.string() }) };',
112
+ 'const Unheard = { kind: "record", name: "never.sent", version: 1, schema: z.object({ id: z.string() }) };',
113
+ 'const Note = record("note", z.object({ text: z.string() }));',
114
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
115
+ " const refused = async (fn) => fn().then(() => \"answered\", (error) => error.message);",
116
+ " return [",
117
+ ' await refused(() => wait(Ahead, { timeout: "1s", label: "ahead" })),',
118
+ ' await refused(() => wait(Unheard, { timeout: "1s", label: "unheard" })),',
119
+ ' await refused(() => read.records(Ahead, {}, { label: "read-ahead" })),',
120
+ ' await refused(() => wait(Note, { timeout: "1s", label: "note" })),',
121
+ " ];",
122
+ "});",
123
+ ]);
124
+ const { run } = await host.call(workflowStart, { file });
125
+ expect(await resultOf(host, run)).toEqual({
126
+ status: "done",
127
+ result: [
128
+ expect.stringContaining('version 2 of the record type "ping.sent", and the host knows version 1'),
129
+ expect.stringContaining('"never.sent", which the host does not know'),
130
+ expect.stringContaining('version 2 of the record type "ping.sent"'),
131
+ expect.stringContaining("is a record a script writes"),
132
+ ],
133
+ });
134
+ }, 60_000);
135
+
136
+ test("a read by reference gives the records of the type at its version; a read by name gives them all", async () => {
137
+ const home = await tempHome();
138
+ homes.push(home);
139
+ const before = createNiwa({ home: home.path });
140
+ before.use(requests(), timers([]), plugin("ping", (api) => api.declare(Ping, Pong)), workflow());
141
+ await before.serve();
142
+ await before.emit(Ping, { id: "old", size: 1, to: { id: "owner" } });
143
+ await before.close();
144
+
145
+ const host = createNiwa({ home: home.path });
146
+ host.use(requests(), timers([]), plugin("ping", (api) => api.declare(PingAhead, Pong)), workflow());
147
+ await host.serve();
148
+ hosts.push(host);
149
+ await host.emit(PingAhead, { id: "new" });
150
+ const file = await scriptIn(home.path, "versions.ts", [
151
+ importing("PingAhead"),
152
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
153
+ " const typed = await read.records(PingAhead);",
154
+ ' const named = await read.records({ types: ["ping.sent"] });',
155
+ " return { typed: typed.records.map((row) => row.data.id), named: named.records.length };",
156
+ "});",
157
+ ]);
158
+ expect(await errorsOf(host, file)).toEqual([]);
159
+ const { run } = await host.call(workflowStart, { file });
160
+ expect(await resultOf(host, run)).toEqual({ status: "done", result: { typed: ["new"], named: 2 } });
161
+ }, 60_000);
162
+
163
+ test("the types of the script package type a wait and a read by reference the same way", async () => {
164
+ const { host, home } = await served();
165
+ const lines = (body: readonly string[]): string[] => [
166
+ importing("Ping, Pong, pingsTo"),
167
+ 'import type { ScriptApi } from "@niwa-labs/script";',
168
+ 'declare const wait: ScriptApi["wait"];',
169
+ 'declare const read: ScriptApi["read"];',
170
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
171
+ ...body,
172
+ "});",
173
+ ];
174
+ const fine = await scriptIn(home, "api.ts", lines([
175
+ ' const ping = await wait(Ping, { filter: { "to.id": "owner" }, from: 0 });',
176
+ ' const pong = await wait(Pong, { filter: { ok: true }, timeout: "1m" });',
177
+ " const { records } = await read.records(Ping, { limit: 1 });",
178
+ " const both = await read.records({ types: [Ping, Pong] });",
179
+ ' const built = await wait(pingsTo("owner"), { from: ping.at });',
180
+ " const owned = await read.records(pingsTo(\"owner\"));",
181
+ ' const ok: boolean = "timedOut" in pong ? false : pong.data.ok;',
182
+ " const size: number = ping.data.size + (records[0]?.data.size ?? 0) + both.records.length + built.data.size + owned.records.length;",
183
+ " return { ok, size };",
184
+ ]));
185
+ expect(await errorsOf(host, fine)).toEqual([]);
186
+ const wrong = await scriptIn(home, "api-wrong.ts", lines([
187
+ ' await wait(Ping, { filter: { "to.id": 3 } });',
188
+ " const ping = await wait(Ping);",
189
+ " const { records } = await read.records(Ping);",
190
+ " return [ping.data.nothing, records[0]?.data.nothing];",
191
+ ]));
192
+ expect((await errorsOf(host, wrong)).map(({ line, code }) => ({ line, code }))).toEqual([
193
+ { line: 6, code: "wait" },
194
+ { line: 6, code: expect.stringMatching(/^TS/) },
195
+ { line: 9, code: "TS2339" },
196
+ { line: 9, code: "TS2339" },
197
+ ]);
198
+ }, 60_000);
199
+
200
+ test("the check follows a reference to the type it names: a type the host does not know or writes at another version is refused, and a filter is held to it in JS too", async () => {
201
+ const { host, home } = await served();
202
+ const refused = await scriptIn(home, "refused.ts", [
203
+ importing("PingAhead, Unheard"),
204
+ 'export default workflow({ uses: ["niwa.records.read"] }, async () => {',
205
+ " await wait(PingAhead);",
206
+ ' await wait(Unheard, { timeout: "1m" });',
207
+ " await read.records(PingAhead);",
208
+ " await read.records({ types: [Unheard] });",
209
+ "});",
210
+ ]);
211
+ expect(await errorsOf(host, refused)).toEqual([
212
+ { line: 3, code: "wait", message: 'wait PingAhead: the script names version 2 of "ping.sent", and the host writes version 1' },
213
+ { line: 4, code: "wait", message: 'wait Unheard: the host knows no record type "never.sent", so the wait would never end' },
214
+ { line: 5, code: "read", message: 'read PingAhead: the script names version 2 of "ping.sent", and the host writes version 1' },
215
+ { line: 6, code: "read", message: 'read Unheard: the host knows no record type "never.sent"' },
216
+ ]);
217
+ const js = (filter: string): string[] => [
218
+ importing("Ping"),
219
+ 'export default workflow({ uses: ["niwa.records.read"] }, async () => {',
220
+ ` await wait(Ping, { filter: ${filter} });`,
221
+ "});",
222
+ ];
223
+ expect(await errorsOf(host, await scriptIn(home, "typo.js", js('{ idd: "a" }')))).toEqual([
224
+ { line: 3, code: "wait", message: expect.stringContaining('wait "ping.sent": the filter names "idd"') },
225
+ ]);
226
+ expect(await errorsOf(host, await scriptIn(home, "kind.js", js('{ "to.id": 3 }')))).toEqual([
227
+ { line: 3, code: "wait", message: expect.stringContaining("cannot hold") },
228
+ ]);
229
+ expect(await errorsOf(host, await scriptIn(home, "fine.js", js('{ "to.id": "owner" }')))).toEqual([]);
230
+ }, 60_000);
231
+
232
+ test("the check follows a query a builder gives to its type, as the host does when the script runs", async () => {
233
+ const { host, home } = await served();
234
+ const file = await scriptIn(home, "built-refused.ts", [
235
+ importing("aheadOf, unheardOf"),
236
+ 'export default workflow({ uses: ["niwa.records.read"] }, async () => {',
237
+ ' await wait(unheardOf("a"), { timeout: "1m" });',
238
+ ' await wait(aheadOf("a"));',
239
+ ' await read.records(unheardOf("a"));',
240
+ ' await read.records(aheadOf("a"));',
241
+ ' const kept = unheardOf("b");',
242
+ " await wait(kept);",
243
+ "});",
244
+ ]);
245
+ expect(await errorsOf(host, file)).toEqual([
246
+ { line: 3, code: "wait", message: 'wait unheardOf("a"): the host knows no record type "never.sent", so the wait would never end' },
247
+ { line: 4, code: "wait", message: 'wait aheadOf("a"): the script names version 2 of "ping.sent", and the host writes version 1' },
248
+ { line: 5, code: "read", message: 'read unheardOf("a"): the host knows no record type "never.sent"' },
249
+ { line: 6, code: "read", message: 'read aheadOf("a"): the script names version 2 of "ping.sent", and the host writes version 1' },
250
+ { line: 8, code: "wait", message: 'wait kept: the host knows no record type "never.sent", so the wait would never end' },
251
+ ]);
252
+ }, 60_000);
253
+
254
+ test("a script that imports a package is not given the errors the package has under the options of the script", async () => {
255
+ const { host, home } = await served();
256
+ const file = await scriptIn(home, "imports.js", [importing("Ping"), "export default workflow({}, async () => {", " void Ping;", "});"]);
257
+ expect(await errorsOf(host, file)).toEqual([]);
258
+ }, 60_000);
259
+
260
+ test("the header names the types a script starts on as text: a reference there is refused and the error says where it goes", async () => {
261
+ const { host, home } = await served();
262
+ for (const on of ["[Ping]", '[{ type: Ping, filter: { "to.id": "owner" } }]']) {
263
+ const file = await scriptIn(home, "header.ts", [importing("Ping"), `export default workflow({ on: ${on} }, async () => {});`]);
264
+ const errors = (await errorsOf(host, file)).filter((error) => error.code === "on");
265
+ expect(errors).toEqual([
266
+ { line: 2, code: "on", message: expect.stringContaining('the header is read as text: name the type as text, as in on: ["task.done"]; a type imported by reference is for wait and read.records') },
267
+ ]);
268
+ }
269
+ }, 60_000);
270
+
271
+ test("a query a builder gives is waited for and read like its type: the data is typed by it and its filter holds", async () => {
272
+ const { host, home } = await served();
273
+ const file = await scriptIn(home, "built.ts", [
274
+ importing("Pong, pingsTo"),
275
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
276
+ ' const { records, next } = await read.records(pingsTo("owner"));',
277
+ ' await wait(Pong, { filter: { id: "go" } });',
278
+ ' const first = await wait(pingsTo("owner"), { from: next });',
279
+ " const size: number = first.data.size;",
280
+ ' const late = await wait(pingsTo("nobody"), { from: first.at, timeout: "1s" });',
281
+ ' return { before: records.map((row) => row.data.id), first: first.data.id, size, late: "timedOut" in late };',
282
+ "});",
283
+ ]);
284
+ expect(await errorsOf(host, file)).toEqual([]);
285
+ await host.emit(Ping, { id: "old", size: 1, to: { id: "owner" } });
286
+ await host.emit(Ping, { id: "else", size: 1, to: { id: "else" } });
287
+ const { run } = await host.call(workflowStart, { file });
288
+ await eventually(
289
+ async () => (await host.records.read({ types: ["niwa.invocation.state"], filter: { state: "waiting" } })).length > 0,
290
+ 20_000,
291
+ "the run should wait for the pong",
292
+ );
293
+ await host.emit(Ping, { id: "elsewhere", size: 2, to: { id: "else" } });
294
+ await host.emit(Ping, { id: "one", size: 3, to: { id: "owner" } });
295
+ await host.emit(Pong, { id: "go", ok: true });
296
+ expect(await resultOf(host, run)).toEqual({ status: "done", result: { before: ["old"], first: "one", size: 3, late: true } });
297
+
298
+ const wrong = await scriptIn(home, "built-wrong.ts", [
299
+ importing("pingsTo"),
300
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
301
+ ' const ping = await wait(pingsTo("owner"));',
302
+ ' const { records } = await read.records(pingsTo("owner"));',
303
+ " return [ping.data.nothing, records[0]?.data.nothing];",
304
+ "});",
305
+ ]);
306
+ expect((await errorsOf(host, wrong)).map(({ line, code }) => ({ line, code }))).toEqual([
307
+ { line: 5, code: "TS2339" },
308
+ { line: 5, code: "TS2339" },
309
+ ]);
310
+ }, 60_000);
@@ -0,0 +1,206 @@
1
+ // A replay does nothing twice, and a script edited mid-run is noticed.
2
+ //
3
+ // A resume starts another invocation of the run and replays the script from the top: the requests
4
+ // that are not calls (emit, ask, sleep, now, random) answer from what the earlier start recorded,
5
+ // as the calls do. The file of the script is pinned by its hash, so a run entered again over an
6
+ // edited file pauses for a revision. A label is the name of one call, kept apart from the records
7
+ // the run writes itself.
8
+
9
+ import { afterEach, expect, test } from "bun:test";
10
+ import { writeFile } from "node:fs/promises";
11
+ import { join } from "node:path";
12
+
13
+ import { seq } from "@niwa-labs/core";
14
+ import type { ActionName, StoredRecord } from "@niwa-labs/core";
15
+ import { connect, createNiwa, requests, counter, eventually, tempHome, testClock, ownerTokenOf, timers, WfNote, stub } from "@niwa-labs/testing";
16
+ import type { Host, TestClock } from "@niwa-labs/testing";
17
+ import { workflow } from "../src/index.ts";
18
+ import { z } from "zod";
19
+
20
+
21
+ const START = 1_700_000_000_000;
22
+ const MINUTE = 60_000;
23
+ const NOTE = 'const Note = record("note", z.object({ text: z.string() })); ';
24
+
25
+ const homes: Array<{ cleanup(): Promise<void> }> = [];
26
+ const hosts: Host[] = [];
27
+
28
+ afterEach(async () => {
29
+ for (const host of hosts.splice(0)) await host.close().catch(() => {});
30
+ for (const home of homes.splice(0)) await home.cleanup();
31
+ });
32
+
33
+ async function hostOn(path: string, clock: TestClock): Promise<Host> {
34
+ const niwa = createNiwa({ home: path, clock });
35
+ niwa.use(requests());
36
+ niwa.use(timers([]));
37
+ niwa.use(stub(path));
38
+ niwa.use(workflow({ emits: [WfNote] }));
39
+ await niwa.serve();
40
+ hosts.push(niwa);
41
+ return niwa;
42
+ }
43
+
44
+ const recordsOf = async (niwa: Host): Promise<StoredRecord[]> => niwa.records.read();
45
+ const ofType = (records: readonly StoredRecord[], type: string): StoredRecord[] =>
46
+ records.filter((row) => row.type === type);
47
+ const count = async (niwa: Host, type: string): Promise<number> => ofType(await recordsOf(niwa), type).length;
48
+
49
+ const Started = z.object({ run: z.string() });
50
+ const Paused = z.object({ reason: z.string() });
51
+ const Finished = z.object({ outcome: z.object({ result: z.unknown() }) });
52
+
53
+ /** Write the script into the home. */
54
+ async function write(home: string, source: string): Promise<string> {
55
+ const file = join(home, "script.js");
56
+ await writeFile(file, source);
57
+ return file;
58
+ }
59
+
60
+ async function start(niwa: Host, file: string): Promise<string> {
61
+ return Started.parse(await niwa.call("workflow.start" as ActionName, { file } as never)).run;
62
+ }
63
+
64
+ const resultsOf = async (niwa: Host): Promise<unknown[]> =>
65
+ ofType(await recordsOf(niwa), "workflow.run.finished").map((row) => Finished.parse(row.data).outcome.result);
66
+
67
+ const reasonsOf = async (niwa: Host): Promise<string[]> =>
68
+ ofType(await recordsOf(niwa), "workflow.run.paused").map((row) => Paused.parse(row.data).reason);
69
+
70
+ test("a resume does not emit, ask or sleep again, and reads the same now and random", async () => {
71
+ const home = await tempHome();
72
+ homes.push(home);
73
+ const clock = testClock(START);
74
+ const niwa = await hostOn(home.path, clock);
75
+ const owner = await connect({ home: home.path, id: "owner:replay", token: ownerTokenOf(home.path) });
76
+ try {
77
+ const file = await write(
78
+ home.path,
79
+ `${NOTE}export default workflow({ name: "demo", emits: [Note], output: z.json() }, async () => {
80
+ const at = await now();
81
+ const dice = await random();
82
+ await emit(Note, { text: "once" });
83
+ await sleep("1m");
84
+ const answer = await ask.confirm("Go on?");
85
+ return { at, dice, answer };
86
+ });`,
87
+ );
88
+ const run = await start(niwa, file);
89
+ await eventually(async () => (await count(niwa, "timer.sleep.requested")) === 1, 10_000, "the sleep should be armed");
90
+ await clock.advance(MINUTE);
91
+ await eventually(async () => (await count(niwa, "request.created")) === 1, 10_000, "the owner should be asked");
92
+ const id = (ofType(await recordsOf(niwa), "request.created")[0]?.data as { id: string }).id;
93
+ await owner.call("request.answer" as ActionName, { id, answer: { ids: ["yes"] } } as never);
94
+ await eventually(async () => (await resultsOf(niwa)).length === 1, 10_000, "the first run should finish");
95
+
96
+ // Much later, the run is resumed: the whole script is replayed, and nothing is done again.
97
+ await clock.advance(30 * MINUTE);
98
+ await niwa.call("workflow.resume" as ActionName, { run } as never);
99
+ await eventually(async () => (await resultsOf(niwa)).length === 2, 10_000, "the replay should finish by itself");
100
+
101
+ const [first, second] = await resultsOf(niwa);
102
+ expect(second).toEqual(first);
103
+ expect((first as { at: number }).at).toBe(START);
104
+ expect(await count(niwa, "wf.demo.note")).toBe(1);
105
+ expect(await count(niwa, "timer.sleep.requested")).toBe(1);
106
+ expect(await count(niwa, "request.created")).toBe(1);
107
+ } finally {
108
+ await owner.close().catch(() => {});
109
+ }
110
+ }, 30_000);
111
+
112
+ test("a script edited while its run was away pauses the run for a revision, and a resume revises it", async () => {
113
+ const home = await tempHome();
114
+ homes.push(home);
115
+ const first = await hostOn(home.path, testClock(START));
116
+ const file = await write(home.path, `export default workflow({ output: z.string() }, async () => { await sleep("5m"); return "old"; });`);
117
+ const run = await start(first, file);
118
+ await eventually(async () => (await count(first, "timer.sleep.requested")) === 1, 10_000, "the sleep should be armed");
119
+ await first.close();
120
+
121
+ // The file is edited while the host is down; the new host finds the run again.
122
+ await writeFile(file, `export default workflow({ output: z.string() }, async () => { await sleep("5m"); return "new"; });`);
123
+ const clock = testClock(START + MINUTE);
124
+ const second = await hostOn(home.path, clock);
125
+ await eventually(async () => (await reasonsOf(second)).length === 1, 10_000, "the run should pause");
126
+ expect((await reasonsOf(second))[0]).toContain("edited");
127
+ await clock.advance(10 * MINUTE);
128
+ await Bun.sleep(300);
129
+ expect(await resultsOf(second)).toEqual([]);
130
+
131
+ // The owner resumes it: the edit is a revision, and the sleep goes on from its own moment.
132
+ await second.call("workflow.resume" as ActionName, { run } as never);
133
+ await eventually(async () => (await resultsOf(second)).length === 1, 10_000, "the revised run should finish");
134
+ expect(await resultsOf(second)).toEqual(["new"]);
135
+ }, 30_000);
136
+
137
+ test("an emit that is the same on a resume is taken, not written again; one with other data is a change, and is written", async () => {
138
+ const home = await tempHome();
139
+ homes.push(home);
140
+ const niwa = await hostOn(home.path, testClock(START));
141
+ const source = (text: string) =>
142
+ `${NOTE}export default workflow({ name: "demo", emits: [Note], output: z.string() }, async () => { await emit(Note, { text: "${text}" }, { label: "note" }); return "ok"; });`;
143
+ const file = await write(home.path, source("old"));
144
+ const run = await start(niwa, file);
145
+ await eventually(async () => (await resultsOf(niwa)).length === 1, 10_000, "the run should finish");
146
+
147
+ await niwa.call("workflow.resume" as ActionName, { run } as never);
148
+ await eventually(async () => (await resultsOf(niwa)).length === 2, 10_000, "the resume should finish");
149
+ expect(await count(niwa, "wf.demo.note")).toBe(1);
150
+ expect(await count(niwa, "workflow.step.reused")).toBe(1);
151
+
152
+ await writeFile(file, source("new"));
153
+ await niwa.call("workflow.resume" as ActionName, { run } as never);
154
+ await eventually(async () => (await resultsOf(niwa)).length === 3, 10_000, "the revision should finish");
155
+ expect(await reasonsOf(niwa)).toEqual([]);
156
+ const notes = (await niwa.records.read()).filter((row) => row.type === "wf.demo.note").map((row) => (row.data as { text: string }).text);
157
+ expect(notes).toEqual(["old", "new"]);
158
+ }, 30_000);
159
+
160
+ test("a label is not the name of a record the run writes itself", async () => {
161
+ const home = await tempHome();
162
+ homes.push(home);
163
+ const niwa = await hostOn(home.path, testClock(START));
164
+ const file = await write(
165
+ home.path,
166
+ `${NOTE}export default workflow({ name: "demo", emits: [Note], output: z.string() }, async () => {
167
+ await emit(Note, { text: "x" }, { label: "finished" });
168
+ await emit(Note, { text: "y" }, { label: "started" });
169
+ return "ok";
170
+ });`,
171
+ );
172
+ await start(niwa, file);
173
+ await eventually(async () => (await resultsOf(niwa)).length === 1, 10_000, "the run should finish");
174
+ expect(await resultsOf(niwa)).toEqual(["ok"]);
175
+ expect(await count(niwa, "wf.demo.note")).toBe(2);
176
+ }, 30_000);
177
+
178
+ test("a label used twice in a run is refused, and the second call does not run", async () => {
179
+ const home = await tempHome();
180
+ homes.push(home);
181
+ const niwa = await hostOn(home.path, testClock(START));
182
+ const file = await write(
183
+ home.path,
184
+ `export default workflow({}, async () => {
185
+ for (const name of ["a", "b"]) await niwa.stub.create({ name }, { label: "make" });
186
+ });`,
187
+ );
188
+ await start(niwa, file);
189
+ await eventually(async () => (await reasonsOf(niwa)).length === 1, 10_000, "the run should pause");
190
+ expect((await reasonsOf(niwa))[0]).toContain('the label "make" is used twice');
191
+ expect(counter(home.path, "w1.create").count()).toBe(1);
192
+ }, 30_000);
193
+
194
+ test("a label that holds the mark of a call's own steps is refused", async () => {
195
+ const home = await tempHome();
196
+ homes.push(home);
197
+ const niwa = await hostOn(home.path, testClock(START));
198
+ const file = await write(
199
+ home.path,
200
+ `export default workflow({}, async () => { await niwa.stub.create({ name: "a" }, { label: "make~auth" }); });`,
201
+ );
202
+ await start(niwa, file);
203
+ await eventually(async () => (await reasonsOf(niwa)).length === 1, 10_000, "the run should pause");
204
+ expect((await reasonsOf(niwa))[0]).toContain('holds "~"');
205
+ expect(counter(home.path, "w1.create").count()).toBe(0);
206
+ }, 30_000);