Files
nilsandClaude Sonnet 5.5 6995364edc Place names for --region, an expiry reminder, and e2e checks through the UI
--region takes a place name as well as a code ("Bonn", "Köln", "cologne",
"name:dub" to force a name that looks like a code). The node uses the same
GeoNames list as the browser (internal/places), follows the place's cell plus
any cell within 40 km, and prints what each name became.

The node reminds the owner on its terminal, once a day per listing, about
every listing with 5 days or less left (the UI marks the same range). The
sign-in token is never in the line.

scripts/e2e/ui.ts drives the real bundle in jsdom against real nodes: a buyer
writes from a listing page, the owner reads and answers in their own page and
the buyer's page shows the verified answer; a node with no region gets its
first one by place name in the browser and keeps it over a restart. e2e.sh
also checks --region by name and the reminder, and keeps its scratch files in
the checkout's .tmp.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
2026-11-18 10:51:56 +01:00

149 lines
7.6 KiB
TypeScript

// Drives the real web UI against real nodes, for the e2e script. The page is
// the built bundle (web/dist) in jsdom; every request goes to a running node,
// nothing is faked. Usage:
//
// node ui.ts regions <node url> <admin token>
// a node with no region: the owner signs in, picks a place by name, saves
// node ui.ts buyer <buyer node url> <owner node url> <admin token> <author> <listing id>
// a buyer writes from a listing page; the owner reads and answers in
// their own page; the buyer's page shows the verified answer
//
// Prints "OK …" for each step and exits non-zero on the first failure.
import { readFileSync } from "node:fs";
import { JSDOM, type DOMWindow } from "../../web/node_modules/jsdom/lib/api.js";
const dist = new URL("../../web/dist/", import.meta.url);
const html = readFileSync(new URL("index.html", dist), "utf8");
const bundle = readFileSync(new URL("app.js", dist), "utf8");
const places = readFileSync(new URL("places.tsv", dist));
interface Page {
w: DOMWindow;
d: Document;
waitFor(pred: () => boolean, what: string, ms?: number): Promise<void>;
close(): void;
}
/** Runs the bundle against `node`, as a browser tab opened on that node would. */
function open(node: string, hash: string): Page {
const dom = new JSDOM(html, { url: `${node}/?lang=en${hash}`, runScripts: "outside-only", pretendToBeVisual: true });
const w = dom.window;
w.fetch = ((input: string | URL, init?: RequestInit) => fetch(new URL(String(input), node), init)) as unknown as typeof w.fetch;
w.scrollTo = () => {};
w.eval(bundle);
return {
w,
d: w.document,
async waitFor(pred, what, ms = 30000) {
const end = Date.now() + ms;
while (Date.now() < end) {
if (pred()) return;
await new Promise((r) => setTimeout(r, 25));
}
throw new Error(`timed out waiting for: ${what}\n${w.document.body.textContent?.slice(0, 500)}`);
},
close: () => w.close(),
};
}
const ok = (what: string) => console.log(`OK ${what}`);
const fail = (what: string): never => {
throw new Error(what);
};
const text = (p: Page) => p.d.body.textContent ?? "";
const q = <T extends Element = HTMLElement>(p: Page, sel: string) => p.d.querySelector<T>(sel);
const qa = (p: Page, sel: string) => [...p.d.querySelectorAll(sel)];
const submit = (p: Page, sel: string) => q(p, sel)!.dispatchEvent(new p.w.Event("submit", { cancelable: true, bubbles: true }));
const click = (el: Element | null | undefined) => (el as HTMLElement | undefined)?.click();
async function regions(node: string, token: string): Promise<void> {
const status = async () => ((await (await fetch(`${node}/api/v1/status`)).json()) as { regions: string[] }).regions;
if ((await status()).length !== 0) fail("the node must start without a region");
// A visitor is told that the node follows nothing.
const visitor = open(node, "#/");
await visitor.waitFor(() => /follows no region yet/.test(text(visitor)), "the notice for visitors");
ok("a visitor is told the node follows no region yet");
visitor.close();
// The owner signs in with the link the node printed and lands on the picker.
const p = open(node, `#/login/${token}`);
await p.waitFor(() => q(p, '[name="place"]') !== null, "the regions page right after signing in");
const search = q<HTMLInputElement>(p, '[name="place"]')!;
await p.waitFor(() => !search.disabled, "the list of places (served by the node)");
ok("signing in leads straight to choosing a region");
search.value = "bonn";
search.dispatchEvent(new p.w.Event("input", { bubbles: true }));
await p.waitFor(() => q(p, ".rp-result") !== null, "a result for 'bonn'");
click(q(p, ".rp-result"));
await p.waitFor(() => /Regions around Bonn/.test(text(p)), "the cells around Bonn");
click(qa(p, "button").find((b) => b.textContent === "Add"));
await p.waitFor(() => q(p, ".rp-chips")!.textContent!.includes("u1j"), "Bonn's cell among the chosen regions");
submit(p, "form");
await p.waitFor(() => /Saved/.test(text(p)), "the node confirms");
const now = await status();
if (now[0] !== "u1j") fail(`the node follows ${now}, want u1j first`);
if (!text(p).includes(`regions: ${now.join(", ")}`)) fail("the status line does not show the new regions");
ok(`the node follows ${now.join(", ")} now, chosen by place name in the browser`);
p.close();
}
async function buyer(buyerNode: string, ownerNode: string, token: string, author: string, id: string): Promise<void> {
const question = "Ist die Wohnung noch frei? Ich schreibe aus der Oberfläche.";
const answer = "Ja, noch frei. Besichtigung diese Woche?";
// The buyer finds the listing on their node, which verifies its signature in the page.
const b = open(buyerNode, `#/l/${author}/${id}`);
await b.waitFor(() => q(b, 'textarea[name="message"]') !== null, "the contact form on the listing page");
ok("the listing page verified the listing and offers to write to the owner");
q<HTMLTextAreaElement>(b, 'textarea[name="message"]')!.value = question;
submit(b, "form.compose");
await b.waitFor(() => b.w.location.hash.startsWith("#/c/"), "the conversation page (the message was mined and accepted)");
await b.waitFor(() => qa(b, ".thread li").some((li) => li.textContent!.includes(question)), "the buyer's own message in the thread");
ok("the buyer sent an encrypted message from the UI and sees it in the conversation");
// The owner, in their own page on their own node, finds the request and answers.
const o = open(ownerNode, `#/login/${token}`);
await o.waitFor(() => qa(o, ".pane-listings .ocard").length >= 1, "the owner's listings");
o.w.location.hash = `#/mine/${id}`;
await o.waitFor(() => qa(o, ".pane-requests .ocard").length >= 1, "the request for the listing (it travelled through the network)");
ok("the owner's page lists the request");
// The listing may have several conversations: open them until the buyer's is there.
const hasQuestion = () => qa(o, ".pane-thread .msg").some((m) => m.textContent!.includes(question));
for (const end = Date.now() + 30000; !hasQuestion(); ) {
if (Date.now() > end) fail("the buyer's text never showed in a conversation of the owner's page");
for (let i = 0; i < qa(o, ".pane-requests .ocard").length && !hasQuestion(); i++) {
click(qa(o, ".pane-requests .ocard")[i]);
await o.waitFor(hasQuestion, "", 1500).catch(() => {}); // not this one: try the next
}
await new Promise((r) => setTimeout(r, 300)); // the message may still be on its way
}
ok("the owner reads the buyer's text");
q<HTMLTextAreaElement>(o, '.pane-thread textarea[name="reply"]')!.value = answer;
submit(o, ".pane-thread form");
await o.waitFor(() => qa(o, ".pane-thread .msg.out").some((m) => m.textContent!.includes(answer)), "the owner's reply in the thread");
ok("the owner answered from the UI");
// The buyer's open page polls its mailbox and shows the answer, verified.
await b.waitFor(() => qa(b, ".thread li").some((li) => li.textContent!.includes(answer)), "the owner's answer on the buyer's page");
const li = qa(b, ".thread li").find((x) => x.textContent!.includes(answer))!;
if (!li.textContent!.includes("verified")) fail(`the answer is not marked as verified: ${li.textContent}`);
ok("the buyer's page shows the answer, signature verified");
b.close();
o.close();
}
const [cmd, ...args] = process.argv.slice(2);
// The place list is served by the node, so the page needs nothing from disk but the bundle.
void places;
try {
if (cmd === "regions") await regions(args[0]!, args[1]!);
else if (cmd === "buyer") await buyer(args[0]!, args[1]!, args[2]!, args[3]!, args[4]!);
else fail(`unknown scenario ${cmd}`);
process.exit(0);
} catch (e) {
console.error(`FAIL ${(e as Error).message}`);
process.exit(1);
}