-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent_config_test.go
More file actions
241 lines (220 loc) · 7.71 KB
/
Copy pathagent_config_test.go
File metadata and controls
241 lines (220 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)
func TestAgentSettingsDefaultSeed(t *testing.T) {
app := testApp(t)
providers, err := app.listAgentProviders(context.Background())
if err != nil {
t.Fatal(err)
}
if len(providers) < 2 {
t.Fatalf("expected seeded providers, got %d", len(providers))
}
ids := map[string]bool{}
for _, p := range providers {
ids[p.ID] = true
if p.Kind != ProviderKindCLI {
t.Fatalf("provider %s kind = %q", p.ID, p.Kind)
}
}
if !ids[ProviderIDCodexCLI] || !ids[ProviderIDGrokCLI] {
t.Fatalf("missing default providers: %#v", ids)
}
cfg, err := app.getAppAgentConfig(context.Background())
if err != nil {
t.Fatal(err)
}
if cfg.ImplementerProviderID != ProviderIDCodexCLI {
t.Fatalf("implementer = %q, want codex_cli", cfg.ImplementerProviderID)
}
if cfg.ReviewerProviderID != ProviderIDGrokCLI {
t.Fatalf("reviewer = %q, want grok_cli", cfg.ReviewerProviderID)
}
}
func TestAgentSettingsUpdateChangesResolution(t *testing.T) {
app := testApp(t)
dir := t.TempDir()
codexBin := filepath.Join(dir, "fake-codex")
grokBin := filepath.Join(dir, "fake-grok")
writeExecutable(t, codexBin, "#!/bin/sh\necho codex-test 1.0\n")
writeExecutable(t, grokBin, "#!/bin/sh\necho grok-test 1.0\n")
// Clear env overrides for this test.
t.Setenv("RIPPLE_CODEX_BIN", "")
t.Setenv("TASKMANAGER_CODEX_BIN", "")
t.Setenv("RIPPLE_GROK_BIN", "")
t.Setenv("TASKMANAGER_GROK_BIN", "")
if err := app.saveAgentSettingsFromForm(context.Background(), ProviderIDCodexCLI, ProviderIDGrokCLI, codexBin, grokBin); err != nil {
t.Fatalf("save settings: %v", err)
}
impl, err := app.resolveImplementer(context.Background())
if err != nil {
t.Fatal(err)
}
if impl.BinaryPath != codexBin {
t.Fatalf("implementer path = %q, want %q", impl.BinaryPath, codexBin)
}
rev, err := app.resolveReviewer(context.Background())
if err != nil {
t.Fatal(err)
}
if rev.BinaryPath != grokBin {
t.Fatalf("reviewer path = %q, want %q", rev.BinaryPath, grokBin)
}
// Round-trip via HTTP POST.
form := url.Values{
"implementerProviderId": {ProviderIDCodexCLI},
"reviewerProviderId": {ProviderIDGrokCLI},
"codexBinaryPath": {codexBin},
"grokBinaryPath": {grokBin},
}
req := httptest.NewRequest(http.MethodPost, "/settings/agents", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res := httptest.NewRecorder()
app.routes().ServeHTTP(res, req)
if res.Code != http.StatusSeeOther {
t.Fatalf("status = %d; body = %s", res.Code, res.Body.String())
}
if loc := res.Header().Get("Location"); !strings.Contains(loc, "flash=saved") {
t.Fatalf("redirect location = %q", loc)
}
getRes := httptest.NewRecorder()
app.routes().ServeHTTP(getRes, httptest.NewRequest(http.MethodGet, "/settings?flash=saved", nil))
if getRes.Code != http.StatusOK {
t.Fatalf("get settings status = %d", getRes.Code)
}
body := getRes.Body.String()
if !strings.Contains(body, "settings-flash") || !strings.Contains(body, "Saved") {
t.Fatal("expected saved flash on settings page")
}
if !strings.Contains(body, codexBin) {
t.Fatal("settings page should show saved codex path")
}
}
func TestAgentBinaryEnvOverrideWinsOverSettings(t *testing.T) {
app := testApp(t)
dir := t.TempDir()
settingsCodex := filepath.Join(dir, "settings-codex")
envCodex := filepath.Join(dir, "env-codex")
settingsGrok := filepath.Join(dir, "settings-grok")
envGrok := filepath.Join(dir, "env-grok")
writeExecutable(t, settingsCodex, "#!/bin/sh\necho settings-codex\n")
writeExecutable(t, envCodex, "#!/bin/sh\necho env-codex\n")
writeExecutable(t, settingsGrok, "#!/bin/sh\necho settings-grok\n")
writeExecutable(t, envGrok, "#!/bin/sh\necho env-grok\n")
if err := app.saveAgentSettingsFromForm(context.Background(), ProviderIDCodexCLI, ProviderIDGrokCLI, settingsCodex, settingsGrok); err != nil {
t.Fatal(err)
}
t.Setenv("RIPPLE_CODEX_BIN", envCodex)
t.Setenv("RIPPLE_GROK_BIN", envGrok)
t.Setenv("TASKMANAGER_CODEX_BIN", "")
t.Setenv("TASKMANAGER_GROK_BIN", "")
impl, err := app.resolveImplementer(context.Background())
if err != nil {
t.Fatal(err)
}
if impl.BinaryPath != envCodex {
t.Fatalf("implementer path = %q, want env override %q", impl.BinaryPath, envCodex)
}
rev, err := app.resolveReviewer(context.Background())
if err != nil {
t.Fatal(err)
}
if rev.BinaryPath != envGrok {
t.Fatalf("reviewer path = %q, want env override %q", rev.BinaryPath, envGrok)
}
// Direct helper also respects precedence.
path, err := resolveCodexBinaryWithSettings(settingsCodex)
if err != nil {
t.Fatal(err)
}
if path != envCodex {
t.Fatalf("resolveCodexBinaryWithSettings = %q, want %q", path, envCodex)
}
}
func TestResolveCodexUsesSettingsPathWhenNoEnv(t *testing.T) {
dir := t.TempDir()
bin := filepath.Join(dir, "codex-from-settings")
writeExecutable(t, bin, "#!/bin/sh\necho ok\n")
t.Setenv("RIPPLE_CODEX_BIN", "")
t.Setenv("TASKMANAGER_CODEX_BIN", "")
path, err := resolveCodexBinaryWithSettings(bin)
if err != nil {
t.Fatal(err)
}
if path != bin {
t.Fatalf("path = %q, want %q", path, bin)
}
}
func TestSaveAgentSettingsRejectsMissingBinaryPath(t *testing.T) {
app := testApp(t)
t.Setenv("RIPPLE_CODEX_BIN", "")
t.Setenv("TASKMANAGER_CODEX_BIN", "")
err := app.saveAgentSettingsFromForm(context.Background(), ProviderIDCodexCLI, ProviderIDGrokCLI, "/no/such/codex-binary", "")
if err == nil {
t.Fatal("expected error for missing codex path")
}
if !strings.Contains(err.Error(), "Codex path") {
t.Fatalf("error = %v", err)
}
}
func TestCLIRoleSuffix(t *testing.T) {
if got := cliRoleSuffix(ProviderIDCodexCLI, ProviderIDCodexCLI, ProviderIDGrokCLI); got != " (implementer)" {
t.Fatalf("codex implementer only = %q", got)
}
if got := cliRoleSuffix(ProviderIDCodexCLI, ProviderIDGrokCLI, ProviderIDCodexCLI); got != " (reviewer)" {
t.Fatalf("codex reviewer only = %q", got)
}
if got := cliRoleSuffix(ProviderIDGrokCLI, ProviderIDGrokCLI, ProviderIDGrokCLI); got != " (implementer + reviewer)" {
t.Fatalf("grok both roles = %q", got)
}
if got := cliRoleSuffix(ProviderIDCodexCLI, ProviderIDGrokCLI, ProviderIDGrokCLI); got != "" {
t.Fatalf("unused tool = %q, want empty", got)
}
}
func TestSettingsProbesReflectSwappedRoles(t *testing.T) {
app := testApp(t)
dir := t.TempDir()
codexBin := filepath.Join(dir, "fake-codex")
grokBin := filepath.Join(dir, "fake-grok")
writeExecutable(t, codexBin, "#!/bin/sh\necho codex-cli 9.9.9\n")
writeExecutable(t, grokBin, "#!/bin/sh\necho 9.9.9\n")
t.Setenv("RIPPLE_CODEX_BIN", codexBin)
t.Setenv("RIPPLE_GROK_BIN", grokBin)
t.Setenv("TASKMANAGER_CODEX_BIN", "")
t.Setenv("TASKMANAGER_GROK_BIN", "")
if err := app.saveAgentSettingsFromForm(context.Background(), ProviderIDGrokCLI, ProviderIDCodexCLI, "", ""); err != nil {
t.Fatal(err)
}
res := httptest.NewRecorder()
app.routes().ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/settings", nil))
if res.Code != http.StatusOK {
t.Fatalf("status = %d", res.Code)
}
body := res.Body.String()
if strings.Contains(body, "Codex (implementer)") || strings.Contains(body, "Codex CLI (implementer)") {
t.Fatal("Codex must not be labeled implementer when Grok is implementer")
}
if !strings.Contains(body, "Codex CLI (reviewer)") {
t.Fatal("expected Codex CLI (reviewer) probe label")
}
if !strings.Contains(body, "Grok CLI (implementer)") {
t.Fatal("expected Grok CLI (implementer) probe label")
}
if !strings.Contains(body, "Active implementer (CLI)") || !strings.Contains(body, "Grok CLI") {
t.Fatal("expected active implementer probe for Grok")
}
}
func writeExecutable(t *testing.T, path, content string) {
t.Helper()
if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
t.Fatal(err)
}
}