import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { afterEach, beforeEach, test } from "node:test"; import { canonicalize } from "../src/jcs.ts"; import { NOW, newParty, openPage, startGateway, type Gateway, type Page } from "./helpers.ts"; const TOKEN = "cd34".repeat(16); const seller = newParty({ title: "Altbau in Ottensen" }); const ref = { author: seller.author, id: seller.listing.id }; const detail = `#/l/${ref.author}/${ref.id}`; let gw: Gateway; let page: Page | undefined; let calls: { method: string; path: string; body: string; auth: string }[]; beforeEach(async () => { gw = await startGateway({ listings: [seller.listing] }); calls = []; }); afterEach(async () => { page?.close(); page = undefined; await gw.close(); }); const open = (hash: string) => (page = openPage(gw.url, hash)); const text = () => page!.document.body.textContent ?? ""; const qa = (sel: string) => [...page!.document.querySelectorAll(sel)]; const button = (label: string) => qa("button").find((b) => b.textContent === label) as HTMLButtonElement | undefined; /** Fills the first form of the report panel (the visitor's) and submits it. */ function tipOff(category: string, note: string): void { const form = page!.document.querySelector("section.report details.report-form form")!; (form.querySelector("select") as HTMLSelectElement).value = category; (form.querySelector("textarea") as HTMLTextAreaElement).value = note; form.dispatchEvent(new page!.window.Event("submit", { cancelable: true })); } const zeroBits = (s: string): number => { let bits = 0; for (const byte of createHash("sha256").update(s).digest()) { if (byte === 0) { bits += 8; continue; } return bits + Math.clz32(byte) - 24; } return bits; }; test("a listing the node hides says so, and who flagged it", async () => { gw.reports = { hidden: true, trusted: { scam: 2, illegal: 1 }, others: 3, signals: 0 }; open(detail); await page!.waitFor(() => /hidden from search/.test(text()), "the notice"); assert.match(text(), /scam \(2\)/); assert.match(text(), /illegal content \(1\)/); assert.match(text(), /3 more reports/); assert.match(text(), /Altbau in Ottensen/, "the listing itself is still shown"); }); test("a listing without reports shows no notice, and an older node without the route still works", async () => { gw.reports = { hidden: false, trusted: {}, others: 0, signals: 0 }; open(detail); await page!.waitFor(() => button("Send report") !== undefined, "the report form"); assert.doesNotMatch(text(), /hidden from search/); page!.close(); gw.reports = undefined; // 404 open(detail); await page!.waitFor(() => button("Send report") !== undefined, "the report form without a summary"); }); test("a visitor's tip is mined, canonical and sent to the node, and nothing else happens", async () => { open(detail); await page!.waitFor(() => button("Send report") !== undefined, "the report form"); tipOff("scam", "Will Vorkasse nach Spanien"); await page!.waitFor(() => gw.tips.length === 1, "the tip to arrive"); const body = gw.tips[0]!; const sent = JSON.parse(body) as Record; assert.equal(body, canonicalize(sent), "the node only accepts the canonical form"); assert.deepEqual(sent.listing, ref); assert.equal(sent.category, "scam"); assert.equal(sent.note, "Will Vorkasse nach Spanien"); assert.equal(sent.v, 1); assert.ok(zeroBits(body) >= 6, "it did the proof-of-work the node asked for"); assert.ok(Math.abs((sent.created_at as number) - NOW) < 60, "dated by the page's clock"); await page!.waitFor(() => /Thank you/.test(text()), "thanks"); assert.equal(button("Send report"), undefined, "the form is gone once it was sent"); }); test("a refused tip is explained and the text is kept", async () => { gw.tipFails = { status: 429, error: "too many requests from this address; try again in a minute" }; open(detail); await page!.waitFor(() => button("Send report") !== undefined, "the report form"); tipOff("other", "Sieht komisch aus"); await page!.waitFor(() => /too many requests/.test(text()), "the node's explanation"); assert.equal(button("Send report")!.disabled, false, "the visitor can try again"); assert.equal(page!.document.querySelector("section.report textarea")!.value, "Sieht komisch aus"); }); test("only the signed-in operator gets the form that signs a report", async () => { open(detail); await page!.waitFor(() => button("Send report") !== undefined, "the report form"); assert.doesNotMatch(text(), /Report as operator/, "a visitor must not be offered it"); page!.close(); gw.owner = (r) => { calls.push({ method: r.method, path: r.path, body: r.body, auth: r.auth }); return r.method === "POST" && r.path === "/api/v1/reports" ? { status: 200, body: {} } : undefined; }; open(`#/login/${TOKEN}`); await page!.waitFor(() => page!.window.location.hash === "#/mine", "signed in"); page!.window.location.hash = detail; await page!.waitFor(() => /Report as operator/.test(text()), "the operator's form"); const form = qa("section.report details.report-form form")[1]!; (form.querySelector("select") as HTMLSelectElement).value = "illegal"; (form.querySelector("textarea") as HTMLTextAreaElement).value = "bestätigt"; form.dispatchEvent(new page!.window.Event("submit", { cancelable: true })); await page!.waitFor(() => calls.some((c) => c.method === "POST" && c.path === "/api/v1/reports"), "the report"); const post = calls.find((c) => c.method === "POST" && c.path === "/api/v1/reports")!; assert.equal(post.auth, `Bearer ${TOKEN}`); assert.deepEqual(JSON.parse(post.body), { listing: ref, category: "illegal", note: "bestätigt" }); button("Withdraw my report")!.click(); await page!.waitFor(() => calls.filter((c) => c.method === "POST" && c.path === "/api/v1/reports").length === 2, "the withdrawal"); assert.equal(JSON.parse(calls.filter((c) => c.method === "POST")[1]!.body).category, "withdrawn"); }); test("the operator's queue lists tips by listing; they can be turned into a report or dismissed", async () => { const tips = [ { id: 1, author: ref.author, listing: ref.id, title: "Altbau in Ottensen", category: "scam", note: "Vorkasse", created_at: 1, work: 19 }, { id: 2, author: ref.author, listing: ref.id, title: "Altbau in Ottensen", category: "other", note: "", created_at: 2, work: 14 }, ]; gw.owner = (r) => { calls.push({ method: r.method, path: r.path, body: r.body, auth: r.auth }); if (r.method === "GET" && r.path === "/api/v1/signals") return { status: 200, body: tips }; if (r.method === "POST" && r.path === "/api/v1/reports") return { status: 200, body: {} }; if (r.method === "DELETE") return { status: 204 }; return undefined; }; open(`#/login/${TOKEN}`); await page!.waitFor(() => page!.window.location.hash === "#/mine", "signed in"); page!.window.location.hash = "#/mine/reports"; await page!.waitFor(() => /Vorkasse/.test(text()), "the tips"); assert.equal(qa("section.panel").length, 1, "two tips about one listing make one card"); assert.match(text(), /Effort 19 bits/); assert.match(text(), /Altbau in Ottensen/); button("Report as “scam”")!.click(); await page!.waitFor(() => calls.some((c) => c.method === "POST" && c.path === "/api/v1/reports"), "the report"); assert.deepEqual(JSON.parse(calls.find((c) => c.path === "/api/v1/reports")!.body), { listing: ref, category: "scam", note: "Vorkasse", }); button("Dismiss")!.click(); await page!.waitFor(() => calls.some((c) => c.method === "DELETE"), "the dismissal"); assert.equal(calls.find((c) => c.method === "DELETE")!.path, `/api/v1/signals/${ref.author}/${ref.id}`); }); test("without signing in the queue leads to the sign-in", async () => { open("#/mine/reports"); await page!.waitFor(() => /sign in|Sign in|token/i.test(text()), "the sign-in"); });