@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,180 @@
|
|
|
1
|
+
// The version of a plugin and moving work between versions.
|
|
2
|
+
//
|
|
3
|
+
// 1. the default version covers the handlers' source, also when a handler is defined outside the
|
|
4
|
+
// setup function; the same code is the same version, and an explicit version is kept
|
|
5
|
+
// 2. a restart onto changed code does not move unfinished work: it pauses as "plugin version
|
|
6
|
+
// unavailable"; the explicit move runs it on the installed version, and it ends as done
|
|
7
|
+
|
|
8
|
+
import { expect, test } from "bun:test";
|
|
9
|
+
|
|
10
|
+
import { MOVE_ACTION, record, seq } from "@niwa-labs/core";
|
|
11
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
12
|
+
import { plugin, withoutHandler } from "@niwa-labs/plugin";
|
|
13
|
+
import type { Handler, StoredRecord } from "@niwa-labs/core";
|
|
14
|
+
import { eventually, tempHome, STATE_TYPE } from "@niwa-labs/testing";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
|
|
17
|
+
const Start = record("pv.start", z.object({ n: z.number() }));
|
|
18
|
+
const Answer = record("pv.answer", z.object({ n: z.number() }));
|
|
19
|
+
const Done = record("pv.done", z.object({ n: z.number(), by: z.string() }));
|
|
20
|
+
|
|
21
|
+
const sink: string[] = [];
|
|
22
|
+
|
|
23
|
+
/** The same setup text twice; only the handler it is given differs. */
|
|
24
|
+
function pluginWith(handler: () => void, version?: string) {
|
|
25
|
+
return plugin(
|
|
26
|
+
"pv-shape",
|
|
27
|
+
(api) => {
|
|
28
|
+
api.declare(Start);
|
|
29
|
+
api.on(Start, handler);
|
|
30
|
+
},
|
|
31
|
+
version === undefined ? {} : { version },
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const handlerOne = () => {
|
|
36
|
+
sink.push("one");
|
|
37
|
+
};
|
|
38
|
+
const handlerOneAgain = () => {
|
|
39
|
+
sink.push("one");
|
|
40
|
+
};
|
|
41
|
+
const handlerTwo = () => {
|
|
42
|
+
sink.push("two");
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** The version a plugin carries once it is installed: the setup has run and the handlers are known. */
|
|
46
|
+
async function installedVersion(made: ReturnType<typeof pluginWith>): Promise<string> {
|
|
47
|
+
const home = await tempHome();
|
|
48
|
+
try {
|
|
49
|
+
createNiwa({ home: home.path }).use(made);
|
|
50
|
+
return String(made.version);
|
|
51
|
+
} finally {
|
|
52
|
+
await home.cleanup();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
test("the default version covers a handler defined outside the setup function", async () => {
|
|
57
|
+
const one = await installedVersion(pluginWith(handlerOne));
|
|
58
|
+
expect(await installedVersion(pluginWith(handlerOneAgain))).toBe(one);
|
|
59
|
+
expect(await installedVersion(pluginWith(handlerTwo))).not.toBe(one);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("an explicit version is kept whatever the code is", async () => {
|
|
63
|
+
expect(await installedVersion(pluginWith(handlerOne, "r1"))).toBe("r1");
|
|
64
|
+
expect(await installedVersion(pluginWith(handlerTwo, "r1"))).toBe("r1");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/** The handlers of the two versions of one plugin: the setup text is the same, the code is not. */
|
|
68
|
+
const finishOld: Handler<z.infer<typeof Start.schema>> = async (data, step) => {
|
|
69
|
+
await step.wait(Answer, { filter: { n: data.n }, key: "answer" });
|
|
70
|
+
await step.emit(Done, { n: data.n, by: "old" });
|
|
71
|
+
};
|
|
72
|
+
const finishNew: Handler<z.infer<typeof Start.schema>> = async (data, step) => {
|
|
73
|
+
await step.wait(Answer, { filter: { n: data.n }, key: "answer" });
|
|
74
|
+
await step.emit(Done, { n: data.n, by: "new" });
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
function worker(handler: Handler<z.infer<typeof Start.schema>>) {
|
|
78
|
+
return plugin("pv-work", (api) => {
|
|
79
|
+
api.declare(Start, Answer, Done);
|
|
80
|
+
api.on(Start, handler);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function statesOf(records: StoredRecord[]): Array<{ state: string; reason?: string; choices?: string[] }> {
|
|
85
|
+
return records
|
|
86
|
+
.filter((r) => r.type === STATE_TYPE)
|
|
87
|
+
.map((r) => r.data as { state: string; reason?: string; choices?: string[] });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
test("a restart onto changed code pauses unfinished work; only the explicit move runs it on the new code", async () => {
|
|
91
|
+
const home = await tempHome();
|
|
92
|
+
try {
|
|
93
|
+
const first = createNiwa({ home: home.path });
|
|
94
|
+
first.use(worker(finishOld));
|
|
95
|
+
await first.serve();
|
|
96
|
+
await first.emit(Start, { n: 1 });
|
|
97
|
+
await eventually(
|
|
98
|
+
async () => statesOf(await first.records.read()).at(-1)?.state === "waiting",
|
|
99
|
+
10_000,
|
|
100
|
+
"the invocation does not wait",
|
|
101
|
+
);
|
|
102
|
+
await first.close();
|
|
103
|
+
|
|
104
|
+
const second = createNiwa({ home: home.path });
|
|
105
|
+
const next = worker(finishNew);
|
|
106
|
+
second.use(next);
|
|
107
|
+
await second.serve();
|
|
108
|
+
try {
|
|
109
|
+
await eventually(
|
|
110
|
+
async () => statesOf(await second.records.read()).at(-1)?.state === "paused",
|
|
111
|
+
10_000,
|
|
112
|
+
"the invocation is not paused",
|
|
113
|
+
);
|
|
114
|
+
const paused = statesOf(await second.records.read()).at(-1);
|
|
115
|
+
expect(paused?.reason).toContain("plugin version unavailable");
|
|
116
|
+
expect(paused?.choices).toEqual(["cancel"]);
|
|
117
|
+
|
|
118
|
+
// The answer arrives, and the restart has still not moved anything.
|
|
119
|
+
await second.emit(Answer, { n: 1 });
|
|
120
|
+
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
121
|
+
expect(await second.records.read({ types: [Done] })).toHaveLength(0);
|
|
122
|
+
|
|
123
|
+
const admitted = (await second.records.read()).find((r) => r.type === "niwa.invocation.admitted");
|
|
124
|
+
const invocation = (admitted?.data as { invocation: string }).invocation;
|
|
125
|
+
const moved = await second.call<{ invocation: string }, { from: string; to: string }>(
|
|
126
|
+
MOVE_ACTION,
|
|
127
|
+
{ invocation },
|
|
128
|
+
);
|
|
129
|
+
expect(moved.to).toBe(String(next.version));
|
|
130
|
+
expect(moved.from).not.toBe(moved.to);
|
|
131
|
+
|
|
132
|
+
await eventually(
|
|
133
|
+
async () => (await second.records.read({ types: [Done] })).length === 1,
|
|
134
|
+
10_000,
|
|
135
|
+
"the moved invocation did not finish",
|
|
136
|
+
);
|
|
137
|
+
const [done] = await second.records.read({ types: [Done] });
|
|
138
|
+
expect(done?.data).toEqual({ n: 1, by: "new" });
|
|
139
|
+
expect(statesOf(await second.records.read()).at(-1)?.state).toBe("done");
|
|
140
|
+
await expect(second.call(MOVE_ACTION, { invocation })).rejects.toThrow("done");
|
|
141
|
+
} finally {
|
|
142
|
+
await second.close();
|
|
143
|
+
}
|
|
144
|
+
} finally {
|
|
145
|
+
await home.cleanup();
|
|
146
|
+
}
|
|
147
|
+
}, 60_000);
|
|
148
|
+
|
|
149
|
+
const Spare = record("pv.spare", z.object({}));
|
|
150
|
+
const spareHandler = () => {};
|
|
151
|
+
|
|
152
|
+
function twoHandlers(handler: () => void) {
|
|
153
|
+
return plugin("pv-two", (api) => {
|
|
154
|
+
api.declare(Start, Spare);
|
|
155
|
+
api.on(Start, handler);
|
|
156
|
+
api.on(Spare, spareHandler);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
test("a plugin without a handler carries the version of its source code, handlers outside the setup included", async () => {
|
|
161
|
+
const home = await tempHome();
|
|
162
|
+
const niwa = createNiwa({ home: home.path });
|
|
163
|
+
try {
|
|
164
|
+
const versionOf = (made: ReturnType<typeof twoHandlers>) => {
|
|
165
|
+
const without = withoutHandler(made, Spare.name);
|
|
166
|
+
niwa.use(without);
|
|
167
|
+
return String(without.version);
|
|
168
|
+
};
|
|
169
|
+
const one = versionOf(twoHandlers(handlerOne));
|
|
170
|
+
expect(versionOf(twoHandlers(handlerOneAgain))).toBe(one);
|
|
171
|
+
expect(versionOf(twoHandlers(handlerTwo))).not.toBe(one);
|
|
172
|
+
|
|
173
|
+
// The source installed whole and then taken without the handler is the same code, the same version.
|
|
174
|
+
const whole = twoHandlers(handlerOne);
|
|
175
|
+
niwa.use(whole);
|
|
176
|
+
expect(String(withoutHandler(whole, Spare.name).version)).toBe(one);
|
|
177
|
+
} finally {
|
|
178
|
+
await home.cleanup();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// The query of `niwa.records.read`, the filters of `niwa.records.search`, and the read that waits for
|
|
2
|
+
// a record: what the CLI's log and an MCP client both ask. The actions are called as a client calls
|
|
3
|
+
// them, over the wire.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { actionName, record } from "@niwa-labs/core";
|
|
8
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
9
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
10
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
11
|
+
import { tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
|
|
14
|
+
const READ = actionName("niwa.records.read");
|
|
15
|
+
const SEARCH = actionName("niwa.records.search");
|
|
16
|
+
|
|
17
|
+
const Ping = record("filter.ping", z.object({ n: z.number(), run: z.string().optional() }), { open: true });
|
|
18
|
+
const Pong = record("filter.pong", z.object({ text: z.string() }), { open: true });
|
|
19
|
+
const Bare = record("filter.bare", z.undefined(), { open: true });
|
|
20
|
+
const Noise = record("filter.noise", z.object({ text: z.string() }), { open: true });
|
|
21
|
+
|
|
22
|
+
interface Read {
|
|
23
|
+
records: StoredRecord[];
|
|
24
|
+
next: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function hosted() {
|
|
28
|
+
const home = await tempHome();
|
|
29
|
+
const niwa = createNiwa({ home: home.path });
|
|
30
|
+
niwa.use(plugin("filter", (api) => api.declare(Ping, Pong, Noise, Bare)));
|
|
31
|
+
await niwa.serve();
|
|
32
|
+
const client = await connectAsOwner(home.path);
|
|
33
|
+
const read = async (input: Record<string, unknown>): Promise<Read> => (await client.call(READ, input)) as Read;
|
|
34
|
+
const search = async (input: Record<string, unknown>): Promise<Read> => (await client.call(SEARCH, input)) as Read;
|
|
35
|
+
return {
|
|
36
|
+
niwa,
|
|
37
|
+
read,
|
|
38
|
+
search,
|
|
39
|
+
close: async () => {
|
|
40
|
+
await client.close().catch(() => {});
|
|
41
|
+
await niwa.close();
|
|
42
|
+
await home.cleanup();
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
test("a read answers with the records and the position to read after next", async () => {
|
|
48
|
+
const h = await hosted();
|
|
49
|
+
try {
|
|
50
|
+
const first = await h.niwa.emit(Ping, { n: 1 });
|
|
51
|
+
const second = await h.niwa.emit(Ping, { n: 2 });
|
|
52
|
+
const all = await h.read({ from: 0, types: ["filter.ping"] });
|
|
53
|
+
expect(all.records.map((row) => row.id)).toEqual([first.id, second.id]);
|
|
54
|
+
// The read went to the end of the journal, which is as far as the last record of its type or further.
|
|
55
|
+
expect(all.next).toBeGreaterThanOrEqual(second.seq);
|
|
56
|
+
// Past the end nothing is found and the position stays where it was.
|
|
57
|
+
const none = await h.read({ from: all.next, types: ["filter.ping"] });
|
|
58
|
+
expect(none).toEqual({ records: [], next: all.next });
|
|
59
|
+
// A limit stops the read at the last record it kept.
|
|
60
|
+
const one = await h.read({ from: 0, types: ["filter.ping"], limit: 1 });
|
|
61
|
+
expect(one.records.map((row) => row.id)).toEqual([first.id]);
|
|
62
|
+
expect(one.next).toBe(first.seq);
|
|
63
|
+
} finally {
|
|
64
|
+
await h.close();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("a filter narrows by type pattern, actor, subject and text, and the filters hold together", async () => {
|
|
69
|
+
const h = await hosted();
|
|
70
|
+
try {
|
|
71
|
+
await h.niwa.emit(Ping, { n: 1, run: "run_aaaa1111" });
|
|
72
|
+
await h.niwa.emit(Ping, { n: 2, run: "run_bbbb2222" });
|
|
73
|
+
await h.niwa.emit(Pong, { text: "Hello World" });
|
|
74
|
+
await h.niwa.emit(Noise, { text: "noise" });
|
|
75
|
+
// A record with no data is not a match for a text, and does not stop the read.
|
|
76
|
+
await h.niwa.emit(Bare, undefined);
|
|
77
|
+
|
|
78
|
+
const types = (found: Read): string[] => found.records.map((row) => String(row.type));
|
|
79
|
+
expect(types(await h.search({ filter: { types: ["filter.p*"] } }))).toEqual(["filter.ping", "filter.ping", "filter.pong"]);
|
|
80
|
+
// A pattern that begins with ! leaves its types out; with nothing else named, everything else stays.
|
|
81
|
+
const without = types(await h.search({ filter: { types: ["!filter.*"] } }));
|
|
82
|
+
expect(without.some((type) => type.startsWith("filter."))).toBe(false);
|
|
83
|
+
expect(without.length).toBeGreaterThan(0);
|
|
84
|
+
expect(types(await h.search({ filter: { types: ["filter.*", "!filter.noise", "!filter.pong", "!filter.bare"] } }))).toEqual(["filter.ping", "filter.ping"]);
|
|
85
|
+
|
|
86
|
+
// A subject is found by the field of the data that names it, and an id may be a prefix.
|
|
87
|
+
expect((await h.search({ filter: { run: "run_bbbb" } })).records.map((row) => (row.data as { n: number }).n)).toEqual([2]);
|
|
88
|
+
expect(types(await h.search({ filter: { grep: "hello world" } }))).toEqual(["filter.pong"]);
|
|
89
|
+
expect(types(await h.search({ filter: { actor: "host" } })).length).toBeGreaterThan(0);
|
|
90
|
+
expect((await h.search({ filter: { actor: "nobody" } })).records).toEqual([]);
|
|
91
|
+
expect(types(await h.search({ filter: { types: ["filter.*"], grep: "noise" } }))).toEqual(["filter.noise"]);
|
|
92
|
+
} finally {
|
|
93
|
+
await h.close();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("until, a span of time and a tail pick records from the end or the middle", async () => {
|
|
98
|
+
const h = await hosted();
|
|
99
|
+
try {
|
|
100
|
+
const rows: StoredRecord[] = [];
|
|
101
|
+
for (let n = 1; n <= 5; n += 1) rows.push(await h.niwa.emit(Ping, { n }));
|
|
102
|
+
const numbers = (found: Read) => found.records.map((row) => (row.data as { n: number }).n);
|
|
103
|
+
expect(numbers(await h.search({ tail: 2, filter: { types: ["filter.*"] } }))).toEqual([4, 5]);
|
|
104
|
+
expect(numbers(await h.search({ filter: { types: ["filter.*"], until: rows[2]?.seq } }))).toEqual([1, 2, 3]);
|
|
105
|
+
const until = await h.search({ filter: { types: ["filter.*"], until: rows[2]?.seq } });
|
|
106
|
+
expect(until.next).toBe(rows[2]?.seq ?? -1);
|
|
107
|
+
expect(numbers(await h.search({ filter: { types: ["filter.*"], after: (rows[4]?.at ?? 0) + 1 } }))).toEqual([]);
|
|
108
|
+
expect(numbers(await h.search({ filter: { types: ["filter.*"], before: (rows[0]?.at ?? 0) + 3_600_000 } }))).toEqual([1, 2, 3, 4, 5]);
|
|
109
|
+
} finally {
|
|
110
|
+
await h.close();
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("a read with waitMs waits for a record and returns at once when one is written, or empty at the end of the time", async () => {
|
|
115
|
+
const h = await hosted();
|
|
116
|
+
try {
|
|
117
|
+
const last = (await h.read({ from: 0 })).next;
|
|
118
|
+
const started = Date.now();
|
|
119
|
+
const empty = await h.search({ from: last, waitMs: 150, filter: { types: ["filter.*"] } });
|
|
120
|
+
expect(empty.records).toEqual([]);
|
|
121
|
+
expect(Date.now() - started).toBeGreaterThanOrEqual(140);
|
|
122
|
+
|
|
123
|
+
const waiting = h.search({ from: last, waitMs: 20_000, filter: { types: ["filter.ping"] } });
|
|
124
|
+
await Bun.sleep(100);
|
|
125
|
+
// A record the filter does not want does not end the wait.
|
|
126
|
+
await h.niwa.emit(Noise, { text: "not wanted" });
|
|
127
|
+
await Bun.sleep(100);
|
|
128
|
+
const wanted = await h.niwa.emit(Ping, { n: 7 });
|
|
129
|
+
const found = await waiting;
|
|
130
|
+
expect(found.records.map((row) => row.id)).toEqual([wanted.id]);
|
|
131
|
+
expect(found.next).toBe(wanted.seq);
|
|
132
|
+
expect(Date.now() - started).toBeLessThan(5_000);
|
|
133
|
+
} finally {
|
|
134
|
+
await h.close();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// One record query for every reader: the read action, a client's snapshot and its live
|
|
2
|
+
// continuation take the same query and give the same records.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { actionName, record } from "@niwa-labs/core";
|
|
7
|
+
import type { RecordQuery, StoredRecord } from "@niwa-labs/core";
|
|
8
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
9
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
10
|
+
import { connectAsOwner, eventually, tempHome } from "@niwa-labs/testing";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
const READ = actionName("niwa.records.read");
|
|
14
|
+
|
|
15
|
+
const Ping = record("query.ping", z.object({ n: z.number(), to: z.object({ kind: z.string() }) }), { open: true });
|
|
16
|
+
const Pong = record("query.pong", z.object({ n: z.number(), to: z.object({ kind: z.string() }) }), { open: true });
|
|
17
|
+
const Other = record("other.ping", z.object({ n: z.number(), to: z.object({ kind: z.string() }) }), { open: true });
|
|
18
|
+
|
|
19
|
+
async function hosted() {
|
|
20
|
+
const home = await tempHome();
|
|
21
|
+
const niwa = createNiwa({ home: home.path });
|
|
22
|
+
niwa.use(plugin("query", (api) => api.declare(Ping, Pong, Other)));
|
|
23
|
+
await niwa.serve();
|
|
24
|
+
const client = await connectAsOwner(home.path);
|
|
25
|
+
return {
|
|
26
|
+
niwa,
|
|
27
|
+
client,
|
|
28
|
+
close: async () => {
|
|
29
|
+
await client.close().catch(() => {});
|
|
30
|
+
await niwa.close();
|
|
31
|
+
await home.cleanup();
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const numbers = (rows: readonly StoredRecord[]): number[] => rows.map((row) => (row.data as { n: number }).n);
|
|
37
|
+
|
|
38
|
+
test("the read action takes a family and equality on a path, and refuses a pattern of another language", async () => {
|
|
39
|
+
const h = await hosted();
|
|
40
|
+
try {
|
|
41
|
+
await h.client.emit(Ping, { n: 1, to: { kind: "session" } });
|
|
42
|
+
await h.client.emit(Pong, { n: 2, to: { kind: "agent" } });
|
|
43
|
+
await h.client.emit(Other, { n: 3, to: { kind: "session" } });
|
|
44
|
+
await h.client.emit(Pong, { n: 4, to: { kind: "session" } });
|
|
45
|
+
const family = (await h.client.call(READ, { types: ["query.*"] })) as { records: StoredRecord[] };
|
|
46
|
+
expect(numbers(family.records)).toEqual([1, 2, 4]);
|
|
47
|
+
const filtered = (await h.client.call(READ, { types: ["query.*"], filter: { "to.kind": "session" } })) as { records: StoredRecord[] };
|
|
48
|
+
expect(numbers(filtered.records)).toEqual([1, 4]);
|
|
49
|
+
await expect(h.client.call(READ, { types: ["!query.*"] })).rejects.toThrow();
|
|
50
|
+
await expect(h.client.call(READ, { types: ["query.p*"] })).rejects.toThrow();
|
|
51
|
+
// A field the read does not take is refused, not ignored: `type` is not a field of a query.
|
|
52
|
+
await expect(h.client.call(READ, { type: "query.ping" })).rejects.toThrow();
|
|
53
|
+
} finally {
|
|
54
|
+
await h.close();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("a snapshot and its live continuation with one query give what the live query alone gives", async () => {
|
|
59
|
+
const h = await hosted();
|
|
60
|
+
try {
|
|
61
|
+
const query: RecordQuery = { types: ["query.*"], filter: { "to.kind": "session" } };
|
|
62
|
+
await h.client.emit(Ping, { n: 1, to: { kind: "session" } });
|
|
63
|
+
await h.client.emit(Pong, { n: 2, to: { kind: "agent" } });
|
|
64
|
+
await h.client.emit(Other, { n: 3, to: { kind: "session" } });
|
|
65
|
+
|
|
66
|
+
const snapshot = await h.client.records.read(query);
|
|
67
|
+
const continued: StoredRecord[] = [];
|
|
68
|
+
const alone: StoredRecord[] = [];
|
|
69
|
+
const follow = async (into: StoredRecord[], from: RecordQuery) => {
|
|
70
|
+
for await (const row of h.client.records.since(from)) {
|
|
71
|
+
into.push(row);
|
|
72
|
+
if ((row.data as { n: number }).n === 7) return;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const both = Promise.all([
|
|
76
|
+
follow(continued, { ...query, from: snapshot.at(-1)?.seq ?? 0 }),
|
|
77
|
+
follow(alone, query),
|
|
78
|
+
]);
|
|
79
|
+
await h.client.emit(Pong, { n: 4, to: { kind: "session" } });
|
|
80
|
+
await h.client.emit(Other, { n: 5, to: { kind: "session" } });
|
|
81
|
+
await h.client.emit(Ping, { n: 6, to: { kind: "agent" } });
|
|
82
|
+
await h.client.emit(Ping, { n: 7, to: { kind: "session" } });
|
|
83
|
+
await both;
|
|
84
|
+
expect(numbers([...snapshot, ...continued])).toEqual([1, 4, 7]);
|
|
85
|
+
expect(numbers(alone)).toEqual([1, 4, 7]);
|
|
86
|
+
// The host's own reader answers the same.
|
|
87
|
+
expect(numbers(await h.niwa.records.read(query))).toEqual([1, 4, 7]);
|
|
88
|
+
await eventually(() => true);
|
|
89
|
+
} finally {
|
|
90
|
+
await h.close();
|
|
91
|
+
}
|
|
92
|
+
});
|