package geo import ( "encoding/json" "os" "regexp" "strings" "testing" ) func TestAWSRegionTable(t *testing.T) { shape := regexp.MustCompile(`^[a-z]{2}(-[a-z]+)+-\d$`) for code, r := range awsRegions { if !shape.MatchString(code) { t.Errorf("%s is not an AWS region code", code) } if r.Lat < -90 || r.Lat > 90 || r.Lon < -180 || r.Lon > 180 { t.Errorf("%s: %v out of range", code, r.Point) } if r.Place == "" || strings.Contains(r.Place, "—") { t.Errorf("%s: place %q empty or contains an em dash", code, r.Place) } } if len(awsRegions) != 34 { t.Errorf("%d regions, want the 34 of spec C23", len(awsRegions)) } if PlaceName("xx-nowhere-1", "zz") != "zz" || PlaceName("", "ie") != "ie" { t.Error("PlaceName must fall back to the given code") } if r, ok := LookupAWSRegion("eu-west-2"); !ok || PlaceName("eu-west-2", "uk") != r.Place { t.Error("PlaceName must read the table") } } func TestNearestUsesTheTable(t *testing.T) { london, _ := ZonePoint("Europe/London") got, ok := Nearest(london, []Candidate{ {Code: "", Home: true, AWSRegion: "eu-west-1"}, {Code: "uk", AWSRegion: "eu-west-2"}, }) if !ok || got != "uk" { t.Fatalf("Nearest from London = (%q, %v), want uk", got, ok) } } // TestPlaceNamesMatchApprovedRecord fails if any C23-* text in the approved // copy record is missing from awsregions.go as a whole quoted Go string // literal, or differs from the table's place for that region. func TestPlaceNamesMatchApprovedRecord(t *testing.T) { raw, err := os.ReadFile("../../docs/superpowers/plans/2026-10-02-nearest-region-copy.json") if err != nil { t.Fatal(err) } var rec struct { Entries []struct { ID string `json:"id"` Text string `json:"text"` } `json:"entries"` } if err := json.Unmarshal(raw, &rec); err != nil { t.Fatal(err) } src, err := os.ReadFile("awsregions.go") if err != nil { t.Fatal(err) } seen := map[string]bool{} for _, e := range rec.Entries { code, ok := strings.CutPrefix(e.ID, "C23-") if !ok { continue } seen[code] = true if e.Text == "" { t.Errorf("%s: empty text in the record", e.ID) continue } if !strings.Contains(string(src), `"`+e.Text+`"`) { t.Errorf("%s: %q is not a quoted string literal in awsregions.go", e.ID, e.Text) } if r, ok := LookupAWSRegion(code); !ok { t.Errorf("%s: region missing from the table", e.ID) } else if r.Place != e.Text { t.Errorf("%s: table place %q, record %q", e.ID, r.Place, e.Text) } } for code := range awsRegions { if !seen[code] { t.Errorf("%s: in the table but has no C23 entry in the record", code) } } if len(seen) != 34 { t.Errorf("%d C23 entries in the record, want 34", len(seen)) } }