Files
allmende/web/test/places.test.ts
T
nilsandClaude Sonnet 5.5 fd439710df 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
2026-11-18 10:28:16 +01:00

94 lines
4.3 KiB
TypeScript

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");
});