| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/event" |
| 9 | ) |
| 10 | |
| 11 | var ansiSequence = regexp.MustCompile(`\x1b\[[0-9;]*m`) |
| 12 | |
| 13 | func cardText(t *testing.T, r *event.CompletionReceipt) string { |
| 14 | t.Helper() |
| 15 | return ansiSequence.ReplaceAllString(strings.Join(renderReceiptCard(r, 100), "\n"), "") |
| 16 | } |
| 17 | |
| 18 | // The user just watched the tools run; repeating the work back is noise. What |
| 19 | // the transcript cannot carry is the absence, so the clean case gets one line. |
| 20 | func TestCleanReceiptIsOneQuietLine(t *testing.T) { |
| 21 | got := cardText(t, &event.CompletionReceipt{ |
| 22 | Verdict: "done", |
| 23 | Changes: []event.ReceiptChange{{Path: "calc.py", Reviewed: true}}, |
| 24 | Verifications: []event.ReceiptVerification{{Command: "go test ./...", Passed: true}}, |
| 25 | }) |
| 26 | if lines := strings.Count(got, "\n"); lines != 0 { |
| 27 | t.Fatalf("clean receipt used %d lines:\n%s", lines+1, got) |
| 28 | } |
| 29 | if !strings.Contains(got, "1 changed") || !strings.Contains(got, "go test ./...") { |
| 30 | t.Fatalf("the clean line must name its evidence, got %q", got) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestReceiptSpendsItsLinesOnWhatIsMissing(t *testing.T) { |
| 35 | got := cardText(t, &event.CompletionReceipt{ |
| 36 | Verdict: "partial", |
| 37 | Changes: []event.ReceiptChange{{Path: "calc.py"}}, |
| 38 | Gaps: []event.ReceiptGap{ |
| 39 | {Kind: "unreviewed_change", Detail: "calc.py"}, |
| 40 | {Kind: "stale_verification", Detail: "go test ./..."}, |
| 41 | }, |
| 42 | }) |
| 43 | for _, want := range []string{"calc.py", "go test ./..."} { |
| 44 | if !strings.Contains(got, want) { |
| 45 | t.Fatalf("card missing %q:\n%s", want, got) |
| 46 | } |
| 47 | } |
| 48 | if strings.Count(got, "\n") != 2 { |
| 49 | t.Fatalf("want a header and two gap lines:\n%s", got) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Dropping one silently is the failure this card exists to prevent. |
| 54 | func TestUnknownGapKindIsStillShown(t *testing.T) { |
| 55 | got := cardText(t, &event.CompletionReceipt{ |
| 56 | Verdict: "partial", |
| 57 | Gaps: []event.ReceiptGap{{Kind: "kind_from_a_newer_kernel", Detail: "x.go"}}, |
| 58 | }) |
| 59 | if !strings.Contains(got, "kind_from_a_newer_kernel") || !strings.Contains(got, "x.go") { |
| 60 | t.Fatalf("an unknown kind must still reach the user:\n%s", got) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestLongGapListIsBoundedAndSaysSo(t *testing.T) { |
| 65 | var gaps []event.ReceiptGap |
| 66 | for i := range 9 { |
| 67 | gaps = append(gaps, event.ReceiptGap{Kind: "unreviewed_change", Detail: string(rune('a'+i)) + ".go"}) |
| 68 | } |
| 69 | got := cardText(t, &event.CompletionReceipt{Verdict: "partial", Gaps: gaps}) |
| 70 | if strings.Count(got, ".go") != maxReceiptGapLines { |
| 71 | t.Fatalf("want exactly %d gap lines:\n%s", maxReceiptGapLines, got) |
| 72 | } |
| 73 | if !strings.Contains(got, "4") { |
| 74 | t.Fatalf("the dropped count must be stated, not hidden:\n%s", got) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestDeclaredRisksSurvive(t *testing.T) { |
| 79 | got := cardText(t, &event.CompletionReceipt{ |
| 80 | Verdict: "partial", |
| 81 | Gaps: []event.ReceiptGap{{Kind: "declared_unverified", Detail: "desktop UI"}}, |
| 82 | Risks: []string{"the migration is one-way"}, |
| 83 | }) |
| 84 | if !strings.Contains(got, "the migration is one-way") { |
| 85 | t.Fatalf("a declared risk must reach the user:\n%s", got) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestNoCardWithoutAReceiptOrAVerdict(t *testing.T) { |
| 90 | if got := renderReceiptCard(nil, 80); got != nil { |
| 91 | t.Fatalf("nil receipt rendered %v", got) |
| 92 | } |
| 93 | if got := renderReceiptCard(&event.CompletionReceipt{Verdict: "incomplete"}, 80); got != nil { |
| 94 | t.Fatalf("an incomplete turn with no gaps has nothing to say, got %v", got) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Printed whole, a real agent command wraps four times and turns the card into |
| 99 | // the wall of text it exists to replace. |
| 100 | func TestLongCommandIsTrimmedToOneLine(t *testing.T) { |
| 101 | long := "cd /private/tmp/very/long/path/that/goes/on && python3 -m pytest tests/test_calc.py -q 2>&1 | tail -5 || python3 -m unittest tests.test_calc -v" |
| 102 | got := cardText(t, &event.CompletionReceipt{ |
| 103 | Verdict: "partial", |
| 104 | Gaps: []event.ReceiptGap{{Kind: "stale_verification", Detail: long}}, |
| 105 | }) |
| 106 | if strings.Contains(got, "/private/tmp/very/long") { |
| 107 | t.Fatalf("the cd prefix should be dropped; the run is already there:\n%s", got) |
| 108 | } |
| 109 | if !strings.Contains(got, "python3 -m pytest") { |
| 110 | t.Fatalf("the command itself must survive:\n%s", got) |
| 111 | } |
| 112 | // wrapForViewport pads to the viewport, so measure the content, not the pad. |
| 113 | for line := range strings.SplitSeq(got, "\n") { |
| 114 | if trimmed := strings.TrimRight(line, " "); len([]rune(trimmed)) > 100 { |
| 115 | t.Fatalf("gap line is %d runes, too long to stay on one row:\n%s", len([]rune(trimmed)), trimmed) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 |