@mostra/conformance 0.0.0-placeholder → 0.2.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.
@@ -0,0 +1,376 @@
1
+ /**
2
+ * Scenarios and what they get to work with. A scenario is one property a host integration must
3
+ * have, checked through the host's own client and ports (the harness) and a viewer playing the
4
+ * browser. Each runs against a fresh home in its own directory, with a fresh host shim.
5
+ */
6
+ import {
7
+ chmodSync,
8
+ lstatSync,
9
+ mkdirSync,
10
+ readdirSync,
11
+ readFileSync,
12
+ rmSync,
13
+ writeFileSync,
14
+ } from "node:fs";
15
+ import { createConnection } from "node:net";
16
+ import { dirname, join } from "node:path";
17
+ import {
18
+ createDiagnostic,
19
+ type EventRecord,
20
+ encodeLine,
21
+ HOST_PROTOCOL_REVISIONS,
22
+ LineSplitter,
23
+ type Results,
24
+ RpcError,
25
+ RpcPeer,
26
+ } from "@mostra/protocol";
27
+ import type { Fixture } from "./fixtures.ts";
28
+ import { HostUnderTest } from "./harness/client.ts";
29
+ import type { StartParams, StartResult, StoreResult } from "./harness/protocol.ts";
30
+ import { kitVersion } from "./version.ts";
31
+ import { Viewer } from "./viewer.ts";
32
+
33
+ /** A scenario: one property of a host integration, with the requirements it covers. */
34
+ export interface Scenario {
35
+ /** A stable ID, `group.name`: baselines name scenarios by it. */
36
+ readonly id: string;
37
+ /** What it proves, in one sentence. */
38
+ readonly title: string;
39
+ /** The requirements and host needs it exercises (`HO-05`, `IN-12`, `H-03` …). */
40
+ readonly covers: readonly string[];
41
+ /** Harness features without which it doesn't apply (it is then skipped, never failed). */
42
+ readonly needs?: readonly string[];
43
+ /** Runs it; throws a {@link ScenarioFailure} (or any error) when the property doesn't hold. */
44
+ run(context: ScenarioContext): Promise<void>;
45
+ }
46
+
47
+ /** A property that didn't hold. */
48
+ export class ScenarioFailure extends Error {
49
+ constructor(message: string) {
50
+ super(message);
51
+ this.name = "ScenarioFailure";
52
+ }
53
+ }
54
+
55
+ /** Thrown to say a scenario doesn't apply to this host (a port or feature it lacks). */
56
+ export class NotApplicable extends Error {
57
+ constructor(reason: string) {
58
+ super(reason);
59
+ this.name = "NotApplicable";
60
+ }
61
+ }
62
+
63
+ /** Fails the scenario with `message` unless `condition` holds. */
64
+ export function check(condition: unknown, message: string): asserts condition {
65
+ if (!condition) throw new ScenarioFailure(message);
66
+ }
67
+
68
+ /** `value` as JSON with every object's keys sorted, so key order never decides equality. */
69
+ function canonical(value: unknown): string {
70
+ return JSON.stringify(value, (_key, inner: unknown) =>
71
+ inner !== null && typeof inner === "object" && !Array.isArray(inner)
72
+ ? Object.fromEntries(
73
+ Object.entries(inner as Record<string, unknown>).sort(([a], [b]) =>
74
+ a < b ? -1 : a > b ? 1 : 0,
75
+ ),
76
+ )
77
+ : inner,
78
+ );
79
+ }
80
+
81
+ /** Fails unless `actual` deep-equals `expected` (JSON comparison, key order aside). */
82
+ export function same(actual: unknown, expected: unknown, what: string): void {
83
+ const a = canonical(actual);
84
+ const b = canonical(expected);
85
+ if (a !== b) throw new ScenarioFailure(`${what}: expected ${b}, got ${a}`);
86
+ }
87
+
88
+ /** Options of a run that scenarios see. */
89
+ export interface RunSettings {
90
+ /** The host shim's command line. */
91
+ readonly host: string;
92
+ /** Variables for the shim's own process (how it finds its runtime). */
93
+ readonly hostEnv: Readonly<Record<string, string>>;
94
+ /** Publish through verify's gate (needs the verify browser); off, publish skips it, loudly. */
95
+ readonly verify: boolean;
96
+ }
97
+
98
+ /** What a scenario works with: its host, its home, its viewers. */
99
+ export class ScenarioContext {
100
+ /** The run's settings. */
101
+ readonly settings: RunSettings;
102
+ /** The scenario's directory: the home, the authoring directories, the runtime directory. */
103
+ readonly dir: string;
104
+ /** The home the host is told to use (HO-08). */
105
+ readonly home: string;
106
+ /** The host: its name and version. */
107
+ host!: HostUnderTest;
108
+ readonly #hosts: HostUnderTest[] = [];
109
+ #published = 0;
110
+
111
+ constructor(settings: RunSettings, dir: string) {
112
+ this.settings = settings;
113
+ this.dir = dir;
114
+ this.home = join(dir, "home");
115
+ }
116
+
117
+ /** Starts the scenario's host shim (the runner does this before `run`). */
118
+ async open(): Promise<void> {
119
+ this.host = await this.anotherHost();
120
+ }
121
+
122
+ /** A second shim of the same host, for scenarios about two hosts of one home. */
123
+ async anotherHost(): Promise<HostUnderTest> {
124
+ const host = await HostUnderTest.start(this.settings.host, this.settings.hostEnv);
125
+ this.#hosts.push(host);
126
+ return host;
127
+ }
128
+
129
+ /** The variables every instance of the run gets: its own runtime and config directories. */
130
+ instanceEnv(extra: Readonly<Record<string, string>> = {}): Record<string, string> {
131
+ return {
132
+ MOSTRA_RUNTIME_DIR: join(this.dir, "run"),
133
+ MOSTRA_CONFIG_DIR: join(this.dir, "config"),
134
+ ...(this.settings.verify ? {} : { MOSTRA_TEST_UNVERIFIED_PUBLISH: "1" }),
135
+ ...extra,
136
+ };
137
+ }
138
+
139
+ /** `harness/start` on this scenario's home. */
140
+ start(
141
+ options: {
142
+ env?: Readonly<Record<string, string>>;
143
+ mode?: StartParams["mode"];
144
+ host?: HostUnderTest;
145
+ } = {},
146
+ ): Promise<StartResult> {
147
+ mkdirSync(join(this.dir, "run"), { recursive: true, mode: 0o700 });
148
+ return (options.host ?? this.host).startInstance({
149
+ home: this.home,
150
+ env: this.instanceEnv(options.env),
151
+ ...(options.mode ? { mode: options.mode } : {}),
152
+ });
153
+ }
154
+
155
+ /** Stops the host's instance and starts it again (the same home). */
156
+ async restart(options: { env?: Readonly<Record<string, string>> } = {}): Promise<StartResult> {
157
+ await this.host.stopInstance();
158
+ return this.start(options);
159
+ }
160
+
161
+ /** Writes a fixture into a fresh authoring directory and returns the directory. */
162
+ author(fixture: Fixture, name = "artifact"): string {
163
+ this.#published += 1;
164
+ const dir = join(this.dir, "authoring", `${name}-${this.#published}`);
165
+ for (const [path, text] of Object.entries(fixture)) {
166
+ mkdirSync(dirname(join(dir, path)), { recursive: true });
167
+ writeFileSync(join(dir, path), text);
168
+ }
169
+ return dir;
170
+ }
171
+
172
+ /** Writes `files` over the authoring directory `dir` (a new version of the same artifact). */
173
+ rewrite(dir: string, fixture: Fixture): void {
174
+ for (const [path, text] of Object.entries(fixture)) writeFileSync(join(dir, path), text);
175
+ }
176
+
177
+ /** Publishes `dir` through the host (`artifact/publish`). */
178
+ publish(
179
+ dir: string,
180
+ options: { policy?: unknown; requestId?: string } = {},
181
+ ): Promise<{
182
+ artifactId: string;
183
+ versionId: string;
184
+ version: number;
185
+ url?: string;
186
+ tokenId?: string;
187
+ cursor: string;
188
+ }> {
189
+ return this.host.call("artifact/publish", {
190
+ source: { path: dir },
191
+ ...(options.policy !== undefined ? { policy: options.policy } : {}),
192
+ ...(options.requestId ? { requestId: options.requestId } : {}),
193
+ });
194
+ }
195
+
196
+ /** Authors and publishes a fixture; the receipt with its authoring directory. */
197
+ async published(
198
+ fixture: Fixture,
199
+ options: { policy?: unknown } = {},
200
+ ): Promise<{
201
+ artifactId: string;
202
+ versionId: string;
203
+ version: number;
204
+ url?: string;
205
+ tokenId?: string;
206
+ cursor: string;
207
+ dir: string;
208
+ }> {
209
+ const dir = this.author(fixture);
210
+ return { ...(await this.publish(dir, options)), dir };
211
+ }
212
+
213
+ /** A new capability link through the host (`view/url`). */
214
+ async link(artifactId: string, params: Record<string, unknown> = {}): Promise<string> {
215
+ const link = await this.host.call<{ url: string }>("view/url", {
216
+ artifact: { artifactId },
217
+ ...params,
218
+ });
219
+ return link.url;
220
+ }
221
+
222
+ /** Opens a new link as a viewer. */
223
+ async viewer(
224
+ artifactId: string,
225
+ options: { link?: Record<string, unknown>; headers?: Record<string, string> } = {},
226
+ ): Promise<Viewer> {
227
+ return Viewer.open(await this.link(artifactId, options.link ?? {}), {
228
+ ...(options.headers ? { headers: options.headers } : {}),
229
+ });
230
+ }
231
+
232
+ /** `results/get` through the host. */
233
+ results(artifactId: string, submission?: string): Promise<Results> {
234
+ return this.host.call("results/get", {
235
+ artifact: { artifactId },
236
+ ...(submission ? { submission } : {}),
237
+ });
238
+ }
239
+
240
+ /** `events/read` through the host. */
241
+ async events(
242
+ filter: { artifacts?: string[]; types?: string[] },
243
+ after?: string,
244
+ ): Promise<{ events: EventRecord[]; cursor: string }> {
245
+ return this.host.call("events/read", { filter, ...(after ? { after } : {}), limit: 100 });
246
+ }
247
+
248
+ /** The host's store. */
249
+ store(): Promise<StoreResult> {
250
+ return this.host.store();
251
+ }
252
+
253
+ /** Waits for `condition` to hold, checking every 100 ms; fails with `what` after `timeoutMs`. */
254
+ async eventually<T>(
255
+ what: string,
256
+ condition: () => Promise<T | undefined | false> | T | undefined | false,
257
+ timeoutMs = 30_000,
258
+ ): Promise<T> {
259
+ const deadline = Date.now() + timeoutMs;
260
+ for (;;) {
261
+ const value = await condition();
262
+ if (value !== undefined && value !== false) return value;
263
+ if (Date.now() > deadline) throw new ScenarioFailure(`${what} (waited ${timeoutMs} ms)`);
264
+ await new Promise((done) => setTimeout(done, 100));
265
+ }
266
+ }
267
+
268
+ /** Skips the scenario unless the host serves `port`. */
269
+ requirePort(port: string): void {
270
+ if (!this.host.serves(port)) throw new NotApplicable(`the host serves no ${port} port`);
271
+ }
272
+
273
+ /** Skips the scenario unless the host supports `feature`. */
274
+ requireFeature(feature: string): void {
275
+ if (!this.host.supports(feature))
276
+ throw new NotApplicable(`the host lacks the ${feature} feature`);
277
+ }
278
+
279
+ /** The socket of the home's running instance, from `instance.json` (D81). */
280
+ socket(): string | undefined {
281
+ try {
282
+ const record = JSON.parse(readFileSync(join(this.home, "instance.json"), "utf8")) as {
283
+ socket?: string;
284
+ };
285
+ return record.socket;
286
+ } catch {
287
+ return undefined;
288
+ }
289
+ }
290
+
291
+ /** Ends every shim, stops a daemon the scenario left, and removes the directory. */
292
+ async close(): Promise<void> {
293
+ for (const host of this.#hosts) {
294
+ await host.stopInstance().catch(() => undefined);
295
+ await host.close().catch(() => undefined);
296
+ }
297
+ const socket = this.socket();
298
+ if (socket) {
299
+ const session = await connectSocket(socket).catch(() => undefined);
300
+ await session?.peer
301
+ .request("mostra/shutdown", {}, { timeoutMs: 10_000 })
302
+ .catch(() => undefined);
303
+ session?.close();
304
+ }
305
+ removeTree(this.dir);
306
+ }
307
+ }
308
+
309
+ /**
310
+ * Removes a scenario's directory. A home's versions are sealed read-only (files 0444,
311
+ * directories 0555, DATA_MODEL §1), so directories are made writable first.
312
+ */
313
+ export function removeTree(dir: string): void {
314
+ const open = (path: string): void => {
315
+ let stats: ReturnType<typeof lstatSync>;
316
+ try {
317
+ stats = lstatSync(path);
318
+ } catch {
319
+ return;
320
+ }
321
+ if (!stats.isDirectory()) return;
322
+ chmodSync(path, 0o700);
323
+ for (const name of readdirSync(path)) open(join(path, name));
324
+ };
325
+ open(dir);
326
+ rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 });
327
+ }
328
+
329
+ /** A raw protocol session over the home's socket, opened by the kit itself. */
330
+ export interface SocketSession {
331
+ readonly peer: RpcPeer;
332
+ close(): void;
333
+ }
334
+
335
+ /** Connects to an instance's socket and initializes a session as `@mostra/conformance`. */
336
+ export async function connectSocket(
337
+ path: string,
338
+ revisions: readonly string[] = HOST_PROTOCOL_REVISIONS,
339
+ ): Promise<SocketSession & { initialize: Promise<unknown> }> {
340
+ const socket = await new Promise<ReturnType<typeof createConnection>>((resolve, reject) => {
341
+ const connection = createConnection(path);
342
+ connection.once("connect", () => resolve(connection));
343
+ connection.once("error", reject);
344
+ });
345
+ const peer = new RpcPeer({
346
+ send: (message) => {
347
+ if (!socket.destroyed) socket.write(encodeLine(message));
348
+ },
349
+ });
350
+ const lines = new LineSplitter();
351
+ socket.setEncoding("utf8");
352
+ socket.on("data", (chunk: string) => {
353
+ for (const line of lines.push(chunk)) peer.receiveLine(line);
354
+ });
355
+ socket.on("error", () => socket.destroy());
356
+ socket.on("close", () =>
357
+ peer.close(createDiagnostic("MOSTRA_HOST_CONNECTION_CLOSED", { message: "socket closed" })),
358
+ );
359
+ const initialize = peer.request(
360
+ "mostra/initialize",
361
+ {
362
+ client: { name: "mostra-conformance", version: kitVersion() },
363
+ revisions: [...revisions],
364
+ capabilities: {},
365
+ ports: [],
366
+ },
367
+ { timeoutMs: 10_000 },
368
+ );
369
+ initialize.catch(() => undefined);
370
+ return { peer, initialize, close: () => socket.destroy() };
371
+ }
372
+
373
+ /** The diagnostic code of a rejected request. */
374
+ export function rpcCode(error: unknown): string | undefined {
375
+ return error instanceof RpcError ? error.diagnostic.code : undefined;
376
+ }
@@ -0,0 +1,267 @@
1
+ /**
2
+ * Submissions reaching the host: durable first, delivered whole, once in effect across crashes,
3
+ * retried while the host can't take them, replayed in order when it returns, and never confused
4
+ * with draft mirrors (IN-06, IN-12, H-01, H-03, H-04, H-08).
5
+ */
6
+ import { FORM } from "../fixtures.ts";
7
+ import { check, connectSocket, type Scenario, type ScenarioContext, same } from "../scenario.ts";
8
+
9
+ /** Publishes the form, answers it and submits on the artifact channel; the submission's ID. */
10
+ async function submitForm(
11
+ context: ScenarioContext,
12
+ fields: Record<string, unknown> = { name: "Ada", comment: "Looks good" },
13
+ ): Promise<{ artifactId: string; submissionId: string }> {
14
+ const receipt = await context.published(FORM);
15
+ const viewer = await context.viewer(receipt.artifactId);
16
+ const answer = await viewer.answer(
17
+ fields,
18
+ viewer.ticket.submitChannels.includes("artifact") ? "artifact" : "shell",
19
+ );
20
+ same(answer.status, 200, `the submit's status (${answer.body})`);
21
+ return { artifactId: receipt.artifactId, submissionId: answer.json<{ id: string }>().id };
22
+ }
23
+
24
+ /** H-01, H-08: the host receives the whole submission once, and Mostra's record agrees. */
25
+ export const delivered: Scenario = {
26
+ id: "delivery.submission",
27
+ title:
28
+ "A submission is durable before delivery, reaches the host's submissions port once with the full results, and is readable through results/get",
29
+ covers: ["HO-01", "HO-05", "IN-12", "H-01", "H-08"],
30
+ async run(context) {
31
+ await context.start();
32
+ const { artifactId, submissionId } = await submitForm(context);
33
+ // Durable at commit, whatever the delivery state (FAILURE_MODEL §4).
34
+ const waited = await context.host.call<{ status: string }>("wait", {
35
+ artifact: { artifactId },
36
+ until: "submitted",
37
+ timeoutMs: 0,
38
+ });
39
+ same(waited.status, "satisfied", "wait --until submitted right after the submit");
40
+ if (context.host.serves("submissions")) {
41
+ const stored = await context.eventually("the host's store holds the submission", async () =>
42
+ (await context.store()).deliveries.find((entry) => entry.submissionId === submissionId),
43
+ );
44
+ same(stored.kind, "submission", "the envelope's kind");
45
+ same(stored.stored, 1, "copies the host's store holds");
46
+ check(stored.deliveryId.startsWith("dlv_"), `a delivery ID (${stored.deliveryId})`);
47
+ const results = await context.eventually(
48
+ "the delivery is recorded as delivered",
49
+ async () => {
50
+ const read = await context.results(artifactId);
51
+ return read.submission.delivery === "delivered" ? read : undefined;
52
+ },
53
+ );
54
+ same(results.submission.id, submissionId, "the submission results/get reads");
55
+ same(results.answers, { name: "Ada", comment: "Looks good" }, "the answers");
56
+ } else {
57
+ // Standalone: Mostra's built-in adapter takes it at once (D58, D105).
58
+ const results = await context.eventually("the built-in adapter accepts it", async () => {
59
+ const read = await context.results(artifactId);
60
+ return read.submission.delivery === "delivered" ? read : undefined;
61
+ });
62
+ same(results.answers, { name: "Ada", comment: "Looks good" }, "the answers");
63
+ }
64
+ },
65
+ };
66
+
67
+ /** H-03, IN-12: a crash after the host accepted redelivers the same delivery, stored once. */
68
+ export const exactlyOnce: Scenario = {
69
+ id: "delivery.exactly-once",
70
+ title:
71
+ "When the instance dies after the host accepted a delivery but before recording it, the restarted instance redelivers the same deliveryId and the host stores it once",
72
+ covers: ["HO-05", "IN-12", "H-03"],
73
+ async run(context) {
74
+ context.requirePort("submissions");
75
+ // The instance kills itself between the host's answer and its record (FAILURE_MODEL §4).
76
+ await context.start({ env: { MOSTRA_TEST_FAULTS: "deliver:after-answer:crash" } });
77
+ const { artifactId, submissionId } = await submitForm(context);
78
+ const first = await context.eventually("the host received the first attempt", async () =>
79
+ (await context.store()).deliveries.find((entry) => entry.submissionId === submissionId),
80
+ );
81
+ await context.host.stopInstance().catch(() => undefined);
82
+ await context.start();
83
+ const after = await context.eventually("the restarted instance redelivers it", async () => {
84
+ const entry = (await context.store()).deliveries.find(
85
+ (candidate) => candidate.submissionId === submissionId,
86
+ );
87
+ return entry && entry.calls >= 2 ? entry : undefined;
88
+ });
89
+ same(after.deliveryId, first.deliveryId, "the redelivery's deliveryId");
90
+ same(after.stored, 1, "copies the host's store holds after the redelivery");
91
+ check(
92
+ (after.attempts.at(-1) ?? 0) > (after.attempts[0] ?? 0),
93
+ `the redelivery's attempt counts up (${after.attempts.join(", ")})`,
94
+ );
95
+ const results = await context.eventually("the delivery ends delivered", async () => {
96
+ const read = await context.results(artifactId);
97
+ return read.submission.delivery === "delivered" ? read : undefined;
98
+ });
99
+ same(results.submission.id, submissionId, "the submission");
100
+ },
101
+ };
102
+
103
+ /** IN-12, H-04: a host that can't take a delivery gets it again with backoff; terminal is final. */
104
+ export const retry: Scenario = {
105
+ id: "delivery.retry",
106
+ title:
107
+ "A delivery the host answers retry: true stays pending and arrives when the host can take it; an undeliverable answer is final and never retried",
108
+ covers: ["HO-05", "IN-12", "H-04"],
109
+ needs: ["faults"],
110
+ async run(context) {
111
+ context.requirePort("submissions");
112
+ await context.start();
113
+ await context.host.script({ port: "submissions", answer: "retry" });
114
+ const { artifactId, submissionId } = await submitForm(context);
115
+ await context.eventually("the delivery is pending while the host can't take it", async () => {
116
+ const read = await context.results(artifactId);
117
+ return read.submission.delivery === "pending" ? read : undefined;
118
+ });
119
+ const retrying = await context.events({
120
+ artifacts: [artifactId],
121
+ types: ["submission.retrying"],
122
+ });
123
+ check(
124
+ retrying.events.length > 0,
125
+ "no submission.retrying event while the delivery waits (IN-13)",
126
+ );
127
+ await context.host.script({ port: "submissions", answer: "accept" });
128
+ await context.eventually(
129
+ "the delivery arrives once the host can take it",
130
+ async () =>
131
+ (await context.store()).deliveries.find((entry) => entry.submissionId === submissionId),
132
+ 45_000,
133
+ );
134
+ await context.eventually("the delivery ends delivered", async () => {
135
+ const read = await context.results(artifactId);
136
+ return read.submission.delivery === "delivered" ? read : undefined;
137
+ });
138
+ // A terminal answer: undeliverable, shown, and never retried.
139
+ await context.host.script({ port: "submissions", answer: "undeliverable" });
140
+ const second = await submitForm(context, { name: "Grace" });
141
+ await context.eventually("the delivery ends undeliverable", async () => {
142
+ const read = await context.results(second.artifactId);
143
+ return read.submission.delivery === "undeliverable" ? read : undefined;
144
+ });
145
+ await context.host.script({ port: "submissions", answer: "accept" });
146
+ await new Promise((done) => setTimeout(done, 3000));
147
+ const store = await context.store();
148
+ check(
149
+ !store.deliveries.some((entry) => entry.submissionId === second.submissionId),
150
+ "an undeliverable delivery was retried",
151
+ );
152
+ },
153
+ };
154
+
155
+ /** IN-12, H-04: submissions made while the host is away wait for it and replay in order. */
156
+ export const outboxReplay: Scenario = {
157
+ id: "delivery.outbox-replay",
158
+ title:
159
+ "Submissions made while the host is away stay pending in the outbox and replay in submission order, each with its deliveryId, when the host attaches again",
160
+ covers: ["HO-05", "IN-12", "H-04"],
161
+ needs: ["attach"],
162
+ async run(context) {
163
+ context.requirePort("submissions");
164
+ await context.start({ mode: "attach" });
165
+ const first = await context.published(FORM);
166
+ const second = await context.published(FORM);
167
+ // The viewers open their links while the host is there (a host that serves identity refuses
168
+ // new links while it is away, D143), then the host goes away and its daemon keeps the home,
169
+ // expecting the host back (D105).
170
+ const viewers = [
171
+ await context.viewer(first.artifactId),
172
+ await context.viewer(second.artifactId),
173
+ ];
174
+ await context.host.stopInstance();
175
+ const submissions: string[] = [];
176
+ for (const viewer of viewers) {
177
+ // A host that took the channel policy over gave these views the shell channel (D108).
178
+ const channel = viewer.ticket.submitChannels.includes("artifact") ? "artifact" : "shell";
179
+ const answer = await viewer.answer({ name: `Ada ${submissions.length + 1}` }, channel);
180
+ same(answer.status, 200, `a submit while the host is away (${answer.body})`);
181
+ submissions.push(answer.json<{ id: string }>().id);
182
+ }
183
+ // A daemon that keeps the home while its host is away records where clients find it: no
184
+ // record is a defect, never a reason to skip (Opus review of WP-12, finding 11).
185
+ const socket = context.socket();
186
+ check(socket, "the daemon kept the home but left no instance.json to find it by");
187
+ const kit = await connectSocket(socket);
188
+ await kit.initialize;
189
+ const pending = (await kit.peer.request("results/get", {
190
+ artifact: { artifactId: first.artifactId },
191
+ })) as { submission: { delivery: string } };
192
+ kit.close();
193
+ same(pending.submission.delivery, "pending", "a delivery while its host is away");
194
+ check(
195
+ (await context.store()).deliveries.length === 0,
196
+ "the host received a delivery while it was away",
197
+ );
198
+ await context.start({ mode: "attach" });
199
+ const store = await context.eventually("both deliveries replay", async () => {
200
+ const read = await context.store();
201
+ return read.deliveries.length >= 2 ? read : undefined;
202
+ });
203
+ same(
204
+ store.deliveries.map((entry) => entry.submissionId),
205
+ submissions,
206
+ "the replay's order (submission order)",
207
+ );
208
+ check(
209
+ store.deliveries.every((entry) => entry.stored === 1),
210
+ "a replayed delivery stored twice",
211
+ );
212
+ await context.host.stopInstance();
213
+ },
214
+ };
215
+
216
+ /** IN-06: drafts reach the host only as mirrors labelled `draft`, never as submissions. */
217
+ export const draftMirrors: Scenario = {
218
+ id: "drafts.mirror",
219
+ title:
220
+ "Draft revisions reach the host only through drafts.mirror, labelled kind: draft, and never through the submissions port",
221
+ covers: ["HO-01", "HO-05", "IN-06", "H-01"],
222
+ async run(context) {
223
+ await context.start();
224
+ const receipt = await context.published(FORM);
225
+ const viewer = await context.viewer(receipt.artifactId);
226
+ const written = await viewer.write({ name: "Ada" });
227
+ same(written.status, 200, "a draft write");
228
+ if (context.host.serves("drafts")) {
229
+ const mirror = await context.eventually("the host receives the draft's mirror", async () =>
230
+ (await context.store()).drafts.find((entry) => entry.artifactId === receipt.artifactId),
231
+ );
232
+ same(mirror.kind, "draft", "the mirror's kind");
233
+ }
234
+ const before = await context.store();
235
+ check(
236
+ before.deliveries.every((entry) => entry.kind === "submission") &&
237
+ !before.deliveries.some((entry) => entry.artifactId === receipt.artifactId),
238
+ "a draft reached the host as a delivery before any submit (IN-06)",
239
+ );
240
+ const { revision } = written.json<{ revision: string }>();
241
+ const submitted = await (viewer.ticket.submitChannels.includes("artifact")
242
+ ? viewer.submit(revision)
243
+ : viewer.shellSubmit(revision));
244
+ same(submitted.status, 200, "the submit");
245
+ if (context.host.serves("submissions")) {
246
+ await context.eventually("the submission arrives", async () =>
247
+ (await context.store()).deliveries.find((entry) => entry.artifactId === receipt.artifactId),
248
+ );
249
+ const after = await context.store();
250
+ const delivered = after.deliveries.filter((entry) => entry.artifactId === receipt.artifactId);
251
+ same(delivered.length, 1, "deliveries for the artifact");
252
+ check(
253
+ after.deliveries.every((entry) => entry.kind === "submission"),
254
+ "the host stores something that isn't a submission as one (IN-06)",
255
+ );
256
+ }
257
+ },
258
+ };
259
+
260
+ /** The delivery scenarios. */
261
+ export const DELIVERY_SCENARIOS: readonly Scenario[] = [
262
+ delivered,
263
+ exactlyOnce,
264
+ retry,
265
+ outboxReplay,
266
+ draftMirrors,
267
+ ];
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Every scenario of the kit, in the order a run takes them. A scenario's ID is stable surface:
3
+ * baselines name it. New scenarios join additively (WP-13 adds addressing, WP-20 reviews and
4
+ * capture, WP-29 remote and WP-31 team scenarios).
5
+ */
6
+ import type { Scenario } from "../scenario.ts";
7
+ import { DELIVERY_SCENARIOS } from "./delivery.ts";
8
+ import { INTERACTION_SCENARIOS } from "./interaction.ts";
9
+ import { PORT_SCENARIOS } from "./ports.ts";
10
+ import { SESSION_SCENARIOS } from "./session.ts";
11
+ import { VIEWING_SCENARIOS } from "./viewing.ts";
12
+
13
+ /** Every scenario. */
14
+ export const SCENARIOS: readonly Scenario[] = [
15
+ ...SESSION_SCENARIOS,
16
+ ...DELIVERY_SCENARIOS,
17
+ ...INTERACTION_SCENARIOS,
18
+ ...PORT_SCENARIOS,
19
+ ...VIEWING_SCENARIOS,
20
+ ];