Let the owner change regions by place name, without a restart
The p2p node can now add and drop regions while running (SetRegions): topics, validators, subscriptions and per-region peer discovery start and stop with them, and a newly followed region catches up by sync. PUT /api/v1/regions (owner token) applies a change and the node saves it in regions.json, which wins over --region from then on. The owner's browser gets a "Regions" page: search a place by name (GeoNames cities, offline, CC BY 4.0), follow its cell plus any cell within about 40 km. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
+6
-1
@@ -211,9 +211,14 @@ export async function startGateway(opts: GatewayOptions): Promise<Gateway> {
|
||||
res.writeHead(code, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify(body));
|
||||
};
|
||||
if (url.pathname === "/places.tsv") {
|
||||
// A static file of the UI, as the real node serves it.
|
||||
res.writeHead(200, { "Content-Type": "text/tab-separated-values" });
|
||||
return void res.end(readFileSync(new URL("../static/places.tsv", import.meta.url)));
|
||||
}
|
||||
// The owner's token-protected routes are played by the test, if it sets `owner`.
|
||||
const writesListing = req.method !== "GET" && /^\/api\/v1\/listings(\/[0-9a-f]{32})?$/.test(url.pathname);
|
||||
if (writesListing || /^\/api\/v1\/(mine|inbox)|^\/api\/v1\/listings\/[0-9a-f]{32}\/address$/.test(url.pathname)) {
|
||||
if (writesListing || /^\/api\/v1\/(mine|inbox|regions)|^\/api\/v1\/listings\/[0-9a-f]{32}\/address$/.test(url.pathname)) {
|
||||
let body = "";
|
||||
req.on("data", (c) => (body += c));
|
||||
req.on("end", () => {
|
||||
|
||||
@@ -41,6 +41,11 @@ function fakeNode() {
|
||||
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/regions" && r.method === "PUT") {
|
||||
const { regions } = JSON.parse(r.body) as { regions: string[] };
|
||||
if (regions.includes("zzz")) return { status: 400, body: { error: `"zzz" is not a region: use 3 geohash characters` } };
|
||||
return { status: 200, body: { regions } };
|
||||
}
|
||||
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) {
|
||||
@@ -383,3 +388,52 @@ test("signing out forgets the token", async () => {
|
||||
assert.equal(page!.window.sessionStorage.getItem("allmende.owner.token"), null);
|
||||
await page!.waitFor(() => q("nav a[href='#/mine']") === null, "the owner link to vanish");
|
||||
});
|
||||
|
||||
test("the owner picks regions by place name and the node follows them at once", async () => {
|
||||
open(`#/login/${TOKEN}`);
|
||||
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
||||
qa(".pane-listings a").find((a) => a.textContent === "Regions")!.click();
|
||||
await page!.waitFor(() => q('[name="place"]') !== null, "the regions page");
|
||||
assert.ok(q(".rp-chips")!.textContent!.includes("u1x"), "what the node follows now");
|
||||
const search = q<HTMLInputElement>('[name="place"]')!;
|
||||
await page!.waitFor(() => !search.disabled, "the place list is loaded");
|
||||
|
||||
search.value = "köln";
|
||||
search.dispatchEvent(new page!.window.Event("input", { bubbles: true }));
|
||||
await page!.waitFor(() => q(".rp-result") !== null, "a result");
|
||||
q(".rp-result")!.click();
|
||||
await page!.waitFor(() => /Regions around/.test(text()), "the cells around it");
|
||||
button("Add")!.click();
|
||||
assert.ok(qa(".rp-chip").length >= 2, "the new regions are added next to the old one");
|
||||
|
||||
submit(q("form")!);
|
||||
await page!.waitFor(() => /Saved/.test(text()), "saved");
|
||||
const put = node.s.calls.find((c) => c.method === "PUT" && c.path === "/api/v1/regions")!;
|
||||
assert.equal(put.auth, `Bearer ${TOKEN}`, "only the owner can change regions");
|
||||
const sent = (JSON.parse(put.body) as { regions: string[] }).regions;
|
||||
assert.equal(sent[0], "u1x");
|
||||
assert.ok(sent.includes("u1j"), `Köln's own cell is followed: ${sent}`);
|
||||
assert.ok(q("#status")!.textContent!.includes(sent.join(", ")), "the status line shows the new regions");
|
||||
});
|
||||
|
||||
test("a region the node refuses is explained and the page stays usable", async () => {
|
||||
open(`#/login/${TOKEN}`);
|
||||
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");
|
||||
location_(`#/mine/regions`);
|
||||
await page!.waitFor(() => q('[name="regions"]') !== null, "the regions page");
|
||||
q<HTMLInputElement>('[name="regions"]')!.value = "zzz";
|
||||
submit(q("form")!);
|
||||
await page!.waitFor(() => /not a region/.test(text()), "the node's explanation");
|
||||
assert.ok(!button("Save")?.disabled, "saving can be tried again");
|
||||
});
|
||||
|
||||
test("without signing in the regions page leads to the sign-in", async () => {
|
||||
open("#/mine/regions");
|
||||
await page!.waitFor(() => q('input[name="token"]') !== null, "the sign-in form");
|
||||
assert.equal(node.s.calls.length, 0);
|
||||
});
|
||||
|
||||
/** Moves the open page to another hash, as a click on a link would. */
|
||||
function location_(hash: string): void {
|
||||
page!.window.location.hash = hash;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { test } from "node:test";
|
||||
|
||||
import { decodeGeohash, encodeGeohash } from "../src/geohash.ts";
|
||||
import { biggestPlaceByCell, cellsNear, fold, parsePlaces, searchPlaces } from "../src/places.ts";
|
||||
|
||||
// The real list the app ships.
|
||||
const places = parsePlaces(readFileSync(new URL("../static/places.tsv", import.meta.url), "utf8"));
|
||||
const first = (q: string) => searchPlaces(places, q, 1)[0];
|
||||
|
||||
test("the list is worldwide and big enough", () => {
|
||||
assert.ok(places.length > 30_000);
|
||||
});
|
||||
|
||||
test("names are found however they are spelled", () => {
|
||||
assert.equal(first("Bonn")?.place.name, "Bonn");
|
||||
assert.equal(first("bonn")?.place.name, "Bonn");
|
||||
assert.equal(first("köln")?.place.name, "Köln");
|
||||
assert.equal(first("koeln")?.place.name, "Köln");
|
||||
assert.equal(first("KOELN")?.place.name, "Köln");
|
||||
assert.equal(first("zurich")?.place.name, "Zürich");
|
||||
assert.equal(first("Zürich")?.place.name, "Zürich");
|
||||
assert.equal(first("Frankfurt am Main")?.place.cc, "DE");
|
||||
});
|
||||
|
||||
test("other languages' names lead to the place, and say what matched", () => {
|
||||
const c = first("cologne");
|
||||
assert.equal(c?.place.name, "Köln");
|
||||
assert.equal(c?.matched, "Cologne");
|
||||
assert.equal(first("münchen")?.place.cc, "DE", "Munich, found by its German name");
|
||||
assert.equal(first("muenchen")?.place.cc, "DE");
|
||||
assert.equal(first("nuernberg")?.place.cc, "DE");
|
||||
assert.equal(first("prag")?.place.cc, "CZ");
|
||||
assert.equal(first("wien")?.place.cc, "AT");
|
||||
assert.equal(first("Köln")?.matched, null, "the main name is not repeated");
|
||||
});
|
||||
|
||||
test("the big city wins over a small namesake, and prefixes work", () => {
|
||||
const vienna = searchPlaces(places, "vienna", 5);
|
||||
assert.equal(vienna[0]!.place.cc, "AT");
|
||||
assert.ok(vienna.some((m) => m.place.cc === "US"), "the others are still offered");
|
||||
const ham = searchPlaces(places, "ham", 8).map((m) => m.place.name);
|
||||
assert.ok(ham.includes("Hamburg") || ham.includes("Hamm"), ham.join());
|
||||
assert.deepEqual(searchPlaces(places, "b"), [], "one letter is too little to search");
|
||||
assert.deepEqual(searchPlaces(places, "qqqzzzxxx"), []);
|
||||
assert.deepEqual(searchPlaces(places, " "), []);
|
||||
});
|
||||
|
||||
test("folding ignores case, accents and ß", () => {
|
||||
assert.equal(fold("Zürich"), "zurich");
|
||||
assert.equal(fold("Straße"), "strasse");
|
||||
assert.equal(fold("ÅRHUS"), "arhus");
|
||||
});
|
||||
|
||||
test("a place gives the cell it lies in first", () => {
|
||||
const bonn = first("Bonn")!.place;
|
||||
const cells = cellsNear(bonn.lat, bonn.lon);
|
||||
assert.equal(cells[0], encodeGeohash(bonn.lat, bonn.lon, 3));
|
||||
assert.equal(cells[0], "u1j", "Bonn, the project's default region");
|
||||
assert.equal(new Set(cells).size, cells.length);
|
||||
assert.ok(cells.every((c) => /^[0-9bcdefghjkmnpqrstuvwxyz]{3}$/.test(c)));
|
||||
});
|
||||
|
||||
test("only a place near a cell's edge brings in its neighbour", () => {
|
||||
const cell = decodeGeohash("u1j")!;
|
||||
// The middle of the cell is far from every edge.
|
||||
assert.deepEqual(cellsNear(cell.lat, cell.lon), ["u1j"]);
|
||||
// 10 km inside the east edge: the next cell east is within reach.
|
||||
const kmPerLon = 111.2 * Math.cos((cell.lat * Math.PI) / 180);
|
||||
const nearEast = cellsNear(cell.lat, cell.lon + cell.lonErr - 10 / kmPerLon);
|
||||
assert.equal(nearEast[0], "u1j");
|
||||
assert.ok(nearEast.length >= 2, "the eastern neighbour");
|
||||
// At this latitude a cell is only about 100 km wide, so its middle is 50 km
|
||||
// from the edges: out of a 40 km reach, inside an 80 km one.
|
||||
assert.ok(cell.lonErr * kmPerLon > 45 && cell.lonErr * kmPerLon < 55);
|
||||
assert.ok(cellsNear(cell.lat, cell.lon, 80).length >= 2, "the reach is a parameter");
|
||||
});
|
||||
|
||||
test("places at the edge of the map still give valid cells", () => {
|
||||
for (const [lat, lon] of [[89.99, 0], [-89.99, 10], [0, 179.99], [0, -179.99], [64.1, -21.9]] as const) {
|
||||
const cells = cellsNear(lat, lon);
|
||||
assert.ok(cells.length >= 1 && cells.every((c) => c.length === 3), `${lat},${lon}: ${cells}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("a cell is named after the biggest place in it", () => {
|
||||
const byCell = biggestPlaceByCell(places);
|
||||
const bonn = first("Bonn")!.place;
|
||||
const named = byCell.get("u1j")!;
|
||||
assert.ok(named.population >= bonn.population, `${named.name} is at least as big as Bonn`);
|
||||
assert.equal(byCell.get("000"), undefined, "an empty cell (open ocean) has no name");
|
||||
});
|
||||
Reference in New Issue
Block a user