package copydesk import ( "amadan.net/oficina/equipo/internal/testci" "bytes" "encoding/json" "os" "os/exec" "path/filepath" "strings" "testing" ) func TestCopyContractScripts(t *testing.T) { script, err := copyScript(t) if err != nil { t.Fatal(err) } root := t.TempDir() base := filepath.Join(root, "base") cand := filepath.Join(root, "candidate") for _, d := range []string{base, cand} { if err := os.MkdirAll(filepath.Join(d, "internal/equipo"), 0700); err != nil { t.Fatal(err) } } os.WriteFile(filepath.Join(base, "internal/equipo/copy.go"), []byte("package equipo\nvar pageCopy=map[string]string{\n\"save\": \"Old words\",\n}\n"), 0600) 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) call := func(mode string, v any) []byte { t.Helper() raw, _ := json.Marshal(v) c := exec.Command("python3", script, mode) c.Stdin = bytes.NewReader(raw) out, err := c.CombinedOutput() if err != nil { t.Fatalf("%s: %v %s", mode, err, out) } return out } out := call("extract", map[string]string{"base": base, "candidate": cand}) var ex Extraction if json.Unmarshal(out, &ex) != nil || len(ex.Strings) != 1 || ex.Strings[0].Key != "save" { t.Fatalf("forged marker hid change %s", out) } 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) call("apply", map[string]any{"candidate": cand, "extraction": ex, "review": Review{Version: 1, Strings: []Wording{{Key: "save", Text: "Save changes", Action: "accept"}}}}) raw, _ := os.ReadFile(filepath.Join(cand, "word_test.go")) if !strings.Contains(string(raw), `expected = "Save changes"`) || !strings.Contains(string(raw), `longer = "prefix New words"`) || !strings.Contains(string(raw), `// wording is "New words"`) { t.Fatalf("test literal replacement: %s", raw) } } func TestCopyBookkeepingAcceptanceAndRecovery(t *testing.T) { root := t.TempDir() script, err := copyScript(t) if err != nil { t.Fatal(err) } os.MkdirAll(filepath.Join(root, "internal/equipo"), 0700) os.MkdirAll(filepath.Join(root, "docs/superpowers/plans"), 0700) source := "package equipo\nvar pageCopy=map[string]string{\n\"save\": \"New words\", // COPY REVIEW PENDING\n}\n" os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), []byte(source), 0600) inv := filepath.Join(root, "docs/superpowers/plans/CONTROLLER-REPORT-plan-3.md") os.WriteFile(inv, []byte("## Plan 3 copy inventory (for Paul's review)\n\n## History\nKeep this.\n"), 0600) call := func(mode string, v any) ([]byte, error) { raw, _ := json.Marshal(v) c := exec.Command("python3", script, mode) c.Stdin = bytes.NewReader(raw) return c.CombinedOutput() } 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}}} if out, err := call("accept", v); err != nil { t.Fatalf("accept: %v %s", err, out) } first, _ := os.ReadFile(inv) if !bytes.Contains(first, []byte("| Copy specialist | COPY ACCEPTED")) { t.Fatalf("malformed inventory: %s", first) } if out, err := call("accept", v); err != nil { t.Fatalf("repeat: %v %s", err, out) } second, _ := os.ReadFile(inv) if !bytes.Equal(first, second) { t.Fatal("acceptance not idempotent") } v["records"] = []map[string]any{{"key": "save", "hash": Hash("stale"), "owner": "paul", "candidate": "head", "revision": 1}} if _, err := call("accept", v); err == nil { t.Fatal("stale record accepted") } if out, err := call("verify", map[string]any{"candidate": root}); err != nil || !bytes.Contains(out, []byte(`"valid": true`)) { t.Fatalf("verify changed accepted bookkeeping: %v %s", err, out) } os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), []byte(strings.ReplaceAll(source, "New words", "Changed words")), 0600) if out, err := call("verify", map[string]any{"candidate": root}); err == nil && bytes.Contains(out, []byte(`"valid": true`)) { t.Fatal("verify accepted stale text") } } func TestCopyRecoveryResolvesOnlyBookkeepingConflicts(t *testing.T) { root := t.TempDir() script, _ := copyScript(t) os.MkdirAll(filepath.Join(root, "internal/equipo"), 0700) os.MkdirAll(filepath.Join(root, "docs/superpowers/plans"), 0700) old := "package equipo\nvar pageCopy = map[string]string{\n\"save\": \"Old words\", // COPY REVIEW PENDING\n}\n" tooling := strings.ReplaceAll(old, "COPY REVIEW PENDING", "COPY ACCEPTED BY DELEGATION (Astra, 2026-11-13)") candidate := strings.ReplaceAll(old, "Old words", "New words") os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), []byte("<<<<<<< conflict\n"), 0600) 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) call := func(tooling string) ([]byte, error) { raw, _ := json.Marshal(map[string]any{"candidate": root, "recovery": map[string]string{"old": old, "tooling": tooling, "candidate": candidate}}) c := exec.Command("python3", script, "regenerate") c.Stdin = bytes.NewReader(raw) return c.CombinedOutput() } if out, err := call(tooling); err != nil { t.Fatalf("marker conflict: %v %s", err, out) } got, _ := os.ReadFile(filepath.Join(root, "internal/equipo/copy.go")) if !bytes.Contains(got, []byte("New words")) || bytes.Contains(got, []byte("<<<<<<<")) { t.Fatalf("not recovered: %s", got) } if _, err := call(strings.ReplaceAll(tooling, "Old words", "Different meaning")); err == nil { t.Fatal("meaning conflict guessed") } } func TestCopyClassifierAccountsForRealFailures(t *testing.T) { script, _ := copyScript(t) call := func(log string, truncated bool) Signature { t.Helper() in := Input{Version: 1, Units: []string{"20-check@1"}, Log: log, Truncated: truncated} raw, _ := json.Marshal(in) c := exec.Command("python3", script, "classify") c.Stdin = bytes.NewReader(raw) out, err := c.CombinedOutput() if err != nil { t.Fatal(err) } var cl Classification if err := json.Unmarshal(out, &cl); err != nil { t.Fatal(err) } return Classify(in, cl, "adapter") } pure := "--- FAIL: TestPlan3ReleaseBoundaries (0.1s)\n --- FAIL: TestPlan3ReleaseBoundaries/inventory (0.1s)\n pending copy omitted or changed in review inventory save\nFAIL" if !call(pure, false).CopyOnly() { t.Fatal("explicit copy-only failure unclassified") } if call(pure+"\n--- FAIL: TestConversation (0.1s)\nGET /conversation: 500", false).CopyOnly() { t.Fatal("real failure hidden") } if call(pure, true).CopyOnly() { t.Fatal("truncated failure accepted") } } func TestScopedReferenceAllowance(t *testing.T) { root := t.TempDir() os.MkdirAll(filepath.Join(root, "scripts"), 0700) os.WriteFile(filepath.Join(root, "view.go"), []byte("package view\n// Render a tree node.\n"), 0600) os.WriteFile(filepath.Join(root, "scripts/runner-reference-allowlist.json"), []byte("[]"), 0600) script, _ := copyScript(t) 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."}}}) c := exec.Command("python3", script, "reference") c.Stdin = bytes.NewReader(raw) if out, err := c.CombinedOutput(); err != nil { t.Fatalf("reference: %v %s", err, out) } data, _ := os.ReadFile(filepath.Join(root, "scripts/runner-reference-allowlist.json")) var records []map[string]any if json.Unmarshal(data, &records) != nil || len(records) != 1 || records[0]["match"] != "node" || records[0]["line_sha256"] != Hash("// Render a tree node.") { t.Fatalf("wrong scoped allowance %s", data) } } func TestCopyExtractsNamedRuntimeConstants(t *testing.T) { root := t.TempDir() base := filepath.Join(root, "base") cand := filepath.Join(root, "candidate") for _, d := range []string{base, cand} { os.MkdirAll(filepath.Join(d, "internal/equipo"), 0700) } os.WriteFile(filepath.Join(base, "internal/equipo/copy.go"), []byte("package equipo\nconst BootLog = \"Old words\"\n"), 0600) os.WriteFile(filepath.Join(cand, "internal/equipo/copy.go"), []byte("package equipo\nconst BootLog = \"New words\"\n"), 0600) script, _ := copyScript(t) raw, _ := json.Marshal(map[string]string{"base": base, "candidate": cand}) c := exec.Command("python3", script, "extract") c.Stdin = bytes.NewReader(raw) out, err := c.CombinedOutput() if err != nil { t.Fatal(err) } var ex Extraction if json.Unmarshal(out, &ex) != nil || len(ex.Strings) != 1 || ex.Strings[0].Key != "BootLog" { t.Fatalf("missed constant %s", out) } } func copyScript(t *testing.T) (string, error) { t.Helper() testci.Files(t, "../../scripts/copy-contract.py") testci.Tools(t, "python3", "gofmt", "git") return filepath.Abs("../../scripts/copy-contract.py") } func TestCopyExtractorRefusesComputedChangedText(t *testing.T) { root := t.TempDir() base := filepath.Join(root, "base") candidate := filepath.Join(root, "candidate") for _, d := range []string{base, candidate} { os.MkdirAll(filepath.Join(d, "internal/equipo"), 0700) } os.WriteFile(filepath.Join(base, "internal/equipo/copy.go"), []byte("package equipo\nvar pageCopy=map[string]string{\n\"save\": \"Save\",\n}\n"), 0600) os.WriteFile(filepath.Join(candidate, "internal/equipo/copy.go"), []byte("package equipo\nvar pageCopy=map[string]string{\n\"save\": makeText(),\n}\n"), 0600) script, _ := copyScript(t) raw, _ := json.Marshal(map[string]string{"base": base, "candidate": candidate}) c := exec.Command("python3", script, "extract") c.Stdin = bytes.NewReader(raw) if out, err := c.CombinedOutput(); err == nil { t.Fatalf("computed text treated as no copy: %s", out) } } func TestCopyRepositoryExtraction(t *testing.T) { testci.Files(t, "../../internal/equipo/copy.go") script, _ := copyScript(t) root, _ := filepath.Abs("../..") raw, _ := json.Marshal(map[string]string{"base": root, "candidate": root}) c := exec.Command("python3", script, "extract") c.Stdin = bytes.NewReader(raw) out, err := c.CombinedOutput() if err != nil { t.Fatalf("actual repository extraction: %v %s", err, out) } var ex Extraction if json.Unmarshal(out, &ex) != nil || len(ex.Strings) != 0 { t.Fatalf("unchanged repository not empty: %s", out) } } func TestCopyAppliesAgainstRepositoryTests(t *testing.T) { root := t.TempDir() repo, _ := filepath.Abs("../..") script, _ := copyScript(t) source, err := os.ReadFile(filepath.Join(repo, "internal/equipo/copy.go")) if err != nil { t.Fatal(err) } os.MkdirAll(filepath.Join(root, "internal/equipo"), 0700) os.WriteFile(filepath.Join(root, "internal/equipo/copy.go"), source, 0600) err = filepath.WalkDir(filepath.Join(repo, "internal"), func(path string, d os.DirEntry, err error) error { if err != nil { return err } if d.IsDir() || !strings.HasSuffix(path, "_test.go") { return nil } raw, err := os.ReadFile(path) if err != nil { return err } rel, _ := filepath.Rel(repo, path) target := filepath.Join(root, rel) if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil { return err } return os.WriteFile(target, raw, 0600) }) if err != nil { t.Fatal(err) } review := Review{Version: 1, Strings: []Wording{{Key: "copy.title", Text: "Copy review", Action: "accept"}}} raw, _ := json.Marshal(map[string]any{"candidate": root, "extraction": Extraction{Version: 1, Strings: []Literal{{Key: "copy.title", Text: "Copy"}}}, "review": review}) c := exec.Command("python3", script, "apply") c.Stdin = bytes.NewReader(raw) if out, err := c.CombinedOutput(); err != nil { t.Fatalf("repository test literals: %v %s", err, out) } }