| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestPreReviewEnabled_defaults(t *testing.T) { |
| 11 | cfg := Defaults() |
| 12 | if !cfg.PreReviewEnabled() { |
| 13 | t.Error("expected pre-review to be enabled by default") |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | func TestPreReviewEnabled_explicit(t *testing.T) { |
| 18 | enabled := true |
| 19 | cfg := Config{PreReview: &PreReviewConfig{Enabled: &enabled}} |
| 20 | if !cfg.PreReviewEnabled() { |
| 21 | t.Error("expected pre-review to be enabled") |
| 22 | } |
| 23 | |
| 24 | disabled := false |
| 25 | cfg = Config{PreReview: &PreReviewConfig{Enabled: &disabled}} |
| 26 | if cfg.PreReviewEnabled() { |
| 27 | t.Error("expected pre-review to be disabled") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestPreReviewModel_defaults(t *testing.T) { |
| 32 | cfg := Defaults() |
| 33 | if got := cfg.PreReviewModel(); got != "haiku" { |
| 34 | t.Errorf("PreReviewModel() = %q, want %q", got, "haiku") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestPreReviewModel_configured(t *testing.T) { |
| 39 | cfg := Config{PreReview: &PreReviewConfig{ReviewModel: "sonnet"}} |
| 40 | if got := cfg.PreReviewModel(); got != "sonnet" { |
| 41 | t.Errorf("PreReviewModel() = %q, want %q", got, "sonnet") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestPreReviewLinters_defaults(t *testing.T) { |
| 46 | cfg := Defaults() |
| 47 | if linters := cfg.PreReviewLinters(); linters != nil { |
| 48 | t.Errorf("expected nil linters by default, got %v", linters) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestPreReviewLinters_configured(t *testing.T) { |
| 53 | cfg := Config{PreReview: &PreReviewConfig{Linters: []string{"go vet ./..."}}} |
| 54 | linters := cfg.PreReviewLinters() |
| 55 | if len(linters) != 1 || linters[0] != "go vet ./..." { |
| 56 | t.Errorf("unexpected linters: %v", linters) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestPreReviewBlockOn_defaults(t *testing.T) { |
| 61 | cfg := Defaults() |
| 62 | if got := cfg.PreReviewBlockOn(); got != "high" { |
| 63 | t.Errorf("PreReviewBlockOn() = %q, want %q", got, "high") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestPreReviewBlockOn_configured(t *testing.T) { |
| 68 | cfg := Config{PreReview: &PreReviewConfig{BlockOn: "critical"}} |
| 69 | if got := cfg.PreReviewBlockOn(); got != "critical" { |
| 70 | t.Errorf("PreReviewBlockOn() = %q, want %q", got, "critical") |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // TestLoadIgnoresUnknownPreReviewKey loads host and repository configs that |
| 75 | // carry pre_review.max_fix_rounds, a key flow does not read: both load, |
| 76 | // and the pre_review settings beside it apply. |
| 77 | func TestLoadIgnoresUnknownPreReviewKey(t *testing.T) { |
| 78 | home := t.TempDir() |
| 79 | t.Setenv("HOME", home) |
| 80 | repo := t.TempDir() |
| 81 | for dir, data := range map[string]string{ |
| 82 | home: `{"pre_review": {"max_fix_rounds": 3, "review_model": "sonnet"}}`, |
| 83 | repo: `{"pre_review": {"max_fix_rounds": 0, "review_model": "sonnet", "block_on": "critical"}}`, |
| 84 | } { |
| 85 | if err := os.MkdirAll(filepath.Join(dir, ".klaus"), 0o755); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | if err := os.WriteFile(filepath.Join(dir, ".klaus", "config.json"), []byte(data), 0o644); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | } |
| 92 | cfg, err := Load(repo) |
| 93 | if err != nil { |
| 94 | t.Fatalf("Load: %v", err) |
| 95 | } |
| 96 | if got := cfg.PreReviewModel(); got != "sonnet" { |
| 97 | t.Errorf("PreReviewModel() = %q, want sonnet", got) |
| 98 | } |
| 99 | if got := cfg.PreReviewBlockOn(); got != "critical" { |
| 100 | t.Errorf("PreReviewBlockOn() = %q, want critical", got) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestProseDefaults(t *testing.T) { |
| 105 | cfg := Defaults() |
| 106 | p := cfg.Prose() |
| 107 | if p.TimeoutSeconds != 600 || p.MaxInputBytes != 40000 || !*p.Enabled { |
| 108 | t.Errorf("Prose() = timeout %d, max input %d, enabled %v; want 600, 40000, true", p.TimeoutSeconds, p.MaxInputBytes, *p.Enabled) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // The writer's model is pre_review.prose.writer's, else klaus's prose model |
| 113 | // for the backend (agy's alone), else the backend's general default; its |
| 114 | // effort skips the middle step. The checker's skips klaus's prose model. |
| 115 | func TestProseModels(t *testing.T) { |
| 116 | for _, tc := range []struct { |
| 117 | name, config, kind string |
| 118 | writer, checker [2]string // model, effort |
| 119 | }{ |
| 120 | {"agy with no settings", `{}`, "agy", |
| 121 | [2]string{"gemini-4-hydrogen-high", ""}, [2]string{"", ""}}, |
| 122 | {"agy over its general default", `{"backends": {"agy": {"model": "gemini-general", "effort": "low"}}}`, "agy", |
| 123 | [2]string{"gemini-4-hydrogen-high", "low"}, [2]string{"gemini-general", "low"}}, |
| 124 | {"agy prose settings", `{"backends": {"agy": {"model": "gemini-general", "effort": "low"}}, |
| 125 | "pre_review": {"prose": {"writer": {"agy": {"model": "gemini-writer", "effort": "high"}}, "checker": {"agy": {"model": "gemini-checker"}}}}}`, "agy", |
| 126 | [2]string{"gemini-writer", "high"}, [2]string{"gemini-checker", "low"}}, |
| 127 | {"codex has no prose model", `{"backends": {"codex": {"model": "gpt-general", "effort": "medium"}}}`, "codex", |
| 128 | [2]string{"gpt-general", "medium"}, [2]string{"gpt-general", "medium"}}, |
| 129 | {"codex prose effort only", `{"backends": {"codex": {"model": "gpt-general"}}, |
| 130 | "pre_review": {"prose": {"writer": {"codex": {"effort": "high"}}}}}`, "codex", |
| 131 | [2]string{"gpt-general", "high"}, [2]string{"gpt-general", ""}}, |
| 132 | {"another backend's prose settings", `{"pre_review": {"prose": {"writer": {"agy": {"model": "gemini-writer"}}}}, "default_agent_model": "sonnet"}`, "claude", |
| 133 | [2]string{"sonnet", ""}, [2]string{"sonnet", ""}}, |
| 134 | } { |
| 135 | t.Run(tc.name, func(t *testing.T) { |
| 136 | var cfg Config |
| 137 | if err := json.Unmarshal([]byte(tc.config), &cfg); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | if m, e := cfg.ProseWriter(tc.kind); [2]string{m, e} != tc.writer { |
| 141 | t.Errorf("ProseWriter(%s) = %q, %q; want %q", tc.kind, m, e, tc.writer) |
| 142 | } |
| 143 | if m, e := cfg.ProseChecker(tc.kind); [2]string{m, e} != tc.checker { |
| 144 | t.Errorf("ProseChecker(%s) = %q, %q; want %q", tc.kind, m, e, tc.checker) |
| 145 | } |
| 146 | }) |
| 147 | } |
| 148 | } |