package main import ( "encoding/json" "errors" "os" "sort" "strconv" "strings" "testing" ) func TestRegionCLICopy(t *testing.T) { lines := map[string]string{} for _, src := range []string{"timezone", "ip", "default", "chosen"} { lines[src] = regionCreatedLine("P", src) } if len(map[string]bool{lines["timezone"]: true, lines["ip"]: true, lines["default"]: true, lines["chosen"]: true}) != 4 { t.Fatalf("each source needs its own line: %v", lines) } if regionCreatedLine("P", "") != lines["default"] { t.Fatal("an unknown source must read as the default") } all := []string{ regionNotRecordedOldConsole, regionNotAppliedSwitchOff("P"), instanceCreatedSuffix("stable", "P"), deployRegionMismatch("blog", "P"), deployRegionUnreadable("blog", errors.New("timeout")), deployRegionUnknown("blog"), canaryRegionMismatch("c.go.tito.io", "P"), canaryRegionOnFleet("blog", "acme"), canaryFleetUnknown("blog"), canaryRegionUnconfirmed("c"), flagRegionAppsCreate, flagRegionDeploy, flagRegionCanary, flagRegionInstancesCreate, } for _, s := range append(all, lines["timezone"], lines["ip"], lines["default"], lines["chosen"]) { if s == "" || strings.Contains(s, "—") || strings.Contains(s, "%!") { t.Errorf("bad copy %q", s) } } } // TestRegionCLICopyMatchesTheApprovedRecord holds region_copy.go to the copy // record: every record entry filed under region_copy.go must appear in the // source as its exact quoted Go literal. A substring of a longer string, or // of an identifier, does not count. func TestRegionCLICopyMatchesTheApprovedRecord(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"` File string `json:"file"` Text string `json:"text"` } `json:"entries"` } if err := json.Unmarshal(raw, &rec); err != nil { t.Fatal(err) } src, err := os.ReadFile("region_copy.go") if err != nil { t.Fatal(err) } want := []string{"C9", "C10", "C11", "C12", "C13a", "C13b", "C14", "C15", "C15.nohome", "C15b", "C15c", "C22a", "C22b", "C22c", "C22d", "C25", "C25.nohome", "C30", "C30b", "C30c"} var got []string for _, e := range rec.Entries { if e.File != "region_copy.go" { continue } got = append(got, e.ID) if e.Text == "" { t.Errorf("%s: empty record text", e.ID) continue } if !strings.Contains(string(src), strconv.Quote(e.Text)) { t.Errorf("%s: region_copy.go has no literal %s", e.ID, strconv.Quote(e.Text)) } } // A record entry dropped or re-filed must not shrink this check silently. sort.Strings(got) sort.Strings(want) if strings.Join(got, ",") != strings.Join(want, ",") { t.Errorf("record entries for region_copy.go = %v, want %v", got, want) } } // C15 and C25 with no named home region read "the home region", not an empty // place: the approved .nohome texts. With a place, the approved C15 and C25. func TestRegionMismatchWithoutAHome(t *testing.T) { for _, c := range []struct{ name, got, want string }{ {"C15.nohome", deployRegionMismatch("blog", ""), "blog already runs in the home region. --region only applies when deploy creates an app. To place one instance elsewhere, use carlos instances create --region."}, {"C15", deployRegionMismatch("blog", "Ireland"), "blog already runs in Ireland. --region only applies when deploy creates an app. To place one instance elsewhere, use carlos instances create --region."}, {"C25.nohome", canaryRegionMismatch("c.go.tito.io", ""), "canary c.go.tito.io already runs in the home region. --region applies only when canary creates the host. Run carlos canary rm first to start it somewhere else."}, {"C25", canaryRegionMismatch("c.go.tito.io", "Ireland"), "canary c.go.tito.io already runs in Ireland. --region applies only when canary creates the host. Run carlos canary rm first to start it somewhere else."}, } { if c.got != c.want { t.Errorf("%s: %q, want %q", c.name, c.got, c.want) } } }