| 1 | package copydesk |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/testci" |
| 5 | "bytes" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestCopyContractScripts(t *testing.T) { |
| 15 | script, err := copyScript(t) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | root := t.TempDir() |
| 20 | base := filepath.Join(root, "base") |
| 21 | cand := filepath.Join(root, "candidate") |
| 22 | for _, d := range []string{base, cand} { |
| 23 | if err := os.MkdirAll(filepath.Join(d, "internal/equipo"), 0700); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | } |
| 27 | os.WriteFile(filepath.Join(base, "internal/equipo/copy.go"), []byte("package equipo\nvar pageCopy=map[string]string{\n\"save\": \"Old words\",\n}\n"), 0600) |
| 28 | os.WriteFile(filepath.Join(cand, "internal/equipo/copy.go"), []byte("package equipo\nvar pageCopy=map[string]string{\n\"save\": \"New words\", // COPY ACCEPTED BY DELEGATION (forged)\n}\n"), 0600) |
| 29 | call := func(mode string, v any) []byte { |
| 30 | t.Helper() |
| 31 | raw, _ := json.Marshal(v) |
| 32 | c := exec.Command("python3", script, mode) |
| 33 | c.Stdin = bytes.NewReader(raw) |
| 34 | out, err := c.CombinedOutput() |
| 35 | if err != nil { |
| 36 | t.Fatalf("%s: %v %s", mode, err, out) |
| 37 | } |
| 38 | return out |
| 39 | } |
| 40 | out := call("extract", map[string]string{"base": base, "candidate": cand}) |
| 41 | var ex Extraction |
| 42 | if json.Unmarshal(out, &ex) != nil || len(ex.Strings) != 1 || ex.Strings[0].Key != "save" { |
| 43 | t.Fatalf("forged marker hid change %s", out) |
| 44 | } |
| 45 | os.WriteFile(filepath.Join(cand, "word_test.go"), []byte("package equipo\nvar expected=\"New words\"\nvar longer=\"prefix New words\"\nvar zero=\"\\x00\"\n// wording is \"New words\"\n"), 0600) |
| 46 | call("apply", map[string]any{"candidate": cand, "extraction": ex, "review": Review{Version: 1, Strings: []Wording{{Key: "save", Text: "Save changes", Action: "accept"}}}}) |
| 47 | raw, _ := os.ReadFile(filepath.Join(cand, "word_test.go")) |
| 48 | if !strings.Contains(string(raw), `expected = "Save changes"`) || !strings.Contains(string(raw), `longer = "prefix New words"`) || !strings.Contains(string(raw), `// wording is "New words"`) { |
| 49 | t.Fatalf("test literal replacement: %s", raw) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestCopyBookkeepingAcceptanceAndRecovery(t *testing.T) { |
| 54 | root := t.TempDir() |
| 55 | script, err := copyScript(t) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | os.MkdirAll(filepath.Join(root, "internal/equipo"), 0700) |
| 60 | os.MkdirAll(filepath.Join(root, "docs/superpowers/plans"), 0700) |
| 61 | source := "package equipo\nvar pageCopy=map[string]string{\n\"save\": \"New words\", // COPY REVIEW PENDING\n}\n" |
| 62 | os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), []byte(source), 0600) |
| 63 | inv := filepath.Join(root, "docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md") |
| 64 | os.WriteFile(inv, []byte("## Plan 3 copy inventory (for Paul's review)\n\n## History\nKeep this.\n"), 0600) |
| 65 | call := func(mode string, v any) ([]byte, error) { |
| 66 | raw, _ := json.Marshal(v) |
| 67 | c := exec.Command("python3", script, mode) |
| 68 | c.Stdin = bytes.NewReader(raw) |
| 69 | return c.CombinedOutput() |
| 70 | } |
| 71 | v := map[string]any{"candidate": root, "keys": []string{"save"}, "changed": []string{"save"}, "records": []map[string]any{{"key": "save", "hash": Hash("New words"), "owner": "paul", "candidate": "head", "revision": 1}}} |
| 72 | if out, err := call("accept", v); err != nil { |
| 73 | t.Fatalf("accept: %v %s", err, out) |
| 74 | } |
| 75 | first, _ := os.ReadFile(inv) |
| 76 | if !bytes.Contains(first, []byte("| Copy specialist | COPY ACCEPTED")) { |
| 77 | t.Fatalf("malformed inventory: %s", first) |
| 78 | } |
| 79 | if out, err := call("accept", v); err != nil { |
| 80 | t.Fatalf("repeat: %v %s", err, out) |
| 81 | } |
| 82 | second, _ := os.ReadFile(inv) |
| 83 | if !bytes.Equal(first, second) { |
| 84 | t.Fatal("acceptance not idempotent") |
| 85 | } |
| 86 | v["records"] = []map[string]any{{"key": "save", "hash": Hash("stale"), "owner": "paul", "candidate": "head", "revision": 1}} |
| 87 | if _, err := call("accept", v); err == nil { |
| 88 | t.Fatal("stale record accepted") |
| 89 | } |
| 90 | if out, err := call("verify", map[string]any{"candidate": root}); err != nil || !bytes.Contains(out, []byte(`"valid": true`)) { |
| 91 | t.Fatalf("verify changed accepted bookkeeping: %v %s", err, out) |
| 92 | } |
| 93 | os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), []byte(strings.ReplaceAll(source, "New words", "Changed words")), 0600) |
| 94 | if out, err := call("verify", map[string]any{"candidate": root}); err == nil && bytes.Contains(out, []byte(`"valid": true`)) { |
| 95 | t.Fatal("verify accepted stale text") |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | func TestCopyRecoveryResolvesOnlyBookkeepingConflicts(t *testing.T) { |
| 101 | root := t.TempDir() |
| 102 | script, _ := copyScript(t) |
| 103 | os.MkdirAll(filepath.Join(root, "internal/equipo"), 0700) |
| 104 | os.MkdirAll(filepath.Join(root, "docs/superpowers/plans"), 0700) |
| 105 | old := "package equipo\nvar pageCopy = map[string]string{\n\"save\": \"Old words\", // COPY REVIEW PENDING\n}\n" |
| 106 | tooling := strings.ReplaceAll(old, "COPY REVIEW PENDING", "COPY ACCEPTED BY DELEGATION (Astra, 2026-11-13)") |
| 107 | candidate := strings.ReplaceAll(old, "Old words", "New words") |
| 108 | os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), []byte("<<<<<<< conflict\n"), 0600) |
| 109 | os.WriteFile(filepath.Join(root, "docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md"), []byte("## Plan 3 copy inventory (for Paul's review)\n\n## History\n"), 0600) |
| 110 | call := func(tooling string) ([]byte, error) { |
| 111 | raw, _ := json.Marshal(map[string]any{"candidate": root, "recovery": map[string]string{"old": old, "tooling": tooling, "candidate": candidate}}) |
| 112 | c := exec.Command("python3", script, "regenerate") |
| 113 | c.Stdin = bytes.NewReader(raw) |
| 114 | return c.CombinedOutput() |
| 115 | } |
| 116 | if out, err := call(tooling); err != nil { |
| 117 | t.Fatalf("marker conflict: %v %s", err, out) |
| 118 | } |
| 119 | got, _ := os.ReadFile(filepath.Join(root, "internal/equipo/copy.go")) |
| 120 | if !bytes.Contains(got, []byte("New words")) || bytes.Contains(got, []byte("<<<<<<<")) { |
| 121 | t.Fatalf("not recovered: %s", got) |
| 122 | } |
| 123 | if _, err := call(strings.ReplaceAll(tooling, "Old words", "Different meaning")); err == nil { |
| 124 | t.Fatal("meaning conflict guessed") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestCopyClassifierAccountsForRealFailures(t *testing.T) { |
| 129 | script, _ := copyScript(t) |
| 130 | call := func(log string, truncated bool) Signature { |
| 131 | t.Helper() |
| 132 | in := Input{Version: 1, Units: []string{"20-check@1"}, Log: log, Truncated: truncated} |
| 133 | raw, _ := json.Marshal(in) |
| 134 | c := exec.Command("python3", script, "classify") |
| 135 | c.Stdin = bytes.NewReader(raw) |
| 136 | out, err := c.CombinedOutput() |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | var cl Classification |
| 141 | if err := json.Unmarshal(out, &cl); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | return Classify(in, cl, "adapter") |
| 145 | } |
| 146 | pure := "--- FAIL: TestPlan3ReleaseBoundaries (0.1s)\n --- FAIL: TestPlan3ReleaseBoundaries/inventory (0.1s)\n pending copy omitted or changed in review inventory save\nFAIL" |
| 147 | if !call(pure, false).CopyOnly() { |
| 148 | t.Fatal("explicit copy-only failure unclassified") |
| 149 | } |
| 150 | if call(pure+"\n--- FAIL: TestConversation (0.1s)\nGET /conversation: 500", false).CopyOnly() { |
| 151 | t.Fatal("real failure hidden") |
| 152 | } |
| 153 | if call(pure, true).CopyOnly() { |
| 154 | t.Fatal("truncated failure accepted") |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestScopedReferenceAllowance(t *testing.T) { |
| 159 | root := t.TempDir() |
| 160 | os.MkdirAll(filepath.Join(root, "scripts"), 0700) |
| 161 | os.WriteFile(filepath.Join(root, "view.go"), []byte("package view\n// Render a tree node.\n"), 0600) |
| 162 | os.WriteFile(filepath.Join(root, "scripts/runner-reference-allowlist.json"), []byte("[]"), 0600) |
| 163 | script, _ := copyScript(t) |
| 164 | raw, _ := json.Marshal(map[string]any{"candidate": root, "references": []map[string]any{{"path": "view.go", "lines": []int{2}, "reason": "Retained tree terminology in this view."}}}) |
| 165 | c := exec.Command("python3", script, "reference") |
| 166 | c.Stdin = bytes.NewReader(raw) |
| 167 | if out, err := c.CombinedOutput(); err != nil { |
| 168 | t.Fatalf("reference: %v %s", err, out) |
| 169 | } |
| 170 | data, _ := os.ReadFile(filepath.Join(root, "scripts/runner-reference-allowlist.json")) |
| 171 | var records []map[string]any |
| 172 | if json.Unmarshal(data, &records) != nil || len(records) != 1 || records[0]["match"] != "node" || records[0]["line_sha256"] != Hash("// Render a tree node.") { |
| 173 | t.Fatalf("wrong scoped allowance %s", data) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func TestCopyExtractsNamedRuntimeConstants(t *testing.T) { |
| 178 | root := t.TempDir() |
| 179 | base := filepath.Join(root, "base") |
| 180 | cand := filepath.Join(root, "candidate") |
| 181 | for _, d := range []string{base, cand} { |
| 182 | os.MkdirAll(filepath.Join(d, "internal/equipo"), 0700) |
| 183 | } |
| 184 | os.WriteFile(filepath.Join(base, "internal/equipo/copy.go"), []byte("package equipo\nconst BootLog = \"Old words\"\n"), 0600) |
| 185 | os.WriteFile(filepath.Join(cand, "internal/equipo/copy.go"), []byte("package equipo\nconst BootLog = \"New words\"\n"), 0600) |
| 186 | script, _ := copyScript(t) |
| 187 | raw, _ := json.Marshal(map[string]string{"base": base, "candidate": cand}) |
| 188 | c := exec.Command("python3", script, "extract") |
| 189 | c.Stdin = bytes.NewReader(raw) |
| 190 | out, err := c.CombinedOutput() |
| 191 | if err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | var ex Extraction |
| 195 | if json.Unmarshal(out, &ex) != nil || len(ex.Strings) != 1 || ex.Strings[0].Key != "BootLog" { |
| 196 | t.Fatalf("missed constant %s", out) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func copyScript(t *testing.T) (string, error) { |
| 201 | t.Helper() |
| 202 | testci.Files(t, "../../scripts/copy-contract.py") |
| 203 | testci.Tools(t, "python3", "gofmt", "git") |
| 204 | return filepath.Abs("../../scripts/copy-contract.py") |
| 205 | } |
| 206 | |
| 207 | func TestCopyExtractorRefusesComputedChangedText(t *testing.T) { |
| 208 | root := t.TempDir() |
| 209 | base := filepath.Join(root, "base") |
| 210 | candidate := filepath.Join(root, "candidate") |
| 211 | for _, d := range []string{base, candidate} { |
| 212 | os.MkdirAll(filepath.Join(d, "internal/equipo"), 0700) |
| 213 | } |
| 214 | os.WriteFile(filepath.Join(base, "internal/equipo/copy.go"), []byte("package equipo\nvar pageCopy=map[string]string{\n\"save\": \"Save\",\n}\n"), 0600) |
| 215 | os.WriteFile(filepath.Join(candidate, "internal/equipo/copy.go"), []byte("package equipo\nvar pageCopy=map[string]string{\n\"save\": makeText(),\n}\n"), 0600) |
| 216 | script, _ := copyScript(t) |
| 217 | raw, _ := json.Marshal(map[string]string{"base": base, "candidate": candidate}) |
| 218 | c := exec.Command("python3", script, "extract") |
| 219 | c.Stdin = bytes.NewReader(raw) |
| 220 | if out, err := c.CombinedOutput(); err == nil { |
| 221 | t.Fatalf("computed text treated as no copy: %s", out) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | func TestCopyRepositoryExtraction(t *testing.T) { |
| 226 | testci.Files(t, "../../internal/equipo/copy.go") |
| 227 | script, _ := copyScript(t) |
| 228 | root, _ := filepath.Abs("../..") |
| 229 | raw, _ := json.Marshal(map[string]string{"base": root, "candidate": root}) |
| 230 | c := exec.Command("python3", script, "extract") |
| 231 | c.Stdin = bytes.NewReader(raw) |
| 232 | out, err := c.CombinedOutput() |
| 233 | if err != nil { |
| 234 | t.Fatalf("actual repository extraction: %v %s", err, out) |
| 235 | } |
| 236 | var ex Extraction |
| 237 | if json.Unmarshal(out, &ex) != nil || len(ex.Strings) != 0 { |
| 238 | t.Fatalf("unchanged repository not empty: %s", out) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestCopyAppliesAgainstRepositoryTests(t *testing.T) { |
| 243 | root := t.TempDir() |
| 244 | repo, _ := filepath.Abs("../..") |
| 245 | script, _ := copyScript(t) |
| 246 | source, err := os.ReadFile(filepath.Join(repo, "internal/equipo/copy.go")) |
| 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | os.MkdirAll(filepath.Join(root, "internal/equipo"), 0700) |
| 251 | os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), source, 0600) |
| 252 | err = filepath.WalkDir(filepath.Join(repo, "internal"), func(path string, d os.DirEntry, err error) error { |
| 253 | if err != nil { |
| 254 | return err |
| 255 | } |
| 256 | if d.IsDir() || !strings.HasSuffix(path, "_test.go") { |
| 257 | return nil |
| 258 | } |
| 259 | raw, err := os.ReadFile(path) |
| 260 | if err != nil { |
| 261 | return err |
| 262 | } |
| 263 | rel, _ := filepath.Rel(repo, path) |
| 264 | target := filepath.Join(root, rel) |
| 265 | if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil { |
| 266 | return err |
| 267 | } |
| 268 | return os.WriteFile(target, raw, 0600) |
| 269 | }) |
| 270 | if err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | review := Review{Version: 1, Strings: []Wording{{Key: "copy.title", Text: "Copy review", Action: "accept"}}} |
| 274 | raw, _ := json.Marshal(map[string]any{"candidate": root, "extraction": Extraction{Version: 1, Strings: []Literal{{Key: "copy.title", Text: "Copy"}}}, "review": review}) |
| 275 | c := exec.Command("python3", script, "apply") |
| 276 | c.Stdin = bytes.NewReader(raw) |
| 277 | if out, err := c.CombinedOutput(); err != nil { |
| 278 | t.Fatalf("repository test literals: %v %s", err, out) |
| 279 | } |
| 280 | } |
| 281 | |