gunk-dev/the-valley

checks_test.go

source88a5a486b52fmain's published tip history
8.3 KiB
1package stream
2
3import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "testing"
8 "time"
9)
10
11func TestCheckKind(t *testing.T) {
12 for _, tc := range []struct {
13 line, want string
14 preReview bool
15 }{
16 {"go test ./...", "test", false},
17 {"go build ./... && go vet ./... && go test -race ./...", "test", false},
18 {"cd internal/cmd && go vet ./...", "lint", false},
19 {"GOFLAGS=-mod=mod CGO_ENABLED=0 go build -o bin/klaus ./cmd/klaus", "build", false},
20 {"timeout 600 go test ./e2e/... 2>&1 | tail -50", "test", false},
21 {"nix develop -c cargo clippy --all-targets", "lint", false},
22 {"(cd web && npm run build)", "build", false},
23 {"make", "build", false},
24 {"make -j8 test-e2e", "test", false},
25 {"make -C docs lint", "lint", false},
26 {"python3 -m pytest -x tests/", "test", false},
27 {"nix flake check", "test", false},
28 {"git status && git diff", "", false},
29 {"grep -rn 'go test' Makefile", "", false},
30 {"echo go test", "", false},
31 {"klaus _pre-review", "", true},
32 {"go build ./... && klaus _pre-review", "build", true},
33 {"go test ./... && KLAUS_BACKEND=codex ~/bin/klaus _pre-review --dir .", "test", true},
34 {"grep -rn 'klaus _pre-review' README.md", "", false},
35 {"echo klaus _pre-review", "", false},
36 {"", "", false},
37 } {
38 if got := CheckKind(tc.line); got != tc.want {
39 t.Errorf("CheckKind(%q) = %q, want %q", tc.line, got, tc.want)
40 }
41 if got := RunsPreReview(tc.line); got != tc.preReview {
42 t.Errorf("RunsPreReview(%q) = %v, want %v", tc.line, got, tc.preReview)
43 }
44 }
45}
46
47// TestCheckWatcherPairsCallsAndResults feeds a Claude stream with parallel
48// calls, a background command and a non-check call, and a Codex stream whose
49// calls carry no IDs, and checks each check ends at its own result.
50func TestCheckWatcherPairsCallsAndResults(t *testing.T) {
51 type ended struct {
52 kind, cmd string
53 ms int64
54 success string
55 }
56 run := func(lines []string) (started []string, done []ended) {
57 t0 := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC)
58 w := &CheckWatcher{
59 OnStart: func(c Check) { started = append(started, c.Kind+": "+c.Command) },
60 OnEnd: func(c Check, end time.Time, success *bool) {
61 s := "unknown"
62 if success != nil && *success {
63 s = "ok"
64 } else if success != nil {
65 s = "failed"
66 }
67 done = append(done, ended{c.Kind, c.Command, end.Sub(c.Start).Milliseconds(), s})
68 },
69 }
70 for i, l := range lines {
71 w.Observe(NormalizeLine([]byte(l)), t0.Add(time.Duration(i)*time.Second))
72 }
73 return started, done
74 }
75
76 t.Run("claude", func(t *testing.T) {
77 started, done := run([]string{
78 `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"a","name":"Bash","input":{"command":"go build ./..."}},{"type":"tool_use","id":"b","name":"Bash","input":{"command":"go test ./...\necho done"}}]}}`,
79 `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"c","name":"Read","input":{"file_path":"go.mod"}}]}}`,
80 `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"d","name":"Bash","input":{"command":"go test ./e2e/...","run_in_background":true}}]}}`,
81 `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"c","content":"module x"}]}}`,
82 `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"b","is_error":true,"content":"FAIL"}]}}`,
83 `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"d","content":"started"}]}}`,
84 `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"a","is_error":false,"content":""}]}}`,
85 })
86 if got := strings.Join(started, "; "); got != "build: go build ./...; test: go test ./..." {
87 t.Errorf("started %q", got)
88 }
89 want := []ended{{"test", "go test ./...", 4000, "failed"}, {"build", "go build ./...", 6000, "ok"}}
90 if len(done) != len(want) || done[0] != want[0] || done[1] != want[1] {
91 t.Errorf("ended %+v, want %+v", done, want)
92 }
93 })
94
95 t.Run("codex", func(t *testing.T) {
96 _, done := run([]string{
97 `{"type":"item.started","item":{"type":"command_execution","command":"ls"}}`,
98 `{"type":"item.completed","item":{"type":"command_execution","command":"ls","aggregated_output":"go.mod"}}`,
99 `{"type":"item.started","item":{"type":"command_execution","command":"cargo test"}}`,
100 `{"type":"item.completed","item":{"type":"agent_message","text":"waiting"}}`,
101 `{"type":"item.completed","item":{"type":"command_execution","command":"cargo test","aggregated_output":"ok"}}`,
102 })
103 if want := []ended{{"test", "cargo test", 2000, "unknown"}}; len(done) != 1 || done[0] != want[0] {
104 t.Errorf("ended %+v, want %+v", done, want)
105 }
106 })
107}
108
109// TestCheckWatcherPreReviewCalls feeds a Claude stream that runs go test and
110// _pre-review in one command line, whose result reports the pre-review's
111// check, then _pre-review alone with a result that reports nothing, and a
112// grep whose output quotes a report, then a failing go build and _pre-review
113// line that reports nothing. The command lines' checks say they include the
114// pre-review, and the report comes out of the first one's result; a sandbox
115// run times the bare _pre-review from the stream, since nothing else reports
116// it, but not the last line's, which go build may have stopped first. The
117// grep runs no _pre-review, so what it prints reports nothing.
118func TestCheckWatcherPreReviewCalls(t *testing.T) {
119 report := ReportPrefix + `{"kind":"pre-review","source":"pre-review","started_at":"2026-10-20T12:00:01Z","ended_at":"2026-10-20T12:00:03Z","duration_ms":2000,"success":true,"findings":0}`
120 result := func(id, output string) string {
121 b, _ := json.Marshal(output)
122 return `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"` + id + `","is_error":false,"content":[{"type":"text","text":` + string(b) + `}]}]}}`
123 }
124 lines := []string{
125 `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"a","name":"Bash","input":{"command":"go test ./... && klaus _pre-review"}}]}}`,
126 result("a", "ok \tgithub.com/acme/widget\t0.1s\n=== Pre-PR Review ===\n"+report+"\nAll checks passed."),
127 `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"b","name":"Bash","input":{"command":"klaus _pre-review 2>&1 | tail -1"}}]}}`,
128 result("b", "All checks passed."),
129 `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"c","name":"Bash","input":{"command":"grep -rn klaus-check-completed ."}}]}}`,
130 result("c", "./log:"+report),
131 `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"d","name":"Bash","input":{"command":"go build ./... && klaus _pre-review"}}]}}`,
132 `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"d","is_error":true,"content":"undefined: x"}]}}`,
133 }
134 run := func(sandbox bool) (checks []string, reports []map[string]interface{}) {
135 t0 := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC)
136 w := &CheckWatcher{
137 Sandbox: sandbox,
138 OnEnd: func(c Check, end time.Time, success *bool) {
139 checks = append(checks, fmt.Sprintf("%s %q %dms pre-review:%v ok:%v", c.Kind, c.Command, end.Sub(c.Start).Milliseconds(), c.IncludesPreReview, success != nil && *success))
140 },
141 OnReport: func(data map[string]interface{}) { reports = append(reports, data) },
142 }
143 for i, l := range lines {
144 w.Observe(NormalizeLine([]byte(l)), t0.Add(time.Duration(i)*time.Second))
145 }
146 return checks, reports
147 }
148
149 checks, reports := run(false)
150 if want := []string{
151 `test "go test ./... && klaus _pre-review" 1000ms pre-review:true ok:true`,
152 `build "go build ./... && klaus _pre-review" 1000ms pre-review:true ok:false`,
153 }; strings.Join(checks, "\n") != strings.Join(want, "\n") {
154 t.Errorf("local run checks:\n%s\nwant:\n%s", strings.Join(checks, "\n"), strings.Join(want, "\n"))
155 }
156 if len(reports) != 1 || reports[0]["kind"] != "pre-review" || reports[0]["duration_ms"] != float64(2000) || reports[0]["findings"] != float64(0) {
157 t.Errorf("reports %+v, want the first call's pre-review only", reports)
158 }
159
160 checks, reports = run(true)
161 want := []string{
162 `test "go test ./... && klaus _pre-review" 1000ms pre-review:true ok:true`,
163 `pre-review "klaus _pre-review 2>&1 | tail -1" 1000ms pre-review:false ok:true`,
164 `build "go build ./... && klaus _pre-review" 1000ms pre-review:true ok:false`,
165 }
166 if strings.Join(checks, "\n") != strings.Join(want, "\n") {
167 t.Errorf("sandbox run checks:\n%s\nwant:\n%s", strings.Join(checks, "\n"), strings.Join(want, "\n"))
168 }
169 if len(reports) != 1 {
170 t.Errorf("sandbox run reports %+v, want the first call's pre-review only", reports)
171 }
172}