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
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestRegionsAreSavedAndLoaded(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if _, ok, err := loadRegions(dir); ok || err != nil {
|
|
t.Fatalf("nothing saved yet: ok=%v err=%v", ok, err)
|
|
}
|
|
want := []string{"u1j", "u1x"}
|
|
if err := saveRegions(dir, want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, ok, err := loadRegions(dir)
|
|
if err != nil || !ok || !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("loaded %v ok=%v err=%v, want %v", got, ok, err, want)
|
|
}
|
|
// Saving again replaces the file whole and leaves no temporary files.
|
|
if err := saveRegions(dir, []string{"u30"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ := loadRegions(dir); !reflect.DeepEqual(got, []string{"u30"}) {
|
|
t.Fatalf("after replacing: %v", got)
|
|
}
|
|
entries, _ := os.ReadDir(dir)
|
|
if len(entries) != 1 {
|
|
t.Fatalf("stray files: %v", entries)
|
|
}
|
|
}
|
|
|
|
func TestBrokenRegionFilesAreReportedNotIgnored(t *testing.T) {
|
|
for name, content := range map[string]string{
|
|
"not json": "u1j",
|
|
"not a region": `{"regions":["u1j","hello"]}`,
|
|
"empty regions": `{"regions":[]}`,
|
|
} {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, regionsFile), []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, ok, err := loadRegions(dir)
|
|
switch name {
|
|
case "empty regions":
|
|
if ok || err != nil {
|
|
t.Errorf("%s: ok=%v err=%v, want it treated as not set", name, ok, err)
|
|
}
|
|
default:
|
|
if err == nil {
|
|
t.Errorf("%s: no error, a broken file must not silently change what the node follows", name)
|
|
}
|
|
}
|
|
}
|
|
}
|