@niwa-labs/host 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 +2 -2
- package/package.json +16 -4
- package/src/access-actions.ts +193 -0
- package/src/access-control.ts +333 -0
- package/src/call-operations.ts +176 -0
- package/src/cockpit-catalog.ts +99 -0
- package/src/describe.ts +55 -0
- package/src/doors.ts +272 -0
- package/src/host-types.ts +43 -0
- package/src/host.ts +3346 -0
- package/src/index.ts +10 -0
- package/src/plugin-install.ts +249 -0
- package/src/state.ts +261 -0
- package/src/stores.ts +131 -0
- package/test/abandon.test.ts +58 -0
- package/test/action-refusal-restart.test.ts +46 -0
- package/test/action-step.test.ts +363 -0
- package/test/action-wait-restart.test.ts +96 -0
- package/test/builtin-actions.test.ts +21 -0
- package/test/cancel-steps.test.ts +150 -0
- package/test/catch-up.test.ts +378 -0
- package/test/causes.test.ts +138 -0
- package/test/client.test.ts +190 -0
- package/test/cockpit-catalog.test.ts +118 -0
- package/test/crash-between-steps.test.ts +88 -0
- package/test/declared-values.test.ts +175 -0
- package/test/deep-nested-pause.test.ts +147 -0
- package/test/fixtures/action-step-host.ts +166 -0
- package/test/fixtures/action-wait-restart-host.ts +62 -0
- package/test/fixtures/catch-up-host.ts +91 -0
- package/test/fixtures/causes-host.ts +78 -0
- package/test/fixtures/client-host.ts +52 -0
- package/test/fixtures/crash-between-steps-host.ts +71 -0
- package/test/fixtures/declared-values-host.ts +110 -0
- package/test/fixtures/intercept-order-host.ts +112 -0
- package/test/fixtures/intercept-step-host.ts +106 -0
- package/test/fixtures/live-frames-host.ts +81 -0
- package/test/fixtures/long-wait-host.ts +78 -0
- package/test/fixtures/nested-wait-host.ts +120 -0
- package/test/fixtures/no-repeat-crash-host.ts +79 -0
- package/test/fixtures/plugin-replace-host.ts +176 -0
- package/test/fixtures/record-refusal-crash.ts +50 -0
- package/test/fixtures/recovery-contracts-host.ts +79 -0
- package/test/fixtures/repeated-write-host.ts +39 -0
- package/test/fixtures/replay-mismatch-host.ts +66 -0
- package/test/fixtures/single-writer-host.ts +113 -0
- package/test/fixtures/state-host.ts +118 -0
- package/test/fixtures/wait-restart-host.ts +82 -0
- package/test/guards.test.ts +448 -0
- package/test/handler-emit.test.ts +116 -0
- package/test/host.test.ts +114 -0
- package/test/intercept-adjust.test.ts +318 -0
- package/test/intercept-order.test.ts +276 -0
- package/test/intercept-step.test.ts +144 -0
- package/test/large-outcome.test.ts +60 -0
- package/test/live-frames.test.ts +150 -0
- package/test/live-ready.test.ts +61 -0
- package/test/long-wait.test.ts +168 -0
- package/test/nested-outcome.test.ts +75 -0
- package/test/nested-wait.test.ts +111 -0
- package/test/no-repeat-crash.test.ts +132 -0
- package/test/offer-replace.test.ts +39 -0
- package/test/plugin-async-setup.test.ts +22 -0
- package/test/plugin-copies.test.ts +87 -0
- package/test/plugin-emit.test.ts +43 -0
- package/test/plugin-install-failure.test.ts +121 -0
- package/test/plugin-install-hash.test.ts +57 -0
- package/test/plugin-needs.test.ts +80 -0
- package/test/plugin-replace.test.ts +225 -0
- package/test/plugin-uninstall.test.ts +70 -0
- package/test/plugin-version-copies.test.ts +53 -0
- package/test/plugin-version.test.ts +180 -0
- package/test/read-filter.test.ts +136 -0
- package/test/read-query.test.ts +92 -0
- package/test/record-admission.test.ts +294 -0
- package/test/recovery-contracts.test.ts +107 -0
- package/test/repeated-write.test.ts +119 -0
- package/test/replay-mismatch.test.ts +79 -0
- package/test/replay.test.ts +135 -0
- package/test/single-writer.test.ts +267 -0
- package/test/state.test.ts +246 -0
- package/test/step-by-name.test.ts +122 -0
- package/test/stores.test.ts +102 -0
- package/test/stream.test.ts +154 -0
- package/test/subscriber-stream.test.ts +71 -0
- package/test/subscribers.test.ts +101 -0
- package/test/subscriptions.test.ts +632 -0
- package/test/trace-call.test.ts +124 -0
- package/test/trigger-filter.test.ts +42 -0
- package/test/use-many.test.ts +151 -0
- package/test/wait-query.test.ts +117 -0
- package/test/wait-restart.test.ts +187 -0
- package/test/wire-read-access.test.ts +85 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { action, CORE_TYPES, grant, principalOf, record, RecordRefused } from "@niwa-labs/core";
|
|
4
|
+
import { connect } from "@niwa-labs/client";
|
|
5
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
6
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
7
|
+
import { connectAsOwner, eventually, tempHome } from "@niwa-labs/testing";
|
|
8
|
+
|
|
9
|
+
const revision = z.object({ id: z.string(), basedOn: z.number(), next: z.number() });
|
|
10
|
+
function guarded(name: string) {
|
|
11
|
+
return record(name, revision, { open: true, guard: {
|
|
12
|
+
group: "admission.pages",
|
|
13
|
+
key: data => data.id,
|
|
14
|
+
initial: () => 0,
|
|
15
|
+
admit: (state, data) => data.basedOn === state ? undefined : { code: "stale", reason: "revision changed" },
|
|
16
|
+
fold: (_state, data) => data.next,
|
|
17
|
+
} });
|
|
18
|
+
}
|
|
19
|
+
const Create = guarded("admission.created");
|
|
20
|
+
const Edit = guarded("admission.edited");
|
|
21
|
+
const Trigger = record("admission.trigger", z.object({}));
|
|
22
|
+
const Wake = record("admission.wake", z.object({}));
|
|
23
|
+
const Result = record("admission.result", z.object({}));
|
|
24
|
+
const data = (id: string, basedOn: number, next: number) => ({ id, basedOn, next });
|
|
25
|
+
const pages = plugin("admission", api => api.declare(Create, Edit, Trigger, Wake, Result));
|
|
26
|
+
|
|
27
|
+
test("record guards serialize competing writes and replay accepted and refused intents after restart", async () => {
|
|
28
|
+
const home = await tempHome();
|
|
29
|
+
let host = createNiwa({home: home.path});
|
|
30
|
+
try {
|
|
31
|
+
host.use(pages);
|
|
32
|
+
await host.serve();
|
|
33
|
+
const writes = [1, 2].map(n => host.emit(Create, data("a", 0, n), {id: "race-" + n}));
|
|
34
|
+
const [winner, loser] = await Promise.allSettled(writes);
|
|
35
|
+
expect(winner?.status).toBe("fulfilled");
|
|
36
|
+
expect(loser).toMatchObject({status: "rejected", reason: {code: "stale", entity: "a"}});
|
|
37
|
+
await host.close();
|
|
38
|
+
|
|
39
|
+
host = createNiwa({home: home.path});
|
|
40
|
+
host.use(pages);
|
|
41
|
+
await host.serve();
|
|
42
|
+
expect((await host.emit(Create, data("a", 0, 1), {id: "race-1"})).id).toBe("race-1");
|
|
43
|
+
await host.emit(Edit, data("a", 1, 0));
|
|
44
|
+
// The previously refused intent is still refused, even though its predicate holds now.
|
|
45
|
+
await expect(host.emit(Create, data("a", 0, 2), {id: "race-2"})).rejects.toBeInstanceOf(RecordRefused);
|
|
46
|
+
await expect(host.emit(Create, data("a", 0, 3), {id: "race-2"})).rejects.toThrow("different content");
|
|
47
|
+
expect(await host.records.read({types: [CORE_TYPES.recordRefused]})).toHaveLength(1);
|
|
48
|
+
await host.emit(Create, data("a", 0, 2));
|
|
49
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("a wire batch checks every grant and rolls back earlier entity transitions on refusal", async () => {
|
|
53
|
+
const home = await tempHome();
|
|
54
|
+
const host = createNiwa({home: home.path});
|
|
55
|
+
host.use(pages);
|
|
56
|
+
await host.serve();
|
|
57
|
+
const client = await connectAsOwner(home.path);
|
|
58
|
+
const token = host.issueToken(principalOf("session", "writer"), {grant: grant(["emit:admission.created"])});
|
|
59
|
+
const limited = await connect({home: home.path, id: "writer", token});
|
|
60
|
+
try {
|
|
61
|
+
const first = {type: Create, data: data("a", 0, 1), opts: {id: "batch-first"}};
|
|
62
|
+
await expect(limited.emitAll([first, {type: Result, data: {}}])).rejects.toThrow();
|
|
63
|
+
expect(await host.records.read({types: [Create, Edit]})).toHaveLength(0);
|
|
64
|
+
|
|
65
|
+
const bad = [first, {type: Edit, data: data("b", 7, 8), opts: {id: "batch-failure"}}] as const;
|
|
66
|
+
await expect(client.emitAll(bad)).rejects.toBeInstanceOf(RecordRefused);
|
|
67
|
+
expect(await host.records.read({types: [Create, Edit]})).toHaveLength(0);
|
|
68
|
+
const audit = (await host.records.read({types: [CORE_TYPES.recordRefused]}))[0];
|
|
69
|
+
expect(audit).toMatchObject({actor: {kind: "client"}, data: {attempted: "batch-failure", entity: "b", code: "stale"}});
|
|
70
|
+
|
|
71
|
+
// Replaying the whole refused batch must not start its first transition again.
|
|
72
|
+
await client.emit(Create, data("a", 0, 3));
|
|
73
|
+
await expect(client.emitAll(bad)).rejects.toMatchObject({audit: {id: audit!.id}});
|
|
74
|
+
expect(await host.records.read({types: [CORE_TYPES.recordRefused]})).toHaveLength(1);
|
|
75
|
+
const rows = await client.emitAll([{type: Edit, data: data("a", 3, 4)}, {type: Edit, data: data("a", 4, 5)}]);
|
|
76
|
+
expect(rows).toHaveLength(2);
|
|
77
|
+
} finally { await limited.close(); await client.close(); await host.close(); await home.cleanup(); }
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("a durable handler catches the same refusal and resumes past a committed action batch", async () => {
|
|
81
|
+
const home = await tempHome();
|
|
82
|
+
const batch = action("admission.batch", {
|
|
83
|
+
description: "Write two page revisions", input: z.object({}), output: z.null(), recovery: "repeat", effect: "record",
|
|
84
|
+
async run(_, ctx) {
|
|
85
|
+
await ctx.emitAll([{type: Create, data: data("a", 0, 1)}, {type: Edit, data: data("a", 1, 2)}]);
|
|
86
|
+
await ctx.wait(Wake);
|
|
87
|
+
return null;
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
const worker = plugin("admission", api => {
|
|
91
|
+
api.declare(Create, Edit, Trigger, Wake, Result);
|
|
92
|
+
api.action(batch);
|
|
93
|
+
api.on(Trigger, async (_, step) => {
|
|
94
|
+
try { await step.emit(Edit, data("x", 1, 2), {key: "refusal"}); }
|
|
95
|
+
catch (error) { if (!(error instanceof RecordRefused)) throw error; }
|
|
96
|
+
await step(batch, {}, {key: "batch"});
|
|
97
|
+
await step.emit(Result, {});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
let host = createNiwa({home: home.path});
|
|
101
|
+
try {
|
|
102
|
+
host.use(worker);
|
|
103
|
+
await host.serve();
|
|
104
|
+
await host.emit(Trigger, {});
|
|
105
|
+
await eventually(async () => (await host.records.read({types: [CORE_TYPES.invocationState]})).some(row => (row.data as {state:string}).state === "waiting"));
|
|
106
|
+
await host.close();
|
|
107
|
+
host = createNiwa({home: home.path});
|
|
108
|
+
host.use(worker);
|
|
109
|
+
await host.serve();
|
|
110
|
+
await host.emit(Wake, {});
|
|
111
|
+
await eventually(async () => (await host.records.read({types:[Result]})).length === 1);
|
|
112
|
+
expect(await host.records.read({types: [Create, Edit]})).toHaveLength(2);
|
|
113
|
+
expect(await host.records.read({types: [CORE_TYPES.recordRefused]})).toHaveLength(1);
|
|
114
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("replacing guard code folds historical records without readmission and rolls back an invalid fold", async () => {
|
|
118
|
+
const home = await tempHome();
|
|
119
|
+
const host = createNiwa({home: home.path});
|
|
120
|
+
host.use(pages);
|
|
121
|
+
await host.serve();
|
|
122
|
+
try {
|
|
123
|
+
await host.emit(Create, data("a", 0, 3));
|
|
124
|
+
const replacement = (fold: (_: number, entry: z.infer<typeof revision>) => number) => {
|
|
125
|
+
const updated = record(Create.name, revision, {guard: {
|
|
126
|
+
...Create.guard!, initial: () => 0,
|
|
127
|
+
admit: () => ({code:"closed", reason:"closed for new writes"}), fold,
|
|
128
|
+
}});
|
|
129
|
+
return plugin("admission", api => api.declare(updated, Edit));
|
|
130
|
+
};
|
|
131
|
+
expect(() => host.use(replacement(() => { throw new Error("invalid historical revision"); }))).toThrow("invalid historical revision");
|
|
132
|
+
await host.emit(Edit, data("a", 3, 4));
|
|
133
|
+
host.use(replacement((_state, entry) => entry.next));
|
|
134
|
+
await host.emit(Edit, data("a", 4, 5));
|
|
135
|
+
await expect(host.emit(Create, data("b", 0, 3))).rejects.toMatchObject({code:"closed"});
|
|
136
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("a named call returns its original typed refusal across connections", async () => {
|
|
140
|
+
const home = await tempHome();
|
|
141
|
+
const host = createNiwa({home: home.path});
|
|
142
|
+
let calls = 0;
|
|
143
|
+
const stale = action("admission.stale", {
|
|
144
|
+
description:"Edit a stale revision", recovery:"repeat", effect:"record", input:z.object({}), output:z.null(),
|
|
145
|
+
async run(_, ctx) {
|
|
146
|
+
calls++;
|
|
147
|
+
await ctx.emit(Edit, data("a", 9, 10));
|
|
148
|
+
return null;
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
host.use(plugin("admission", api => { api.declare(Edit); api.action(stale); }));
|
|
152
|
+
await host.serve();
|
|
153
|
+
try {
|
|
154
|
+
for (let n = 0; n < 2; n++) {
|
|
155
|
+
const client = await connectAsOwner(home.path);
|
|
156
|
+
try {
|
|
157
|
+
await expect(client.call(stale, {}, {operation: "named-edit"})).rejects.toMatchObject({
|
|
158
|
+
name: "RecordRefused", code: "stale", recordType: Edit.name,
|
|
159
|
+
});
|
|
160
|
+
} finally { await client.close(); }
|
|
161
|
+
}
|
|
162
|
+
expect(calls).toBe(1);
|
|
163
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a named operation retrying on a new connection reuses its committed guarded write", async () => {
|
|
167
|
+
const home = await tempHome();
|
|
168
|
+
const host = createNiwa({home: home.path});
|
|
169
|
+
let first = true;
|
|
170
|
+
const edit = action("admission.retry", {
|
|
171
|
+
description:"Retry after writing", recovery:"repeat-with-key", effect:"record", input:z.object({}), output:z.string(),
|
|
172
|
+
async run(_, ctx) {
|
|
173
|
+
const row = await ctx.emit(Create, data("a", 0, 1));
|
|
174
|
+
if (first) { first = false; throw new Error("interrupted after write"); }
|
|
175
|
+
return row.id;
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
host.use(plugin("admission", api => { api.declare(Create); api.action(edit); }));
|
|
179
|
+
await host.serve();
|
|
180
|
+
try {
|
|
181
|
+
const original = await connectAsOwner(home.path);
|
|
182
|
+
try { await expect(original.call(edit, {}, {operation: "same-write"})).rejects.toThrow("interrupted after write"); }
|
|
183
|
+
finally { await original.close(); }
|
|
184
|
+
const retry = await connectAsOwner(home.path);
|
|
185
|
+
try {
|
|
186
|
+
const id = await retry.call(edit, {}, {operation: "same-write"});
|
|
187
|
+
expect((await host.records.read({types: [Create]})).map(row => row.id)).toEqual([id]);
|
|
188
|
+
} finally { await retry.close(); }
|
|
189
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("the entity state cache preserves a fold's map values within a batch", async () => {
|
|
193
|
+
const home = await tempHome();
|
|
194
|
+
const host = createNiwa({home: home.path});
|
|
195
|
+
const Item = record("admission.members", z.object({owner: z.string(), member: z.string()}), {guard: {
|
|
196
|
+
group: "admission.members", key: data => data.owner, initial: () => new Map<string, number>(),
|
|
197
|
+
admit: (members, data) => members.has(data.member) ? {code: "duplicate", reason: "member already exists"} : undefined,
|
|
198
|
+
fold: (members, data) => new Map(members).set(data.member, members.size + 1),
|
|
199
|
+
}});
|
|
200
|
+
host.use(plugin("admission", api => api.declare(Item)));
|
|
201
|
+
try {
|
|
202
|
+
await host.serve();
|
|
203
|
+
const item = {type: Item, data: {owner: "a", member: "x"}};
|
|
204
|
+
await expect(host.emitAll([item, item])).rejects.toMatchObject({code: "duplicate"});
|
|
205
|
+
await host.emitAll([item]);
|
|
206
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("an action's next write follows the refusal when the action handles a lost race", async () => {
|
|
210
|
+
const home = await tempHome();
|
|
211
|
+
const host = createNiwa({home: home.path});
|
|
212
|
+
const handle = action("admission.handle", {
|
|
213
|
+
description: "Handle a lost race", input: z.object({}), output: z.null(), effect: "record", recovery: "repeat",
|
|
214
|
+
async run(_, ctx) {
|
|
215
|
+
try { await ctx.emit(Edit, data("a", 9, 10)); }
|
|
216
|
+
catch (error) { if (!(error instanceof RecordRefused)) throw error; }
|
|
217
|
+
await ctx.emit(Result, {});
|
|
218
|
+
return null;
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
host.use(plugin("admission", api => { api.declare(Edit, Result); api.action(handle); }));
|
|
222
|
+
try {
|
|
223
|
+
await host.serve();
|
|
224
|
+
await host.call(handle, {});
|
|
225
|
+
const [audit, note] = await host.records.read({types: [CORE_TYPES.recordRefused, Result]});
|
|
226
|
+
expect(note).toMatchObject({cause: audit!.id, root: audit!.id});
|
|
227
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("guard keys see the same JSON on first write and after restart", async () => {
|
|
231
|
+
const home = await tempHome();
|
|
232
|
+
const Dates = record("admission.dates", z.object({when: z.coerce.date()}), {guard: {
|
|
233
|
+
group: "admission.dates", initial: () => 0, key: data => data.when.toISOString(),
|
|
234
|
+
admit: () => undefined, fold: count => count + 1,
|
|
235
|
+
}});
|
|
236
|
+
const normalized = record("admission.normalized", z.object({when: z.string().transform(value => value.trim())}), {guard: {
|
|
237
|
+
group: "admission.normalized", initial: () => false, key: data => data.when,
|
|
238
|
+
admit: seen => seen ? {code: "duplicate", reason: "already seen"} : undefined, fold: () => true,
|
|
239
|
+
}});
|
|
240
|
+
const provider = plugin("admission", api => api.declare(Dates, normalized));
|
|
241
|
+
let host = createNiwa({home: home.path});
|
|
242
|
+
try {
|
|
243
|
+
host.use(provider);
|
|
244
|
+
await host.serve();
|
|
245
|
+
await expect(host.emit(Dates, {when: new Date(0)})).rejects.toThrow("must preserve its data in JSON");
|
|
246
|
+
await host.emit(normalized, {when: " day "});
|
|
247
|
+
await host.close();
|
|
248
|
+
host = createNiwa({home: home.path});
|
|
249
|
+
host.use(provider);
|
|
250
|
+
await host.serve();
|
|
251
|
+
expect(await host.records.read({types: [Dates]})).toHaveLength(0);
|
|
252
|
+
await expect(host.emit(normalized, {when: "day"})).rejects.toMatchObject({code: "duplicate"});
|
|
253
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
for (const refused of [false, true]) {
|
|
257
|
+
for (const replaySize of [0, 1, 2, 3]) {
|
|
258
|
+
test(`a durable ${refused ? "refused" : "accepted"} batch of two replays with ${replaySize} entries`, async () => {
|
|
259
|
+
const home = await tempHome();
|
|
260
|
+
let size = 2;
|
|
261
|
+
const worker = plugin("admission", api => {
|
|
262
|
+
api.declare(Create, Trigger, Wake, Result);
|
|
263
|
+
api.on(Trigger, async (_, step) => {
|
|
264
|
+
const entries = Array.from({length: size}, (_, i) => ({
|
|
265
|
+
type: Create, data: data(String(i), refused && i === 1 ? 9 : 0, 1),
|
|
266
|
+
}));
|
|
267
|
+
try { await step.emitAll(entries, {key: "batch"}); }
|
|
268
|
+
catch (error) { if (!(error instanceof RecordRefused)) throw error; }
|
|
269
|
+
await step.wait(Wake);
|
|
270
|
+
await step.emit(Result, {});
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
let host = createNiwa({home: home.path});
|
|
274
|
+
const state = async (value: string) => (await host.records.read({types: [CORE_TYPES.invocationState]}))
|
|
275
|
+
.some(row => (row.data as {state: string}).state === value);
|
|
276
|
+
try {
|
|
277
|
+
host.use(worker);
|
|
278
|
+
await host.serve();
|
|
279
|
+
await host.emit(Trigger, {});
|
|
280
|
+
await eventually(() => state("waiting"));
|
|
281
|
+
await host.close();
|
|
282
|
+
size = replaySize;
|
|
283
|
+
host = createNiwa({home: home.path});
|
|
284
|
+
host.use(worker);
|
|
285
|
+
await host.serve();
|
|
286
|
+
await host.emit(Wake, {});
|
|
287
|
+
if (size === 2) await eventually(async () => (await host.records.read({types: [Result]})).length === 1);
|
|
288
|
+
else await eventually(() => state("paused"));
|
|
289
|
+
expect(await host.records.read({types: [Create]})).toHaveLength(refused ? 0 : 2);
|
|
290
|
+
expect(await host.records.read({types: [CORE_TYPES.recordRefused]})).toHaveLength(refused ? 1 : 0);
|
|
291
|
+
} finally { await host.close(); await home.cleanup(); }
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Recovery contracts.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs against a real host process (fixtures/recovery-contracts-host.ts). The recovery
|
|
4
|
+
// contract of the action comes from the third argument of the host:
|
|
5
|
+
//
|
|
6
|
+
// 1. spawn the host, connect a client, emit Start { n }
|
|
7
|
+
// 2. the handler calls the action "effect", which blocks on the gate "g1"
|
|
8
|
+
// 3. kill the host with SIGKILL — a real crash, in the middle of the call
|
|
9
|
+
// 4. spawn the host again with the same contract: the step has an intent and no outcome
|
|
10
|
+
// 5. with "repeat": the action is called again and the invocation finishes
|
|
11
|
+
// 6. with "repeat-with-key": the action is called again with the same operation id as the
|
|
12
|
+
// first time — the ids are recorded by the stub, one per line, in the home
|
|
13
|
+
//
|
|
14
|
+
// Only the public API is used: the entries of the packages (types) and "@niwa-labs/testing".
|
|
15
|
+
|
|
16
|
+
import { expect, test } from "bun:test";
|
|
17
|
+
|
|
18
|
+
import { connect } from "@niwa-labs/client";
|
|
19
|
+
import { counter, eventually, gate, operations, spawnHost, tempHome, lastState, watch, connectAsOwner } from "@niwa-labs/testing";
|
|
20
|
+
|
|
21
|
+
import { Start } from "./fixtures/recovery-contracts-host.ts";
|
|
22
|
+
import { fileURLToPath } from "node:url";
|
|
23
|
+
|
|
24
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/recovery-contracts-host.ts", import.meta.url));
|
|
25
|
+
|
|
26
|
+
/** Run the crash and the restart with one recovery contract; `seen` is the journal as observed. */
|
|
27
|
+
async function crashAndRestart(recovery: "repeat" | "repeat-with-key") {
|
|
28
|
+
const home = await tempHome();
|
|
29
|
+
const homePath = home.path;
|
|
30
|
+
const hosts: Array<ReturnType<typeof spawnHost>> = [];
|
|
31
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
32
|
+
const cleanup = async (): Promise<void> => {
|
|
33
|
+
await watcher?.stop();
|
|
34
|
+
for (const host of hosts) await host.kill().catch(() => {});
|
|
35
|
+
await home.cleanup();
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const first = spawnHost(HOST_SCRIPT, homePath, [recovery]);
|
|
40
|
+
hosts.push(first);
|
|
41
|
+
await first.ready();
|
|
42
|
+
|
|
43
|
+
// One observer for the whole run: it reconnects to the second host by itself.
|
|
44
|
+
const observed = await watch(homePath);
|
|
45
|
+
watcher = observed;
|
|
46
|
+
const client = await connectAsOwner(homePath);
|
|
47
|
+
await client.emit(Start, { n: 7 });
|
|
48
|
+
|
|
49
|
+
// The action is blocked on the gate: it was called, and its outcome is not recorded.
|
|
50
|
+
await eventually(
|
|
51
|
+
() => counter(homePath, "effect").count() >= 1,
|
|
52
|
+
10_000,
|
|
53
|
+
"the effect should have been called once before the crash",
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// A real crash: SIGKILL, no clean shutdown.
|
|
57
|
+
await first.kill();
|
|
58
|
+
await client.close().catch(() => {});
|
|
59
|
+
|
|
60
|
+
// The second host resumes the unfinished invocation. The gate is opened right away.
|
|
61
|
+
const second = spawnHost(HOST_SCRIPT, homePath, [recovery]);
|
|
62
|
+
hosts.push(second);
|
|
63
|
+
gate(homePath, "g1").open();
|
|
64
|
+
await second.ready();
|
|
65
|
+
|
|
66
|
+
return { homePath, seen: observed.seen, cleanup };
|
|
67
|
+
} catch (error) {
|
|
68
|
+
await cleanup();
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
test("repeat: the action is called again after the crash and the invocation finishes", async () => {
|
|
74
|
+
const run = await crashAndRestart("repeat");
|
|
75
|
+
try {
|
|
76
|
+
// The action is called again: the contract allows it.
|
|
77
|
+
await eventually(
|
|
78
|
+
() => lastState(run.seen)?.state === "done",
|
|
79
|
+
15_000,
|
|
80
|
+
"the invocation should have finished",
|
|
81
|
+
);
|
|
82
|
+
expect(counter(run.homePath, "effect").count()).toBe(2);
|
|
83
|
+
} finally {
|
|
84
|
+
await run.cleanup();
|
|
85
|
+
}
|
|
86
|
+
}, 40_000);
|
|
87
|
+
|
|
88
|
+
test("repeat-with-key: the action is called again with the same operation id", async () => {
|
|
89
|
+
const run = await crashAndRestart("repeat-with-key");
|
|
90
|
+
try {
|
|
91
|
+
await eventually(
|
|
92
|
+
() => lastState(run.seen)?.state === "done",
|
|
93
|
+
15_000,
|
|
94
|
+
"the invocation should have finished",
|
|
95
|
+
);
|
|
96
|
+
expect(counter(run.homePath, "effect").count()).toBe(2);
|
|
97
|
+
|
|
98
|
+
// The operation id of the second call is the one of the first: the other side can dedupe
|
|
99
|
+
// it, because it is the same operation.
|
|
100
|
+
const ids = operations(run.homePath, "effect").ids();
|
|
101
|
+
expect(ids.length).toBe(2);
|
|
102
|
+
expect(ids[0]).toBeTruthy();
|
|
103
|
+
expect(ids[1]).toBe(ids[0]);
|
|
104
|
+
} finally {
|
|
105
|
+
await run.cleanup();
|
|
106
|
+
}
|
|
107
|
+
}, 40_000);
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Repeated write.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs against a real host process (fixtures/repeated-write-host.ts):
|
|
4
|
+
//
|
|
5
|
+
// 1. spawn the host, connect a client
|
|
6
|
+
// 2. write Note { n: 1 } with the explicit id "repeated-write.note.1"; the record is
|
|
7
|
+
// committed
|
|
8
|
+
// 3. write the same id with the same content: the record that is already
|
|
9
|
+
// committed comes back — the same id, the same position, and nothing new
|
|
10
|
+
// in the journal
|
|
11
|
+
// 4. write the same id with different content: the write is refused with an
|
|
12
|
+
// integrity error
|
|
13
|
+
// 5. the journal is unchanged: the record still holds the first content, and
|
|
14
|
+
// no record was added (a sentinel record written last marks the end)
|
|
15
|
+
//
|
|
16
|
+
// Only the public API is used: the entries of the packages and "@niwa-labs/testing".
|
|
17
|
+
|
|
18
|
+
import { expect, test } from "bun:test";
|
|
19
|
+
import { fileURLToPath } from "node:url";
|
|
20
|
+
|
|
21
|
+
import { connect } from "@niwa-labs/client";
|
|
22
|
+
import { seq } from "@niwa-labs/core";
|
|
23
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
24
|
+
import { eventually, spawnHost, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
25
|
+
|
|
26
|
+
import { Note } from "./fixtures/repeated-write-host.ts";
|
|
27
|
+
|
|
28
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/repeated-write-host.ts", import.meta.url));
|
|
29
|
+
|
|
30
|
+
/** The explicit id the test writes under. */
|
|
31
|
+
const ID = "repeated-write.note.1";
|
|
32
|
+
|
|
33
|
+
/** From the beginning of the journal. */
|
|
34
|
+
const FROM_START = seq(0);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The code of a refused write.
|
|
38
|
+
*
|
|
39
|
+
* The host names its refusals by the class of the error, so the code is what says that the
|
|
40
|
+
* refusal was an integrity error — whether the refusal arrived as the typed error or as the
|
|
41
|
+
* host's own error carrying that name.
|
|
42
|
+
*/
|
|
43
|
+
function codeOf(error: unknown): string | undefined {
|
|
44
|
+
if (typeof error !== "object" || error === null) return undefined;
|
|
45
|
+
const code: unknown = Reflect.get(error, "code");
|
|
46
|
+
return typeof code === "string" ? code : undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test("repeated write: the same id with the same content returns the existing record, and different content is an integrity error", async () => {
|
|
50
|
+
const home = await tempHome();
|
|
51
|
+
const homePath = home.path;
|
|
52
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
// 1. The host and a client.
|
|
56
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
57
|
+
await host.ready();
|
|
58
|
+
const client = await connectAsOwner(homePath);
|
|
59
|
+
|
|
60
|
+
// 2. The first write of that id commits a record.
|
|
61
|
+
const first = await client.emit(Note, { n: 1 }, { id: ID });
|
|
62
|
+
expect(first.id).toBe(ID as unknown as typeof first.id);
|
|
63
|
+
expect(first.data).toEqual({ n: 1 });
|
|
64
|
+
|
|
65
|
+
// The journal holds exactly one record of that type.
|
|
66
|
+
const seen: StoredRecord[] = [];
|
|
67
|
+
const watcher = await connectAsOwner(homePath);
|
|
68
|
+
const watch = (async () => {
|
|
69
|
+
for await (const record of watcher.records.since()) {
|
|
70
|
+
seen.push(record);
|
|
71
|
+
}
|
|
72
|
+
})().catch(() => {});
|
|
73
|
+
const notes = () => seen.filter((record) => record.type === Note.name);
|
|
74
|
+
await eventually(() => notes().length === 1, 10_000, "the first write should be in the journal");
|
|
75
|
+
|
|
76
|
+
// 3. The same id with the same content: the record that is already there
|
|
77
|
+
// comes back, and nothing is written.
|
|
78
|
+
const second = await client.emit(Note, { n: 1 }, { id: ID });
|
|
79
|
+
expect(second.id).toBe(first.id);
|
|
80
|
+
expect(second.seq).toBe(first.seq);
|
|
81
|
+
expect(second.data).toEqual({ n: 1 });
|
|
82
|
+
|
|
83
|
+
// 4. The same id with different content: the write is refused.
|
|
84
|
+
let refused: unknown;
|
|
85
|
+
try {
|
|
86
|
+
await client.emit(Note, { n: 2 }, { id: ID });
|
|
87
|
+
} catch (error) {
|
|
88
|
+
refused = error;
|
|
89
|
+
}
|
|
90
|
+
expect(refused).toBeInstanceOf(Error);
|
|
91
|
+
expect(codeOf(refused)).toBe("IntegrityError");
|
|
92
|
+
expect((refused as Error).message).toContain(ID);
|
|
93
|
+
|
|
94
|
+
// 5. Nothing changed: the record still holds the first content, and neither
|
|
95
|
+
// repeated write added a record. A sentinel written last is observed after
|
|
96
|
+
// anything the earlier writes could have added, so the journal is complete
|
|
97
|
+
// when it arrives.
|
|
98
|
+
const sentinel = await client.emit(Note, { n: 3 }, { id: "repeated-write.note.sentinel" });
|
|
99
|
+
await eventually(
|
|
100
|
+
() => seen.some((record) => record.id === sentinel.id),
|
|
101
|
+
10_000,
|
|
102
|
+
"the sentinel should be in the journal",
|
|
103
|
+
);
|
|
104
|
+
expect(notes().map((record) => record.id)).toEqual([first.id, sentinel.id]);
|
|
105
|
+
expect(notes()[0]?.data).toEqual({ n: 1 });
|
|
106
|
+
expect(notes()[0]?.seq).toBe(first.seq);
|
|
107
|
+
|
|
108
|
+
// The record is still readable under its id, unchanged.
|
|
109
|
+
const traced = await client.trace(first.id);
|
|
110
|
+
expect(traced[0]?.data).toEqual({ n: 1 });
|
|
111
|
+
|
|
112
|
+
await client.close().catch(() => {});
|
|
113
|
+
await watcher.close().catch(() => {});
|
|
114
|
+
await watch;
|
|
115
|
+
} finally {
|
|
116
|
+
await host?.kill().catch(() => {});
|
|
117
|
+
await home.cleanup();
|
|
118
|
+
}
|
|
119
|
+
}, 30_000);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Replay mismatch.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs against a real host process (fixtures/replay-mismatch-host.ts). The number the step
|
|
4
|
+
// is called with comes from the third argument of the host:
|
|
5
|
+
//
|
|
6
|
+
// 1. spawn the host with the step input 7, connect a client, emit Start { n }
|
|
7
|
+
// 2. the handler calls the action "effect", which blocks on the gate "g1"
|
|
8
|
+
// 3. kill the host with SIGKILL — a real crash, in the middle of the call
|
|
9
|
+
// 4. spawn the host again with the step input 9: the invocation resumes on the same record,
|
|
10
|
+
// but the step is now called with a different input under the same key
|
|
11
|
+
// 5. the invocation pauses with a mismatch, and the effect was not called again
|
|
12
|
+
//
|
|
13
|
+
// Only the public API is used: the entries of the packages (types) and "@niwa-labs/testing".
|
|
14
|
+
|
|
15
|
+
import { expect, test } from "bun:test";
|
|
16
|
+
|
|
17
|
+
import { connect } from "@niwa-labs/client";
|
|
18
|
+
import { counter, eventually, gate, spawnHost, tempHome, lastState, watch, connectAsOwner } from "@niwa-labs/testing";
|
|
19
|
+
|
|
20
|
+
import { Start } from "./fixtures/replay-mismatch-host.ts";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
22
|
+
|
|
23
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/replay-mismatch-host.ts", import.meta.url));
|
|
24
|
+
|
|
25
|
+
test("replay mismatch: the step is called with another input under the same key, and the invocation pauses", async () => {
|
|
26
|
+
const home = await tempHome();
|
|
27
|
+
const homePath = home.path;
|
|
28
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
29
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
30
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
// 1. The first host. It is killed in the middle of the call.
|
|
34
|
+
first = spawnHost(HOST_SCRIPT, homePath, ["7"]);
|
|
35
|
+
await first.ready();
|
|
36
|
+
|
|
37
|
+
const client = await connectAsOwner(homePath);
|
|
38
|
+
await client.emit(Start, { n: 7 });
|
|
39
|
+
|
|
40
|
+
// 2. The action is blocked on the gate: it was called, and its outcome is not recorded.
|
|
41
|
+
await eventually(
|
|
42
|
+
() => counter(homePath, "effect").count() >= 1,
|
|
43
|
+
10_000,
|
|
44
|
+
"the effect should have been called once before the crash",
|
|
45
|
+
);
|
|
46
|
+
expect(counter(homePath, "effect").count()).toBe(1);
|
|
47
|
+
|
|
48
|
+
// 3. A real crash: SIGKILL, no clean shutdown.
|
|
49
|
+
await first.kill();
|
|
50
|
+
await client.close().catch(() => {});
|
|
51
|
+
|
|
52
|
+
// 4. The second host resumes the unfinished invocation, but the step is called with
|
|
53
|
+
// another input under the same key.
|
|
54
|
+
second = spawnHost(HOST_SCRIPT, homePath, ["9"]);
|
|
55
|
+
gate(homePath, "g1").open();
|
|
56
|
+
await second.ready();
|
|
57
|
+
|
|
58
|
+
const observed = await watch(homePath);
|
|
59
|
+
watcher = observed;
|
|
60
|
+
|
|
61
|
+
// 5. The invocation pauses with a mismatch.
|
|
62
|
+
await eventually(
|
|
63
|
+
() => lastState(observed.seen)?.state === "paused",
|
|
64
|
+
15_000,
|
|
65
|
+
"the invocation should have paused",
|
|
66
|
+
);
|
|
67
|
+
const paused = lastState(observed.seen);
|
|
68
|
+
expect(paused?.state).toBe("paused");
|
|
69
|
+
expect(paused?.reason ?? "").toContain("replay mismatch");
|
|
70
|
+
|
|
71
|
+
// No second effect happened: the action was not called again.
|
|
72
|
+
expect(counter(homePath, "effect").count()).toBe(1);
|
|
73
|
+
} finally {
|
|
74
|
+
await watcher?.stop();
|
|
75
|
+
await first?.kill().catch(() => {});
|
|
76
|
+
await second?.kill().catch(() => {});
|
|
77
|
+
await home.cleanup();
|
|
78
|
+
}
|
|
79
|
+
}, 40_000);
|