Files
allmende/internal/places/places_test.go
T
nilsandClaude Sonnet 5.5 6995364edc Place names for --region, an expiry reminder, and e2e checks through the UI
--region takes a place name as well as a code ("Bonn", "Köln", "cologne",
"name:dub" to force a name that looks like a code). The node uses the same
GeoNames list as the browser (internal/places), follows the place's cell plus
any cell within 40 km, and prints what each name became.

The node reminds the owner on its terminal, once a day per listing, about
every listing with 5 days or less left (the UI marks the same range). The
sign-in token is never in the line.

scripts/e2e/ui.ts drives the real bundle in jsdom against real nodes: a buyer
writes from a listing page, the owner reads and answers in their own page and
the buyer's page shows the verified answer; a node with no region gets its
first one by place name in the browser and keeps it over a restart. e2e.sh
also checks --region by name and the reminder, and keeps its scratch files in
the checkout's .tmp.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
2026-11-18 10:51:56 +01:00

102 lines
2.8 KiB
Go

package places
import (
"os"
"testing"
)
// The real list the node ships (web/dist is the one it embeds).
func load(t *testing.T) []Place {
t.Helper()
ps, err := Load(os.DirFS("../../web/dist"))
if err != nil {
t.Fatal(err)
}
if len(ps) < 30000 {
t.Fatalf("only %d places", len(ps))
}
return ps
}
func first(t *testing.T, ps []Place, q string) Match {
t.Helper()
m := Search(ps, q, 1)
if len(m) == 0 {
t.Fatalf("%q: nothing found", q)
}
return m[0]
}
func TestNamesAreFoundHoweverTheyAreSpelled(t *testing.T) {
ps := load(t)
for q, name := range map[string]string{
"Bonn": "Bonn", "bonn": "Bonn", "köln": "Köln", "koeln": "Köln", "KOELN": "Köln",
"zurich": "Zürich", "Zürich": "Zürich",
} {
if got := first(t, ps, q).Place.Name; got != name {
t.Errorf("%q: found %q, want %q", q, got, name)
}
}
}
func TestOtherLanguagesLeadToThePlaceAndSayWhatMatched(t *testing.T) {
ps := load(t)
m := first(t, ps, "cologne")
if m.Place.Name != "Köln" || m.Matched != "Cologne" {
t.Fatalf("cologne: %s (%s)", m.Place.Name, m.Matched)
}
for q, cc := range map[string]string{"münchen": "DE", "muenchen": "DE", "nuernberg": "DE", "prag": "CZ", "wien": "AT"} {
if got := first(t, ps, q).Place.Country; got != cc {
t.Errorf("%q: country %s, want %s", q, got, cc)
}
}
if first(t, ps, "Köln").Matched != "" {
t.Error("the main name is not repeated")
}
}
func TestTheBigCityWinsOverASmallNamesake(t *testing.T) {
ps := load(t)
v := Search(ps, "vienna", 5)
if v[0].Place.Country != "AT" {
t.Fatalf("first is %s", v[0].Place.Country)
}
if Search(ps, "b", 5) != nil || Search(ps, "qqqzzzxxx", 5) != nil || Search(ps, " ", 5) != nil {
t.Error("too short or unknown queries must find nothing")
}
}
func TestGeohashesAndCells(t *testing.T) {
// Known values: the geohash of Hamburg's centre starts u1x, Bonn is in u1j.
if got := Encode(57.64911, 10.40744, 11); got != "u4pruydqqvj" {
t.Fatalf("Encode = %s (the standard test vector)", got)
}
ps := load(t)
bonn := first(t, ps, "Bonn").Place
cells := CellsNear(bonn.Lat, bonn.Lon, 40)
if cells[0] != "u1j" {
t.Fatalf("Bonn's own cell comes first: %v", cells)
}
seen := map[string]bool{}
for _, c := range cells {
if seen[c] || len(c) != 3 {
t.Fatalf("bad or repeated cell in %v", cells)
}
seen[c] = true
}
// The middle of a cell is far from every edge; at 80 km its neighbours are in reach.
if got := CellsNear(50.9, 7.2, 1); len(got) != 1 {
t.Fatalf("a tiny reach stays in one cell: %v", got)
}
if got := CellsNear(50.9, 7.2, 80); len(got) < 2 {
t.Fatalf("a wide reach crosses a border: %v", got)
}
for _, p := range [][2]float64{{89.99, 0}, {-89.99, 10}, {0, 179.99}, {0, -179.99}, {64.1, -21.9}} {
for _, c := range CellsNear(p[0], p[1], 40) {
if len(c) != 3 {
t.Fatalf("%v: %v", p, c)
}
}
}
}