Publish, edit, renew and delete listings from the web UI

- Form at #/mine/new and #/mine/<id>/edit: coordinates pasted and encoded
  to a precision-6 geohash in the browser (about 1 km, OSM link to check, no
  map tiles), euro price parsing, optional exact address saved on the node only
- Region guard: refuses places the node does not follow; region fixed on edit
- Owner view: days left with a warning under 5 days, manual Renew (30 days),
  Edit, Delete with confirmation
- Tests (jsdom and pure logic), docs, rebuilt web/dist

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 20:18:08 +01:00
co-authored by Claude Sonnet 5.5
parent 7348067350
commit d021989f09
16 changed files with 1846 additions and 87 deletions
+333
View File
@@ -0,0 +1,333 @@
import assert from "node:assert/strict";
import { afterEach, beforeEach, test } from "node:test";
import { encodeGeohash } from "../src/geohash.ts";
import type { Address, Mine } from "../src/ownerApi.ts";
import { NOW, openPage, startGateway, type Gateway, type Page } from "./helpers.ts";
const TOKEN = "cd34".repeat(16);
const AUTHOR = "a".repeat(64); // the stub gateway's /status reports this author
const NEW_ID = "33".repeat(16);
const L1 = "11".repeat(16);
const HAMBURG = "53.5511, 9.9937";
const HAMBURG_CELL = encodeGeohash(53.5511, 9.9937);
/** A listing of this node, as the public API serves it. */
const own = (over: Record<string, unknown> = {}) => ({
v: 1, id: L1, author: AUTHOR, device: "b".repeat(64), seq: 3, created_at: NOW - 86400, expires_at: NOW + 3 * 86400,
kind: "rent", category: "apartment", geohash: HAMBURG_CELL,
price: { amount_cents: 115000, currency: "EUR", period: "month" },
area_m2: 62, rooms_x2: 5, features: ["Balkon", "Aufzug"], title: "Helle Wohnung", description: "Ruhig.\nSüdseite.",
pow: 1, cert: {}, sig: "", ...over,
});
function fakeNode() {
const s = {
calls: [] as { method: string; path: string; body: string; auth: string }[],
address: null as Address | null,
fail: null as { status: number; error: string } | null,
mine: [] as Mine[],
};
const handler: NonNullable<Gateway["owner"]> = (r) => {
s.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.method === "POST" && r.path === "/api/v1/listings") {
if (s.fail) return { status: s.fail.status, body: { error: s.fail.error } };
return { status: 200, body: own({ id: NEW_ID, ...JSON.parse(r.body) }) };
}
if (r.method === "PUT" && /^\/api\/v1\/listings\/[0-9a-f]{32}$/.test(r.path)) {
if (s.fail) return { status: s.fail.status, body: { error: s.fail.error } };
return { status: 200, body: own({ ...JSON.parse(r.body) }) };
}
if (r.method === "DELETE" && /^\/api\/v1\/listings\/[0-9a-f]{32}$/.test(r.path)) return { status: 200, body: {} };
if (r.path === "/api/v1/mine") return { status: 200, body: s.mine };
if (r.path === "/api/v1/inbox") return { status: 200, body: [] };
if (/\/address$/.test(r.path)) {
if (r.method === "PUT") { s.address = JSON.parse(r.body) as Address; return { status: 204 }; }
return s.address ? { status: 200, body: s.address } : { status: 404, body: { error: "none" } };
}
return undefined;
};
return { s, handler };
}
let gw: Gateway;
let node: ReturnType<typeof fakeNode>;
let page: Page | undefined;
beforeEach(async () => {
gw = await startGateway({ listings: [own()] });
node = fakeNode();
gw.owner = node.handler;
node.s.mine = [
{ id: L1, title: "Helle Wohnung", kind: "rent", price: { amount_cents: 115000, currency: "EUR", period: "month" }, expires_at: NOW + 3 * 86400, threads: 0, needs_reply: 0, hidden: 0, address_saved: false },
];
});
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 input = (name: string) => q<HTMLInputElement>(`[name="${name}"]`)!;
const button = (label: string) => qa("button").find((b) => b.textContent === label) as HTMLButtonElement | undefined;
const submit = () => q("form.form")!.dispatchEvent(new page!.window.Event("submit", { cancelable: true }));
const writes = (method: string) => node.s.calls.filter((c) => c.method === method && c.path.startsWith("/api/v1/listings") && !c.path.endsWith("/address"));
const set = (name: string, value: string) => { input(name).value = value; input(name).dispatchEvent(new page!.window.Event("input", { bubbles: true })); };
/** Signs in with the login link and opens the new-listing form. */
async function newForm(): Promise<void> {
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
page!.window.location.hash = "#/mine/new";
await page!.waitFor(() => q("form.form") !== null, "the form");
}
function fillValid(): void {
set("title", "Helle 2,5-Zimmer-Wohnung – Altbau");
set("description", "Ruhige Altbauwohnung.\nBezug ab 1. Februar.");
set("price", "1.150,50");
set("area", "62");
set("rooms", "2,5");
set("features", "Balkon, Aufzug, Balkon ,");
set("location", HAMBURG);
}
test("signed out, the form asks to sign in; signed in, the listings offer a New listing button", async () => {
open("#/mine/new");
await page!.waitFor(() => q('input[name="token"]') !== null, "the sign-in form");
assert.equal(q("form.form"), null);
page!.close();
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
assert.equal(q<HTMLAnchorElement>(".pane-listings a.btn.primary")!.getAttribute("href"), "#/mine/new");
});
test("a valid form publishes the draft, saves the address, and opens the listing", async () => {
await newForm();
assert.match(text(), /Publish a listing/);
fillValid();
set("address", "Musterweg 7, 22765 Hamburg");
set("phone", "+49 40 1234567");
submit();
await page!.waitFor(() => writes("POST").length === 1, "the listing to be sent");
const post = writes("POST")[0]!;
assert.equal(post.auth, `Bearer ${TOKEN}`);
assert.deepEqual(JSON.parse(post.body), {
kind: "rent",
category: "apartment",
geohash: HAMBURG_CELL,
price: { amount_cents: 115050, currency: "EUR", period: "month" },
area_m2: 62,
rooms_x2: 5,
features: ["Balkon", "Aufzug"],
title: "Helle 2,5-Zimmer-Wohnung – Altbau",
description: "Ruhige Altbauwohnung.\nBezug ab 1. Februar.",
expires_in_days: 30,
});
await page!.waitFor(() => page!.window.location.hash === `#/mine/${NEW_ID}`, "navigation to the new listing");
assert.deepEqual(node.s.address, { address: "Musterweg 7, 22765 Hamburg", phone: "+49 40 1234567" });
assert.ok(!post.body.includes("Musterweg"), "the exact address never goes into the listing");
});
test("optional parts may be left empty, and a sale is priced once", async () => {
await newForm();
set("title", "Reihenhaus");
set("price", "485000");
set("location", HAMBURG);
q<HTMLInputElement>('input[name="kind"][value="sale"]')!.checked = true;
q('input[name="kind"][value="sale"]')!.dispatchEvent(new page!.window.Event("change", { bubbles: true }));
q<HTMLSelectElement>('select[name="category"]')!.value = "house";
q<HTMLSelectElement>('select[name="days"]')!.value = "14";
assert.match(q("form.form")!.textContent!, /Price \(€\)/, "the label follows the offer");
submit();
await page!.waitFor(() => writes("POST").length === 1, "the listing");
assert.deepEqual(JSON.parse(writes("POST")[0]!.body), {
kind: "sale", category: "house", geohash: HAMBURG_CELL,
price: { amount_cents: 48500000, currency: "EUR", period: "once" },
area_m2: 0, rooms_x2: 0, features: [], title: "Reihenhaus", description: "", expires_in_days: 14,
});
await page!.waitFor(() => page!.window.location.hash === `#/mine/${NEW_ID}`, "navigation to the new listing");
assert.equal(node.s.calls.filter((c) => c.method === "PUT" && c.path.endsWith("/address")).length, 0, "no address, nothing saved");
});
test("every problem is explained next to its field, and nothing is sent", async () => {
await newForm();
submit();
await page!.waitFor(() => qa(".form .error").some((e) => e.textContent), "errors");
const msgs = () => qa(".form .field .error").map((e) => e.textContent).filter(Boolean);
assert.equal(msgs().length, 3, "title, price and location are required");
assert.match(text(), /enter a title/);
assert.match(text(), /amount above 0/);
assert.match(text(), /coordinates could not be read/);
assert.equal(input("title").getAttribute("aria-invalid"), "true");
assert.equal(writes("POST").length, 0);
set("title", "x".repeat(121));
set("price", "abc");
set("area", "6x");
set("rooms", "2.3");
set("features", Array.from({ length: 21 }, (_, i) => `f${i}`).join(","));
set("description", "a".repeat(4001));
set("location", "Hamburg");
submit();
assert.equal(msgs().length, 7);
assert.match(text(), /whole number/);
assert.match(text(), /half steps/);
assert.match(text(), /At most 20 features/);
assert.match(text(), /description is too long/);
assert.equal(writes("POST").length, 0);
fillValid(); // fixing the fields clears the messages
submit();
await page!.waitFor(() => writes("POST").length === 1, "the listing once it is valid");
assert.equal(msgs().length, 0);
});
test("a place in a region this node does not follow is refused, with the way out", async () => {
await newForm();
fillValid();
set("location", "48.1371, 11.5754"); // Munich
submit();
await page!.waitFor(() => /does not follow region/.test(text()), "the region message");
assert.match(text(), /--region u28/);
assert.match(text(), /choose a place in u1x/);
assert.equal(writes("POST").length, 0);
});
test("the location shows what will be published, and accepts a map link", async () => {
await newForm();
set("location", "https://www.openstreetmap.org/#map=17/53.5511/9.9937");
const preview = () => qa(".form p.muted.small").map((p) => p.textContent).join("|");
assert.match(preview(), new RegExp(`code ${HAMBURG_CELL}`));
assert.match(preview(), /about \d+ × \d+ m/);
const a = q<HTMLAnchorElement>('.form a[href^="https://www.openstreetmap.org/"]')!;
assert.match(a.getAttribute("rel")!, /noopener/);
assert.equal(a.getAttribute("target"), "_blank");
fillValid();
set("location", "https://www.openstreetmap.org/#map=17/53.5511/9.9937");
set("title", "Mit Link eingefügt");
submit();
await page!.waitFor(() => writes("POST").length === 1, "the listing");
assert.equal(JSON.parse(writes("POST")[0]!.body).geohash, HAMBURG_CELL);
});
test("a refusal from the node is shown and the form keeps what was typed", async () => {
await newForm();
fillValid();
node.s.fail = { status: 400, error: "price out of range" };
submit();
await page!.waitFor(() => /Failed: price out of range/.test(text()), "the node's message");
assert.equal(input("title").value, "Helle 2,5-Zimmer-Wohnung – Altbau");
assert.equal(q<HTMLButtonElement>('button[type="submit"]')!.disabled, false, "it can be tried again");
assert.equal(page!.window.location.hash, "#/mine/new");
node.s.fail = null;
submit();
await page!.waitFor(() => page!.window.location.hash === `#/mine/${NEW_ID}`, "a second try works");
});
test("changing a listing starts from what is published, and re-publishes it", async () => {
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
page!.window.location.hash = `#/mine/${L1}/edit`;
await page!.waitFor(() => q("form.form") !== null, "the form");
assert.match(text(), /Edit listing/);
assert.equal(input("title").value, "Helle Wohnung");
assert.equal(input("price").value, "1150");
assert.equal(input("area").value, "62");
assert.equal(input("rooms").value, "2.5");
assert.equal(input("features").value, "Balkon, Aufzug");
assert.match(q<HTMLTextAreaElement>('textarea[name="description"]')!.value, /Ruhig\.\nSüdseite\./);
assert.match(input("location").value, /^53\.\d+, 9\.\d+$/);
assert.equal(q('input[name="address"]'), null, "the address is edited where requests are handled");
set("title", "Helle Wohnung, jetzt renoviert");
set("price", "1200");
submit();
await page!.waitFor(() => writes("PUT").length === 1, "the new version");
const put = writes("PUT")[0]!;
assert.equal(put.path, `/api/v1/listings/${L1}`);
const body = JSON.parse(put.body);
assert.equal(body.title, "Helle Wohnung, jetzt renoviert");
assert.equal(body.price.amount_cents, 120000);
assert.equal(body.geohash, HAMBURG_CELL, "the unchanged location keeps its cell");
assert.deepEqual(body.features, ["Balkon", "Aufzug"]);
await page!.waitFor(() => page!.window.location.hash === `#/mine/${L1}`, "back to the listing");
});
test("the region of an existing listing cannot be changed", async () => {
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
page!.window.location.hash = `#/mine/${L1}/edit`;
await page!.waitFor(() => q("form.form") !== null, "the form");
set("location", "52.52, 13.405"); // Berlin: another region
submit();
await page!.waitFor(() => /region cannot change/.test(text()), "the region message");
assert.equal(writes("PUT").length, 0);
});
test("a listing that does not exist cannot be edited", async () => {
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
page!.window.location.hash = `#/mine/${"99".repeat(16)}/edit`;
await page!.waitFor(() => /does not exist/.test(text()), "the not-found notice");
assert.equal(q("form.form"), null);
});
test("a listing that expires soon is flagged, and Renew publishes it again for 30 days", async () => {
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
assert.match(qa(".pane-listings .ocard")[0]!.textContent!, /3 days left/);
assert.ok(qa(".pane-listings .ocard .badge.warn").some((b) => /days left/.test(b.textContent!)), "flagged as running out");
page!.window.location.hash = `#/mine/${L1}`;
await page!.waitFor(() => button("Renew") !== undefined, "the Renew button");
assert.match(text(), /Expires soon/);
button("Renew")!.click();
await page!.waitFor(() => writes("PUT").length === 1, "the renewal");
const body = JSON.parse(writes("PUT")[0]!.body);
assert.equal(body.expires_in_days, 30);
assert.equal(body.title, "Helle Wohnung", "nothing else changes");
assert.equal(body.geohash, HAMBURG_CELL);
assert.deepEqual(body.features, ["Balkon", "Aufzug"]);
assert.equal(body.price.period, "month");
});
test("a comfortable listing is not nagged about, and an expired one says so", async () => {
node.s.mine = [{ ...node.s.mine[0]!, expires_at: NOW + 20 * 86400 }];
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
assert.match(qa(".pane-listings .ocard")[0]!.textContent!, /20 days left/);
page!.window.location.hash = `#/mine/${L1}`;
await page!.waitFor(() => button("Renew") !== undefined, "the bar");
assert.doesNotMatch(text(), /Expires soon/);
page!.close();
node.s.mine = [{ ...node.s.mine[0]!, expires_at: NOW - 10 }];
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings again");
assert.match(qa(".pane-listings .ocard")[0]!.textContent!, /Expired/);
});
test("deleting a listing asks first, then withdraws it", async () => {
open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 1, "the listings");
page!.window.location.hash = `#/mine/${L1}`;
await page!.waitFor(() => button("Delete listing") !== undefined, "the delete button");
button("Delete listing")!.click();
assert.match(text(), /people can no longer reach you/);
assert.equal(writes("DELETE").length, 0, "one click deletes nothing");
button("Yes, delete")!.click();
await page!.waitFor(() => writes("DELETE").length === 1, "the deletion");
assert.equal(writes("DELETE")[0]!.path, `/api/v1/listings/${L1}`);
await page!.waitFor(() => page!.window.location.hash === "#/mine", "back to the listings");
});