| 1 | package stream |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func 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 | {"valley flow _pre-review", "", true}, |
| 33 | {"valley-flow _pre-review", "", true}, |
| 34 | {"/nix/store/abc/bin/valley flow _pre-review --dir .", "", true}, |
| 35 | {"/nix/store/abc/bin/valley-flow _pre-review --dir .", "", true}, |
| 36 | {"go build ./... && klaus _pre-review", "build", true}, |
| 37 | {"go build ./... && valley flow _pre-review", "build", true}, |
| 38 | {"go test ./... && VALLEY_FLOW_BACKEND=codex valley-flow _pre-review --dir .", "test", true}, |
| 39 | {"go test ./... && KLAUS_BACKEND=codex ~/bin/klaus _pre-review --dir .", "test", true}, |
| 40 | {"klaus _pre-review && valley flow _pre-review", "", true}, |
| 41 | {"grep -rn 'klaus _pre-review' README.md", "", false}, |
| 42 | {"grep -rn 'valley flow _pre-review' README.md", "", false}, |
| 43 | {"echo klaus _pre-review", "", false}, |
| 44 | {"echo valley flow _pre-review", "", false}, |
| 45 | {"valley _pre-review", "", false}, |
| 46 | {"valley flow status", "", false}, |
| 47 | {"myvalley flow _pre-review", "", false}, |
| 48 | {"my-valley-flow _pre-review", "", false}, |
| 49 | {"", "", false}, |
| 50 | } { |
| 51 | if got := CheckKind(tc.line); got != tc.want { |
| 52 | t.Errorf("CheckKind(%q) = %q, want %q", tc.line, got, tc.want) |
| 53 | } |
| 54 | if got := RunsPreReview(tc.line); got != tc.preReview { |
| 55 | t.Errorf("RunsPreReview(%q) = %v, want %v", tc.line, got, tc.preReview) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // TestCheckWatcherPairsCallsAndResults feeds a Claude stream with parallel |
| 61 | // calls, a background command and a non-check call, and a Codex stream whose |
| 62 | // calls carry no IDs, and checks each check ends at its own result. |
| 63 | func TestCheckWatcherPairsCallsAndResults(t *testing.T) { |
| 64 | type ended struct { |
| 65 | kind, cmd string |
| 66 | ms int64 |
| 67 | success string |
| 68 | } |
| 69 | run := func(lines []string) (started []string, done []ended) { |
| 70 | t0 := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC) |
| 71 | w := &CheckWatcher{ |
| 72 | OnStart: func(c Check) { started = append(started, c.Kind+": "+c.Command) }, |
| 73 | OnEnd: func(c Check, end time.Time, success *bool) { |
| 74 | s := "unknown" |
| 75 | if success != nil && *success { |
| 76 | s = "ok" |
| 77 | } else if success != nil { |
| 78 | s = "failed" |
| 79 | } |
| 80 | done = append(done, ended{c.Kind, c.Command, end.Sub(c.Start).Milliseconds(), s}) |
| 81 | }, |
| 82 | } |
| 83 | for i, l := range lines { |
| 84 | w.Observe(NormalizeLine([]byte(l)), t0.Add(time.Duration(i)*time.Second)) |
| 85 | } |
| 86 | return started, done |
| 87 | } |
| 88 | |
| 89 | t.Run("claude", func(t *testing.T) { |
| 90 | started, done := run([]string{ |
| 91 | `{"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"}}]}}`, |
| 92 | `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"c","name":"Read","input":{"file_path":"go.mod"}}]}}`, |
| 93 | `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"d","name":"Bash","input":{"command":"go test ./e2e/...","run_in_background":true}}]}}`, |
| 94 | `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"c","content":"module x"}]}}`, |
| 95 | `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"b","is_error":true,"content":"FAIL"}]}}`, |
| 96 | `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"d","content":"started"}]}}`, |
| 97 | `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"a","is_error":false,"content":""}]}}`, |
| 98 | }) |
| 99 | if got := strings.Join(started, "; "); got != "build: go build ./...; test: go test ./..." { |
| 100 | t.Errorf("started %q", got) |
| 101 | } |
| 102 | want := []ended{{"test", "go test ./...", 4000, "failed"}, {"build", "go build ./...", 6000, "ok"}} |
| 103 | if len(done) != len(want) || done[0] != want[0] || done[1] != want[1] { |
| 104 | t.Errorf("ended %+v, want %+v", done, want) |
| 105 | } |
| 106 | }) |
| 107 | |
| 108 | t.Run("codex", func(t *testing.T) { |
| 109 | _, done := run([]string{ |
| 110 | `{"type":"item.started","item":{"type":"command_execution","command":"ls"}}`, |
| 111 | `{"type":"item.completed","item":{"type":"command_execution","command":"ls","aggregated_output":"go.mod"}}`, |
| 112 | `{"type":"item.started","item":{"type":"command_execution","command":"cargo test"}}`, |
| 113 | `{"type":"item.completed","item":{"type":"agent_message","text":"waiting"}}`, |
| 114 | `{"type":"item.completed","item":{"type":"command_execution","command":"cargo test","aggregated_output":"ok"}}`, |
| 115 | }) |
| 116 | if want := []ended{{"test", "cargo test", 2000, "unknown"}}; len(done) != 1 || done[0] != want[0] { |
| 117 | t.Errorf("ended %+v, want %+v", done, want) |
| 118 | } |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | // TestCheckWatcherPreReviewCalls feeds a Claude stream that runs go test and |
| 123 | // _pre-review in one command line, whose result reports the pre-review's |
| 124 | // check, then _pre-review alone with a result that reports nothing, and a |
| 125 | // grep whose output quotes a report, then a failing go build and _pre-review |
| 126 | // line that reports nothing. The command lines' checks say they include the |
| 127 | // pre-review, and the report comes out of the first one's result; a sandbox |
| 128 | // run times the bare _pre-review from the stream, since nothing else reports |
| 129 | // it, but not the last line's, which go build may have stopped first. The |
| 130 | // grep runs no _pre-review, so what it prints reports nothing. |
| 131 | func TestCheckWatcherPreReviewCalls(t *testing.T) { |
| 132 | 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}` |
| 133 | result := func(id, output string) string { |
| 134 | b, _ := json.Marshal(output) |
| 135 | return `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"` + id + `","is_error":false,"content":[{"type":"text","text":` + string(b) + `}]}]}}` |
| 136 | } |
| 137 | lines := []string{ |
| 138 | `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"a","name":"Bash","input":{"command":"go test ./... && klaus _pre-review"}}]}}`, |
| 139 | result("a", "ok \tgithub.com/acme/widget\t0.1s\n=== Pre-PR Review ===\n"+report+"\nAll checks passed."), |
| 140 | `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"b","name":"Bash","input":{"command":"klaus _pre-review 2>&1 | tail -1"}}]}}`, |
| 141 | result("b", "All checks passed."), |
| 142 | `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"c","name":"Bash","input":{"command":"grep -rn klaus-check-completed ."}}]}}`, |
| 143 | result("c", "./log:"+report), |
| 144 | `{"type":"assistant","message":{"content":[{"type":"tool_use","id":"d","name":"Bash","input":{"command":"go build ./... && klaus _pre-review"}}]}}`, |
| 145 | `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"d","is_error":true,"content":"undefined: x"}]}}`, |
| 146 | } |
| 147 | run := func(sandbox bool) (checks []string, reports []map[string]interface{}) { |
| 148 | t0 := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC) |
| 149 | w := &CheckWatcher{ |
| 150 | Sandbox: sandbox, |
| 151 | OnEnd: func(c Check, end time.Time, success *bool) { |
| 152 | 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)) |
| 153 | }, |
| 154 | OnReport: func(data map[string]interface{}) { reports = append(reports, data) }, |
| 155 | } |
| 156 | for i, l := range lines { |
| 157 | w.Observe(NormalizeLine([]byte(l)), t0.Add(time.Duration(i)*time.Second)) |
| 158 | } |
| 159 | return checks, reports |
| 160 | } |
| 161 | |
| 162 | checks, reports := run(false) |
| 163 | if want := []string{ |
| 164 | `test "go test ./... && klaus _pre-review" 1000ms pre-review:true ok:true`, |
| 165 | `build "go build ./... && klaus _pre-review" 1000ms pre-review:true ok:false`, |
| 166 | }; strings.Join(checks, "\n") != strings.Join(want, "\n") { |
| 167 | t.Errorf("local run checks:\n%s\nwant:\n%s", strings.Join(checks, "\n"), strings.Join(want, "\n")) |
| 168 | } |
| 169 | if len(reports) != 1 || reports[0]["kind"] != "pre-review" || reports[0]["duration_ms"] != float64(2000) || reports[0]["findings"] != float64(0) { |
| 170 | t.Errorf("reports %+v, want the first call's pre-review only", reports) |
| 171 | } |
| 172 | |
| 173 | checks, reports = run(true) |
| 174 | want := []string{ |
| 175 | `test "go test ./... && klaus _pre-review" 1000ms pre-review:true ok:true`, |
| 176 | `pre-review "klaus _pre-review 2>&1 | tail -1" 1000ms pre-review:false ok:true`, |
| 177 | `build "go build ./... && klaus _pre-review" 1000ms pre-review:true ok:false`, |
| 178 | } |
| 179 | if strings.Join(checks, "\n") != strings.Join(want, "\n") { |
| 180 | t.Errorf("sandbox run checks:\n%s\nwant:\n%s", strings.Join(checks, "\n"), strings.Join(want, "\n")) |
| 181 | } |
| 182 | if len(reports) != 1 { |
| 183 | t.Errorf("sandbox run reports %+v, want the first call's pre-review only", reports) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestCheckReportsBothPrefixes(t *testing.T) { |
| 188 | payload := `{"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}` |
| 189 | otherPayload := `{"kind":"pre-review","source":"pre-review","started_at":"2026-10-20T12:01:01Z","ended_at":"2026-10-20T12:01:03Z","duration_ms":2000,"success":true,"findings":1}` |
| 190 | |
| 191 | result := func(id, output string) string { |
| 192 | b, _ := json.Marshal(output) |
| 193 | return `{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"` + id + `","is_error":false,"content":[{"type":"text","text":` + string(b) + `}]}]}}` |
| 194 | } |
| 195 | |
| 196 | tests := []struct { |
| 197 | name string |
| 198 | cmd string |
| 199 | output string |
| 200 | wantCnt int |
| 201 | }{ |
| 202 | { |
| 203 | name: "legacy prefix with legacy command", |
| 204 | cmd: "klaus _pre-review", |
| 205 | output: ReportPrefix + payload, |
| 206 | wantCnt: 1, |
| 207 | }, |
| 208 | { |
| 209 | name: "new prefix with valley flow command", |
| 210 | cmd: "valley flow _pre-review", |
| 211 | output: FlowReportPrefix + payload, |
| 212 | wantCnt: 1, |
| 213 | }, |
| 214 | { |
| 215 | name: "new prefix with valley-flow command", |
| 216 | cmd: "valley-flow _pre-review", |
| 217 | output: FlowReportPrefix + payload, |
| 218 | wantCnt: 1, |
| 219 | }, |
| 220 | { |
| 221 | name: "both prefixes with identical report act once", |
| 222 | cmd: "valley flow _pre-review", |
| 223 | output: ReportPrefix + payload + "\n" + FlowReportPrefix + payload, |
| 224 | wantCnt: 1, |
| 225 | }, |
| 226 | { |
| 227 | name: "two distinct reports across both prefixes both kept", |
| 228 | cmd: "klaus _pre-review && valley flow _pre-review", |
| 229 | output: ReportPrefix + payload + "\n" + FlowReportPrefix + otherPayload, |
| 230 | wantCnt: 2, |
| 231 | }, |
| 232 | } |
| 233 | |
| 234 | for _, tt := range tests { |
| 235 | t.Run(tt.name, func(t *testing.T) { |
| 236 | var reports []map[string]interface{} |
| 237 | w := &CheckWatcher{ |
| 238 | Sandbox: true, |
| 239 | OnReport: func(data map[string]interface{}) { reports = append(reports, data) }, |
| 240 | } |
| 241 | cmdJSON, _ := json.Marshal(tt.cmd) |
| 242 | t0 := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC) |
| 243 | w.Observe(NormalizeLine([]byte(`{"type":"assistant","message":{"content":[{"type":"tool_use","id":"a","name":"Bash","input":{"command":`+string(cmdJSON)+`}}]}}`)), t0) |
| 244 | w.Observe(NormalizeLine([]byte(result("a", tt.output))), t0.Add(time.Second)) |
| 245 | if len(reports) != tt.wantCnt { |
| 246 | t.Errorf("got %d reports %+v, want %d", len(reports), reports, tt.wantCnt) |
| 247 | } |
| 248 | }) |
| 249 | } |
| 250 | } |