package console import ( "encoding/json" "fmt" "os" "strconv" "strings" "testing" ) func TestRegionMessagesRender(t *testing.T) { all := []string{ msgAppRegionUnavailable("uk", "absent", []string{"ie", "au"}), msgAppRegionNotAllowed("uk", []string{"ie"}), msgRegionUnknown("zz", []string{"ie", "uk"}), msgRegionNotArmed("uk", []string{"ie"}), msgAppRegionUnreadable, msgReplicaOverlap("uk"), msgPolicyUnreadable, msgAppRegionUnconfirmed("uk"), msgPlacementDiscrepancy("a.go.tito.io", "blog", "uk"), msgNoSecretsKey("uk"), // codex plan-R4 2: the empty-list variants, C16b to C19b. msgAppRegionUnavailable("uk", "absent", nil), msgAppRegionNotAllowed("uk", nil), msgRegionUnknown("zz", nil), msgRegionNotArmed("uk", nil), } for _, s := range all { if s == "" || strings.Contains(s, "—") || strings.Contains(s, "%!") { t.Errorf("bad message %q", s) } } if !strings.Contains(all[0], reasonText("absent")) || !strings.Contains(all[0], "ie, au") { t.Errorf("C16 = %q, want the readable reason and the list", all[0]) } for _, s := range all[len(all)-4:] { if strings.Contains(s, ": .") || strings.Contains(s, ": ,") { t.Errorf("an empty list rendered as a list: %q", s) } } seen := map[string]bool{} for _, r := range []string{"absent", "not-hosting", "no-secrets-key", "not-eligible"} { txt := reasonText(r) if txt == r || seen[txt] { t.Errorf("reasonText(%s) = %q: untranslated or duplicated", r, txt) } seen[txt] = true } } // TestRegionMessagesMatchTheApprovedRecord holds every record entry mapped to // regionmsgs.go to the source as a quoted literal (so a short text cannot // pass by matching part of an identifier), and holds each rendered message to // the record's text filled with the same verbs, in the record's order. func TestRegionMessagesMatchTheApprovedRecord(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("regionmsgs.go") if err != nil { t.Fatal(err) } byID := map[string]string{} mapped := 0 for _, e := range rec.Entries { byID[e.ID] = e.Text if e.File != "internal/console/regionmsgs.go" { continue } mapped++ if !strings.Contains(string(src), strconv.Quote(e.Text)) { t.Errorf("%s: regionmsgs.go has no literal %s", e.ID, strconv.Quote(e.Text)) } } // The record maps 4 reasons, C16 to C20, C16b to C19b, C26 to C29 and C31. if mapped != 18 { t.Errorf("the record maps %d entries to regionmsgs.go, want 18", mapped) } list := []string{"ie", "au"} cases := []struct { id, got, want string }{ {"R-absent", reasonText("absent"), byID["R-absent"]}, {"R-not-hosting", reasonText("not-hosting"), byID["R-not-hosting"]}, {"R-no-secrets-key", reasonText("no-secrets-key"), byID["R-no-secrets-key"]}, {"R-not-eligible", reasonText("not-eligible"), byID["R-not-eligible"]}, {"C16", msgAppRegionUnavailable("uk", "absent", list), fmt.Sprintf(byID["C16"], "uk", reasonText("absent"), "ie, au")}, {"C17", msgAppRegionNotAllowed("uk", list), fmt.Sprintf(byID["C17"], "uk", "ie, au")}, {"C18", msgRegionUnknown("zz", list), fmt.Sprintf(byID["C18"], "zz", "ie, au")}, {"C19", msgRegionNotArmed("uk", list), fmt.Sprintf(byID["C19"], "uk", "ie, au")}, {"C16b", msgAppRegionUnavailable("uk", "absent", nil), fmt.Sprintf(byID["C16b"], "uk", reasonText("absent"))}, {"C17b", msgAppRegionNotAllowed("uk", nil), fmt.Sprintf(byID["C17b"], "uk")}, {"C18b", msgRegionUnknown("zz", nil), fmt.Sprintf(byID["C18b"], "zz")}, {"C19b", msgRegionNotArmed("uk", nil), fmt.Sprintf(byID["C19b"], "uk")}, {"C20", msgAppRegionUnreadable, byID["C20"]}, {"C26", msgReplicaOverlap("uk"), fmt.Sprintf(byID["C26"], "uk", "uk")}, {"C27", msgPolicyUnreadable, byID["C27"]}, {"C28", msgAppRegionUnconfirmed("uk"), fmt.Sprintf(byID["C28"], "uk")}, {"C29", msgPlacementDiscrepancy("a.go.tito.io", "blog", "uk"), fmt.Sprintf(byID["C29"], "a.go.tito.io", "blog", "blog", "uk")}, {"C31", msgNoSecretsKey("uk"), fmt.Sprintf(byID["C31"], "uk")}, } for _, c := range cases { if byID[c.id] == "" { t.Errorf("%s: not in the record", c.id) continue } if c.got != c.want { t.Errorf("%s = %q, record gives %q", c.id, c.got, c.want) } } if reasonText("nonsense") != "nonsense" { t.Errorf("an unknown reason should pass through unchanged") } }