| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "the-valley/flow/internal/backend" |
| 9 | "the-valley/flow/internal/config" |
| 10 | ) |
| 11 | |
| 12 | func TestChooseReviewer(t *testing.T) { |
| 13 | off := false |
| 14 | tests := []struct { |
| 15 | name string |
| 16 | onPath []string |
| 17 | author backend.Kind |
| 18 | cfg config.Config |
| 19 | wantKind backend.Kind |
| 20 | wantModel string |
| 21 | }{ |
| 22 | { |
| 23 | name: "explicit review_backend wins even when missing from PATH", |
| 24 | onPath: []string{"claude"}, |
| 25 | author: backend.Claude, |
| 26 | cfg: config.Config{BackendDefaults: map[string]config.BackendDefaults{"claude": {ReviewBackend: "codex"}, "codex": {ReviewModel: "m1"}}}, |
| 27 | wantKind: backend.Codex, wantModel: "m1", |
| 28 | }, |
| 29 | { |
| 30 | name: "order skips the author", |
| 31 | onPath: []string{"claude", "codex", "agy"}, |
| 32 | author: backend.Codex, |
| 33 | wantKind: backend.Claude, wantModel: "haiku", |
| 34 | }, |
| 35 | { |
| 36 | name: "order skips CLIs missing from PATH", |
| 37 | onPath: []string{"claude", "agy"}, |
| 38 | author: backend.Agy, |
| 39 | wantKind: backend.Claude, wantModel: "haiku", |
| 40 | }, |
| 41 | { |
| 42 | name: "order selects agy with its configured model", |
| 43 | onPath: []string{"claude", "codex", "agy"}, |
| 44 | author: backend.Claude, |
| 45 | cfg: config.Config{CrossReview: &config.CrossReviewConfig{Order: []string{"agy", "codex"}}, BackendDefaults: map[string]config.BackendDefaults{"agy": {ReviewModel: "m"}}}, |
| 46 | wantKind: backend.Agy, wantModel: "m", |
| 47 | }, |
| 48 | { |
| 49 | name: "falls back to the author's family when nothing else is installed", |
| 50 | onPath: []string{"claude"}, |
| 51 | author: backend.Claude, |
| 52 | wantKind: backend.Claude, wantModel: "haiku", |
| 53 | }, |
| 54 | { |
| 55 | name: "disabled keeps the author's family", |
| 56 | onPath: []string{"claude", "codex", "agy"}, |
| 57 | author: backend.Codex, |
| 58 | cfg: config.Config{CrossReview: &config.CrossReviewConfig{Enabled: &off}}, |
| 59 | wantKind: backend.Codex, |
| 60 | }, |
| 61 | } |
| 62 | for _, tt := range tests { |
| 63 | t.Run(tt.name, func(t *testing.T) { |
| 64 | dir := t.TempDir() |
| 65 | for _, bin := range tt.onPath { |
| 66 | if err := os.WriteFile(filepath.Join(dir, bin), []byte("#!/bin/sh\n"), 0o755); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | } |
| 70 | t.Setenv("PATH", dir) |
| 71 | kind, model, err := ChooseReviewer(tt.author, tt.cfg) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if kind != tt.wantKind || model != tt.wantModel { |
| 76 | t.Errorf("ChooseReviewer(%s) = %s %q, want %s %q", tt.author, kind, model, tt.wantKind, tt.wantModel) |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestChooseReviewerAllowsAgy(t *testing.T) { |
| 83 | t.Setenv("PATH", t.TempDir()) |
| 84 | for name, tt := range map[string]struct { |
| 85 | author backend.Kind |
| 86 | cfg config.Config |
| 87 | }{ |
| 88 | "explicit review_backend": {backend.Claude, config.Config{BackendDefaults: map[string]config.BackendDefaults{"claude": {ReviewBackend: "agy"}}}}, |
| 89 | "agy author, no other CLI": {backend.Agy, config.Config{}}, |
| 90 | } { |
| 91 | if kind, _, err := ChooseReviewer(tt.author, tt.cfg); err != nil || kind != backend.Agy { |
| 92 | t.Errorf("%s: kind = %s, err = %v, want agy", name, kind, err) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestChooseReviewerRejectsUnknownBackend(t *testing.T) { |
| 98 | cfg := config.Config{CrossReview: &config.CrossReviewConfig{Order: []string{"gemini"}}} |
| 99 | if _, _, err := ChooseReviewer(backend.Claude, cfg); err == nil { |
| 100 | t.Error("unknown backend in cross_review.order must be an error, not silently skipped") |
| 101 | } |
| 102 | } |