The release sheet gets an optional email field, prefilled from the saved address and kept with it. A conversation suggests releasing the address when the buyer's latest message contains words like "address" or "wo genau" (a keyword guess, German and English, only ever a suggestion). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
356 lines
19 KiB
TypeScript
356 lines
19 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { afterEach, beforeEach, test } from "node:test";
|
|
|
|
import type { Address, Mine, OwnerMessage, Thread } from "../src/ownerApi.ts";
|
|
import { NOW, openPage, startGateway, type Gateway, type Page } from "./helpers.ts";
|
|
|
|
const TOKEN = "ab12".repeat(16);
|
|
const L1 = "11".repeat(16);
|
|
const L2 = "22".repeat(16);
|
|
const T_NEW = "a".repeat(60) + "a3f9"; // needs a reply
|
|
const T_LINK = "b".repeat(60) + "5b10"; // hid a link
|
|
const T_GRANT = "c".repeat(60) + "e204"; // address released
|
|
|
|
const XSS = `<img src=x onerror="window.__pwned=1"> noch frei?`;
|
|
|
|
/** A stand-in for the owner's node: state, and a record of what the page asked. */
|
|
function fakeNode() {
|
|
const s = {
|
|
token: TOKEN,
|
|
calls: [] as { method: string; path: string; body: string; auth: string }[],
|
|
address: { address: "Musterweg 7, 22765 Hamburg", phone: "+49 40 1" } as Address | null,
|
|
mine: [
|
|
{ id: L1, title: "Helle Wohnung", kind: "rent", price: { amount_cents: 115000, currency: "EUR", period: "month" }, expires_at: NOW + 86400 * 30, threads: 3, needs_reply: 1, hidden: 1, address_saved: true },
|
|
{ id: L2, title: "Gewerbefläche", kind: "rent", expires_at: NOW + 86400 * 30, threads: 0, needs_reply: 0, hidden: 0, address_saved: false },
|
|
] as Mine[],
|
|
threads: [
|
|
{ listing: L1, thread: T_NEW, messages: 1, hidden: 0, replied: false, first_at: NOW - 600, last_at: NOW - 600, last_dir: "in", needs_reply: true, preview: XSS, effort: 19 },
|
|
{ listing: L1, thread: T_LINK, messages: 1, hidden: 1, replied: false, first_at: NOW - 90000, last_at: NOW - 90000, last_dir: "in", needs_reply: false, effort: 15 },
|
|
{ listing: L1, thread: T_GRANT, messages: 3, hidden: 0, replied: true, first_at: NOW - 90000, last_at: NOW - 80000, last_dir: "out", needs_reply: false, effort: 17, grant_until: NOW + 86400 },
|
|
] as Thread[],
|
|
msgs: {
|
|
[T_NEW]: [{ hash: "h1", dir: "in", received_at: NOW - 600, type: "text", n: 1, sent_at: NOW - 600, text: XSS }],
|
|
[T_LINK]: [{ hash: "h2", dir: "in", hidden: true, reason: "x", received_at: NOW, type: "text", n: 1, sent_at: NOW - 90000 }],
|
|
[T_GRANT]: [
|
|
{ hash: "h3", dir: "in", received_at: NOW, type: "text", n: 1, sent_at: NOW - 90000, text: "Guten Tag" },
|
|
{ hash: "h4", dir: "out", received_at: NOW, type: "grant", n: 1, sent_at: NOW - 80000, address: "Musterweg 7, 22765 Hamburg", note: "Klingel: Meyer", valid_until: NOW + 86400 },
|
|
],
|
|
} as Record<string, OwnerMessage[]>,
|
|
};
|
|
const handler: NonNullable<Gateway["owner"]> = (r) => {
|
|
s.calls.push({ method: r.method, path: r.path + (r.query.size ? "?" + r.query : ""), body: r.body, auth: r.auth });
|
|
if (r.auth !== `Bearer ${s.token}`) return { status: 401, body: { error: "unauthorized" } };
|
|
if (r.path === "/api/v1/mine") return { status: 200, body: s.mine };
|
|
if (r.path === "/api/v1/inbox") return { status: 200, body: s.threads.filter((t) => t.listing === r.query.get("listing")) };
|
|
const th = /^\/api\/v1\/inbox\/([0-9a-f]{32})\/([0-9a-f]{64})$/.exec(r.path);
|
|
if (th) {
|
|
const msgs = s.msgs[th[2]!];
|
|
if (!msgs) return { status: 404, body: { error: "no such conversation" } };
|
|
if (r.method === "POST") return { status: 200, body: { hash: "x" } };
|
|
if (r.method === "DELETE") return { status: 204 };
|
|
return { status: 200, body: msgs };
|
|
}
|
|
if (/\/address$/.test(r.path)) {
|
|
if (r.method === "PUT") { s.address = JSON.parse(r.body) as Address; return { status: 204 }; }
|
|
if (r.method === "DELETE") { s.address = null; return { status: 204 }; }
|
|
return s.address ? { status: 200, body: s.address } : { status: 404, body: { error: "no address saved for this listing" } };
|
|
}
|
|
return undefined;
|
|
};
|
|
return { s, handler };
|
|
}
|
|
|
|
let gw: Gateway;
|
|
let node: ReturnType<typeof fakeNode>;
|
|
let page: Page | undefined;
|
|
|
|
beforeEach(async () => {
|
|
gw = await startGateway({ listings: [] });
|
|
node = fakeNode();
|
|
gw.owner = node.handler;
|
|
});
|
|
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 q = <T extends Element = HTMLElement>(sel: string) => page!.document.querySelector<T>(sel);
|
|
const qa = (sel: string) => [...page!.document.querySelectorAll(sel)];
|
|
const button = (label: string) => qa("button").find((b) => b.textContent === label) as HTMLButtonElement | undefined;
|
|
const submit = (form: Element) => form.dispatchEvent(new page!.window.Event("submit", { cancelable: true }));
|
|
const posts = () => node.s.calls.filter((c) => c.method === "POST");
|
|
|
|
test("without a token the page asks for one, and a wrong token is refused", async () => {
|
|
open("#/mine");
|
|
await page!.waitFor(() => q('input[name="token"]') !== null, "the sign-in form");
|
|
assert.equal(node.s.calls.length, 0, "nothing is requested before signing in");
|
|
assert.equal(q("nav a[href='#/mine']"), null, "the owner link is hidden until signed in");
|
|
|
|
q<HTMLInputElement>('input[name="token"]')!.value = "not-the-token";
|
|
submit(q("form")!);
|
|
await page!.waitFor(() => /rejected the token/.test(text()), "a rejection notice");
|
|
assert.equal(page!.window.sessionStorage.getItem("allmende.owner.token"), null, "a refused token is forgotten");
|
|
|
|
q<HTMLInputElement>('input[name="token"]')!.value = TOKEN;
|
|
submit(q("form")!);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
assert.ok(node.s.calls.every((c) => c.auth === "Bearer not-the-token" || c.auth === `Bearer ${TOKEN}`));
|
|
});
|
|
|
|
test("the login link signs in and disappears from the address bar", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
assert.equal(page!.window.location.hash, "#/mine", "the token is not left in the URL");
|
|
assert.equal(page!.window.sessionStorage.getItem("allmende.owner.token"), TOKEN);
|
|
assert.equal(page!.window.localStorage.getItem("allmende.owner.token"), null, "never in localStorage");
|
|
assert.ok(q("nav a[href='#/mine']"), "the owner link appears");
|
|
assert.ok(node.s.calls.every((c) => c.auth === `Bearer ${TOKEN}`));
|
|
|
|
const [first, second] = qa(".pane-listings .ocard");
|
|
assert.match(first!.textContent!, /Helle Wohnung/);
|
|
assert.match(first!.textContent!, /1 need a reply/);
|
|
assert.match(first!.textContent!, /Address saved/);
|
|
assert.match(first!.textContent!, /1,150\.00/);
|
|
assert.match(second!.textContent!, /No requests/);
|
|
assert.match(second!.textContent!, /No address saved/);
|
|
});
|
|
|
|
test("a listing's requests show status, preview and effort, and can be filtered", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}`;
|
|
await page!.waitFor(() => qa(".pane-requests .olist .ocard").length === 3, "three requests");
|
|
|
|
const rows = qa(".pane-requests .olist .ocard");
|
|
const [neu, link, grant] = rows.map((r) => r.textContent!);
|
|
assert.match(neu!, /#a3f9/);
|
|
assert.match(neu!, /Needs reply/);
|
|
assert.match(neu!, /Effort 19 bit/);
|
|
assert.match(link!, /#5b10/);
|
|
assert.match(link!, /Link hidden/);
|
|
assert.match(link!, /Hidden: contains a link/);
|
|
assert.match(grant!, /#e204/);
|
|
assert.match(grant!, /Address until/);
|
|
assert.match(text(), /Musterweg 7, 22765 Hamburg/, "the saved address is shown");
|
|
|
|
button("Needs reply 1")!.click();
|
|
assert.equal(qa(".pane-requests .olist .ocard").length, 1);
|
|
button("Link hidden 1")!.click();
|
|
assert.equal(qa(".pane-requests .olist .ocard").length, 1);
|
|
assert.match(qa(".pane-requests .olist .ocard")[0]!.textContent!, /#5b10/);
|
|
button("All 3")!.click();
|
|
assert.equal(qa(".pane-requests .olist .ocard").length, 3);
|
|
assert.equal(button("All 3")!.getAttribute("aria-pressed"), "true");
|
|
});
|
|
|
|
test("buyer text is shown as text only, and the first-message note disappears once you replied", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
|
|
assert.equal(q(".pane-thread .msg img"), null, "no element is created from buyer text");
|
|
assert.equal((page!.window as unknown as { __pwned?: number }).__pwned, undefined);
|
|
assert.ok(q(".pane-thread .msg .pre")!.textContent!.includes("<img src=x"), "the markup is visible as text");
|
|
assert.equal(q<HTMLElement>(".pane-thread .note")!.hidden, false, "first message: the rules are explained");
|
|
assert.match(text(), /Request #a3f9/);
|
|
|
|
// An answered conversation does not show that note.
|
|
page!.window.location.hash = `#/mine/${L1}/${T_GRANT}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 2, "the second conversation");
|
|
assert.equal(q<HTMLElement>(".pane-thread .note")!.hidden, true);
|
|
assert.match(q(".pane-thread .grant")!.textContent!, /Address released until/);
|
|
assert.match(q(".pane-thread .grant")!.textContent!, /Klingel: Meyer/);
|
|
|
|
// A hidden message never shows its text, only why.
|
|
page!.window.location.hash = `#/mine/${L1}/${T_LINK}`;
|
|
await page!.waitFor(() => /Hidden: a first message must be short/.test(text()), "hidden explanation");
|
|
});
|
|
|
|
test("a reply is sent as text and the box is cleared", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
|
|
const area = q<HTMLTextAreaElement>(".pane-thread textarea")!;
|
|
area.value = "Ja, noch frei. Besichtigung am Samstag?";
|
|
submit(q(".pane-thread form.compose")!);
|
|
await page!.waitFor(() => posts().length === 1, "the reply");
|
|
assert.equal(posts()[0]!.path, `/api/v1/inbox/${L1}/${T_NEW}`);
|
|
assert.deepEqual(JSON.parse(posts()[0]!.body), { type: "text", text: "Ja, noch frei. Besichtigung am Samstag?" });
|
|
await page!.waitFor(() => area.value === "", "the box to clear");
|
|
|
|
// Empty text is not sent.
|
|
submit(q(".pane-thread form.compose")!);
|
|
await new Promise((r) => setTimeout(r, 50));
|
|
assert.equal(posts().length, 1);
|
|
});
|
|
|
|
test("releasing the address: prefilled sheet, signed grant request, closes", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
|
|
button("Release address")!.click();
|
|
await page!.waitFor(() => q('.sheet input[name="address"]') !== null, "the sheet");
|
|
await page!.waitFor(() => q<HTMLInputElement>('.sheet input[name="address"]')!.value !== "", "prefill from the saved address");
|
|
assert.equal(q<HTMLInputElement>('.sheet input[name="address"]')!.value, "Musterweg 7, 22765 Hamburg");
|
|
assert.equal(q<HTMLInputElement>('.sheet input[name="phone"]')!.value, "+49 40 1");
|
|
assert.equal(q<HTMLSelectElement>('.sheet select[name="valid"]')!.value, "7", "a week by default");
|
|
assert.equal(q<HTMLInputElement>('.sheet input[name="remember"]')!.checked, false, "already saved: not offered again");
|
|
assert.match(q(".sheet")!.textContent!, /could still have copied it/);
|
|
|
|
q<HTMLTextAreaElement>('.sheet textarea[name="note"]')!.value = "Samstag 10 Uhr, bitte zweimal klingeln";
|
|
q<HTMLSelectElement>('.sheet select[name="valid"]')!.value = "3";
|
|
submit(q(".sheet form")!);
|
|
await page!.waitFor(() => posts().length === 1, "the grant");
|
|
assert.deepEqual(JSON.parse(posts()[0]!.body), {
|
|
type: "grant",
|
|
address: "Musterweg 7, 22765 Hamburg",
|
|
phone: "+49 40 1",
|
|
note: "Samstag 10 Uhr, bitte zweimal klingeln",
|
|
valid_days: 3,
|
|
});
|
|
await page!.waitFor(() => q(".sheet") === null, "the sheet to close");
|
|
assert.equal(node.s.calls.filter((c) => c.method === "PUT").length, 0, "an unchanged address is not saved again");
|
|
});
|
|
|
|
test("releasing without a saved address offers to keep it, and refuses an empty one", async () => {
|
|
node.s.address = null;
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
|
|
button("Release address")!.click();
|
|
await page!.waitFor(() => q<HTMLInputElement>('.sheet input[name="remember"]')?.checked === true, "remember is on by default");
|
|
|
|
submit(q(".sheet form")!);
|
|
await page!.waitFor(() => /enter an address/.test(text()), "a refusal");
|
|
assert.equal(posts().length, 0);
|
|
|
|
q<HTMLInputElement>('.sheet input[name="address"]')!.value = "Neue Straße 3, 20095 Hamburg";
|
|
submit(q(".sheet form")!);
|
|
await page!.waitFor(() => posts().length === 1, "the grant");
|
|
const put = node.s.calls.find((c) => c.method === "PUT")!;
|
|
assert.deepEqual(JSON.parse(put.body), { address: "Neue Straße 3, 20095 Hamburg" });
|
|
assert.deepEqual(node.s.address, { address: "Neue Straße 3, 20095 Hamburg" });
|
|
assert.equal(JSON.parse(posts()[0]!.body).valid_days, 7);
|
|
});
|
|
|
|
test("the sheet closes with Escape and with the backdrop, and sends nothing", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
|
|
button("Release address")!.click();
|
|
await page!.waitFor(() => q(".sheet") !== null, "the sheet");
|
|
q<HTMLElement>(".sheet input")!.dispatchEvent(new page!.window.KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
|
|
assert.equal(q(".sheet"), null);
|
|
|
|
button("Release address")!.click();
|
|
await page!.waitFor(() => q(".sheet") !== null, "the sheet again");
|
|
q<HTMLElement>(".backdrop")!.dispatchEvent(new page!.window.MouseEvent("click", { bubbles: true }));
|
|
assert.equal(q(".sheet"), null);
|
|
assert.equal(posts().length, 0);
|
|
});
|
|
|
|
test("the saved address can be changed and removed", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}`;
|
|
await page!.waitFor(() => /Musterweg 7/.test(text()), "the saved address");
|
|
|
|
button("Change")!.click();
|
|
const form = q(".addr form")!;
|
|
q<HTMLInputElement>('.addr input[name="address"]')!.value = "Hafenstraße 9, 20459 Hamburg";
|
|
q<HTMLInputElement>('.addr input[name="phone"]')!.value = "";
|
|
submit(form);
|
|
await page!.waitFor(() => /Hafenstraße 9/.test(q(".addr")!.textContent!), "the new address");
|
|
assert.deepEqual(node.s.address, { address: "Hafenstraße 9, 20459 Hamburg" });
|
|
|
|
button("Change")!.click();
|
|
button("Remove")!.click();
|
|
await page!.waitFor(() => /No address saved yet/.test(text()), "the address removed");
|
|
assert.equal(node.s.address, null);
|
|
});
|
|
|
|
test("deleting a conversation asks first, then goes back to the requests", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_LINK}`;
|
|
await page!.waitFor(() => button("Delete conversation") !== undefined, "the delete button");
|
|
|
|
button("Delete conversation")!.click();
|
|
assert.equal(node.s.calls.filter((c) => c.method === "DELETE").length, 0, "nothing is deleted by one click");
|
|
button("Yes, delete")!.click();
|
|
await page!.waitFor(() => page!.window.location.hash === `#/mine/${L1}`, "back to the requests");
|
|
assert.equal(node.s.calls.filter((c) => c.method === "DELETE")[0]!.path, `/api/v1/inbox/${L1}/${T_LINK}`);
|
|
});
|
|
|
|
test("a token that stops working sends the owner back to sign in", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
|
|
node.s.token = "rotated"; // e.g. after `recover`
|
|
q<HTMLTextAreaElement>(".pane-thread textarea")!.value = "Hallo";
|
|
submit(q(".pane-thread form.compose")!);
|
|
await page!.waitFor(() => /rejected the token/.test(text()), "a rejection");
|
|
assert.equal(page!.window.sessionStorage.getItem("allmende.owner.token"), null);
|
|
});
|
|
|
|
test("the sheet offers an email, prefilled from the saved address, and sends it", async () => {
|
|
node.s.address = { address: "Musterweg 7, 22765 Hamburg", phone: "+49 40 1", email: "vermietung@example.org" };
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
|
|
button("Release address")!.click();
|
|
await page!.waitFor(() => q<HTMLInputElement>('.sheet input[name="email"]')?.value === "vermietung@example.org", "email prefill");
|
|
submit(q(".sheet form")!);
|
|
await page!.waitFor(() => posts().length === 1, "the grant");
|
|
assert.equal(JSON.parse(posts()[0]!.body).email, "vermietung@example.org");
|
|
|
|
// Cleared, it is not sent at all.
|
|
button("Release address")!.click();
|
|
await page!.waitFor(() => q<HTMLInputElement>('.sheet input[name="email"]')?.value === "vermietung@example.org", "prefill again");
|
|
q<HTMLInputElement>('.sheet input[name="email"]')!.value = "";
|
|
submit(q(".sheet form")!);
|
|
await page!.waitFor(() => posts().length === 2, "the second grant");
|
|
assert.equal("email" in JSON.parse(posts()[1]!.body), false);
|
|
});
|
|
|
|
test("a buyer asking where the place is gets a suggestion to release the address", async () => {
|
|
const ask = async (text: string, dir: "in" | "out" = "in", hidden = false) => {
|
|
node.s.msgs[T_NEW] = [{ hash: "q", dir, hidden, received_at: NOW, type: "text", n: 1, sent_at: NOW - 60, text }];
|
|
page?.close();
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
page!.window.location.hash = `#/mine/${L1}/${T_NEW}`;
|
|
await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation");
|
|
return q<HTMLElement>(".pane-thread .hint")!.hidden;
|
|
};
|
|
assert.equal(await ask("Sehr gerne! Wo genau ist die Wohnung?"), false, "German question");
|
|
assert.equal(await ask("Could you send me the address please?"), false, "English request");
|
|
assert.equal(await ask("Ist die Wohnung noch frei?"), true, "an unrelated question");
|
|
assert.equal(await ask("Wo genau ist die Wohnung?", "out"), true, "only the buyer's words count");
|
|
assert.equal(await ask("Wie ist die Adresse?", "in", true), true, "a hidden message is never read");
|
|
});
|
|
|
|
test("signing out forgets the token", async () => {
|
|
open(`#/login/${TOKEN}`);
|
|
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
|
button("Sign out")!.click();
|
|
assert.equal(page!.window.sessionStorage.getItem("allmende.owner.token"), null);
|
|
await page!.waitFor(() => q("nav a[href='#/mine']") === null, "the owner link to vanish");
|
|
});
|