package updatewords import ( "encoding/json" "os" "sort" "strings" "testing" ) type approvedEntry struct{ ID, File, Text string } type approvedRecord struct { ApprovedBy string `json:"approved_by"` ApprovedOn string `json:"approved_on"` Entries []approvedEntry } func loadApproved(t *testing.T, path string) approvedRecord { t.Helper() raw, err := os.ReadFile(path) if err != nil { t.Fatal(err) } var rec approvedRecord if err := json.Unmarshal(raw, &rec); err != nil { t.Fatal(err) } return rec } // The copy Paul approved lives in four records: plan 2's (2026-10-09, the // authorize strings), plan 3's (2026-10-11, the settings.console.* strings), // plan 4's (2026-10-12, the settings.box.* strings), and plan 5's (2026-10-13, // the settings.cli.* and settings.page.* strings). // Each record must name exactly its own IDs, each text must be what Text // returns byte for byte, each entry must name the file that holds it, and // together the four records must name exactly the IDs of All(), so a new // draft with no approved record fails. func TestApprovedCopyIsApplied(t *testing.T) { const console = "settings.console." const box = "settings.box." const surfaces = "settings.cli." const pagePrefix = "settings.page." records := []struct { path string file string in func(id string) bool }{ {"../../docs/superpowers/plans/2026-10-07-instance-settings-2-copy.json", "internal/updatewords/words_authorize.go", func(id string) bool { return !strings.HasPrefix(id, console) && !strings.HasPrefix(id, box) && !strings.HasPrefix(id, surfaces) && !strings.HasPrefix(id, pagePrefix) }}, {"../../docs/superpowers/plans/2026-10-07-instance-settings-3-copy.json", "internal/updatewords/words_console.go", func(id string) bool { return strings.HasPrefix(id, console) }}, {"../../docs/superpowers/plans/2026-10-07-instance-settings-4-copy.json", "internal/updatewords/words_box.go", func(id string) bool { return strings.HasPrefix(id, box) }}, {"../../docs/superpowers/plans/2026-10-07-instance-settings-5-copy.json", "internal/updatewords/words_surfaces.go", func(id string) bool { return strings.HasPrefix(id, surfaces) || strings.HasPrefix(id, pagePrefix) }}, } var union []string docs := "" for _, r := range records { rec := loadApproved(t, r.path) if rec.ApprovedBy == "" || rec.ApprovedOn == "" { t.Fatalf("%s: the record has no approval", r.path) } var got, want []string seen := map[string]bool{} for _, e := range rec.Entries { got = append(got, e.ID) if seen[e.ID] { t.Errorf("%s: %s appears twice", r.path, e.ID) } seen[e.ID] = true if e.Text == "" { t.Errorf("%s: the approved text is empty", e.ID) continue } if e.File != r.file { t.Errorf("%s: unexpected file %s, want %s", e.ID, e.File, r.file) } if e.ID == "settings.authorize.docs" { docs = e.Text } if !has(e.ID) { continue // reported by the ID comparison below } if txt := Text(e.ID); txt != e.Text { t.Errorf("%s: text is %q, approved %q", e.ID, txt, e.Text) } } for _, d := range All() { if r.in(d.ID) { want = append(want, d.ID) } } sort.Strings(got) sort.Strings(want) if strings.Join(got, "\n") != strings.Join(want, "\n") { t.Errorf("%s: record IDs %v, want %v", r.path, got, want) } union = append(union, got...) } var all []string for _, d := range All() { all = append(all, d.ID) } sort.Strings(union) sort.Strings(all) if strings.Join(union, "\n") != strings.Join(all, "\n") { t.Errorf("the approved records name %v, want exactly the IDs of All() %v", union, all) } if docs == "" { t.Fatal("the record has no settings.authorize.docs entry") } page, err := os.ReadFile("../../docs/site/cli.md") if err != nil { t.Fatal(err) } if !strings.Contains(string(page), docs) { t.Errorf("docs/site/cli.md does not contain the approved authorize paragraph %q", docs) } } func has(id string) bool { for _, d := range All() { if d.ID == id { return true } } return false }