| 1 |
package providerconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/clnkr-ai/clnkr" |
| 8 |
providerdomain "github.com/clnkr-ai/clnkr/internal/providers/providerconfig" |
| 9 |
) |
| 10 |
|
| 11 |
func TestResolveConfigRequiresProviderModelAndAPIKey(t *testing.T) { |
| 12 |
tests := []struct { |
| 13 |
name string |
| 14 |
inputs Inputs |
| 15 |
env map[string]string |
| 16 |
want string |
| 17 |
wantMissingAPIKey bool |
| 18 |
}{ |
| 19 |
{ |
| 20 |
name: "provider required", |
| 21 |
want: "provider is required; set --provider or CLNKR_PROVIDER", |
| 22 |
}, |
| 23 |
{ |
| 24 |
name: "base url does not replace provider", |
| 25 |
inputs: Inputs{BaseURL: "https://mock-provider.example/v1", Model: "test-model"}, |
| 26 |
want: "provider is required; set --provider or CLNKR_PROVIDER", |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "env base url does not replace provider", |
| 30 |
inputs: Inputs{Model: "test-model"}, |
| 31 |
env: map[string]string{"CLNKR_BASE_URL": "https://mock-provider.example/v1"}, |
| 32 |
want: "provider is required; set --provider or CLNKR_PROVIDER", |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "model required", |
| 36 |
inputs: Inputs{Provider: "anthropic"}, |
| 37 |
want: "model is required; set --model or CLNKR_MODEL", |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "api key required", |
| 41 |
inputs: Inputs{Provider: "openai", Model: "gpt-4.1"}, |
| 42 |
want: "api key is required; set CLNKR_API_KEY", |
| 43 |
wantMissingAPIKey: true, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, tt := range tests { |
| 48 |
t.Run(tt.name, func(t *testing.T) { |
| 49 |
_, err := ResolveConfig(tt.inputs, envMap(tt.env)) |
| 50 |
if err == nil || err.Error() != tt.want { |
| 51 |
t.Fatalf("ResolveConfig() err = %v, want %q", err, tt.want) |
| 52 |
} |
| 53 |
if IsMissingAPIKey(err) != tt.wantMissingAPIKey { |
| 54 |
t.Fatalf( |
| 55 |
"IsMissingAPIKey(%v) = %v, want %v", |
| 56 |
err, |
| 57 |
IsMissingAPIKey(err), |
| 58 |
tt.wantMissingAPIKey, |
| 59 |
) |
| 60 |
} |
| 61 |
}) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestActProtocolFlagValue(t *testing.T) { |
| 66 |
tests := []struct { |
| 67 |
name string |
| 68 |
flagValue string |
| 69 |
flagSet bool |
| 70 |
env string |
| 71 |
want string |
| 72 |
}{ |
| 73 |
{ |
| 74 |
name: "uses env when flag unset", |
| 75 |
flagValue: "auto", |
| 76 |
env: "tool-calls", |
| 77 |
want: "tool-calls", |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "prefers flag", |
| 81 |
flagValue: "clnkr-inline", |
| 82 |
flagSet: true, |
| 83 |
env: "tool-calls", |
| 84 |
want: "clnkr-inline", |
| 85 |
}, |
| 86 |
{name: "defaults auto", flagValue: "auto", want: "auto"}, |
| 87 |
} |
| 88 |
|
| 89 |
for _, tt := range tests { |
| 90 |
t.Run(tt.name, func(t *testing.T) { |
| 91 |
got := ActProtocolFlagValue(tt.flagValue, tt.flagSet, func(key string) string { |
| 92 |
if key == "CLNKR_ACT_PROTOCOL" { |
| 93 |
return tt.env |
| 94 |
} |
| 95 |
return "" |
| 96 |
}) |
| 97 |
if got != tt.want { |
| 98 |
t.Fatalf("ActProtocolFlagValue() = %q, want %q", got, tt.want) |
| 99 |
} |
| 100 |
}) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestResolveConfigPrefersFlagsOverEnv(t *testing.T) { |
| 105 |
cfg := mustResolveConfig(t, Inputs{ |
| 106 |
Provider: "openai", |
| 107 |
ProviderAPI: "openai-chat-completions", |
| 108 |
Model: "flag-model", |
| 109 |
BaseURL: "https://flags.example.com/v1", |
| 110 |
}, map[string]string{ |
| 111 |
"CLNKR_PROVIDER": "anthropic", |
| 112 |
"CLNKR_PROVIDER_API": "openai-responses", |
| 113 |
"CLNKR_MODEL": "env-model", |
| 114 |
"CLNKR_BASE_URL": "https://env.example.com/v1", |
| 115 |
"CLNKR_API_KEY": "env-key", |
| 116 |
}) |
| 117 |
if cfg.Provider != providerdomain.ProviderOpenAI { |
| 118 |
t.Fatalf("Provider = %q, want %q", cfg.Provider, providerdomain.ProviderOpenAI) |
| 119 |
} |
| 120 |
if cfg.ProviderAPI != providerdomain.ProviderAPIOpenAIChatCompletions { |
| 121 |
t.Fatalf( |
| 122 |
"ProviderAPI = %q, want %q", |
| 123 |
cfg.ProviderAPI, |
| 124 |
providerdomain.ProviderAPIOpenAIChatCompletions, |
| 125 |
) |
| 126 |
} |
| 127 |
if cfg.Model != "flag-model" { |
| 128 |
t.Fatalf("Model = %q, want flag-model", cfg.Model) |
| 129 |
} |
| 130 |
if cfg.BaseURL != "https://flags.example.com/v1" { |
| 131 |
t.Fatalf("BaseURL = %q, want https://flags.example.com/v1", cfg.BaseURL) |
| 132 |
} |
| 133 |
if cfg.APIKey != "env-key" { |
| 134 |
t.Fatalf("APIKey = %q, want env-key", cfg.APIKey) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestResolveConfigUsesOnlyCLNKREnvSurface(t *testing.T) { |
| 139 |
lookups := make(map[string]int) |
| 140 |
cfg, err := ResolveConfig(Inputs{}, func(key string) string { |
| 141 |
lookups[key]++ |
| 142 |
switch key { |
| 143 |
case "CLNKR_PROVIDER": |
| 144 |
return "anthropic" |
| 145 |
case "CLNKR_MODEL": |
| 146 |
return "claude-sonnet-4-6" |
| 147 |
case "CLNKR_API_KEY": |
| 148 |
return "clnkr-key" |
| 149 |
case "ANTHROPIC_API_KEY": |
| 150 |
return "anthropic-key" |
| 151 |
default: |
| 152 |
return "" |
| 153 |
} |
| 154 |
}) |
| 155 |
if err != nil { |
| 156 |
t.Fatalf("ResolveConfig(): %v", err) |
| 157 |
} |
| 158 |
if cfg.APIKey != "clnkr-key" { |
| 159 |
t.Fatalf("APIKey = %q, want clnkr-key", cfg.APIKey) |
| 160 |
} |
| 161 |
if lookups["ANTHROPIC_API_KEY"] != 0 { |
| 162 |
t.Fatalf("ANTHROPIC_API_KEY lookups = %d, want 0", lookups["ANTHROPIC_API_KEY"]) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestResolveConfigRejectsProviderAPIForAnthropic(t *testing.T) { |
| 167 |
_, err := ResolveConfig( |
| 168 |
Inputs{Provider: "anthropic", ProviderAPI: "auto", Model: "claude-sonnet-4-6"}, |
| 169 |
keyedEnv(), |
| 170 |
) |
| 171 |
if err == nil || err.Error() != `provider-api is only valid for provider "openai"` { |
| 172 |
t.Fatalf("ResolveConfig() err = %v, want anthropic provider-api rejection", err) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestResolveConfigProviderAndBaseURLSelection(t *testing.T) { |
| 177 |
tests := []struct { |
| 178 |
name string |
| 179 |
inputs Inputs |
| 180 |
wantProvider providerdomain.Provider |
| 181 |
wantProviderAPI providerdomain.ProviderAPI |
| 182 |
wantBaseURL string |
| 183 |
}{ |
| 184 |
{ |
| 185 |
name: "anthropic default", |
| 186 |
inputs: Inputs{Provider: "anthropic", Model: "claude-sonnet-4-6"}, |
| 187 |
wantProvider: providerdomain.ProviderAnthropic, |
| 188 |
wantProviderAPI: providerdomain.ProviderAPIAuto, |
| 189 |
wantBaseURL: DefaultAnthropicBaseURL, |
| 190 |
}, |
| 191 |
{ |
| 192 |
name: "openai default", |
| 193 |
inputs: Inputs{Provider: "openai", Model: "proxy-model"}, |
| 194 |
wantProvider: providerdomain.ProviderOpenAI, |
| 195 |
wantProviderAPI: providerdomain.ProviderAPIOpenAIChatCompletions, |
| 196 |
wantBaseURL: DefaultOpenAIBaseURL, |
| 197 |
}, |
| 198 |
{ |
| 199 |
name: "custom openai base url", |
| 200 |
inputs: Inputs{ |
| 201 |
Provider: "openai", |
| 202 |
Model: "test-model", |
| 203 |
BaseURL: "https://mock-provider.example/v1", |
| 204 |
}, |
| 205 |
wantProvider: providerdomain.ProviderOpenAI, |
| 206 |
wantProviderAPI: providerdomain.ProviderAPIOpenAIChatCompletions, |
| 207 |
wantBaseURL: "https://mock-provider.example/v1", |
| 208 |
}, |
| 209 |
{ |
| 210 |
name: "custom anthropic base url", |
| 211 |
inputs: Inputs{ |
| 212 |
Provider: "anthropic", |
| 213 |
Model: "claude-sonnet-4-6", |
| 214 |
BaseURL: "https://api.anthropic.com", |
| 215 |
}, |
| 216 |
wantProvider: providerdomain.ProviderAnthropic, |
| 217 |
wantProviderAPI: providerdomain.ProviderAPIAuto, |
| 218 |
wantBaseURL: "https://api.anthropic.com", |
| 219 |
}, |
| 220 |
} |
| 221 |
|
| 222 |
for _, tt := range tests { |
| 223 |
t.Run(tt.name, func(t *testing.T) { |
| 224 |
cfg := mustResolveConfig(t, tt.inputs, keyedEnvMap()) |
| 225 |
if cfg.Provider != tt.wantProvider { |
| 226 |
t.Fatalf("Provider = %q, want %q", cfg.Provider, tt.wantProvider) |
| 227 |
} |
| 228 |
if cfg.ProviderAPI != tt.wantProviderAPI { |
| 229 |
t.Fatalf("ProviderAPI = %q, want %q", cfg.ProviderAPI, tt.wantProviderAPI) |
| 230 |
} |
| 231 |
if cfg.BaseURL != tt.wantBaseURL { |
| 232 |
t.Fatalf("BaseURL = %q, want %q", cfg.BaseURL, tt.wantBaseURL) |
| 233 |
} |
| 234 |
}) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestResolveConfigOpenAIProviderAPISelection(t *testing.T) { |
| 239 |
tests := []struct { |
| 240 |
name string |
| 241 |
model string |
| 242 |
providerAPI string |
| 243 |
want providerdomain.ProviderAPI |
| 244 |
}{ |
| 245 |
{ |
| 246 |
name: "allowlisted gpt-5", |
| 247 |
model: "gpt-5", |
| 248 |
want: providerdomain.ProviderAPIOpenAIResponses, |
| 249 |
}, |
| 250 |
{ |
| 251 |
name: "allowlisted codex", |
| 252 |
model: "gpt-5-codex", |
| 253 |
want: providerdomain.ProviderAPIOpenAIResponses, |
| 254 |
}, |
| 255 |
{ |
| 256 |
name: "allowlisted o-series", |
| 257 |
model: "o3-pro", |
| 258 |
want: providerdomain.ProviderAPIOpenAIResponses, |
| 259 |
}, |
| 260 |
{ |
| 261 |
name: "codex suffix", |
| 262 |
model: "swift-codex", |
| 263 |
want: providerdomain.ProviderAPIOpenAIResponses, |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "case and space normalized", |
| 267 |
model: " GPT-5.9-preview ", |
| 268 |
want: providerdomain.ProviderAPIOpenAIResponses, |
| 269 |
}, |
| 270 |
{name: "future gpt", model: "gpt-6", want: providerdomain.ProviderAPIOpenAIResponses}, |
| 271 |
{ |
| 272 |
name: "dated snapshot", |
| 273 |
model: "gpt-5.4-2026-03-05", |
| 274 |
want: providerdomain.ProviderAPIOpenAIResponses, |
| 275 |
}, |
| 276 |
{ |
| 277 |
name: "known non-openai chatgpt", |
| 278 |
model: "chatgpt-4o-latest", |
| 279 |
want: providerdomain.ProviderAPIOpenAIChatCompletions, |
| 280 |
}, |
| 281 |
{ |
| 282 |
name: "known non-openai orca", |
| 283 |
model: "orca-mini", |
| 284 |
want: providerdomain.ProviderAPIOpenAIChatCompletions, |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "unmatched llama", |
| 288 |
model: "llama3", |
| 289 |
want: providerdomain.ProviderAPIOpenAIChatCompletions, |
| 290 |
}, |
| 291 |
{ |
| 292 |
name: "non openai-looking dated suffix", |
| 293 |
model: "llama3-2026-03-05", |
| 294 |
want: providerdomain.ProviderAPIOpenAIChatCompletions, |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "explicit codex responses", |
| 298 |
model: "future-codex-model", |
| 299 |
providerAPI: "openai-responses", |
| 300 |
want: providerdomain.ProviderAPIOpenAIResponses, |
| 301 |
}, |
| 302 |
{ |
| 303 |
name: "explicit codex chat completions", |
| 304 |
model: "gpt-5.1-codex-max", |
| 305 |
providerAPI: "openai-chat-completions", |
| 306 |
want: providerdomain.ProviderAPIOpenAIChatCompletions, |
| 307 |
}, |
| 308 |
} |
| 309 |
|
| 310 |
for _, tt := range tests { |
| 311 |
t.Run(tt.name, func(t *testing.T) { |
| 312 |
cfg := mustResolveConfig( |
| 313 |
t, |
| 314 |
Inputs{Provider: "openai", ProviderAPI: tt.providerAPI, Model: tt.model}, |
| 315 |
keyedEnvMap(), |
| 316 |
) |
| 317 |
if cfg.ProviderAPI != tt.want { |
| 318 |
t.Fatalf("ProviderAPI = %q, want %q", cfg.ProviderAPI, tt.want) |
| 319 |
} |
| 320 |
}) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestResolveConfigRejectsUnsupportedProModels(t *testing.T) { |
| 325 |
tests := []struct { |
| 326 |
name string |
| 327 |
inputs Inputs |
| 328 |
}{ |
| 329 |
{ |
| 330 |
name: "auto rejects gpt-5.2-pro", |
| 331 |
inputs: Inputs{Provider: "openai", Model: "gpt-5.2-pro"}, |
| 332 |
}, |
| 333 |
{ |
| 334 |
name: "forced chat completions still rejects gpt-5.2-pro", |
| 335 |
inputs: Inputs{ |
| 336 |
Provider: "openai", |
| 337 |
ProviderAPI: "openai-chat-completions", |
| 338 |
Model: "gpt-5.2-pro", |
| 339 |
}, |
| 340 |
}, |
| 341 |
} |
| 342 |
|
| 343 |
for _, tt := range tests { |
| 344 |
t.Run(tt.name, func(t *testing.T) { |
| 345 |
_, err := ResolveConfig(tt.inputs, keyedEnv()) |
| 346 |
requireErrorContains(t, err, "structured outputs") |
| 347 |
}) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
func TestParseActProtocolSetting(t *testing.T) { |
| 352 |
tests := []struct { |
| 353 |
name string |
| 354 |
raw string |
| 355 |
want ActProtocolSetting |
| 356 |
wantErr string |
| 357 |
}{ |
| 358 |
{name: "default", want: ActProtocolSettingAuto}, |
| 359 |
{name: "auto", raw: " AUTO ", want: ActProtocolSettingAuto}, |
| 360 |
{name: "inline", raw: "clnkr-inline", want: ActProtocolSettingClnkrInline}, |
| 361 |
{name: "tool calls", raw: "tool-calls", want: ActProtocolSettingToolCalls}, |
| 362 |
{name: "rejects unknown", raw: "native-bash-tools", wantErr: "invalid act-protocol"}, |
| 363 |
} |
| 364 |
|
| 365 |
for _, tt := range tests { |
| 366 |
t.Run(tt.name, func(t *testing.T) { |
| 367 |
got, err := ParseActProtocolSetting(tt.raw) |
| 368 |
if tt.wantErr != "" { |
| 369 |
requireErrorContains(t, err, tt.wantErr) |
| 370 |
requireErrorContains(t, err, "auto, clnkr-inline, tool-calls") |
| 371 |
return |
| 372 |
} |
| 373 |
if err != nil { |
| 374 |
t.Fatalf("ParseActProtocolSetting(%q): %v", tt.raw, err) |
| 375 |
} |
| 376 |
if got != tt.want { |
| 377 |
t.Fatalf("ParseActProtocolSetting(%q) = %q, want %q", tt.raw, got, tt.want) |
| 378 |
} |
| 379 |
}) |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
func TestResolveConfigActProtocol(t *testing.T) { |
| 384 |
tests := []struct { |
| 385 |
name string |
| 386 |
inputs Inputs |
| 387 |
want clnkr.ActProtocol |
| 388 |
wantErr string |
| 389 |
}{ |
| 390 |
{ |
| 391 |
name: "defaults auto to anthropic tool calls", |
| 392 |
inputs: Inputs{Provider: "anthropic", Model: "claude-sonnet-4-6"}, |
| 393 |
want: clnkr.ActProtocolToolCalls, |
| 394 |
}, |
| 395 |
{ |
| 396 |
name: "auto uses openai responses tool calls", |
| 397 |
inputs: Inputs{Provider: "openai", ProviderAPI: "openai-responses", Model: "gpt-5"}, |
| 398 |
want: clnkr.ActProtocolToolCalls, |
| 399 |
}, |
| 400 |
{ |
| 401 |
name: "auto falls back for openai chat completions", |
| 402 |
inputs: Inputs{ |
| 403 |
Provider: "openai", |
| 404 |
ProviderAPI: "openai-chat-completions", |
| 405 |
Model: "proxy-model", |
| 406 |
}, |
| 407 |
want: clnkr.ActProtocolClnkrInline, |
| 408 |
}, |
| 409 |
{ |
| 410 |
name: "explicit inline stays inline", |
| 411 |
inputs: Inputs{ |
| 412 |
Provider: "anthropic", |
| 413 |
Model: "claude-sonnet-4-6", |
| 414 |
ActProtocol: ActProtocolSettingClnkrInline, |
| 415 |
}, |
| 416 |
want: clnkr.ActProtocolClnkrInline, |
| 417 |
}, |
| 418 |
{ |
| 419 |
name: "accepts explicit anthropic tool calls", |
| 420 |
inputs: Inputs{ |
| 421 |
Provider: "anthropic", |
| 422 |
Model: "claude-sonnet-4-6", |
| 423 |
ActProtocol: ActProtocolSettingToolCalls, |
| 424 |
}, |
| 425 |
want: clnkr.ActProtocolToolCalls, |
| 426 |
}, |
| 427 |
{ |
| 428 |
name: "rejects openai chat completions tool calls", |
| 429 |
inputs: Inputs{ |
| 430 |
Provider: "openai", |
| 431 |
ProviderAPI: "openai-chat-completions", |
| 432 |
Model: "proxy-model", |
| 433 |
ActProtocol: ActProtocolSettingToolCalls, |
| 434 |
}, |
| 435 |
wantErr: "bash tool calls", |
| 436 |
}, |
| 437 |
} |
| 438 |
|
| 439 |
for _, tt := range tests { |
| 440 |
t.Run(tt.name, func(t *testing.T) { |
| 441 |
cfg, err := ResolveConfig(tt.inputs, keyedEnv()) |
| 442 |
if tt.wantErr != "" { |
| 443 |
requireErrorContains(t, err, tt.wantErr) |
| 444 |
return |
| 445 |
} |
| 446 |
if err != nil { |
| 447 |
t.Fatalf("ResolveConfig(): %v", err) |
| 448 |
} |
| 449 |
if cfg.ActProtocol != tt.want { |
| 450 |
t.Fatalf("ActProtocol = %q, want %q", cfg.ActProtocol, tt.want) |
| 451 |
} |
| 452 |
}) |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
func TestResolvePromptActProtocol(t *testing.T) { |
| 457 |
tests := []struct { |
| 458 |
name string |
| 459 |
inputs Inputs |
| 460 |
dumpSystemPrompt bool |
| 461 |
want clnkr.ActProtocol |
| 462 |
wantErr string |
| 463 |
rejectErr string |
| 464 |
}{ |
| 465 |
{ |
| 466 |
name: "concrete inline needs no provider config", |
| 467 |
inputs: Inputs{ActProtocol: ActProtocolSettingClnkrInline}, |
| 468 |
want: clnkr.ActProtocolClnkrInline, |
| 469 |
}, |
| 470 |
{ |
| 471 |
name: "concrete tool calls needs no provider config", |
| 472 |
inputs: Inputs{ActProtocol: ActProtocolSettingToolCalls}, |
| 473 |
want: clnkr.ActProtocolToolCalls, |
| 474 |
}, |
| 475 |
{ |
| 476 |
name: "auto resolves anthropic without api key", |
| 477 |
inputs: Inputs{Provider: "anthropic", Model: "claude-sonnet-4-6"}, |
| 478 |
want: clnkr.ActProtocolToolCalls, |
| 479 |
}, |
| 480 |
{ |
| 481 |
name: "auto resolves openai responses without api key", |
| 482 |
inputs: Inputs{Provider: "openai", ProviderAPI: "openai-responses", Model: "gpt-5"}, |
| 483 |
want: clnkr.ActProtocolToolCalls, |
| 484 |
}, |
| 485 |
{ |
| 486 |
name: "auto resolves openai chat completions without api key", |
| 487 |
inputs: Inputs{ |
| 488 |
Provider: "openai", |
| 489 |
ProviderAPI: "openai-chat-completions", |
| 490 |
Model: "proxy-model", |
| 491 |
}, |
| 492 |
want: clnkr.ActProtocolClnkrInline, |
| 493 |
}, |
| 494 |
{ |
| 495 |
name: "auto reports missing context", |
| 496 |
wantErr: "provider is required", |
| 497 |
rejectErr: "--act-protocol clnkr-inline", |
| 498 |
}, |
| 499 |
} |
| 500 |
|
| 501 |
for _, tt := range tests { |
| 502 |
t.Run(tt.name, func(t *testing.T) { |
| 503 |
got, err := ResolvePromptActProtocol( |
| 504 |
tt.inputs, |
| 505 |
func(string) string { return "" }, |
| 506 |
tt.dumpSystemPrompt, |
| 507 |
) |
| 508 |
if tt.wantErr != "" { |
| 509 |
requireErrorContains(t, err, tt.wantErr) |
| 510 |
if strings.Contains(err.Error(), tt.rejectErr) { |
| 511 |
t.Fatalf( |
| 512 |
"ResolvePromptActProtocol err = %q, want no %q", |
| 513 |
err.Error(), |
| 514 |
tt.rejectErr, |
| 515 |
) |
| 516 |
} |
| 517 |
return |
| 518 |
} |
| 519 |
if err != nil { |
| 520 |
t.Fatalf("ResolvePromptActProtocol(): %v", err) |
| 521 |
} |
| 522 |
if got != tt.want { |
| 523 |
t.Fatalf("ActProtocol = %q, want %q", got, tt.want) |
| 524 |
} |
| 525 |
}) |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
func TestResolveConfigAcceptsRequestOptions(t *testing.T) { |
| 530 |
tests := []struct { |
| 531 |
name string |
| 532 |
in Inputs |
| 533 |
want providerdomain.ProviderRequestOptions |
| 534 |
}{ |
| 535 |
{ |
| 536 |
name: "openai responses effort and max output", |
| 537 |
in: Inputs{ |
| 538 |
Provider: "openai", |
| 539 |
ProviderAPI: "openai-responses", |
| 540 |
Model: "gpt-5.1-codex-max", |
| 541 |
RequestOptions: providerdomain.ProviderRequestOptions{ |
| 542 |
Effort: providerdomain.ProviderEffortOptions{Level: "high", Set: true}, |
| 543 |
Output: providerdomain.ProviderOutputOptions{ |
| 544 |
MaxOutputTokens: providerdomain.OptionalInt{Value: 8000, Set: true}, |
| 545 |
}, |
| 546 |
}, |
| 547 |
}, |
| 548 |
want: providerdomain.ProviderRequestOptions{ |
| 549 |
Effort: providerdomain.ProviderEffortOptions{Level: "high", Set: true}, |
| 550 |
Output: providerdomain.ProviderOutputOptions{ |
| 551 |
MaxOutputTokens: providerdomain.OptionalInt{Value: 8000, Set: true}, |
| 552 |
}, |
| 553 |
}, |
| 554 |
}, |
| 555 |
{ |
| 556 |
name: "anthropic thinking and max output", |
| 557 |
in: Inputs{ |
| 558 |
Provider: "anthropic", |
| 559 |
Model: "claude-sonnet-4-20250514", |
| 560 |
RequestOptions: providerdomain.ProviderRequestOptions{ |
| 561 |
AnthropicManual: providerdomain.AnthropicManualThinkingOptions{ |
| 562 |
ThinkingBudgetTokens: providerdomain.OptionalInt{Value: 2048, Set: true}, |
| 563 |
}, |
| 564 |
Output: providerdomain.ProviderOutputOptions{ |
| 565 |
MaxOutputTokens: providerdomain.OptionalInt{Value: 4096, Set: true}, |
| 566 |
}, |
| 567 |
}, |
| 568 |
}, |
| 569 |
want: providerdomain.ProviderRequestOptions{ |
| 570 |
AnthropicManual: providerdomain.AnthropicManualThinkingOptions{ |
| 571 |
ThinkingBudgetTokens: providerdomain.OptionalInt{Value: 2048, Set: true}, |
| 572 |
}, |
| 573 |
Output: providerdomain.ProviderOutputOptions{ |
| 574 |
MaxOutputTokens: providerdomain.OptionalInt{Value: 4096, Set: true}, |
| 575 |
}, |
| 576 |
}, |
| 577 |
}, |
| 578 |
{ |
| 579 |
name: "anthropic opus 4 date snapshot manual thinking", |
| 580 |
in: Inputs{ |
| 581 |
Provider: "anthropic", |
| 582 |
Model: "claude-opus-4-20250514", |
| 583 |
RequestOptions: providerdomain.ProviderRequestOptions{ |
| 584 |
AnthropicManual: providerdomain.AnthropicManualThinkingOptions{ |
| 585 |
ThinkingBudgetTokens: providerdomain.OptionalInt{Value: 2048, Set: true}, |
| 586 |
}, |
| 587 |
}, |
| 588 |
}, |
| 589 |
want: providerdomain.ProviderRequestOptions{ |
| 590 |
AnthropicManual: providerdomain.AnthropicManualThinkingOptions{ |
| 591 |
ThinkingBudgetTokens: providerdomain.OptionalInt{Value: 2048, Set: true}, |
| 592 |
}, |
| 593 |
}, |
| 594 |
}, |
| 595 |
} |
| 596 |
|
| 597 |
for _, tt := range tests { |
| 598 |
t.Run(tt.name, func(t *testing.T) { |
| 599 |
cfg := mustResolveConfig(t, tt.in, keyedEnvMap()) |
| 600 |
if cfg.RequestOptions != tt.want { |
| 601 |
t.Fatalf("RequestOptions = %#v, want %#v", cfg.RequestOptions, tt.want) |
| 602 |
} |
| 603 |
}) |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
func TestResolveConfigRejectsUnsupportedRequestOptions(t *testing.T) { |
| 608 |
tests := []struct { |
| 609 |
name string |
| 610 |
in Inputs |
| 611 |
want string |
| 612 |
}{ |
| 613 |
{ |
| 614 |
name: "invalid effort", |
| 615 |
in: Inputs{ |
| 616 |
Provider: "openai", |
| 617 |
Model: "gpt-5.1", |
| 618 |
RequestOptions: requestOptions(effort("extreme")), |
| 619 |
}, |
| 620 |
want: "invalid effort", |
| 621 |
}, |
| 622 |
{ |
| 623 |
name: "effort rejects chat completions", |
| 624 |
in: Inputs{ |
| 625 |
Provider: "openai", |
| 626 |
ProviderAPI: "openai-chat-completions", |
| 627 |
Model: "gpt-5.1", |
| 628 |
RequestOptions: requestOptions(effort("high")), |
| 629 |
}, |
| 630 |
want: `effort is not supported for provider-api "openai-chat-completions"`, |
| 631 |
}, |
| 632 |
{ |
| 633 |
name: "effort rejects anthropic max", |
| 634 |
in: Inputs{ |
| 635 |
Provider: "anthropic", |
| 636 |
Model: "claude-sonnet-4-20250514", |
| 637 |
RequestOptions: requestOptions(effort("xhigh")), |
| 638 |
}, |
| 639 |
want: `effort "xhigh" is not supported for provider anthropic`, |
| 640 |
}, |
| 641 |
{ |
| 642 |
name: "gpt-5-pro rejects low", |
| 643 |
in: Inputs{ |
| 644 |
Provider: "openai", |
| 645 |
Model: "gpt-5-pro", |
| 646 |
RequestOptions: requestOptions(effort("low")), |
| 647 |
}, |
| 648 |
want: "gpt-5-pro only supports high", |
| 649 |
}, |
| 650 |
{ |
| 651 |
name: "xhigh requires codex max", |
| 652 |
in: Inputs{ |
| 653 |
Provider: "openai", |
| 654 |
Model: "gpt-5.1", |
| 655 |
RequestOptions: requestOptions(effort("xhigh")), |
| 656 |
}, |
| 657 |
want: `effort "xhigh" is not supported for model "gpt-5.1"`, |
| 658 |
}, |
| 659 |
{ |
| 660 |
name: "effort requires reasoning model", |
| 661 |
in: Inputs{ |
| 662 |
Provider: "openai", |
| 663 |
Model: "gpt-4.1", |
| 664 |
RequestOptions: requestOptions(effort("high")), |
| 665 |
}, |
| 666 |
want: "effort requires an OpenAI reasoning-capable model", |
| 667 |
}, |
| 668 |
{ |
| 669 |
name: "max output requires positive", |
| 670 |
in: Inputs{ |
| 671 |
Provider: "openai", |
| 672 |
Model: "gpt-5", |
| 673 |
RequestOptions: requestOptions(maxOutput(0)), |
| 674 |
}, |
| 675 |
want: "max-output-tokens must be at least 1", |
| 676 |
}, |
| 677 |
{ |
| 678 |
name: "max output rejects chat completions", |
| 679 |
in: Inputs{ |
| 680 |
Provider: "openai", |
| 681 |
ProviderAPI: "openai-chat-completions", |
| 682 |
Model: "gpt-5", |
| 683 |
RequestOptions: requestOptions(maxOutput(1000)), |
| 684 |
}, |
| 685 |
want: `max-output-tokens is not supported for provider-api "openai-chat-completions"`, |
| 686 |
}, |
| 687 |
{ |
| 688 |
name: "anthropic max output rejects streaming-sized value", |
| 689 |
in: Inputs{ |
| 690 |
Provider: "anthropic", |
| 691 |
Model: "claude-sonnet-4-20250514", |
| 692 |
RequestOptions: requestOptions( |
| 693 |
maxOutput(providerdomain.MaxAnthropicNonStreamingTokens + 1), |
| 694 |
), |
| 695 |
}, |
| 696 |
want: "while streaming is unsupported", |
| 697 |
}, |
| 698 |
{ |
| 699 |
name: "thinking budget rejects openai", |
| 700 |
in: Inputs{ |
| 701 |
Provider: "openai", |
| 702 |
Model: "gpt-5", |
| 703 |
RequestOptions: requestOptions(thinkingBudget(2048)), |
| 704 |
}, |
| 705 |
want: "thinking-budget-tokens requires provider anthropic", |
| 706 |
}, |
| 707 |
{ |
| 708 |
name: "thinking budget rejects small value", |
| 709 |
in: Inputs{ |
| 710 |
Provider: "anthropic", |
| 711 |
Model: "claude-sonnet-4-20250514", |
| 712 |
RequestOptions: requestOptions(thinkingBudget(1023)), |
| 713 |
}, |
| 714 |
want: "thinking-budget-tokens must be at least 1024", |
| 715 |
}, |
| 716 |
{ |
| 717 |
name: "thinking budget rejects unsupported model", |
| 718 |
in: Inputs{ |
| 719 |
Provider: "anthropic", |
| 720 |
Model: "claude-3-5-sonnet-20241022", |
| 721 |
RequestOptions: requestOptions(thinkingBudget(2048)), |
| 722 |
}, |
| 723 |
want: "thinking-budget-tokens requires an Anthropic extended-thinking-capable model", |
| 724 |
}, |
| 725 |
{ |
| 726 |
name: "thinking budget must be less than default max tokens", |
| 727 |
in: Inputs{ |
| 728 |
Provider: "anthropic", |
| 729 |
Model: "claude-sonnet-4-20250514", |
| 730 |
RequestOptions: requestOptions( |
| 731 |
thinkingBudget(providerdomain.DefaultAnthropicMaxTokens), |
| 732 |
), |
| 733 |
}, |
| 734 |
want: "thinking-budget-tokens must be less than effective anthropic max_tokens (4096)", |
| 735 |
}, |
| 736 |
{ |
| 737 |
name: "thinking budget rejects non-auto effort", |
| 738 |
in: Inputs{ |
| 739 |
Provider: "anthropic", |
| 740 |
Model: "claude-sonnet-4-20250514", |
| 741 |
RequestOptions: requestOptions(effort("high"), thinkingBudget(2048)), |
| 742 |
}, |
| 743 |
want: "thinking-budget-tokens requires either no --effort flag or --effort auto", |
| 744 |
}, |
| 745 |
{ |
| 746 |
name: "thinking budget rejects Opus 4.7+", |
| 747 |
in: Inputs{ |
| 748 |
Provider: "anthropic", |
| 749 |
Model: "claude-opus-4-7-20250514", |
| 750 |
RequestOptions: requestOptions(thinkingBudget(2048)), |
| 751 |
}, |
| 752 |
want: "thinking-budget-tokens is not supported for model", |
| 753 |
}, |
| 754 |
} |
| 755 |
|
| 756 |
for _, tt := range tests { |
| 757 |
t.Run(tt.name, func(t *testing.T) { |
| 758 |
_, err := ResolveConfig(tt.in, keyedEnv()) |
| 759 |
requireErrorContains(t, err, tt.want) |
| 760 |
}) |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
func TestResolveConfigNormalizesBaseURLTrailingSlash(t *testing.T) { |
| 765 |
tests := []struct { |
| 766 |
name string |
| 767 |
inputs Inputs |
| 768 |
wantURL string |
| 769 |
}{ |
| 770 |
{ |
| 771 |
name: "openai path", |
| 772 |
inputs: Inputs{ |
| 773 |
Provider: "openai", |
| 774 |
Model: "gpt-test", |
| 775 |
BaseURL: "https://api.openai.com/v1/", |
| 776 |
}, |
| 777 |
wantURL: "https://api.openai.com/v1", |
| 778 |
}, |
| 779 |
{ |
| 780 |
name: "anthropic root", |
| 781 |
inputs: Inputs{ |
| 782 |
Provider: "anthropic", |
| 783 |
Model: "claude-sonnet-4-6", |
| 784 |
BaseURL: "https://api.anthropic.com/", |
| 785 |
}, |
| 786 |
wantURL: "https://api.anthropic.com", |
| 787 |
}, |
| 788 |
{ |
| 789 |
name: "preserves internal repeated slashes", |
| 790 |
inputs: Inputs{ |
| 791 |
Provider: "openai", |
| 792 |
Model: "gpt-test", |
| 793 |
BaseURL: "https://example.com/proxy//v1/", |
| 794 |
}, |
| 795 |
wantURL: "https://example.com/proxy//v1", |
| 796 |
}, |
| 797 |
{ |
| 798 |
name: "preserves escaped slash", |
| 799 |
inputs: Inputs{ |
| 800 |
Provider: "openai", |
| 801 |
Model: "gpt-test", |
| 802 |
BaseURL: "https://example.com/proxy%2Fv1/", |
| 803 |
}, |
| 804 |
wantURL: "https://example.com/proxy%2Fv1", |
| 805 |
}, |
| 806 |
} |
| 807 |
|
| 808 |
for _, tt := range tests { |
| 809 |
t.Run(tt.name, func(t *testing.T) { |
| 810 |
cfg := mustResolveConfig(t, tt.inputs, keyedEnvMap()) |
| 811 |
if cfg.BaseURL != tt.wantURL { |
| 812 |
t.Fatalf("BaseURL = %q, want %q", cfg.BaseURL, tt.wantURL) |
| 813 |
} |
| 814 |
}) |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
type requestOption func(*providerdomain.ProviderRequestOptions) |
| 819 |
|
| 820 |
func effort(level string) requestOption { |
| 821 |
return func(opts *providerdomain.ProviderRequestOptions) { |
| 822 |
opts.Effort = providerdomain.ProviderEffortOptions{Level: level, Set: true} |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
func maxOutput(tokens int) requestOption { |
| 827 |
return func(opts *providerdomain.ProviderRequestOptions) { |
| 828 |
opts.Output.MaxOutputTokens = providerdomain.OptionalInt{Value: tokens, Set: true} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
func thinkingBudget(tokens int) requestOption { |
| 833 |
return func(opts *providerdomain.ProviderRequestOptions) { |
| 834 |
opts.AnthropicManual.ThinkingBudgetTokens = providerdomain.OptionalInt{ |
| 835 |
Value: tokens, |
| 836 |
Set: true, |
| 837 |
} |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
func requestOptions(options ...requestOption) providerdomain.ProviderRequestOptions { |
| 842 |
var opts providerdomain.ProviderRequestOptions |
| 843 |
for _, option := range options { |
| 844 |
option(&opts) |
| 845 |
} |
| 846 |
return opts |
| 847 |
} |
| 848 |
|
| 849 |
func mustResolveConfig(t *testing.T, inputs Inputs, env map[string]string) ResolvedProviderConfig { |
| 850 |
t.Helper() |
| 851 |
cfg, err := ResolveConfig(inputs, envMap(env)) |
| 852 |
if err != nil { |
| 853 |
t.Fatalf("ResolveConfig(): %v", err) |
| 854 |
} |
| 855 |
return cfg |
| 856 |
} |
| 857 |
|
| 858 |
func requireErrorContains(t *testing.T, err error, want string) { |
| 859 |
t.Helper() |
| 860 |
if err == nil || !strings.Contains(err.Error(), want) { |
| 861 |
t.Fatalf("err = %v, want containing %q", err, want) |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
func keyedEnv() func(string) string { |
| 866 |
return envMap(keyedEnvMap()) |
| 867 |
} |
| 868 |
|
| 869 |
func keyedEnvMap() map[string]string { |
| 870 |
return map[string]string{"CLNKR_API_KEY": "test-key"} |
| 871 |
} |
| 872 |
|
| 873 |
func envMap(values map[string]string) func(string) string { |
| 874 |
return func(key string) string { |
| 875 |
return values[key] |
| 876 |
} |
| 877 |
} |