-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapplication_test.go
More file actions
468 lines (446 loc) · 11.9 KB
/
application_test.go
File metadata and controls
468 lines (446 loc) · 11.9 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package main
import (
"testing"
"github.com/sashabaranov/go-openai"
)
func TestParseModel(t *testing.T) {
tests := []struct {
name string
modelFlag string
config *Config
agent Agent
wantProvider string
wantModel string
wantInfo providerInfo
}{
{
name: "OpenAI default model",
modelFlag: "openai/gpt-4",
config: nil,
agent: Agent{},
wantProvider: "openai",
wantModel: "gpt-4",
wantInfo: providerInfo{
baseURL: "https://api.openai.com/v1",
apiKeyEnvar: "OPENAI_API_KEY",
},
},
{
name: "Custom provider from config",
modelFlag: "custom/model-1",
config: &Config{
Providers: map[string]ProviderConfig{
"custom": {
BaseURL: "https://custom.api/v1",
APIKeyEnvar: "CUSTOM_API_KEY",
},
},
},
agent: Agent{},
wantProvider: "custom",
wantModel: "model-1",
wantInfo: providerInfo{
baseURL: "https://custom.api/v1",
apiKeyEnvar: "CUSTOM_API_KEY",
},
},
{
name: "Partial provider override in config",
modelFlag: "openai/gpt-4",
config: &Config{
Providers: map[string]ProviderConfig{
"openai": {
BaseURL: "https://custom-openai.api/v2",
// APIKeyEnvar not specified, should use default
},
},
},
agent: Agent{},
wantProvider: "openai",
wantModel: "gpt-4",
wantInfo: providerInfo{
baseURL: "https://custom-openai.api/v2",
apiKeyEnvar: "OPENAI_API_KEY", // Should keep default
},
},
{
name: "Custom provider with builtin still available",
modelFlag: "custom/model-1",
config: &Config{
Providers: map[string]ProviderConfig{
"custom": {
BaseURL: "https://custom.api/v1",
APIKeyEnvar: "CUSTOM_API_KEY",
},
},
},
agent: Agent{},
wantProvider: "custom",
wantModel: "model-1",
wantInfo: providerInfo{
baseURL: "https://custom.api/v1",
apiKeyEnvar: "CUSTOM_API_KEY",
},
},
{
name: "Built-in provider unchanged",
modelFlag: "ollama/llama2",
config: &Config{}, // Empty config
agent: Agent{},
wantProvider: "ollama",
wantModel: "llama2",
wantInfo: providerInfo{
baseURL: "http://localhost:11434/v1",
apiKeyEnvar: "OLLAMA_API_KEY",
},
},
{
name: "Agent default model used when no CLI model provided",
modelFlag: "",
config: nil,
agent: Agent{
DefaultModel: "groq/llama3-8b",
},
wantProvider: "groq",
wantModel: "llama3-8b",
wantInfo: providerInfo{
baseURL: "https://api.groq.com/openai/v1",
apiKeyEnvar: "GROQ_API_KEY",
},
},
{
name: "CLI model overrides agent default model",
modelFlag: "openai/gpt-4",
config: nil,
agent: Agent{
DefaultModel: "groq/llama3-8b",
},
wantProvider: "openai",
wantModel: "gpt-4",
wantInfo: providerInfo{
baseURL: "https://api.openai.com/v1",
apiKeyEnvar: "OPENAI_API_KEY",
},
},
{
name: "Agent default model overrides config default model",
modelFlag: "",
config: &Config{
Settings: Settings{
DefaultModel: "ollama/codellama",
},
},
agent: Agent{
DefaultModel: "groq/llama3-8b",
},
wantProvider: "groq",
wantModel: "llama3-8b",
wantInfo: providerInfo{
baseURL: "https://api.groq.com/openai/v1",
apiKeyEnvar: "GROQ_API_KEY",
},
},
{
name: "Agent default model overrides global fallback",
modelFlag: "",
config: nil,
agent: Agent{
DefaultModel: "ollama/llama3.2",
},
wantProvider: "ollama",
wantModel: "llama3.2",
wantInfo: providerInfo{
baseURL: "http://localhost:11434/v1",
apiKeyEnvar: "OLLAMA_API_KEY",
},
},
{
name: "Config default model used when agent has no default",
modelFlag: "",
config: &Config{
Settings: Settings{
DefaultModel: "ollama/codellama",
},
},
agent: Agent{}, // No default model
wantProvider: "ollama",
wantModel: "codellama",
wantInfo: providerInfo{
baseURL: "http://localhost:11434/v1",
apiKeyEnvar: "OLLAMA_API_KEY",
},
},
{
name: "Anthropic default provider",
modelFlag: "anthropic/claude-sonnet-4-20250514",
config: nil,
agent: Agent{},
wantProvider: "anthropic",
wantModel: "claude-sonnet-4-20250514",
wantInfo: providerInfo{
baseURL: "https://api.anthropic.com",
apiKeyEnvar: "ANTHROPIC_API_KEY",
},
},
{
name: "Anthropic provider with config override",
modelFlag: "anthropic/claude-sonnet-4-20250514",
config: &Config{
Providers: map[string]ProviderConfig{
"anthropic": {
BaseURL: "https://custom-anthropic.api",
},
},
},
agent: Agent{},
wantProvider: "anthropic",
wantModel: "claude-sonnet-4-20250514",
wantInfo: providerInfo{
baseURL: "https://custom-anthropic.api",
apiKeyEnvar: "ANTHROPIC_API_KEY",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &Application{
modelFlag: tt.modelFlag,
config: tt.config,
agent: tt.agent,
}
gotProvider, gotModel, gotInfo := app.parseModel()
if gotProvider != tt.wantProvider {
t.Errorf("parseModel() provider = %v, want %v", gotProvider, tt.wantProvider)
}
if gotModel != tt.wantModel {
t.Errorf("parseModel() model = %v, want %v", gotModel, tt.wantModel)
}
if gotInfo.apiKeyEnvar != tt.wantInfo.apiKeyEnvar {
t.Errorf("parseModel() info.apiKeyEnvar = %+v, want %+v", gotInfo.apiKeyEnvar, tt.wantInfo.apiKeyEnvar)
}
if gotInfo.baseURL != tt.wantInfo.baseURL {
t.Errorf("parseModel() info.baseURL = %+v, want %+v", gotInfo.baseURL, tt.wantInfo.baseURL)
}
if gotInfo.baseURL == "" {
t.Errorf("parseModel() info baseURL should not be empty")
}
// Improved additionalHeaders test: check keys and values
if len(gotInfo.additionalHeaders) != len(tt.wantInfo.additionalHeaders) {
t.Errorf("parseModel() info.additionalHeaders len = %d, want %d", len(gotInfo.additionalHeaders), len(tt.wantInfo.additionalHeaders))
}
for k, v := range tt.wantInfo.additionalHeaders {
gotVal, ok := gotInfo.additionalHeaders[k]
if !ok {
t.Errorf("parseModel() info.additionalHeaders missing key %q", k)
} else if gotVal != v {
t.Errorf("parseModel() info.additionalHeaders[%q] = %q, want %q", k, gotVal, v)
}
}
for k := range gotInfo.additionalHeaders {
if _, ok := tt.wantInfo.additionalHeaders[k]; !ok {
t.Errorf("parseModel() info.additionalHeaders has unexpected key %q", k)
}
}
})
}
}
func TestProviderAdditionalHeadersMerging(t *testing.T) {
cfg := &Config{
Providers: map[string]ProviderConfig{
"copilot": {
AdditionalHeaders: map[string]string{
"Copilot-Integration-Id": "custom-override",
"X-Extra": "foo",
},
},
},
}
app := &Application{
modelFlag: "copilot/some-model",
config: cfg,
agent: Agent{},
}
_, _, info := app.parseModel()
expected := map[string]string{
"Content-Type": "application/json",
"Copilot-Integration-Id": "custom-override", // overridden
"X-Extra": "foo",
}
if len(info.additionalHeaders) != len(expected) {
t.Errorf("additionalHeaders len = %d, want %d", len(info.additionalHeaders), len(expected))
}
for k, v := range expected {
got, ok := info.additionalHeaders[k]
if !ok {
t.Errorf("missing additionalHeader %q", k)
} else if got != v {
t.Errorf("additionalHeader[%q] = %q, want %q", k, got, v)
}
}
}
func TestOllamaHostFromEnvironment(t *testing.T) {
tests := []struct {
name string
hostEnv string
wantHost string
needsTrim bool
}{
{
name: "Default host when env not set",
hostEnv: "",
wantHost: "http://localhost:11434/v1",
},
{
name: "Custom host from environment",
hostEnv: "http://192.168.1.100:11434",
wantHost: "http://192.168.1.100:11434/v1",
needsTrim: false,
},
{
name: "Custom host with trailing slash",
hostEnv: "http://ollama-server:11434/",
wantHost: "http://ollama-server:11434/v1",
needsTrim: true,
},
{
name: "Host with existing /v1 path",
hostEnv: "http://custom-ollama:8000/v1",
wantHost: "http://custom-ollama:8000/v1",
needsTrim: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("OLLAMA_HOST", tt.hostEnv)
app := &Application{modelFlag: "ollama/llama2"}
_, _, info := app.parseModel()
if info.baseURL != tt.wantHost {
t.Errorf("Expected Ollama host %q, got %q", tt.wantHost, info.baseURL)
}
})
}
}
func TestEmptyApiKeyAcceptance(t *testing.T) {
t.Setenv("OLLAMA_API_KEY", "")
t.Setenv("OPENAI_API_KEY", "")
t.Setenv("ANTHROPIC_API_KEY", "")
tests := []struct {
name string
modelStr string
expectError bool
errorDescription string
}{
{
name: "Ollama accepts empty API key",
modelStr: "ollama/llama2",
expectError: false,
errorDescription: "",
},
{
name: "OpenAI requires API key",
modelStr: "openai/gpt-4",
expectError: true,
errorDescription: "OPENAI_API_KEY env not found",
},
{
name: "Anthropic requires API key",
modelStr: "anthropic/claude-sonnet-4-20250514",
expectError: true,
errorDescription: "ANTHROPIC_API_KEY env not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := setupLLMClient(tt.modelStr, Agent{}, &Config{})
if (err != nil) != tt.expectError {
t.Errorf("Expected error: %v, got: %v", tt.expectError, err)
}
if tt.expectError && err != nil && err.Error() != tt.errorDescription {
t.Errorf("Expected error message %q, got %q", tt.errorDescription, err.Error())
}
})
}
}
func TestSetupLLMClientReturnsCorrectType(t *testing.T) {
tests := []struct {
name string
modelStr string
envKey string
envValue string
wantType string
}{
{
name: "Anthropic provider returns anthropic client",
modelStr: "anthropic/claude-sonnet-4-20250514",
envKey: "ANTHROPIC_API_KEY",
envValue: "test-key",
wantType: "anthropic",
},
{
name: "OpenAI provider returns openai client",
modelStr: "openai/gpt-4",
envKey: "OPENAI_API_KEY",
envValue: "test-key",
wantType: "openai",
},
{
name: "Ollama provider returns openai client",
modelStr: "ollama/llama2",
envKey: "OLLAMA_API_KEY",
envValue: "",
wantType: "openai",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv(tt.envKey, tt.envValue)
client, err := setupLLMClient(tt.modelStr, Agent{}, &Config{})
if err != nil {
t.Fatalf("setupLLMClient() error = %v", err)
}
switch tt.wantType {
case "anthropic":
if _, ok := client.(*anthropicLLMClient); !ok {
t.Errorf("expected *anthropicLLMClient, got %T", client)
}
case "openai":
if _, ok := client.(*openAILLMClient); !ok {
t.Errorf("expected *openAILLMClient, got %T", client)
}
}
})
}
}
func TestPrepareRetryMessages_Empty(t *testing.T) {
result := prepareRetryMessages(nil, "hello")
if result != nil {
t.Errorf("Expected nil for empty messages, got %v", result)
}
result = prepareRetryMessages([]openai.ChatCompletionMessage{}, "hello")
if result != nil {
t.Errorf("Expected nil for empty slice, got %v", result)
}
}
func TestSystemPromptOverrideFromCLI(t *testing.T) {
// Agent with default system prompt
agent := Agent{
SystemPrompt: "Default system prompt",
}
opts := &CLIOptions{
SystemPrompt: "CLI system prompt override",
}
app := &Application{
agent: agent,
}
// Simulate NewApplication logic
if opts.SystemPrompt != "" {
app.agent.SystemPrompt = opts.SystemPrompt
}
prompt, err := app.getSystemPrompt()
if err != nil {
t.Fatalf("getSystemPrompt error: %v", err)
}
if prompt != "CLI system prompt override" {
t.Errorf("Expected system prompt to be overridden by CLI, got: %q", prompt)
}
}