| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/copydesk" |
| 5 | "amadan.net/oficina/equipo/internal/wire" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestCopyLocalEvidenceCaps(t *testing.T) { |
| 14 | r, row, _ := newCopyRig(t) |
| 15 | row.Specialist = wire.SpecialistChecks |
| 16 | diagnostic := "--- FAIL: TestReleaseCopyReviewed (0.1s)\nFAIL\nFAIL example.com/app 0.1s" |
| 17 | for _, kind := range []string{"lines", "bytes"} { |
| 18 | t.Run(kind, func(t *testing.T) { |
| 19 | log := diagnostic + strings.Repeat("\nx", wire.MaxChecksLines-3) |
| 20 | if kind == "bytes" { |
| 21 | pad := wire.MaxChecksOutput - len(diagnostic) - 1 |
| 22 | log = strings.Repeat("x ", pad/2) + strings.Repeat(" ", pad%2) + "\n" + diagnostic |
| 23 | } |
| 24 | rep := r.e.classifyChecks(r.ctx, row, wire.StepReport{State: wire.StepDone, Checks: wire.ChecksFailed, Gate: "tests", Output: log}) |
| 25 | if rep.Failure == nil || rep.Failure.CopyOnly() || rep.Failure.Complete { |
| 26 | t.Fatalf("capped local evidence became complete: %+v", rep.Failure) |
| 27 | } |
| 28 | }) |
| 29 | } |
| 30 | } |
| 31 | func TestCopyGenericMalformedFilesStillFinish(t *testing.T) { |
| 32 | for name, text := range map[string]string{"view.html": "<p>Hello world</p>", "view.js": "const label=`Hello ${name}`", "escaped.js": `const label="Hello\nworld"`} { |
| 33 | t.Run(name, func(t *testing.T) { |
| 34 | r, row, seed := newCopyRigWith(t, func(seed string) { os.Remove(filepath.Join(seed, ".equipo/copy.json")) }) |
| 35 | if err := putCopyFile(seed, name, []byte(text)); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | gitOut(t, seed, "add", name) |
| 39 | gitOut(t, seed, "commit", "-qm", "generic candidate") |
| 40 | gitOut(t, seed, "push", "-q", "origin", row.Branch) |
| 41 | row.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 42 | rep := r.e.Exec(r.ctx, row, func(wire.StepReport) {}) |
| 43 | if rep.State != wire.StepDone || rep.CandidateSHA != row.CandidateSHA { |
| 44 | t.Fatalf("generic extraction stranded task: %+v", rep) |
| 45 | } |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | func TestCopyPackageFailureCannotHideBehindNamedTest(t *testing.T) { |
| 50 | r, row, _ := newCopyRig(t) |
| 51 | row.Specialist = wire.SpecialistChecks |
| 52 | for _, log := range []string{ |
| 53 | "--- FAIL: TestReleaseCopyReviewed (0.1s)\nFAIL\nFAIL example.com/copy 0.1s\nFAIL example.com/product 0.2s", |
| 54 | "--- FAIL example.com/copy TestReleaseCopyReviewed\n--- FAIL: TestReleaseCopyReviewed (0.1s)\nFAIL example.com/product 0.2s\nFAIL example.com/copy 0.1s", |
| 55 | } { |
| 56 | rep := r.e.classifyChecks(r.ctx, row, wire.StepReport{State: wire.StepDone, Checks: wire.ChecksFailed, Gate: "tests", Output: log}) |
| 57 | if rep.Failure == nil || rep.Failure.CopyOnly() { |
| 58 | t.Fatalf("package failure hidden: %+v", rep.Failure) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | func TestCopyLargeBatchSplitsAndKeepsBudget(t *testing.T) { |
| 63 | candidate := largeCopyCandidate() |
| 64 | for _, limited := range []bool{false, true} { |
| 65 | t.Run(fmt.Sprint(limited), func(t *testing.T) { |
| 66 | r, row, _ := newCopyRigCandidate(t, nil, candidate) |
| 67 | if limited { |
| 68 | row.Copy.Remaining = copydesk.PassTokens |
| 69 | } |
| 70 | calls := 0 |
| 71 | r.e.beforeAdmit = func(string) { calls++ } |
| 72 | rep := r.e.Exec(r.ctx, row, func(wire.StepReport) {}) |
| 73 | if limited { |
| 74 | if calls != 0 || rep.State != wire.StepFailed || rep.Reason != "copy_budget" { |
| 75 | t.Fatalf("unaffordable batch did not stop clearly before spend: calls=%d %+v", calls, rep) |
| 76 | } |
| 77 | return |
| 78 | } |
| 79 | if rep.State != wire.StepDone || rep.Copy == nil || len(rep.Copy.Review.Strings) != 40 || calls < 2 { |
| 80 | t.Fatalf("large batch did not finish in bounded passes: calls=%d %+v", calls, rep) |
| 81 | } |
| 82 | if rep.Copy.Tokens <= 0 || rep.Copy.Tokens > copydesk.TaskTokens { |
| 83 | t.Fatal("invalid total batch charge", rep.Copy.Tokens) |
| 84 | } |
| 85 | var charges int |
| 86 | if err := r.state.db.QueryRow("SELECT count(*) FROM budget_charge WHERE step=? AND attempt=?", row.StepID, row.Attempt).Scan(&charges); err != nil || charges != calls { |
| 87 | t.Fatalf("passes not metered separately: %d calls=%d %v", charges, calls, err) |
| 88 | } |
| 89 | if len(rep.Usage) != 1 { |
| 90 | t.Fatal("usage not aggregated for server idempotency", len(rep.Usage)) |
| 91 | } |
| 92 | if tokens, known := copyUsage(rep.Usage); !known || tokens != rep.Copy.Tokens { |
| 93 | t.Fatalf("aggregate usage lost a pass: %d %v", tokens, known) |
| 94 | } |
| 95 | var spent int64 |
| 96 | if err := r.state.db.QueryRow("SELECT spent FROM copy_attempts WHERE step_id=? AND attempt=?", row.StepID, row.Attempt).Scan(&spent); err != nil || spent != rep.Copy.Tokens { |
| 97 | t.Fatalf("batch ledger lost usage: %d %v", spent, err) |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func largeCopyCandidate() string { |
| 104 | var candidate strings.Builder |
| 105 | candidate.WriteString("package equipo\nvar pageCopy=map[string]string{\n") |
| 106 | for i := 0; i < 40; i++ { |
| 107 | fmt.Fprintf(&candidate, "%q: %q,\n", fmt.Sprintf("key%d", i), strings.Repeat("Word ", 90)) |
| 108 | } |
| 109 | candidate.WriteString("}\n") |
| 110 | return candidate.String() |
| 111 | } |
| 112 | |
| 113 | func TestCopyBatchStopsBeforeUnaffordableNextPass(t *testing.T) { |
| 114 | r, row, _ := newCopyRigCandidate(t, func(seed string) { |
| 115 | if err := putCopyFile(seed, "AGENTS.md", []byte("## Copy\ncopy-usage-overrun")); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | }, largeCopyCandidate()) |
| 119 | row.Copy.Remaining = 70000 |
| 120 | calls := 0 |
| 121 | r.e.beforeAdmit = func(string) { calls++ } |
| 122 | rep := r.e.Exec(r.ctx, row, func(wire.StepReport) {}) |
| 123 | if calls != 1 || rep.State != wire.StepFailed || rep.Reason != "copy_budget" || rep.Pushed { |
| 124 | t.Fatalf("spent into unaffordable next pass: calls=%d %+v", calls, rep) |
| 125 | } |
| 126 | if rep.Copy == nil || rep.Copy.Tokens <= copydesk.PassTokens || len(rep.Copy.Review.Strings) == 0 { |
| 127 | t.Fatal("lost partial review charge") |
| 128 | } |
| 129 | } |
| 130 | |