| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "reflect" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestRegionCopyHasNoEmDashesAndRendersItsVerbs(t *testing.T) { |
| 14 | v := reflect.ValueOf(regionCopy) |
| 15 | for i := 0; i < v.NumField(); i++ { |
| 16 | s := v.Field(i).String() |
| 17 | if s == "" || strings.Contains(s, "—") { |
| 18 | t.Errorf("%s = %q: empty or has an em dash", v.Type().Field(i).Name, s) |
| 19 | } |
| 20 | } |
| 21 | for _, s := range []string{ |
| 22 | fmt.Sprintf(regionCopy.AutoOption, "P"), |
| 23 | fmt.Sprintf(regionCopy.StartsIn, "P"), |
| 24 | fmt.Sprintf(regionCopy.CantStart, "P"), |
| 25 | fmt.Sprintf(regionCopy.AuditCreated, "blog", "P"), |
| 26 | } { |
| 27 | if strings.Contains(s, "%!") { |
| 28 | t.Errorf("bad verb: %q", s) |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // TestRegionCopyMatchesTheApprovedRecord holds every string to the copy |
| 34 | // record byte for byte, and to the quoted literal in the source, so a short |
| 35 | // text such as "Region" cannot pass by matching part of an identifier. |
| 36 | func TestRegionCopyMatchesTheApprovedRecord(t *testing.T) { |
| 37 | raw, err := os.ReadFile("../../docs/superpowers/plans/2026-10-02-nearest-region-copy.json") |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | var rec struct { |
| 42 | Entries []struct { |
| 43 | ID string `json:"id"` |
| 44 | Text string `json:"text"` |
| 45 | Link string `json:"link"` |
| 46 | } `json:"entries"` |
| 47 | } |
| 48 | if err := json.Unmarshal(raw, &rec); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | src, err := os.ReadFile("regioncopy.go") |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | byID := map[string]string{} |
| 56 | links := map[string]string{} |
| 57 | for _, e := range rec.Entries { |
| 58 | byID[e.ID] = e.Text |
| 59 | links[e.ID] = e.Link |
| 60 | } |
| 61 | for id, got := range map[string]string{ |
| 62 | "C1": regionCopy.PickerLabel, |
| 63 | "C2": regionCopy.PickerHelp, |
| 64 | "C3": regionCopy.AutoOption, |
| 65 | "C5": regionCopy.Unavailable, |
| 66 | "C7": regionCopy.StartsIn, |
| 67 | "C8": regionCopy.CantStart, |
| 68 | "C21": regionCopy.AuditCreated, |
| 69 | "C24": regionCopy.Attribution, |
| 70 | } { |
| 71 | want, ok := byID[id] |
| 72 | if !ok { |
| 73 | t.Errorf("%s: not in the record", id) |
| 74 | continue |
| 75 | } |
| 76 | if got != want { |
| 77 | t.Errorf("%s = %q, record has %q", id, got, want) |
| 78 | } |
| 79 | if !strings.Contains(string(src), strconv.Quote(want)) { |
| 80 | t.Errorf("%s: regioncopy.go has no literal %s", id, strconv.Quote(want)) |
| 81 | } |
| 82 | } |
| 83 | // C24 is DB-IP's required credit; the licence wants it linked. |
| 84 | if links["C24"] == "" { |
| 85 | t.Fatal("C24: the record carries no link") |
| 86 | } |
| 87 | if regionCopy.AttributionURL != links["C24"] || attributionURL != links["C24"] { |
| 88 | t.Errorf("attribution URL = %q / %q, record has %q", regionCopy.AttributionURL, attributionURL, links["C24"]) |
| 89 | } |
| 90 | if regionCopy.Attribution != "IP Geolocation by DB-IP" { |
| 91 | t.Errorf("Attribution = %q", regionCopy.Attribution) |
| 92 | } |
| 93 | } |
| 94 | |