| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestParseReviewResponse_valid(t *testing.T) { |
| 10 | input := `{ |
| 11 | "findings": [ |
| 12 | {"severity": "CRITICAL", "file": "main.go", "line": 10, "description": "nil pointer"}, |
| 13 | {"severity": "low", "file": "util.go", "line": 5, "description": "unused var"} |
| 14 | ], |
| 15 | "summary": "Found 2 issues" |
| 16 | }` |
| 17 | result, err := parseReviewResponse(input) |
| 18 | if err != nil { |
| 19 | t.Fatalf("unexpected error: %v", err) |
| 20 | } |
| 21 | if len(result.Findings) != 2 { |
| 22 | t.Fatalf("expected 2 findings, got %d", len(result.Findings)) |
| 23 | } |
| 24 | if result.Findings[0].Severity != "critical" { |
| 25 | t.Errorf("severity = %q, want %q", result.Findings[0].Severity, "critical") |
| 26 | } |
| 27 | if result.Findings[0].File != "main.go" { |
| 28 | t.Errorf("file = %q, want %q", result.Findings[0].File, "main.go") |
| 29 | } |
| 30 | if result.Findings[0].Line != 10 { |
| 31 | t.Errorf("line = %d, want %d", result.Findings[0].Line, 10) |
| 32 | } |
| 33 | if result.Findings[1].Severity != "low" { |
| 34 | t.Errorf("severity = %q, want %q", result.Findings[1].Severity, "low") |
| 35 | } |
| 36 | if result.Summary != "Found 2 issues" { |
| 37 | t.Errorf("summary = %q, want %q", result.Summary, "Found 2 issues") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestParseReviewResponse_withCodeFences(t *testing.T) { |
| 42 | input := "```json\n{\"findings\": [], \"summary\": \"Clean\"}\n```" |
| 43 | result, err := parseReviewResponse(input) |
| 44 | if err != nil { |
| 45 | t.Fatalf("unexpected error: %v", err) |
| 46 | } |
| 47 | if len(result.Findings) != 0 { |
| 48 | t.Errorf("expected 0 findings, got %d", len(result.Findings)) |
| 49 | } |
| 50 | if result.Summary != "Clean" { |
| 51 | t.Errorf("summary = %q, want %q", result.Summary, "Clean") |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestParseReviewResponse_noFindings(t *testing.T) { |
| 56 | input := `{"findings": [], "summary": "No issues found."}` |
| 57 | result, err := parseReviewResponse(input) |
| 58 | if err != nil { |
| 59 | t.Fatalf("unexpected error: %v", err) |
| 60 | } |
| 61 | if len(result.Findings) != 0 { |
| 62 | t.Errorf("expected 0 findings, got %d", len(result.Findings)) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Real haiku output: prose, a fenced object, then more prose with Go braces. |
| 67 | func TestParseReviewResponse_proseWrapped(t *testing.T) { |
| 68 | input := "Looking at this diff, I found a bug:\n\n```json\n{\n \"findings\": [{\"severity\": \"Critical\", \"file\": \"a.go\", \"line\": 147, \"description\": \"x\"}],\n \"summary\": \"one\"\n}\n```\n\nFix:\n```go\nif operator == nil {\n operator = &login\n}\n```" |
| 69 | result, err := parseReviewResponse(input) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if len(result.Findings) != 1 || result.Findings[0].Severity != "critical" || result.Summary != "one" { |
| 74 | t.Errorf("result = %+v", result) |
| 75 | } |
| 76 | if _, err := parseReviewResponse("I would use {} here, nothing else."); err == nil { |
| 77 | t.Error("prose with no review object must not parse as an empty review") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestParseReviewResponse_malformed(t *testing.T) { |
| 82 | input := "this is not json at all" |
| 83 | result, err := parseReviewResponse(input) |
| 84 | if err == nil { |
| 85 | t.Fatal("expected error for malformed JSON input") |
| 86 | } |
| 87 | if result != nil { |
| 88 | t.Errorf("expected nil result, got %v", result) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestBuildReviewPrompt(t *testing.T) { |
| 93 | diff := "diff --git a/main.go b/main.go\n+fmt.Println(\"hello\")" |
| 94 | prompt := buildReviewPrompt(diff) |
| 95 | if prompt == "" { |
| 96 | t.Fatal("expected non-empty prompt") |
| 97 | } |
| 98 | if !contains(prompt, "Correctness bugs") { |
| 99 | t.Error("prompt should mention correctness bugs") |
| 100 | } |
| 101 | if !contains(prompt, diff) { |
| 102 | t.Error("prompt should contain the diff") |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestCallReviewAPI_success(t *testing.T) { |
| 107 | // Create a fake claude script that returns valid JSON |
| 108 | tmp := t.TempDir() |
| 109 | script := filepath.Join(tmp, "claude") |
| 110 | err := os.WriteFile(script, []byte(`#!/bin/sh |
| 111 | echo '{"findings":[{"severity":"high","file":"foo.go","line":1,"description":"test issue"}],"summary":"one issue"}' |
| 112 | `), 0o755) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | t.Setenv("PATH", tmp+":"+os.Getenv("PATH")) |
| 117 | |
| 118 | result, err := callReviewAPI("diff --git a/foo.go", ReviewConfig{Model: "haiku"}) |
| 119 | if err != nil { |
| 120 | t.Fatalf("unexpected error: %v", err) |
| 121 | } |
| 122 | if len(result.Findings) != 1 { |
| 123 | t.Fatalf("expected 1 finding, got %d", len(result.Findings)) |
| 124 | } |
| 125 | if result.Findings[0].Severity != "high" { |
| 126 | t.Errorf("severity = %q, want %q", result.Findings[0].Severity, "high") |
| 127 | } |
| 128 | if result.Summary != "one issue" { |
| 129 | t.Errorf("summary = %q, want %q", result.Summary, "one issue") |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestCallReviewAPI_cliFailure(t *testing.T) { |
| 134 | // Create a fake claude script that exits with an error |
| 135 | tmp := t.TempDir() |
| 136 | script := filepath.Join(tmp, "claude") |
| 137 | err := os.WriteFile(script, []byte(`#!/bin/sh |
| 138 | echo "something went wrong" >&2 |
| 139 | exit 1 |
| 140 | `), 0o755) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | t.Setenv("PATH", tmp+":"+os.Getenv("PATH")) |
| 145 | |
| 146 | _, err = callReviewAPI("diff --git a/foo.go", ReviewConfig{}) |
| 147 | if err == nil { |
| 148 | t.Fatal("expected error when claude CLI fails") |
| 149 | } |
| 150 | if !searchString(err.Error(), "calling claude CLI") { |
| 151 | t.Errorf("error = %q, want it to contain %q", err.Error(), "calling claude CLI") |
| 152 | } |
| 153 | if !searchString(err.Error(), "something went wrong") { |
| 154 | t.Errorf("error = %q, want it to contain stderr output", err.Error()) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestCallReviewAPI_defaultModel(t *testing.T) { |
| 159 | // Create a fake claude script that validates the --model flag |
| 160 | tmp := t.TempDir() |
| 161 | script := filepath.Join(tmp, "claude") |
| 162 | err := os.WriteFile(script, []byte(`#!/bin/sh |
| 163 | # Check that --model haiku is passed (the default) |
| 164 | while [ $# -gt 0 ]; do |
| 165 | case "$1" in |
| 166 | --model) shift; echo "$1" >&2; break;; |
| 167 | *) shift;; |
| 168 | esac |
| 169 | done |
| 170 | echo '{"findings":[],"summary":"clean"}' |
| 171 | `), 0o755) |
| 172 | if err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | t.Setenv("PATH", tmp+":"+os.Getenv("PATH")) |
| 176 | |
| 177 | result, err := callReviewAPI("some diff", ReviewConfig{}) |
| 178 | if err != nil { |
| 179 | t.Fatalf("unexpected error: %v", err) |
| 180 | } |
| 181 | if result.Summary != "clean" { |
| 182 | t.Errorf("summary = %q, want %q", result.Summary, "clean") |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func contains(s, substr string) bool { |
| 187 | return len(s) >= len(substr) && searchString(s, substr) |
| 188 | } |
| 189 | |
| 190 | func searchString(s, substr string) bool { |
| 191 | for i := 0; i <= len(s)-len(substr); i++ { |
| 192 | if s[i:i+len(substr)] == substr { |
| 193 | return true |
| 194 | } |
| 195 | } |
| 196 | return false |
| 197 | } |