@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,122 @@
|
|
|
1
|
+
// A step that names its action: the host resolves the name.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs in one process, against a real host on a temporary home:
|
|
4
|
+
//
|
|
5
|
+
// 1. a step by name calls the action the host serves under it, and its answer is the value
|
|
6
|
+
// 2. a name nobody serves throws the typed UnknownAction to the code, and the invocation
|
|
7
|
+
// pauses on the step even when the code swallows the error
|
|
8
|
+
// 3. `step.outcome` with a name nobody serves answers "failed" and still takes the step's key,
|
|
9
|
+
// so the step after it keeps its own
|
|
10
|
+
|
|
11
|
+
import { expect, test } from "bun:test";
|
|
12
|
+
|
|
13
|
+
import { action, actionName, CORE_TYPES, record, seq, UnknownAction } from "@niwa-labs/core";
|
|
14
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
15
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
16
|
+
import type { ActionName, RecordTypeName, StoredRecord } from "@niwa-labs/core";
|
|
17
|
+
import type { Host } from "@niwa-labs/host";
|
|
18
|
+
import { eventually, tempHome } from "@niwa-labs/testing";
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
|
|
21
|
+
const Trigger = record("sbn.trigger", z.object({ mode: z.enum(["found", "missing", "outcome"]) }));
|
|
22
|
+
const Result = record("sbn.result", z.object({ text: z.string() }));
|
|
23
|
+
|
|
24
|
+
const SERVED: ActionName = actionName("sbn.upper");
|
|
25
|
+
const MISSING: ActionName = actionName("sbn.nobody");
|
|
26
|
+
|
|
27
|
+
const upper = action("sbn.upper", {
|
|
28
|
+
description: "Upper-cases a text.",
|
|
29
|
+
input: z.object({ text: z.string() }),
|
|
30
|
+
output: z.object({ text: z.string() }),
|
|
31
|
+
recovery: "repeat",
|
|
32
|
+
effect: "write",
|
|
33
|
+
run: async (input) => ({ text: input.text.toUpperCase() }),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const start = action("sbn.start", {
|
|
37
|
+
description: "Writes the trigger.",
|
|
38
|
+
input: z.object({ mode: z.enum(["found", "missing", "outcome"]) }),
|
|
39
|
+
output: z.object({}),
|
|
40
|
+
recovery: "repeat",
|
|
41
|
+
effect: "write",
|
|
42
|
+
run: async (input, ctx) => {
|
|
43
|
+
await ctx.emit(Trigger, { mode: input.mode });
|
|
44
|
+
return {};
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const caller = plugin("sbn", (api) => {
|
|
49
|
+
api.declare(Trigger, Result);
|
|
50
|
+
api.action(upper);
|
|
51
|
+
api.action(start);
|
|
52
|
+
api.on(Trigger, async (trigger, step) => {
|
|
53
|
+
if (trigger.mode === "found") {
|
|
54
|
+
const answer = await step<{ text: string }, { text: string }>(SERVED, { text: "done" });
|
|
55
|
+
await step.emit(Result, { text: answer.text });
|
|
56
|
+
} else if (trigger.mode === "missing") {
|
|
57
|
+
try {
|
|
58
|
+
await step(MISSING, {});
|
|
59
|
+
} catch (error) {
|
|
60
|
+
// The code swallows the error: the invocation still pauses.
|
|
61
|
+
await step.emit(Result, { text: error instanceof UnknownAction ? error.action : "other" });
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
const outcome = await step.outcome(MISSING, {});
|
|
65
|
+
const after = await step<{ text: string }, { text: string }>(SERVED, { text: outcome.status });
|
|
66
|
+
await step.emit(Result, { text: after.text });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
async function recordsOf(niwa: Host, type: RecordTypeName): Promise<StoredRecord[]> {
|
|
72
|
+
return niwa.records.read({ types: [type] });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function run(mode: "found" | "missing" | "outcome", then: (niwa: Host) => Promise<void>) {
|
|
76
|
+
const home = await tempHome();
|
|
77
|
+
const niwa = createNiwa({ home: home.path });
|
|
78
|
+
niwa.use(caller);
|
|
79
|
+
await niwa.serve();
|
|
80
|
+
try {
|
|
81
|
+
await niwa.call(start, { mode });
|
|
82
|
+
await then(niwa);
|
|
83
|
+
} finally {
|
|
84
|
+
await niwa.close();
|
|
85
|
+
await home.cleanup();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
test("a step by name calls the action the host serves", async () => {
|
|
90
|
+
await run("found", async (niwa) => {
|
|
91
|
+
await eventually(async () => (await recordsOf(niwa, Result.name)).length === 1, 5_000, "the result");
|
|
92
|
+
expect((await recordsOf(niwa, Result.name))[0]?.data).toEqual({ text: "DONE" });
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("a name nobody serves is a typed error, and the invocation pauses on the step", async () => {
|
|
97
|
+
await run("missing", async (niwa) => {
|
|
98
|
+
await eventually(
|
|
99
|
+
async () =>
|
|
100
|
+
(await recordsOf(niwa, CORE_TYPES.invocationState)).some(
|
|
101
|
+
(r) => (r.data as { state?: string }).state === "paused",
|
|
102
|
+
),
|
|
103
|
+
5_000,
|
|
104
|
+
"the invocation should pause",
|
|
105
|
+
);
|
|
106
|
+
const results = await recordsOf(niwa, Result.name);
|
|
107
|
+
expect(results.map((r) => r.data)).toEqual([{ text: MISSING }]);
|
|
108
|
+
const paused = (await recordsOf(niwa, CORE_TYPES.invocationState)).filter(
|
|
109
|
+
(r) => (r.data as { state?: string }).state === "paused",
|
|
110
|
+
);
|
|
111
|
+
expect(JSON.stringify(paused[0]?.data)).toContain(MISSING);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("an outcome by an unserved name is failed, and the next step keeps its key", async () => {
|
|
116
|
+
await run("outcome", async (niwa) => {
|
|
117
|
+
await eventually(async () => (await recordsOf(niwa, Result.name)).length === 1, 5_000, "the result");
|
|
118
|
+
expect((await recordsOf(niwa, Result.name))[0]?.data).toEqual({ text: "FAILED" });
|
|
119
|
+
const intents = await recordsOf(niwa, CORE_TYPES.stepIntent);
|
|
120
|
+
expect(intents.map((r) => (r.data as { key?: string }).key)).toEqual(["2"]);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// The stores of a host: artifacts named by their bytes and durable before anything points at them,
|
|
2
|
+
// and secrets resolved from the owner's sources, never written down.
|
|
3
|
+
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { chmodSync, existsSync, mkdirSync, readdirSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { afterEach, expect, test } from "bun:test";
|
|
8
|
+
|
|
9
|
+
import { ArtifactMissing, SecretRefusal } from "@niwa-labs/core";
|
|
10
|
+
import type { ArtifactStore, SecretStore } from "@niwa-labs/core";
|
|
11
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
12
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
13
|
+
import { tempHome } from "@niwa-labs/testing";
|
|
14
|
+
|
|
15
|
+
const homes: Array<() => Promise<void>> = [];
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
for (const cleanup of homes.splice(0)) await cleanup();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/** A served host, and the stores a plugin of it is given. */
|
|
21
|
+
async function host() {
|
|
22
|
+
const home = await tempHome();
|
|
23
|
+
homes.push(home.cleanup);
|
|
24
|
+
const niwa = createNiwa({ home: home.path });
|
|
25
|
+
let given: { artifacts: ArtifactStore; secrets: SecretStore } | undefined;
|
|
26
|
+
niwa.use(plugin("stores-probe", (api) => {
|
|
27
|
+
given = { artifacts: api.artifacts, secrets: api.secrets };
|
|
28
|
+
}));
|
|
29
|
+
await niwa.serve();
|
|
30
|
+
homes.push(() => niwa.close());
|
|
31
|
+
return { niwa, home: home.path, stores: given! };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const sha = (text: string) => `sha256:${createHash("sha256").update(text).digest("hex")}`;
|
|
35
|
+
|
|
36
|
+
test("an artifact is its bytes under their digest, read back whole, and the same bytes are one artifact", async () => {
|
|
37
|
+
const { niwa, home, stores } = await host();
|
|
38
|
+
const ref = stores.artifacts.put("hello world\n", "text/plain");
|
|
39
|
+
expect(ref).toEqual({ digest: sha("hello world\n"), size: 12, type: "text/plain" });
|
|
40
|
+
expect(new TextDecoder().decode(niwa.artifacts.read(ref))).toBe("hello world\n");
|
|
41
|
+
expect(stores.artifacts.put(new TextEncoder().encode("hello world\n"), "text/plain")).toEqual(ref);
|
|
42
|
+
expect(readdirSync(join(home, "artifacts"))).toEqual([ref.digest.slice("sha256:".length)]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a written artifact is named once it is finished, and a discarded one leaves nothing", async () => {
|
|
46
|
+
const { home, stores } = await host();
|
|
47
|
+
const out = stores.artifacts.writer("application/json");
|
|
48
|
+
out.write(new TextEncoder().encode('{"a":'));
|
|
49
|
+
out.write(new TextEncoder().encode("1}"));
|
|
50
|
+
expect(readdirSync(join(home, "artifacts")).filter((name) => !name.startsWith(".partial-"))).toEqual([]);
|
|
51
|
+
const ref = out.finish();
|
|
52
|
+
expect(ref).toEqual({ digest: sha('{"a":1}'), size: 7, type: "application/json" });
|
|
53
|
+
expect(() => out.write(new Uint8Array([1]))).toThrow(/finished/);
|
|
54
|
+
|
|
55
|
+
const dropped = stores.artifacts.writer("text/plain");
|
|
56
|
+
dropped.write(new TextEncoder().encode("half"));
|
|
57
|
+
dropped.discard();
|
|
58
|
+
expect(readdirSync(join(home, "artifacts"))).toEqual([ref.digest.slice("sha256:".length)]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("an artifact the host does not have is said to be missing, and a name that is not a digest is refused", async () => {
|
|
62
|
+
const { stores } = await host();
|
|
63
|
+
expect(() => stores.artifacts.read({ digest: sha("never kept") })).toThrow(ArtifactMissing);
|
|
64
|
+
expect(() => stores.artifacts.read({ digest: "sha256:../../owner.token" })).toThrow(/not the digest/);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const VALUE = "s3cr3t-Value-0042";
|
|
68
|
+
|
|
69
|
+
test("a secret comes from a file of the owner alone, else from the host's environment", async () => {
|
|
70
|
+
const { niwa, stores } = await host();
|
|
71
|
+
mkdirSync(stores.secrets.dir, { recursive: true });
|
|
72
|
+
writeFileSync(join(stores.secrets.dir, "DEPLOY"), `${VALUE}\n`, { mode: 0o600 });
|
|
73
|
+
expect(stores.secrets.resolve("DEPLOY")).toBe(VALUE);
|
|
74
|
+
expect(niwa.secrets.resolve("DEPLOY")).toBe(VALUE);
|
|
75
|
+
process.env["NIWA_SECRET_FROM_ENV"] = VALUE;
|
|
76
|
+
try {
|
|
77
|
+
expect(stores.secrets.resolve("FROM_ENV")).toBe(VALUE);
|
|
78
|
+
} finally {
|
|
79
|
+
delete process.env["NIWA_SECRET_FROM_ENV"];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("a secret that is not there, a file others may read, or a name that is a path is refused without its value", async () => {
|
|
84
|
+
const { stores } = await host();
|
|
85
|
+
mkdirSync(stores.secrets.dir, { recursive: true });
|
|
86
|
+
const open = join(stores.secrets.dir, "OPEN");
|
|
87
|
+
writeFileSync(open, VALUE);
|
|
88
|
+
chmodSync(open, 0o644);
|
|
89
|
+
const refusal = (name: string) => {
|
|
90
|
+
try {
|
|
91
|
+
stores.secrets.resolve(name);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
return error as SecretRefusal;
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`the secret "${name}" was given`);
|
|
96
|
+
};
|
|
97
|
+
expect(refusal("MISSING")).toMatchObject({ code: "unknown-secret", message: expect.stringContaining('"MISSING"') });
|
|
98
|
+
expect(refusal("OPEN")).toMatchObject({ code: "unsafe-secret", message: expect.stringMatching(/"OPEN".*0600/) });
|
|
99
|
+
expect(refusal("OPEN").message).not.toContain(VALUE);
|
|
100
|
+
expect(refusal("../OPEN")).toBeInstanceOf(SecretRefusal);
|
|
101
|
+
expect(existsSync(join(stores.secrets.dir, "..", "OPEN"))).toBe(false);
|
|
102
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Observation by stream: one stream, over history and live.
|
|
2
|
+
//
|
|
3
|
+
// A subscription may name the stream it watches. What it sees is the history of that stream
|
|
4
|
+
// and then its live records, in order, with no gap and no duplicate — the same guarantee as by
|
|
5
|
+
// type.
|
|
6
|
+
//
|
|
7
|
+
// Only the public API is used: the entries of the packages and "@niwa-labs/testing".
|
|
8
|
+
|
|
9
|
+
import { expect, test } from "bun:test";
|
|
10
|
+
|
|
11
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
12
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
13
|
+
import { record, seq } from "@niwa-labs/core";
|
|
14
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
15
|
+
import { eventually, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
|
|
18
|
+
const Note = record("stream.note", z.object({ n: z.number() }), { open: true });
|
|
19
|
+
const Other = record("stream.other", z.object({ text: z.string() }), { open: true });
|
|
20
|
+
|
|
21
|
+
/** A host with the two record types declared, and a client connected to it. */
|
|
22
|
+
async function hosted() {
|
|
23
|
+
const home = await tempHome();
|
|
24
|
+
const niwa = createNiwa({ home: home.path });
|
|
25
|
+
niwa.use(
|
|
26
|
+
plugin("stream", (api) => {
|
|
27
|
+
api.declare(Note, Other);
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
await niwa.serve();
|
|
31
|
+
const client = await connectAsOwner(home.path);
|
|
32
|
+
return {
|
|
33
|
+
niwa,
|
|
34
|
+
client,
|
|
35
|
+
close: async () => {
|
|
36
|
+
await client.close().catch(() => {});
|
|
37
|
+
await niwa.close();
|
|
38
|
+
await home.cleanup();
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
test("a subscription by stream sees that stream only, over history and live", async () => {
|
|
44
|
+
const h = await hosted();
|
|
45
|
+
try {
|
|
46
|
+
const first = await h.niwa.emit(Note, { n: 1 });
|
|
47
|
+
const other = await h.niwa.emit(Other, { text: "other" });
|
|
48
|
+
const second = await h.niwa.emit(Note, { n: 2 });
|
|
49
|
+
// Two record types are two streams: the records of one are not the records of the other.
|
|
50
|
+
expect(other.stream).not.toBe(first.stream);
|
|
51
|
+
const stream = first.stream;
|
|
52
|
+
|
|
53
|
+
const seen: StoredRecord[] = [];
|
|
54
|
+
const collecting = (async () => {
|
|
55
|
+
for await (const record of h.client.records.since({ stream })) {
|
|
56
|
+
seen.push(record);
|
|
57
|
+
if (seen.length >= 4) break;
|
|
58
|
+
}
|
|
59
|
+
})();
|
|
60
|
+
|
|
61
|
+
// The history of the stream comes first: the two notes that were written.
|
|
62
|
+
await eventually(() => seen.length >= 2, 5_000, "the history of the stream did not come");
|
|
63
|
+
expect(seen.map((record) => record.id)).toEqual([first.id, second.id]);
|
|
64
|
+
|
|
65
|
+
// Then the live records of the same stream, and nothing of the other one.
|
|
66
|
+
await h.niwa.emit(Note, { n: 3 });
|
|
67
|
+
await h.niwa.emit(Other, { text: "other again" });
|
|
68
|
+
await h.niwa.emit(Note, { n: 4 });
|
|
69
|
+
await eventually(() => seen.length >= 4, 5_000, "the live records of the stream did not come");
|
|
70
|
+
await collecting;
|
|
71
|
+
|
|
72
|
+
// Exactly one stream, in order, no gap and no duplicate.
|
|
73
|
+
expect(seen.every((record) => record.stream === stream)).toBe(true);
|
|
74
|
+
expect(new Set(seen.map((record) => record.id)).size).toBe(seen.length);
|
|
75
|
+
for (let index = 1; index < seen.length; index += 1) {
|
|
76
|
+
expect(seen[index]!.seq).toBeGreaterThan(seen[index - 1]!.seq);
|
|
77
|
+
}
|
|
78
|
+
// What the journal holds of that stream is what was seen: nothing was lost.
|
|
79
|
+
const truth = await h.niwa.records.read({ stream });
|
|
80
|
+
expect(seen.map((record) => record.id)).toEqual(truth.map((record) => record.id));
|
|
81
|
+
} finally {
|
|
82
|
+
await h.close();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("the host filters its own subscription by stream too", async () => {
|
|
87
|
+
const h = await hosted();
|
|
88
|
+
try {
|
|
89
|
+
const note = await h.niwa.emit(Note, { n: 1 });
|
|
90
|
+
await h.niwa.emit(Other, { text: "other" });
|
|
91
|
+
await h.niwa.emit(Note, { n: 2 });
|
|
92
|
+
|
|
93
|
+
const seen: StoredRecord[] = [];
|
|
94
|
+
for await (const record of h.niwa.records.since({ stream: note.stream })) {
|
|
95
|
+
seen.push(record);
|
|
96
|
+
if (seen.length >= 2) break;
|
|
97
|
+
}
|
|
98
|
+
expect(seen.length).toBe(2);
|
|
99
|
+
expect(seen.every((record) => record.stream === note.stream)).toBe(true);
|
|
100
|
+
} finally {
|
|
101
|
+
await h.close();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("the read over the wire carries the stream and the types", async () => {
|
|
106
|
+
const h = await hosted();
|
|
107
|
+
try {
|
|
108
|
+
const note = await h.client.emit(Note, { n: 1 });
|
|
109
|
+
await h.client.emit(Other, { text: "other" });
|
|
110
|
+
const second = await h.client.emit(Note, { n: 2 });
|
|
111
|
+
|
|
112
|
+
// The stream of the wire's read op: the records of that stream only.
|
|
113
|
+
const ofStream = await h.client.records.read({ stream: note.stream });
|
|
114
|
+
expect(ofStream.every((record) => record.stream === note.stream)).toBe(true);
|
|
115
|
+
expect(ofStream.map((record) => record.id)).toEqual([note.id, second.id]);
|
|
116
|
+
|
|
117
|
+
// The types of the wire's read op: several types at once.
|
|
118
|
+
const ofTypes = await h.client.records.read({ types: [Note.name, Other.name] });
|
|
119
|
+
expect(ofTypes.map((record) => record.type)).toEqual([Note.name, Other.name, Note.name]);
|
|
120
|
+
|
|
121
|
+
// The stream and the types together.
|
|
122
|
+
const both = await h.client.records.read({ types: [Note.name], stream: note.stream });
|
|
123
|
+
expect(both.map((record) => record.id)).toEqual([note.id, second.id]);
|
|
124
|
+
} finally {
|
|
125
|
+
await h.close();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("a type and a stream together: both must match, in history and live", async () => {
|
|
130
|
+
const h = await hosted();
|
|
131
|
+
try {
|
|
132
|
+
const note = await h.niwa.emit(Note, { n: 1 });
|
|
133
|
+
await h.niwa.emit(Other, { text: "other" });
|
|
134
|
+
|
|
135
|
+
const seen: StoredRecord[] = [];
|
|
136
|
+
const collecting = (async () => {
|
|
137
|
+
for await (const record of h.client.records.since({ types: [Note], stream: note.stream })) {
|
|
138
|
+
seen.push(record);
|
|
139
|
+
}
|
|
140
|
+
})();
|
|
141
|
+
await eventually(() => seen.length >= 1, 5_000, "the history did not come");
|
|
142
|
+
await h.niwa.emit(Other, { text: "other again" });
|
|
143
|
+
const live = await h.niwa.emit(Note, { n: 2 });
|
|
144
|
+
await eventually(() => seen.length >= 2, 5_000, "the live record did not come");
|
|
145
|
+
expect(seen.map((record) => record.id)).toEqual([note.id, live.id]);
|
|
146
|
+
|
|
147
|
+
// A type of one stream with the stream of another matches nothing.
|
|
148
|
+
const none = await h.niwa.records.read({ types: [Other], stream: note.stream });
|
|
149
|
+
expect(none).toEqual([]);
|
|
150
|
+
void collecting;
|
|
151
|
+
} finally {
|
|
152
|
+
await h.close();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// A query of a subscriber is the query of the core: it names a stream as a read does.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { record } from "@niwa-labs/core";
|
|
6
|
+
import type { RecordQuery, StoredRecord } from "@niwa-labs/core";
|
|
7
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
8
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
9
|
+
import type { Subscriber } from "@niwa-labs/plugin";
|
|
10
|
+
import { eventually, tempHome } from "@niwa-labs/testing";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
const Beat = record("contract.beat", z.object({ n: z.number() }), { open: true });
|
|
14
|
+
const Pulse = record("contract.pulse", z.object({ n: z.number() }), { open: true });
|
|
15
|
+
|
|
16
|
+
const declares = plugin("contract-types", (api) => api.declare(Beat, Pulse));
|
|
17
|
+
|
|
18
|
+
test("one query on a stream gives the same records read and subscribed", async () => {
|
|
19
|
+
const home = await tempHome();
|
|
20
|
+
const niwa = createNiwa({ home: home.path });
|
|
21
|
+
const seen: number[] = [];
|
|
22
|
+
let query: RecordQuery | undefined;
|
|
23
|
+
try {
|
|
24
|
+
niwa.use(
|
|
25
|
+
declares,
|
|
26
|
+
plugin("contract-subscriber", (api) => {
|
|
27
|
+
api.subscribers(
|
|
28
|
+
(): Subscriber[] =>
|
|
29
|
+
query === undefined
|
|
30
|
+
? []
|
|
31
|
+
: [{ id: "one-stream", queries: [query], from: 0, handler: async (_data, _step, stored: StoredRecord) => void seen.push((stored.data as { n: number }).n) }],
|
|
32
|
+
);
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
await niwa.serve();
|
|
36
|
+
const first = await niwa.emit(Beat, { n: 1 });
|
|
37
|
+
query = { types: ["contract.*"], stream: first.stream };
|
|
38
|
+
await niwa.emit(Pulse, { n: 2 });
|
|
39
|
+
await niwa.emit(Beat, { n: 3 });
|
|
40
|
+
await eventually(() => seen.length >= 2);
|
|
41
|
+
await Bun.sleep(100);
|
|
42
|
+
expect(seen.sort()).toEqual([1, 3]);
|
|
43
|
+
expect((await niwa.records.read(query)).map((row) => (row.data as { n: number }).n)).toEqual([1, 3]);
|
|
44
|
+
} finally {
|
|
45
|
+
await niwa.close();
|
|
46
|
+
await home.cleanup();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("a query of a subscriber that carries a position of its own wakes it on nothing", async () => {
|
|
51
|
+
const home = await tempHome();
|
|
52
|
+
const niwa = createNiwa({ home: home.path });
|
|
53
|
+
const seen: number[] = [];
|
|
54
|
+
try {
|
|
55
|
+
niwa.use(
|
|
56
|
+
declares,
|
|
57
|
+
plugin("contract-subscriber", (api) => {
|
|
58
|
+
api.subscribers((): Subscriber[] => [
|
|
59
|
+
{ id: "placed", queries: [{ types: ["contract.*"], from: 0 } as RecordQuery], from: 0, handler: async (_data, _step, stored: StoredRecord) => void seen.push((stored.data as { n: number }).n) },
|
|
60
|
+
]);
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
await niwa.serve();
|
|
64
|
+
await niwa.emit(Beat, { n: 1 });
|
|
65
|
+
await Bun.sleep(150);
|
|
66
|
+
expect(seen).toEqual([]);
|
|
67
|
+
} finally {
|
|
68
|
+
await niwa.close();
|
|
69
|
+
await home.cleanup();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Subscriptions that are data: a plugin reads them from the journal (an installed workflow is one),
|
|
2
|
+
// and the host serves each as it serves a handler — its own position, its own identity.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { record, seq } from "@niwa-labs/core";
|
|
7
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
8
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
9
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
10
|
+
import type { Plugin, Subscriber } from "@niwa-labs/plugin";
|
|
11
|
+
import { eventually, tempHome } from "@niwa-labs/testing";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
|
|
14
|
+
const Watch = record("subs-test.watch", z.object({ name: z.string(), type: z.string(), kind: z.string().optional() }));
|
|
15
|
+
const Unwatch = record("subs-test.unwatch", z.object({ name: z.string() }));
|
|
16
|
+
const Ping = record("subs-test.ping", z.object({ n: z.number(), kind: z.string().optional() }));
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A plugin whose subscribers are the watches written in the journal: each is a subscriber from the
|
|
20
|
+
* position of its watch, and an unwatch ends it at its own position.
|
|
21
|
+
*/
|
|
22
|
+
function watcher(seen: Array<{ name: string; n: number }>): Plugin {
|
|
23
|
+
return plugin("subs-watcher", (api) => {
|
|
24
|
+
api.declare(Watch, Unwatch, Ping);
|
|
25
|
+
api.subscribers(() => {
|
|
26
|
+
const journal = api.journal?.();
|
|
27
|
+
if (journal === undefined) return [];
|
|
28
|
+
const ends = journal.find({ type: Unwatch.name });
|
|
29
|
+
return journal.find({ type: Watch.name }).map((row): Subscriber => {
|
|
30
|
+
const watch = Watch.schema.parse(row.data);
|
|
31
|
+
const end = ends.find((one) => (one.data as { name: string }).name === watch.name && one.seq > row.seq);
|
|
32
|
+
return {
|
|
33
|
+
id: `watch:${watch.name}`,
|
|
34
|
+
queries: [{ types: [watch.type], ...(watch.kind === undefined ? {} : { filter: { kind: watch.kind } }) }],
|
|
35
|
+
from: row.seq,
|
|
36
|
+
...(end === undefined ? {} : { until: end.seq }),
|
|
37
|
+
handler: async (_data, step, stored: StoredRecord) => {
|
|
38
|
+
const n = (stored.data as { n: number }).n;
|
|
39
|
+
seen.push({ name: watch.name, n });
|
|
40
|
+
// A subscriber that writes what it watches is not woken by its own record.
|
|
41
|
+
if (watch.name === "a") await step.emit(Ping, { n: n + 100, kind: "x" });
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test("a subscriber from the journal starts at its own position and ends at its end", async () => {
|
|
50
|
+
const home = await tempHome();
|
|
51
|
+
const seen: Array<{ name: string; n: number }> = [];
|
|
52
|
+
const niwa = createNiwa({ home: home.path });
|
|
53
|
+
niwa.use(watcher(seen));
|
|
54
|
+
try {
|
|
55
|
+
await niwa.serve();
|
|
56
|
+
await niwa.emit(Ping, { n: 1 });
|
|
57
|
+
await niwa.emit(Watch, { name: "b", type: Ping.name, kind: "x" });
|
|
58
|
+
await niwa.emit(Watch, { name: "a", type: Ping.name });
|
|
59
|
+
await niwa.emit(Ping, { n: 2 });
|
|
60
|
+
await niwa.emit(Ping, { n: 3, kind: "x" });
|
|
61
|
+
await niwa.emit(Ping, { n: 4, kind: "y" });
|
|
62
|
+
await eventually(() => seen.filter((one) => one.name === "a").length === 3);
|
|
63
|
+
await niwa.emit(Unwatch, { name: "a" });
|
|
64
|
+
await niwa.emit(Ping, { n: 5, kind: "x" });
|
|
65
|
+
await eventually(() => seen.filter((one) => one.name === "b").length === 5);
|
|
66
|
+
await Bun.sleep(100);
|
|
67
|
+
const of = (name: string): number[] => seen.filter((one) => one.name === name).map((one) => one.n).sort((x, y) => x - y);
|
|
68
|
+
// The watch of a began after the first ping and ended before the last; it never sees its own echoes.
|
|
69
|
+
expect(of("a")).toEqual([2, 3, 4]);
|
|
70
|
+
// The watch of b holds its filter, and sees what a wrote.
|
|
71
|
+
expect(of("b")).toEqual([3, 5, 102, 103, 104]);
|
|
72
|
+
} finally {
|
|
73
|
+
await niwa.close();
|
|
74
|
+
await home.cleanup();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("a subscriber's position survives a restart", async () => {
|
|
79
|
+
const home = await tempHome();
|
|
80
|
+
const seen: Array<{ name: string; n: number }> = [];
|
|
81
|
+
try {
|
|
82
|
+
const first = createNiwa({ home: home.path });
|
|
83
|
+
first.use(watcher(seen));
|
|
84
|
+
await first.serve();
|
|
85
|
+
await first.emit(Watch, { name: "a", type: Ping.name, kind: "x" });
|
|
86
|
+
await first.emit(Ping, { n: 1, kind: "x" });
|
|
87
|
+
await eventually(() => seen.length === 1);
|
|
88
|
+
await first.close();
|
|
89
|
+
const again = createNiwa({ home: home.path });
|
|
90
|
+
again.use(watcher(seen));
|
|
91
|
+
await again.serve();
|
|
92
|
+
await again.emit(Ping, { n: 2, kind: "x" });
|
|
93
|
+
await eventually(() => seen.length === 2);
|
|
94
|
+
await Bun.sleep(100);
|
|
95
|
+
expect(seen.map((one) => one.n)).toEqual([1, 2]);
|
|
96
|
+
expect((await again.records.read()).length).toBeGreaterThan(0);
|
|
97
|
+
await again.close();
|
|
98
|
+
} finally {
|
|
99
|
+
await home.cleanup();
|
|
100
|
+
}
|
|
101
|
+
});
|