// approved_copy_test.go package main import ( "bytes" "encoding/json" "go/scanner" "go/token" "os" "path/filepath" "sort" "strconv" "strings" "testing" ) // approvedCopyIDs is the copy inventory of the nearest-region plan, Task 0 // 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 // C23 place names, D1 to D10 and F1. The record must name every one of them, // once, and nothing else. A record that loses an entry fails here even though // every entry left in it is applied. var approvedCopyIDs = []string{ "C1", "C2", "C3", "C5", "C7", "C8", "C21", "C24", "R-absent", "R-not-hosting", "R-no-secrets-key", "R-not-eligible", "C16", "C17", "C18", "C19", "C16b", "C17b", "C18b", "C19b", "C20", "C26", "C27", "C28", "C29", "C31", "O1", "O2", "O3", "O4", "O5", "O6", "O7", "O8", "O9", "O10", "O11", "O12", "O13", "O14", "O15", "O16", "O17", "O18", "O19", "O20", "C9", "C10", "C11", "C12", "C13a", "C13b", "C14", "C15", "C15.nohome", "C15b", "C15c", "C25", "C25.nohome", "C30", "C30b", "C30c", "C22a", "C22b", "C22c", "C22d", "U1", "U2", "U3", "S1", "S2", "S3", "G1", "G2", "G3", "G4", "G5", "G6", "C23-us-east-1", "C23-us-east-2", "C23-us-west-1", "C23-us-west-2", "C23-ca-central-1", "C23-ca-west-1", "C23-mx-central-1", "C23-sa-east-1", "C23-eu-west-1", "C23-eu-west-2", "C23-eu-west-3", "C23-eu-central-1", "C23-eu-central-2", "C23-eu-south-1", "C23-eu-south-2", "C23-eu-north-1", "C23-il-central-1", "C23-me-south-1", "C23-me-central-1", "C23-af-south-1", "C23-ap-south-1", "C23-ap-south-2", "C23-ap-east-1", "C23-ap-east-2", "C23-ap-southeast-1", "C23-ap-southeast-2", "C23-ap-southeast-3", "C23-ap-southeast-4", "C23-ap-southeast-5", "C23-ap-southeast-6", "C23-ap-southeast-7", "C23-ap-northeast-1", "C23-ap-northeast-2", "C23-ap-northeast-3", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "F1", } // approvedCopyFragments are record entries whose text is a piece of a larger // string literal (a flag inside a usage line), so no Go-quoted form of the // text alone can appear in the file. They are checked against the values of // the file's string literals instead of its raw bytes. var approvedCopyFragments = map[string]bool{"U1": true, "U2": true, "U3": true} // shortCopyBytes is the length under which a text is a word or a flag name. // A short text could appear by accident (the word "Region" in a comment), so // in a Go file it must be the whole decoded value of a string literal. const shortCopyBytes = 25 // Every string Paul approved in the nearest-region copy review is present, // byte for byte, in the file the record names (codex plan-R1 13). A string // edited after review, or never applied, fails here, and so does an approved // string the record no longer lists. func TestApprovedCopyIsApplied(t *testing.T) { raw, err := os.ReadFile("docs/superpowers/plans/2026-10-02-nearest-region-copy.json") if err != nil { t.Fatal(err) } checkApprovedCopy(t, raw, ".", approvedCopyIDs, approvedCopyFragments) } // checkApprovedCopy checks a record against the files under root. ids is the // pinned inventory the record must name; fragments are the entries that sit // inside a longer literal. func checkApprovedCopy(t *testing.T, raw []byte, root string, ids []string, fragments map[string]bool) { t.Helper() var rec struct { ApprovedBy string `json:"approved_by"` ApprovedOn string `json:"approved_on"` Entries []struct{ ID, File, Text string } } if err := json.Unmarshal(raw, &rec); err != nil { t.Fatal(err) } if rec.ApprovedBy == "" || rec.ApprovedOn == "" || len(rec.Entries) == 0 { t.Fatalf("the record has no approval: %+v", rec) } seen := map[string]int{} for _, e := range rec.Entries { seen[e.ID]++ } want := map[string]bool{} for _, id := range ids { want[id] = true if seen[id] == 0 { t.Errorf("the record has no entry %s, which the plan's copy inventory names", id) } } var extra []string for id, n := range seen { if n > 1 { t.Errorf("the record names %s %d times", id, n) } if !want[id] { extra = append(extra, id) } } sort.Strings(extra) for _, id := range extra { t.Errorf("the record has entry %s, which the pinned inventory does not name (add it to the pinned inventory)", id) } for _, e := range rec.Entries { if e.Text == "" { t.Errorf("%s: the approved text is empty", e.ID) continue } body, err := os.ReadFile(filepath.Join(root, e.File)) if err != nil { t.Errorf("%s: %v", e.ID, err) continue } if !copyAppliedWith(fragments, e.ID, e.File, e.Text, body) { t.Errorf("%s: %s does not contain the approved text %q", e.ID, e.File, e.Text) } } } // copyApplied reports whether body holds the approved text. Docs, scripts and // long Go texts match raw. A short text in a Go file must be the whole decoded // value of a string literal token, so a word cannot pass by appearing in a // comment, an identifier or a longer literal. U1 to U3 are pieces of a larger // literal, so they need only be inside some literal's decoded value. func copyAppliedWith(fragments map[string]bool, id, file, text string, body []byte) bool { if !strings.HasSuffix(file, ".go") { return bytes.Contains(body, []byte(text)) } if fragments[id] { return goLiteralMatches(body, func(v string) bool { return strings.Contains(v, text) }) } if len(text) >= shortCopyBytes { return bytes.Contains(body, []byte(text)) } return goLiteralMatches(body, func(v string) bool { return v == text }) } // copyApplied is the nearest-region record's check, kept for its own test. func copyApplied(id, file, text string, body []byte) bool { return copyAppliedWith(approvedCopyFragments, id, file, text, body) } // goLiteralMatches reports whether the decoded value of any string literal in // the Go source satisfies match. The scanner skips comments, so text that // appears only in a comment never matches. func goLiteralMatches(src []byte, match func(string) bool) bool { fset := token.NewFileSet() file := fset.AddFile("", fset.Base(), len(src)) var s scanner.Scanner s.Init(file, src, nil, 0) for { _, tok, lit := s.Scan() if tok == token.EOF { return false } if tok != token.STRING { continue } if v, err := strconv.Unquote(lit); err == nil && match(v) { return true } } } // The check itself, run on in-memory sources: the cases that must not pass // and the ones that must. func TestApprovedCopyCheckRejectsFalsePasses(t *testing.T) { const real = "package p\n\nvar c = struct{ PickerLabel string }{\n\tPickerLabel: \"Region\",\n}\n" cases := []struct { name string id string text string src string want bool }{ {"short: the real literal", "C1", "Region", real, true}, {"short: a raw string literal", "C1", "Region", "package p\n\nvar c = `Region`\n", true}, {"short: a quoted comment only", "C1", "Region", "package p\n\nvar c = \"Zone\" // Previous label: \"Region\"\n", false}, {"short: a quoted block comment only", "C1", "Region", "package p\n\nvar c = \"Zone\"\n/* was \"Region\" */\n", false}, {"short: a backtick comment only", "C1", "Region", "package p\n\nvar c = \"Zone\" // was `Region`\n", false}, {"short: an unrelated identifier only", "C1", "Region", "package p\n\nvar Region = \"Zone\"\n", false}, {"short: a longer literal that contains it", "C1", "Region", "package p\n\nvar c = \"Region: %s\"\n", false}, {"short: a literal that differs by case", "C1", "Region", "package p\n\nvar c = \"region\"\n", false}, {"short: non-ASCII text as a literal", "C23-ca-central-1", "Canada · Montréal", "package p\n\nvar c = \"Canada · Montréal\"\n", true}, {"fragment: inside a larger literal", "U1", " [--region ]", "package p\n\nvar u = \"apps create [--region ] [--account ]\"\n", true}, {"fragment: in a comment only", "U1", " [--region ]", "package p\n\nvar u = \"apps create\" // [--region ]\n", false}, {"fragment: in an identifier-free comment block", "U3", "[--region ]", "package p\n\nvar u = `canary`\n/* [--region ] */\n", false}, {"long: a text of 25 bytes or more matches raw", "C2", "Where this app's instances run, near you.", "package p\n\nvar c = \"Where this app's instances run, near you.\"\n", true}, } for _, c := range cases { if got := copyApplied(c.id, "x.go", c.text, []byte(c.src)); got != c.want { t.Errorf("%s: copyApplied = %v, want %v", c.name, got, c.want) } } // Docs keep raw matching. if !copyApplied("D4", "x.md", "Without `--region`, a new instance starts", []byte("a Without `--region`, a new instance starts b")) { t.Error("a docs text is matched raw") } }