import assert from "node:assert/strict"; import { afterEach, test } from "node:test"; import { NOW, openPage, startGateway, type Gateway, type Page } from "./helpers.ts"; const TOKEN = "ef56".repeat(16); const DAY = 86_400; const PHRASE = "legal winner thank year wave sausage worth useful legal winner thank yellow"; let gw: Gateway | undefined; let page: Page | undefined; let calls: { method: string; path: string; body: string; auth: string }[] = []; afterEach(async () => { page?.close(); page = undefined; await gw?.close(); gw = undefined; }); /** A node whose certificate runs out in `days` days, with an owner who may sign in. */ async function node(days: number | null, canRenew = true, answer?: { status: number; body: unknown }): Promise { gw = await startGateway({ listings: [], ...(days === null ? {} : { cert: { expires_at: NOW + days * DAY, can_renew: canRenew } }) }); calls = []; gw.owner = (r) => { calls.push({ method: r.method, path: r.path, body: r.body, auth: r.auth }); if (r.auth !== `Bearer ${TOKEN}`) return { status: 401, body: { error: "unauthorized" } }; if (r.path === "/api/v1/mine") return { status: 200, body: [] }; if (r.path === "/api/v1/certificate" && r.method === "PUT") { return answer ?? { status: 200, body: { cert_expires_at: NOW + 365 * DAY } }; } return undefined; }; page = openPage(gw.url, `#/login/${TOKEN}`); await page.waitFor(() => page!.document.querySelector(".pane-listings") !== null, "the owner page"); } const text = () => page!.document.body.textContent ?? ""; const panel = () => page!.document.querySelector("section.certificate"); const renew = () => [...page!.document.querySelectorAll("button")].find((b) => b.textContent === "Renew certificate") as HTMLButtonElement | undefined; function submit(words: string): void { const form = panel()!.querySelector("form")!; (form.querySelector("input[name=phrase]") as HTMLInputElement).value = words; form.dispatchEvent(new page!.window.Event("submit", { cancelable: true })); } test("with plenty of time left the owner page says nothing about the certificate", async () => { await node(200); assert.equal(panel(), null); assert.doesNotMatch(text(), /certificate/i); }); test("an older node that does not report the certificate shows no panel", async () => { await node(null); assert.equal(panel(), null); }); test("in the last 30 days it says when the certificate ends and offers to renew it", async () => { await node(12); assert.ok(panel(), "the panel"); assert.match(panel()!.textContent!, /expires on .* \(in 12 days\)/); assert.ok(renew(), "the renew button"); const input = panel()!.querySelector("input[name=phrase]") as HTMLInputElement; assert.equal(input.type, "password", "the words are not shown while typing"); assert.equal(input.getAttribute("autocomplete"), "off"); }); test("exactly 30 days left is the first day it asks, 31 is not", async () => { await node(30); assert.ok(panel(), "30 days left"); page!.close(); await gw!.close(); await node(31); assert.equal(panel(), null, "31 days left"); }); test("renewing sends the words once, with the token, and the panel then confirms", async () => { await node(12); submit(` ${PHRASE} `); await page!.waitFor(() => /Renewed\. The certificate is now valid until/.test(text()), "the confirmation"); const puts = calls.filter((c) => c.path === "/api/v1/certificate"); assert.equal(puts.length, 1); assert.equal(puts[0]!.method, "PUT"); assert.equal(puts[0]!.auth, `Bearer ${TOKEN}`); assert.deepEqual(JSON.parse(puts[0]!.body), { phrase: PHRASE }, "the words arrive trimmed, in a JSON body"); assert.equal(panel()!.querySelector("form"), null, "the form is gone"); const left = [...page!.document.querySelectorAll("input, textarea")].filter((i) => (i as HTMLInputElement).value.includes("winner")); assert.equal(left.length, 0, "the words are not left in any field on the page"); }); test("empty words are not sent", async () => { await node(12); submit(" "); assert.match(panel()!.textContent!, /That did not work/); assert.equal(calls.filter((c) => c.path === "/api/v1/certificate").length, 0); }); test("a refusal is shown, the words stay for another try, and the button works again", async () => { await node(12, true, { status: 409, body: { error: "these words belong to a different identity than this node" } }); submit(PHRASE); await page!.waitFor(() => /different identity/.test(text()), "the refusal"); assert.match(panel()!.textContent!, /That did not work: these words belong to a different identity than this node/); assert.equal(renew()!.disabled, false); assert.equal((panel()!.querySelector("input[name=phrase]") as HTMLInputElement).value, PHRASE); assert.ok(panel()!.querySelector("form"), "the form is still there"); }); test("an identity without a recovery phrase is told so and gets no form", async () => { await node(5, false); assert.match(panel()!.textContent!, /no recovery phrase/); assert.equal(panel()!.querySelector("form"), null); assert.equal(renew(), undefined); }); test("a certificate that already ran out says so", async () => { await node(-2); assert.match(panel()!.textContent!, /expired on/); assert.ok(renew(), "it can still be renewed while the node runs"); });