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
This commit is contained in:
2026-11-18 10:51:56 +01:00
co-authored by Claude Sonnet 5.5
parent e3d0c12e55
commit 6995364edc
10 changed files with 903 additions and 5 deletions
+49 -1
View File
@@ -9,7 +9,8 @@ ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
[ -d "$ROOT/web/node_modules" ] || { echo "web dependencies missing: run 'cd web && npm ci' first"; exit 1; }
(cd "$ROOT" && go build -o bin/allmende ./cmd/allmende) || exit 1
BIN=$ROOT/bin/allmende
W=$(mktemp -d "${TMPDIR:-/tmp}/allmende-e2e.XXXXXX")
mkdir -p "$ROOT/.tmp" # scratch space inside the checkout (git ignores it), not /tmp
W=$(mktemp -d "${TMPDIR:-$ROOT/.tmp}/allmende-e2e.XXXXXX")
A=$W/a; B=$W/b
PIDS=()
cleanup() {
@@ -119,6 +120,11 @@ ok "buyer's browser code verified and read the grant from B"
node $BUYER read http://127.0.0.1:18102 $AUTHOR $ID $(node -e 'console.log("77".repeat(32))') | grep -q . && bad "another thread key saw a reply"
ok "a different thread key sees nothing"
echo "== the same through the web UI: a real page for the buyer on B, one for the owner on A"
UI=$ROOT/scripts/e2e/ui.ts
node $UI buyer http://127.0.0.1:18102 http://127.0.0.1:18101 $TOKEN $AUTHOR $ID > $W/ui-buyer.txt 2>&1 \
&& { sed 's/^OK /PASS: UI: /' $W/ui-buyer.txt; } || { cat $W/ui-buyer.txt; bad "the buyer flow through the web UI"; }
echo "== recover on A (simulate a stolen machine)"
OLD_DEVICE=$(curl -s http://127.0.0.1:18101/api/v1/status | python3 -c 'import json,sys; print(json.load(sys.stdin)["device"])')
kill ${PIDS[0]}; wait ${PIDS[0]} 2>/dev/null
@@ -176,4 +182,46 @@ wait_for "X restarted" 10 curl -sf http://127.0.0.1:18104/api/v1/status
wait_for "X reconnects from its remembered peers" 30 check ge 1 peers 18104
ok "X rejoined the network with no bootstrap configured"
echo "== regions: chosen by place name in the browser, even the first one"
D=$W/d
$BIN init --data $D >/dev/null || bad "init D"
# No --region: the node starts empty and says what to do.
$BIN run --data $D --no-mdns --no-default-bootstrap --difficulty $DIFF --http 127.0.0.1:18106 \
--listen /ip4/127.0.0.1/tcp/14106 2> $W/d.log &
DPID=$!; PIDS+=($DPID)
wait_for "D http" 10 curl -sf http://127.0.0.1:18106/api/v1/status
grep -q "follows no region yet" $W/d.log && ok "a node without a region starts and tells the owner what to do" || bad "no hint for the owner"
DTOKEN=$(cat $D/admin.token)
node $UI regions http://127.0.0.1:18106 $DTOKEN > $W/ui-regions.txt 2>&1 \
&& { sed 's/^OK /PASS: UI: /' $W/ui-regions.txt; } || { cat $W/ui-regions.txt; bad "choosing the first region through the web UI"; }
[ -s $D/regions.json ] && ok "the choice is saved in the data directory" || bad "regions.json missing"
kill $DPID; wait $DPID 2>/dev/null
$BIN run --data $D --no-mdns --no-default-bootstrap --difficulty $DIFF --http 127.0.0.1:18106 \
--listen /ip4/127.0.0.1/tcp/14106 2> $W/d2.log &
DPID=$!; PIDS+=($DPID)
wait_for "D restarted" 10 curl -sf http://127.0.0.1:18106/api/v1/status
curl -s http://127.0.0.1:18106/api/v1/status | grep -q '"regions":\["u1j"' && ok "after a restart without --region it still follows what the browser chose" || bad "regions were lost on restart"
echo "== --region takes place names"
$BIN run --data $W/nope --region Atlantisxyz 2>&1 | grep -q "neither a region code" && ok "an unknown place is refused with a hint" || bad "unknown place was not refused"
E=$W/e
$BIN init --data $E >/dev/null || bad "init E"
$BIN run --data $E --region cologne --no-mdns --no-default-bootstrap --difficulty $DIFF --http 127.0.0.1:18107 \
--listen /ip4/127.0.0.1/tcp/14107 2> $W/e.log &
PIDS+=($!)
wait_for "E http" 10 curl -sf http://127.0.0.1:18107/api/v1/status
grep -q 'Köln (Cologne)' $W/e.log && ok "'cologne' is announced as Köln, with the cells it follows" || bad "no note about what the name turned into"
curl -s http://127.0.0.1:18107/api/v1/status | grep -q '"regions":\["u1h"' && ok "Cologne's own cell (u1h) comes first" || bad "wrong region for cologne: $(curl -s http://127.0.0.1:18107/api/v1/status)"
echo "== a listing that runs out soon is reminded on the owner's terminal"
SOON='{"kind":"rent","category":"room","geohash":"u1jjjj","price":{"amount_cents":50000,"currency":"EUR","period":"month"},"title":"Zimmer, nur noch kurz","expires_in_days":3}'
curl -s -o /dev/null -w "%{http_code}" -X POST -H "Authorization: Bearer $DTOKEN" -d "$SOON" http://127.0.0.1:18106/api/v1/listings | grep -qE "^(200|201)$" || bad "could not publish the short listing on D"
wait_for "D's terminal reminds about the listing" 60 grep -q 'a listing runs out soon' $W/d2.log
grep 'a listing runs out soon' $W/d2.log | grep -q 'listing="Zimmer, nur noch kurz"' && ok "the reminder names the listing" || bad "reminder does not name the listing"
grep 'a listing runs out soon' $W/d2.log | grep -q 'left="3 days"' && ok "and says how long is left" || bad "reminder does not say how long is left"
grep 'a listing runs out soon' $W/d2.log | grep -q "renew=http://127.0.0.1:18106/#/mine" && ok "and links to where to renew" || bad "reminder has no link"
grep 'a listing runs out soon' $W/d2.log | grep -q "$DTOKEN" && bad "the sign-in token leaked into the log"
ok "the reminder does not carry the sign-in token"
grep -q "Helle Wohnung" $W/d2.log && bad "a listing with plenty of time left was reminded"
echo "ALL E2E CHECKS PASSED"
+148
View File
@@ -0,0 +1,148 @@
// 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);
}