| new file mode 100644 |
| index 000000000..f2db026b5 |
| --- /dev/null |
| +++ b/internal/console/regioncopy_test.go |
| @@ -0,0 +1,93 @@ |
| +package console |
| + |
| +import ( |
| + "encoding/json" |
| + "fmt" |
| + "os" |
| + "reflect" |
| + "strconv" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func TestRegionCopyHasNoEmDashesAndRendersItsVerbs(t *testing.T) { |
| + v := reflect.ValueOf(regionCopy) |
| + for i := 0; i < v.NumField(); i++ { |
| + s := v.Field(i).String() |
| + if s == "" || strings.Contains(s, "—") { |
| + t.Errorf("%s = %q: empty or has an em dash", v.Type().Field(i).Name, s) |
| + } |
| + } |
| + for _, s := range []string{ |
| + fmt.Sprintf(regionCopy.AutoOption, "P"), |
| + fmt.Sprintf(regionCopy.StartsIn, "P"), |
| + fmt.Sprintf(regionCopy.CantStart, "P"), |
| + fmt.Sprintf(regionCopy.AuditCreated, "blog", "P"), |
| + } { |
| + if strings.Contains(s, "%!") { |
| + t.Errorf("bad verb: %q", s) |
| + } |
| + } |
| +} |
| + |
| +// TestRegionCopyMatchesTheApprovedRecord holds every string to the copy |
| +// record byte for byte, and to the quoted literal in the source, so a short |
| +// text such as "Region" cannot pass by matching part of an identifier. |
| +func TestRegionCopyMatchesTheApprovedRecord(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"` |
| + Link string `json:"link"` |
| + } `json:"entries"` |
| + } |
| + if err := json.Unmarshal(raw, &rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + src, err := os.ReadFile("regioncopy.go") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + byID := map[string]string{} |
| + links := map[string]string{} |
| + for _, e := range rec.Entries { |
| + byID[e.ID] = e.Text |
| + links[e.ID] = e.Link |
| + } |
| + for id, got := range map[string]string{ |
| + "C1": regionCopy.PickerLabel, |
| + "C2": regionCopy.PickerHelp, |
| + "C3": regionCopy.AutoOption, |
| + "C5": regionCopy.Unavailable, |
| + "C7": regionCopy.StartsIn, |
| + "C8": regionCopy.CantStart, |
| + "C21": regionCopy.AuditCreated, |
| + "C24": regionCopy.Attribution, |
| + } { |
| + want, ok := byID[id] |
| + if !ok { |
| + t.Errorf("%s: not in the record", id) |
| + continue |
| + } |
| + if got != want { |
| + t.Errorf("%s = %q, record has %q", id, got, want) |
| + } |
| + if !strings.Contains(string(src), strconv.Quote(want)) { |
| + t.Errorf("%s: regioncopy.go has no literal %s", id, strconv.Quote(want)) |
| + } |
| + } |
| + // C24 is DB-IP's required credit; the licence wants it linked. |
| + if links["C24"] == "" { |
| + t.Fatal("C24: the record carries no link") |
| + } |
| + if regionCopy.AttributionURL != links["C24"] || attributionURL != links["C24"] { |
| + t.Errorf("attribution URL = %q / %q, record has %q", regionCopy.AttributionURL, attributionURL, links["C24"]) |
| + } |
| + if regionCopy.Attribution != "IP Geolocation by DB-IP" { |
| + t.Errorf("Attribution = %q", regionCopy.Attribution) |
| + } |
| +} |