carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1// approved_copy_test.go
2package main
3
4import (
5 "bytes"
6 "encoding/json"
7 "go/scanner"
8 "go/token"
9 "os"
10 "path/filepath"
11 "sort"
12 "strconv"
13 "strings"
14 "testing"
15)
16
17// approvedCopyIDs is the copy inventory of the nearest-region plan, Task 0
18// Step 1, pinned as a literal: C1 to C31, the C15 and C25 no-home variants, R-*, O1 to O20, U, S, G, the 34
19// C23 place names, D1 to D10 and F1. The record must name every one of them,
20// once, and nothing else. A record that loses an entry fails here even though
21// every entry left in it is applied.
22var approvedCopyIDs = []string{
23 "C1", "C2", "C3", "C5", "C7", "C8", "C21", "C24", "R-absent",
24 "R-not-hosting", "R-no-secrets-key", "R-not-eligible", "C16",
25 "C17", "C18", "C19", "C16b", "C17b", "C18b", "C19b", "C20", "C26",
26 "C27", "C28", "C29", "C31", "O1", "O2", "O3", "O4", "O5", "O6",
27 "O7", "O8", "O9", "O10", "O11", "O12", "O13", "O14", "O15", "O16",
28 "O17", "O18", "O19", "O20", "C9", "C10", "C11", "C12", "C13a",
29 "C13b", "C14", "C15", "C15.nohome", "C15b", "C15c", "C25", "C25.nohome",
30 "C30", "C30b", "C30c",
31 "C22a", "C22b", "C22c", "C22d", "U1", "U2", "U3", "S1", "S2",
32 "S3", "G1", "G2", "G3", "G4", "G5", "G6", "C23-us-east-1", "C23-us-east-2",
33 "C23-us-west-1", "C23-us-west-2", "C23-ca-central-1", "C23-ca-west-1",
34 "C23-mx-central-1", "C23-sa-east-1", "C23-eu-west-1", "C23-eu-west-2",
35 "C23-eu-west-3", "C23-eu-central-1", "C23-eu-central-2", "C23-eu-south-1",
36 "C23-eu-south-2", "C23-eu-north-1", "C23-il-central-1", "C23-me-south-1",
37 "C23-me-central-1", "C23-af-south-1", "C23-ap-south-1", "C23-ap-south-2",
38 "C23-ap-east-1", "C23-ap-east-2", "C23-ap-southeast-1", "C23-ap-southeast-2",
39 "C23-ap-southeast-3", "C23-ap-southeast-4", "C23-ap-southeast-5",
40 "C23-ap-southeast-6", "C23-ap-southeast-7", "C23-ap-northeast-1",
41 "C23-ap-northeast-2", "C23-ap-northeast-3", "D1", "D2", "D3",
42 "D4", "D5", "D6", "D7", "D8", "D9", "D10", "F1",
43}
44
45// approvedCopyFragments are record entries whose text is a piece of a larger
46// string literal (a flag inside a usage line), so no Go-quoted form of the
47// text alone can appear in the file. They are checked against the values of
48// the file's string literals instead of its raw bytes.
49var approvedCopyFragments = map[string]bool{"U1": true, "U2": true, "U3": true}
50
51// shortCopyBytes is the length under which a text is a word or a flag name.
52// A short text could appear by accident (the word "Region" in a comment), so
53// in a Go file it must be the whole decoded value of a string literal.
54const shortCopyBytes = 25
55
56// Every string Paul approved in the nearest-region copy review is present,
57// byte for byte, in the file the record names (codex plan-R1 13). A string
58// edited after review, or never applied, fails here, and so does an approved
59// string the record no longer lists.
60func TestApprovedCopyIsApplied(t *testing.T) {
61 raw, err := os.ReadFile("docs/superpowers/plans/2026-10-02-nearest-region-copy.json")
62 if err != nil {
63 t.Fatal(err)
64 }
65 checkApprovedCopy(t, raw, ".", approvedCopyIDs, approvedCopyFragments)
66}
67
68// checkApprovedCopy checks a record against the files under root. ids is the
69// pinned inventory the record must name; fragments are the entries that sit
70// inside a longer literal.
71func checkApprovedCopy(t *testing.T, raw []byte, root string, ids []string, fragments map[string]bool) {
72 t.Helper()
73 var rec struct {
74 ApprovedBy string `json:"approved_by"`
75 ApprovedOn string `json:"approved_on"`
76 Entries []struct{ ID, File, Text string }
77 }
78 if err := json.Unmarshal(raw, &rec); err != nil {
79 t.Fatal(err)
80 }
81 if rec.ApprovedBy == "" || rec.ApprovedOn == "" || len(rec.Entries) == 0 {
82 t.Fatalf("the record has no approval: %+v", rec)
83 }
84
85 seen := map[string]int{}
86 for _, e := range rec.Entries {
87 seen[e.ID]++
88 }
89 want := map[string]bool{}
90 for _, id := range ids {
91 want[id] = true
92 if seen[id] == 0 {
93 t.Errorf("the record has no entry %s, which the plan's copy inventory names", id)
94 }
95 }
96 var extra []string
97 for id, n := range seen {
98 if n > 1 {
99 t.Errorf("the record names %s %d times", id, n)
100 }
101 if !want[id] {
102 extra = append(extra, id)
103 }
104 }
105 sort.Strings(extra)
106 for _, id := range extra {
107 t.Errorf("the record has entry %s, which the pinned inventory does not name (add it to the pinned inventory)", id)
108 }
109
110 for _, e := range rec.Entries {
111 if e.Text == "" {
112 t.Errorf("%s: the approved text is empty", e.ID)
113 continue
114 }
115 body, err := os.ReadFile(filepath.Join(root, e.File))
116 if err != nil {
117 t.Errorf("%s: %v", e.ID, err)
118 continue
119 }
120 if !copyAppliedWith(fragments, e.ID, e.File, e.Text, body) {
121 t.Errorf("%s: %s does not contain the approved text %q", e.ID, e.File, e.Text)
122 }
123 }
124}
125
126// copyApplied reports whether body holds the approved text. Docs, scripts and
127// long Go texts match raw. A short text in a Go file must be the whole decoded
128// value of a string literal token, so a word cannot pass by appearing in a
129// comment, an identifier or a longer literal. U1 to U3 are pieces of a larger
130// literal, so they need only be inside some literal's decoded value.
131func copyAppliedWith(fragments map[string]bool, id, file, text string, body []byte) bool {
132 if !strings.HasSuffix(file, ".go") {
133 return bytes.Contains(body, []byte(text))
134 }
135 if fragments[id] {
136 return goLiteralMatches(body, func(v string) bool { return strings.Contains(v, text) })
137 }
138 if len(text) >= shortCopyBytes {
139 return bytes.Contains(body, []byte(text))
140 }
141 return goLiteralMatches(body, func(v string) bool { return v == text })
142}
143
144// copyApplied is the nearest-region record's check, kept for its own test.
145func copyApplied(id, file, text string, body []byte) bool {
146 return copyAppliedWith(approvedCopyFragments, id, file, text, body)
147}
148
149// goLiteralMatches reports whether the decoded value of any string literal in
150// the Go source satisfies match. The scanner skips comments, so text that
151// appears only in a comment never matches.
152func goLiteralMatches(src []byte, match func(string) bool) bool {
153 fset := token.NewFileSet()
154 file := fset.AddFile("", fset.Base(), len(src))
155 var s scanner.Scanner
156 s.Init(file, src, nil, 0)
157 for {
158 _, tok, lit := s.Scan()
159 if tok == token.EOF {
160 return false
161 }
162 if tok != token.STRING {
163 continue
164 }
165 if v, err := strconv.Unquote(lit); err == nil && match(v) {
166 return true
167 }
168 }
169}
170
171// The check itself, run on in-memory sources: the cases that must not pass
172// and the ones that must.
173func TestApprovedCopyCheckRejectsFalsePasses(t *testing.T) {
174 const real = "package p\n\nvar c = struct{ PickerLabel string }{\n\tPickerLabel: \"Region\",\n}\n"
175 cases := []struct {
176 name string
177 id string
178 text string
179 src string
180 want bool
181 }{
182 {"short: the real literal", "C1", "Region", real, true},
183 {"short: a raw string literal", "C1", "Region", "package p\n\nvar c = `Region`\n", true},
184 {"short: a quoted comment only", "C1", "Region",
185 "package p\n\nvar c = \"Zone\" // Previous label: \"Region\"\n", false},
186 {"short: a quoted block comment only", "C1", "Region",
187 "package p\n\nvar c = \"Zone\"\n/* was \"Region\" */\n", false},
188 {"short: a backtick comment only", "C1", "Region",
189 "package p\n\nvar c = \"Zone\" // was `Region`\n", false},
190 {"short: an unrelated identifier only", "C1", "Region",
191 "package p\n\nvar Region = \"Zone\"\n", false},
192 {"short: a longer literal that contains it", "C1", "Region",
193 "package p\n\nvar c = \"Region: %s\"\n", false},
194 {"short: a literal that differs by case", "C1", "Region",
195 "package p\n\nvar c = \"region\"\n", false},
196 {"short: non-ASCII text as a literal", "C23-ca-central-1", "Canada · Montréal",
197 "package p\n\nvar c = \"Canada · Montréal\"\n", true},
198 {"fragment: inside a larger literal", "U1", " [--region <code>]",
199 "package p\n\nvar u = \"apps create [--region <code>] [--account <a>]\"\n", true},
200 {"fragment: in a comment only", "U1", " [--region <code>]",
201 "package p\n\nvar u = \"apps create\" // [--region <code>]\n", false},
202 {"fragment: in an identifier-free comment block", "U3", "[--region <code>]",
203 "package p\n\nvar u = `canary`\n/* [--region <code>] */\n", false},
204 {"long: a text of 25 bytes or more matches raw", "C2", "Where this app's instances run, near you.",
205 "package p\n\nvar c = \"Where this app's instances run, near you.\"\n", true},
206 }
207 for _, c := range cases {
208 if got := copyApplied(c.id, "x.go", c.text, []byte(c.src)); got != c.want {
209 t.Errorf("%s: copyApplied = %v, want %v", c.name, got, c.want)
210 }
211 }
212 // Docs keep raw matching.
213 if !copyApplied("D4", "x.md", "Without `--region`, a new instance starts", []byte("a Without `--region`, a new instance starts b")) {
214 t.Error("a docs text is matched raw")
215 }
216}
217