| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // Stub CLIs record their argv and stdin; the review must reach them only with |
| 11 | // read-only flags, and its prompt on stdin, where a diff over the 128KB a |
| 12 | // single exec argument may hold still fits. |
| 13 | func TestReviewSelectedBackend(t *testing.T) { |
| 14 | diff := strings.Repeat("+a line of a long diff\n", 150*1024/23) |
| 15 | for _, tt := range []struct { |
| 16 | kind string |
| 17 | want []string |
| 18 | }{ |
| 19 | {kind: "claude", want: []string{"--safe-mode", "--tools Read,Grep,Glob --permission-mode dontAsk", "--strict-mcp-config", "--disable-slash-commands", "--no-session-persistence", "--model haiku"}}, |
| 20 | {kind: "codex", want: []string{"--sandbox read-only", "--ignore-user-config", "--ephemeral", "--skip-git-repo-check"}}, |
| 21 | {kind: "agy", want: []string{"--mode plan", "--sandbox", "--agent klaus-consult-"}}, |
| 22 | } { |
| 23 | t.Run(tt.kind, func(t *testing.T) { |
| 24 | dir := t.TempDir() |
| 25 | t.Setenv("HOME", t.TempDir()) |
| 26 | stub := `#!/bin/sh |
| 27 | echo "$*" > "$ARGS_FILE" |
| 28 | cat > "$STDIN_FILE" |
| 29 | while [ "$#" -gt 0 ]; do |
| 30 | if [ "$1" = --agent ]; then shift; cat "$HOME/.gemini/config/agents/$1.md" > "$AGENT_FILE" || exit 21; fi |
| 31 | if [ "$1" = --output-last-message ]; then shift; printf '%s\n' '{"findings":[],"summary":"reviewed"}' > "$1"; echo 'progress chatter, not JSON'; exit 0; fi |
| 32 | shift |
| 33 | done |
| 34 | printf '%s\n' '{"findings":[],"summary":"reviewed"}' |
| 35 | ` |
| 36 | for _, bin := range []string{"claude", "codex", "agy"} { |
| 37 | body := "#!/bin/sh\nexit 20\n" |
| 38 | if bin == tt.kind { |
| 39 | body = stub |
| 40 | } |
| 41 | if err := os.WriteFile(filepath.Join(dir, bin), []byte(body), 0o700); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | } |
| 45 | t.Setenv("AGENT_FILE", filepath.Join(dir, "agent")) |
| 46 | t.Setenv("ARGS_FILE", filepath.Join(dir, "args")) |
| 47 | t.Setenv("STDIN_FILE", filepath.Join(dir, "stdin")) |
| 48 | t.Setenv("PATH", dir+":"+os.Getenv("PATH")) |
| 49 | result, err := callReviewInDir(dir, diff, ReviewConfig{Backend: tt.kind}) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if result.Summary != "reviewed" { |
| 54 | t.Fatalf("result: %+v", result) |
| 55 | } |
| 56 | if tt.kind == "agy" { |
| 57 | definition, err := os.ReadFile(filepath.Join(dir, "agent")) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if !strings.Contains(string(definition), "tools:\n - view_file\n - grep_search\nmainAgent: true\nsubagent: false") { |
| 62 | t.Fatalf("agent must expose only reading tools: %s", definition) |
| 63 | } |
| 64 | leftovers, err := filepath.Glob(filepath.Join(os.Getenv("HOME"), ".gemini", "config", "agents", "klaus-consult-*.md")) |
| 65 | if err != nil || len(leftovers) != 0 { |
| 66 | t.Fatalf("agent cleanup: %v, %v", leftovers, err) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | args, _ := os.ReadFile(filepath.Join(dir, "args")) |
| 71 | for _, w := range tt.want { |
| 72 | if !strings.Contains(string(args), w) { |
| 73 | t.Errorf("%s argv missing %q: %s", tt.kind, w, args) |
| 74 | } |
| 75 | } |
| 76 | if strings.Contains(string(args), "a line of a long diff") { |
| 77 | t.Errorf("%s got the diff in argv", tt.kind) |
| 78 | } |
| 79 | stdin, _ := os.ReadFile(filepath.Join(dir, "stdin")) |
| 80 | if want := buildReviewPrompt(diff); !strings.HasSuffix(string(stdin), want) { |
| 81 | t.Errorf("%s stdin lacks the whole %d-byte review prompt: got %d bytes", tt.kind, len(want), len(stdin)) |
| 82 | } |
| 83 | // agy has no system prompt flag; Codex reads it on stdin too. |
| 84 | if hasSystem := strings.HasPrefix(string(stdin), reviewSystemPrompt+"\n\n"); hasSystem != (tt.kind != "claude") { |
| 85 | t.Errorf("%s stdin starts with the system prompt = %v", tt.kind, hasSystem) |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | } |