@niwa-labs/web 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 +15 -4
- package/src/app.ts +298 -0
- package/src/denial.ts +13 -0
- package/src/feed.ts +215 -0
- package/src/index.ts +737 -0
- package/src/login.ts +51 -0
- package/src/source.ts +41 -0
- package/src/trace.ts +85 -0
- package/src/viewer.ts +55 -0
- package/test/answer.test.ts +245 -0
- package/test/app.test.ts +282 -0
- package/test/call-by-grant.test.ts +245 -0
- package/test/denial.test.ts +57 -0
- package/test/feed-snapshot.test.ts +83 -0
- package/test/feed.test.ts +163 -0
- package/test/fixtures/feed-host.ts +86 -0
- package/test/fixtures/serve-web.ts +58 -0
- package/test/fixtures/why-host.ts +44 -0
- package/test/instance-host.test.ts +80 -0
- package/test/web-login.test.ts +171 -0
- package/test/why.test.ts +149 -0
- package/tsconfig.json +6 -0
package/src/login.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* login.ts — who may use the web surface.
|
|
3
|
+
*
|
|
4
|
+
* The page is served on a local port, so any local process or browser tab can reach it. A caller
|
|
5
|
+
* is let in by a cookie, and the cookie is had in exchange for a secret the server made when it
|
|
6
|
+
* started and showed once, in the link it prints. There are no accounts: one secret per start,
|
|
7
|
+
* and it works once.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
11
|
+
|
|
12
|
+
/** The path that takes the secret and gives the cookie. */
|
|
13
|
+
export const LOGIN_PATH = "/login";
|
|
14
|
+
|
|
15
|
+
/** The name of the cookie. */
|
|
16
|
+
const COOKIE_NAME = "niwa_web";
|
|
17
|
+
|
|
18
|
+
function randomValue(): string {
|
|
19
|
+
return randomBytes(24).toString("base64url");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function same(left: string, right: string): boolean {
|
|
23
|
+
const a = Buffer.from(left);
|
|
24
|
+
const b = Buffer.from(right);
|
|
25
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The secret of one server start, and the cookie it was exchanged for. */
|
|
29
|
+
export class WebLogin {
|
|
30
|
+
readonly secret = randomValue();
|
|
31
|
+
private session: string | undefined;
|
|
32
|
+
private spent = false;
|
|
33
|
+
|
|
34
|
+
/** The cookie for this secret, once; undefined for a wrong secret or one that was used. */
|
|
35
|
+
exchange(given: string | null): string | undefined {
|
|
36
|
+
if (given === null || this.spent || !same(given, this.secret)) return undefined;
|
|
37
|
+
this.spent = true;
|
|
38
|
+
this.session = randomValue();
|
|
39
|
+
return `${COOKIE_NAME}=${this.session}; HttpOnly; SameSite=Strict; Path=/`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Does the `cookie` header of a request carry the cookie this server gave? */
|
|
43
|
+
allows(header: string | null): boolean {
|
|
44
|
+
if (this.session === undefined || header === null) return false;
|
|
45
|
+
for (const part of header.split(";")) {
|
|
46
|
+
const [name, ...value] = part.trim().split("=");
|
|
47
|
+
if (name === COOKIE_NAME && same(value.join("="), this.session)) return true;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/source.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { open } from "node:fs/promises";
|
|
4
|
+
import { isAbsolute } from "node:path";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
import { seq } from "@niwa-labs/core";
|
|
8
|
+
import type { NiwaApi } from "@niwa-labs/client";
|
|
9
|
+
|
|
10
|
+
const Script = z.object({ file: z.string(), hash: z.string().regex(/^[a-f0-9]{64}$/) });
|
|
11
|
+
const MAX_SOURCE_BYTES = 1024 * 1024;
|
|
12
|
+
|
|
13
|
+
/** Only return the exact bytes witnessed by the workflow handler when this run started. */
|
|
14
|
+
export async function runSource(api: NiwaApi, position: number): Promise<string | undefined> {
|
|
15
|
+
const [record] = await api.records.read({ from: seq(position - 1), limit: 1 });
|
|
16
|
+
if (record?.seq !== position || record.type !== "workflow.run.started" ||
|
|
17
|
+
record.actor.kind !== "handler" || !record.actor.handler.startsWith("workflow/")) return;
|
|
18
|
+
const data = Script.safeParse(record.data);
|
|
19
|
+
if (!data.success || !isAbsolute(data.data.file) || !/\.(?:[cm]?[jt]s|[jt]sx)$/.test(data.data.file)) return;
|
|
20
|
+
const file = await open(data.data.file, constants.O_RDONLY | constants.O_NONBLOCK).catch(() => undefined);
|
|
21
|
+
if (!file) return;
|
|
22
|
+
try {
|
|
23
|
+
const stat = await file.stat();
|
|
24
|
+
if (!stat.isFile() || stat.size > MAX_SOURCE_BYTES) return;
|
|
25
|
+
// The extra byte distinguishes EOF from a file that grew after stat.
|
|
26
|
+
const bytes = Buffer.alloc(stat.size + 1);
|
|
27
|
+
let offset = 0;
|
|
28
|
+
while (offset < bytes.length) {
|
|
29
|
+
const { bytesRead } = await file.read(bytes, offset, bytes.length - offset, offset);
|
|
30
|
+
if (bytesRead === 0) {
|
|
31
|
+
const source = bytes.subarray(0, offset);
|
|
32
|
+
return createHash("sha256").update(source).digest("hex") === data.data.hash ? source.toString("utf8") : undefined;
|
|
33
|
+
}
|
|
34
|
+
offset += bytesRead;
|
|
35
|
+
}
|
|
36
|
+
} catch {
|
|
37
|
+
// The recorded version cannot be read from this file anymore.
|
|
38
|
+
} finally {
|
|
39
|
+
await file.close();
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/trace.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* trace.ts — what was done and why.
|
|
3
|
+
*
|
|
4
|
+
* Two directions, both from the core's own observation API:
|
|
5
|
+
*
|
|
6
|
+
* - UP: the causes of a record — what it depends on. Walking up far enough reaches the external
|
|
7
|
+
* input that started the whole chain: the record whose cause is nothing.
|
|
8
|
+
* - DOWN: the trace of a record — everything that grew from it.
|
|
9
|
+
*
|
|
10
|
+
* Every record of the chain links to its own trace page, so from any result one can walk to the
|
|
11
|
+
* message that started it.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { NiwaApi } from "@niwa-labs/client";
|
|
15
|
+
import type { RecordId, StoredRecord } from "@niwa-labs/core";
|
|
16
|
+
|
|
17
|
+
/** One record of a chain, as the page shows it. */
|
|
18
|
+
export interface ChainRecord {
|
|
19
|
+
/** The id of the record. */
|
|
20
|
+
readonly id: string;
|
|
21
|
+
/** The type of the record: what kind of fact it is. */
|
|
22
|
+
readonly type: string;
|
|
23
|
+
/** The data of the record, as it was written. */
|
|
24
|
+
readonly data: unknown;
|
|
25
|
+
/** Who wrote it. */
|
|
26
|
+
readonly actor: string;
|
|
27
|
+
/** The record it was made from, when it was made from one. */
|
|
28
|
+
readonly cause: string | null;
|
|
29
|
+
/** The first record of the chain it belongs to. */
|
|
30
|
+
readonly root: string;
|
|
31
|
+
/** When it was written. */
|
|
32
|
+
readonly at: number;
|
|
33
|
+
/** How far down from the start of the walk it is; 0 for the start itself and for every cause. */
|
|
34
|
+
readonly depth: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The chain of one record: what it depends on, and what grew from it. */
|
|
38
|
+
export interface Chain {
|
|
39
|
+
/** The record the chain is about. */
|
|
40
|
+
readonly record: string;
|
|
41
|
+
/** What it depends on: the walk UP. The external input is the last of them. */
|
|
42
|
+
readonly causes: readonly ChainRecord[];
|
|
43
|
+
/** What grew from it: the walk DOWN. */
|
|
44
|
+
readonly trace: readonly ChainRecord[];
|
|
45
|
+
/** The record that started the chain: the one whose cause is nothing. */
|
|
46
|
+
readonly root: string | null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** How a record says who wrote it. */
|
|
50
|
+
function actorOf(record: StoredRecord): string {
|
|
51
|
+
const actor = record.actor;
|
|
52
|
+
if (actor.kind === "host") return "host";
|
|
53
|
+
if (actor.kind === "client") return actor.id;
|
|
54
|
+
return actor.handler;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The chain of a record: what it depends on, and what grew from it.
|
|
59
|
+
*
|
|
60
|
+
* The root is the one the journal recorded on the record itself; an unknown record has none.
|
|
61
|
+
*/
|
|
62
|
+
export async function chainOf(api: NiwaApi, id: RecordId): Promise<Chain> {
|
|
63
|
+
const [causes, traced] = await Promise.all([api.causes(id), api.trace(id)]);
|
|
64
|
+
const start = traced.find((record) => record.depth === 0);
|
|
65
|
+
return {
|
|
66
|
+
record: id,
|
|
67
|
+
causes: causes.map((record) => toChain(record, 0)),
|
|
68
|
+
trace: traced.map((record) => toChain(record, record.depth)),
|
|
69
|
+
root: start?.root ?? null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One record of a chain, as the page shows it. */
|
|
74
|
+
function toChain(record: StoredRecord, depth: number): ChainRecord {
|
|
75
|
+
return {
|
|
76
|
+
id: record.id,
|
|
77
|
+
type: record.type,
|
|
78
|
+
data: record.data,
|
|
79
|
+
actor: actorOf(record),
|
|
80
|
+
cause: record.cause,
|
|
81
|
+
root: record.root,
|
|
82
|
+
at: record.at,
|
|
83
|
+
depth,
|
|
84
|
+
};
|
|
85
|
+
}
|
package/src/viewer.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who is looking, and what they may call.
|
|
3
|
+
*
|
|
4
|
+
* The web surface is one client of the host and holds one grant: the owner's, when the page speaks for the owner, or the
|
|
5
|
+
* grant of a plain client. The host decides every call by that grant; this only reads it, so the page can hide what it
|
|
6
|
+
* could not call. Reading it is not a check: a call is allowed or refused by the host, whatever this says.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
import { accessMine, grant, match, principalSchema, RuleSpecSchema } from "@niwa-labs/core";
|
|
12
|
+
import type { NiwaApi } from "@niwa-labs/client";
|
|
13
|
+
|
|
14
|
+
import { isDenied } from "./denial.ts";
|
|
15
|
+
|
|
16
|
+
/** What the cockpit's SDK builds a viewer from. */
|
|
17
|
+
export interface Viewer {
|
|
18
|
+
/** The id the host knows this client by. */
|
|
19
|
+
readonly id: string;
|
|
20
|
+
/** The viewer speaks for the owner. */
|
|
21
|
+
readonly owner: boolean;
|
|
22
|
+
/** The actions the viewer may call, by name. */
|
|
23
|
+
readonly actions: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const Mine = z.object({
|
|
27
|
+
principal: principalSchema.optional(),
|
|
28
|
+
unlimited: z.boolean(),
|
|
29
|
+
closed: z.string().optional(),
|
|
30
|
+
rules: z.array(RuleSpecSchema),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The viewer of this connection: the principal it acts as, and the actions its grant lets it call.
|
|
35
|
+
*
|
|
36
|
+
* A rule that asks the owner at the moment of the call counts: the action is callable. A grant that was closed, or a client
|
|
37
|
+
* that may not read its own rules, may call nothing as far as this can tell.
|
|
38
|
+
*/
|
|
39
|
+
export async function viewerOf(api: NiwaApi, fallbackId: string): Promise<Viewer> {
|
|
40
|
+
const everything = (await api.describe()).actions.map(({ name }) => String(name));
|
|
41
|
+
let mine: z.output<typeof Mine>;
|
|
42
|
+
try {
|
|
43
|
+
mine = Mine.parse(await api.call(accessMine, {}));
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (isDenied(error)) return { id: fallbackId, owner: false, actions: [] };
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
const id = mine.principal === undefined ? fallbackId : String(mine.principal.id);
|
|
49
|
+
const owner = mine.principal?.kind === "owner";
|
|
50
|
+
if (mine.closed !== undefined) return { id, owner, actions: [] };
|
|
51
|
+
if (mine.unlimited) return { id, owner, actions: everything.sort() };
|
|
52
|
+
const rules = grant(mine.rules);
|
|
53
|
+
return { id, owner, actions: everything.filter((name) => match(rules, "call", name) !== undefined).sort() };
|
|
54
|
+
}
|
|
55
|
+
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// The web surface answers a request of the owner: open requests and paused work are on the page,
|
|
2
|
+
// and answering from the page continues the waiting invocation.
|
|
3
|
+
//
|
|
4
|
+
// The paused invocation is covered in feed.test.ts. This is the open request: the
|
|
5
|
+
// executor asks the owner a question, and the page is what waits for the answer.
|
|
6
|
+
//
|
|
7
|
+
// A real host, a real web surface, and real HTTP requests:
|
|
8
|
+
//
|
|
9
|
+
// 1. the host is started with the requests module; the executor asks the owner a question
|
|
10
|
+
// 2. the open request is on the feed of the page: the page is what waits for the owner
|
|
11
|
+
// 3. POST /answer answers it — the body of the page names the request, the action wants the
|
|
12
|
+
// id of the request and the answer itself
|
|
13
|
+
// 4. the answer stands, and the waiting work goes on with the answer it got
|
|
14
|
+
// 5. an answer that comes after the deadline does not stand: the page is told why
|
|
15
|
+
//
|
|
16
|
+
// The web surface is a client of the host, and it speaks for the owner: it is configured with
|
|
17
|
+
// speaksFor, so it connects with the owner's token and the host lets it answer. Nothing is written into the
|
|
18
|
+
// journal by the surface itself: the answer goes through the core's own action.
|
|
19
|
+
//
|
|
20
|
+
// Only public entries are used: the core and the testing helpers.
|
|
21
|
+
|
|
22
|
+
import { expect, test } from "bun:test";
|
|
23
|
+
|
|
24
|
+
import { owner, record } from "@niwa-labs/core";
|
|
25
|
+
import type { StoredRecord, Timestamp } from "@niwa-labs/core";
|
|
26
|
+
import { createNiwa, Deadline, eventually, tempHome, loginWeb, plugin, watch, webFetch, connectAsOwner, opt, pick, answeredBy, requestAsk, question, requests } from "@niwa-labs/testing";
|
|
27
|
+
import { z } from "zod";
|
|
28
|
+
|
|
29
|
+
import type { WebServer } from "../src/index.ts";
|
|
30
|
+
import { serveWebPages } from "./fixtures/serve-web.ts";
|
|
31
|
+
|
|
32
|
+
/** The record the test writes: it wakes the executor, which asks the owner. */
|
|
33
|
+
const Start = record(
|
|
34
|
+
"answer.start",
|
|
35
|
+
z.object({ release: z.string(), deadline: Deadline.optional(), other: z.boolean().optional() }),
|
|
36
|
+
{ open: true },
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/** The work that continues once the answer has arrived. */
|
|
40
|
+
const Answered = record("answer.answered", z.object({ release: z.string(), branch: z.enum(["main", "next"]) }));
|
|
41
|
+
|
|
42
|
+
/** The question the executor asks the owner. */
|
|
43
|
+
const ReleaseBranch = question("answer.release.branch", { kind: "choice", title: "Which branch?", options: [opt("main"), opt("next")] });
|
|
44
|
+
|
|
45
|
+
/** The executor: it asks the owner, and waits for the answer. */
|
|
46
|
+
function executor() {
|
|
47
|
+
return plugin("answer-executor", (api) => {
|
|
48
|
+
api.declare(Start, Answered);
|
|
49
|
+
// Each release waits for its own answer: one that waits holds no other.
|
|
50
|
+
api.on(Start, { concurrency: Number.POSITIVE_INFINITY }, async (data, step) => {
|
|
51
|
+
const { answer: raw } = answeredBy(
|
|
52
|
+
await step(requestAsk, {
|
|
53
|
+
to: data.other === true ? { kind: "client", id: "someone-else" } : owner,
|
|
54
|
+
...ReleaseBranch,
|
|
55
|
+
input: { ...ReleaseBranch.input, detail: data.release },
|
|
56
|
+
...(data.deadline === undefined ? {} : { deadline: data.deadline }),
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
await step.emit(Answered, { release: data.release, branch: "ids" in raw && raw.ids[0] === "next" ? "next" : "main" });
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** One thing on the feed of the page. */
|
|
65
|
+
interface FeedEntry {
|
|
66
|
+
kind: string;
|
|
67
|
+
id: string;
|
|
68
|
+
record: string;
|
|
69
|
+
text: string;
|
|
70
|
+
choices: string[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What the page answers with: whether the answer stands, and why it does not. */
|
|
74
|
+
interface Verdict {
|
|
75
|
+
accepted: boolean;
|
|
76
|
+
reason?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Read the feed of the page, as the server answers it. */
|
|
80
|
+
async function readFeed(url: string): Promise<FeedEntry[]> {
|
|
81
|
+
const response = await webFetch(`${url}/feed`);
|
|
82
|
+
expect(response.status).toBe(200);
|
|
83
|
+
return (await response.json()) as FeedEntry[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Answer a request from the page: the body of the page names the request it answers. */
|
|
87
|
+
async function answer(url: string, request: string, value: unknown): Promise<{ status: number; verdict: Verdict }> {
|
|
88
|
+
const response = await webFetch(`${url}/answer`, {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: { "content-type": "application/json" },
|
|
91
|
+
body: JSON.stringify({ request, answer: value }),
|
|
92
|
+
});
|
|
93
|
+
return { status: response.status, verdict: (await response.json()) as Verdict };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The records of one type, as the observer saw them. */
|
|
97
|
+
function of(seen: StoredRecord[], type: string): StoredRecord[] {
|
|
98
|
+
return seen.filter((record) => record.type === type);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
test("the answer: the page answers a request of the owner, and the work that waited goes on", async () => {
|
|
102
|
+
const home = await tempHome();
|
|
103
|
+
const host = createNiwa({ home: home.path });
|
|
104
|
+
host.use(requests());
|
|
105
|
+
host.use(executor());
|
|
106
|
+
await host.serve();
|
|
107
|
+
|
|
108
|
+
let web: WebServer | undefined;
|
|
109
|
+
const writer = await connectAsOwner(home.path);
|
|
110
|
+
const observer = await watch(home.path);
|
|
111
|
+
const seen = observer.seen;
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
web = await serveWebPages({ home: home.path, port: 0, speaksFor: "owner" });
|
|
115
|
+
await loginWeb(web);
|
|
116
|
+
|
|
117
|
+
// The executor asks the owner a question.
|
|
118
|
+
await writer.emit(Start, { release: "1.0" });
|
|
119
|
+
await eventually(() => of(seen, "request.created").length === 1, 10_000, "the request was not made");
|
|
120
|
+
const id = (of(seen, "request.created")[0]?.data as { id: string }).id;
|
|
121
|
+
|
|
122
|
+
// The open request is on the page: the page is what waits for the owner.
|
|
123
|
+
await eventually(
|
|
124
|
+
async () => (await readFeed(web!.url)).some((entry) => entry.kind === "request" && entry.id === id),
|
|
125
|
+
10_000,
|
|
126
|
+
"the request did not reach the feed",
|
|
127
|
+
);
|
|
128
|
+
const entry = (await readFeed(web.url)).find((item) => item.kind === "request");
|
|
129
|
+
expect(entry?.id).toBe(id);
|
|
130
|
+
// The entry names the record it came from, so the page can link to it.
|
|
131
|
+
expect(entry?.record).toBe(of(seen, "request.created")[0]?.id);
|
|
132
|
+
expect(of(seen, "request.answered")).toHaveLength(0);
|
|
133
|
+
|
|
134
|
+
// The page answers it: the answer stands, and the page is told that it does.
|
|
135
|
+
const answered = await answer(web.url, id, pick("next"));
|
|
136
|
+
expect(answered.status).toBe(200);
|
|
137
|
+
expect(answered.verdict.accepted).toBe(true);
|
|
138
|
+
expect(answered.verdict.reason).toBeUndefined();
|
|
139
|
+
|
|
140
|
+
// The waiting work goes on with the answer the page gave.
|
|
141
|
+
await eventually(() => of(seen, "answer.answered").length === 1, 10_000, "the work did not continue");
|
|
142
|
+
expect(of(seen, "answer.answered")[0]?.data).toEqual({ release: "1.0", branch: "next" });
|
|
143
|
+
expect(of(seen, "request.answered")).toHaveLength(1);
|
|
144
|
+
expect((of(seen, "request.answered")[0]?.data as { by: unknown }).by).toEqual(owner);
|
|
145
|
+
|
|
146
|
+
// The request is not on the page any more: it was answered.
|
|
147
|
+
await eventually(async () => !(await readFeed(web!.url)).some((item) => item.id === id), 10_000);
|
|
148
|
+
} finally {
|
|
149
|
+
await web?.close();
|
|
150
|
+
await writer.close();
|
|
151
|
+
await observer.stop();
|
|
152
|
+
await host.close();
|
|
153
|
+
await home.cleanup();
|
|
154
|
+
}
|
|
155
|
+
}, 30_000);
|
|
156
|
+
|
|
157
|
+
test("the answer: an answer that comes after the deadline does not stand, and the page is told why", async () => {
|
|
158
|
+
const home = await tempHome();
|
|
159
|
+
const host = createNiwa({ home: home.path });
|
|
160
|
+
host.use(requests());
|
|
161
|
+
host.use(executor());
|
|
162
|
+
await host.serve();
|
|
163
|
+
|
|
164
|
+
let web: WebServer | undefined;
|
|
165
|
+
const writer = await connectAsOwner(home.path);
|
|
166
|
+
const observer = await watch(home.path);
|
|
167
|
+
const seen = observer.seen;
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
web = await serveWebPages({ home: home.path, port: 0, speaksFor: "owner" });
|
|
171
|
+
await loginWeb(web);
|
|
172
|
+
|
|
173
|
+
// The question is asked with a deadline that is still ahead.
|
|
174
|
+
const deadline = (Date.now() + 500) as Timestamp;
|
|
175
|
+
await writer.emit(Start, { release: "2.0", deadline });
|
|
176
|
+
await eventually(() => of(seen, "request.created").length === 1, 10_000, "the request was not made");
|
|
177
|
+
const id = (of(seen, "request.created")[0]?.data as { id: string }).id;
|
|
178
|
+
|
|
179
|
+
// The time runs out: the request expires.
|
|
180
|
+
await eventually(() => of(seen, "request.expired").length === 1, 10_000, "the request did not expire");
|
|
181
|
+
|
|
182
|
+
// The answer comes too late: it does not stand, and the page is told why.
|
|
183
|
+
const late = await answer(web.url, id, pick("main"));
|
|
184
|
+
expect(late.status).toBe(200);
|
|
185
|
+
expect(late.verdict.accepted).toBe(false);
|
|
186
|
+
expect(late.verdict.reason).toBe("expired");
|
|
187
|
+
|
|
188
|
+
// Nothing was written: the answer did not stand.
|
|
189
|
+
expect(of(seen, "request.answered")).toHaveLength(0);
|
|
190
|
+
expect(of(seen, "answer.answered")).toHaveLength(0);
|
|
191
|
+
} finally {
|
|
192
|
+
await web?.close();
|
|
193
|
+
await writer.close();
|
|
194
|
+
await observer.stop();
|
|
195
|
+
await host.close();
|
|
196
|
+
await home.cleanup();
|
|
197
|
+
}
|
|
198
|
+
}, 30_000);
|
|
199
|
+
|
|
200
|
+
test("the feed: a request to someone else is not on the page", async () => {
|
|
201
|
+
const home = await tempHome();
|
|
202
|
+
const host = createNiwa({ home: home.path });
|
|
203
|
+
host.use(requests());
|
|
204
|
+
host.use(executor());
|
|
205
|
+
await host.serve();
|
|
206
|
+
|
|
207
|
+
let web: WebServer | undefined;
|
|
208
|
+
const writer = await connectAsOwner(home.path);
|
|
209
|
+
const observer = await watch(home.path);
|
|
210
|
+
const seen = observer.seen;
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
web = await serveWebPages({ home: home.path, port: 0, speaksFor: "owner" });
|
|
214
|
+
await loginWeb(web);
|
|
215
|
+
|
|
216
|
+
await writer.emit(Start, { release: "3.0", other: true });
|
|
217
|
+
await writer.emit(Start, { release: "3.1" });
|
|
218
|
+
await eventually(() => of(seen, "request.created").length === 2, 10_000, "the requests were not made");
|
|
219
|
+
const created = of(seen, "request.created").map((item) => item.data as { id: string; to: { kind: string } });
|
|
220
|
+
const mine = created.find((item) => item.to.kind === "owner")?.id;
|
|
221
|
+
const theirs = created.find((item) => item.to.kind !== "owner")?.id;
|
|
222
|
+
|
|
223
|
+
await eventually(
|
|
224
|
+
async () => (await readFeed(web!.url)).some((entry) => entry.id === mine),
|
|
225
|
+
10_000,
|
|
226
|
+
"the request of the owner did not reach the feed",
|
|
227
|
+
);
|
|
228
|
+
expect((await readFeed(web.url)).some((entry) => entry.id === theirs)).toBe(false);
|
|
229
|
+
|
|
230
|
+
// The page speaks for the owner, not for the one the other request was asked of.
|
|
231
|
+
const refused = await webFetch(`${web.url}/answer`, {
|
|
232
|
+
method: "POST",
|
|
233
|
+
headers: { "content-type": "application/json" },
|
|
234
|
+
body: JSON.stringify({ request: theirs, answer: "main" }),
|
|
235
|
+
});
|
|
236
|
+
expect(refused.status).toBe(409);
|
|
237
|
+
expect(of(seen, "request.answered")).toHaveLength(0);
|
|
238
|
+
} finally {
|
|
239
|
+
await web?.close();
|
|
240
|
+
await writer.close();
|
|
241
|
+
await observer.stop();
|
|
242
|
+
await host.close();
|
|
243
|
+
await home.cleanup();
|
|
244
|
+
}
|
|
245
|
+
}, 30_000);
|