|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "os" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/stretchr/testify/assert" |
8 | 9 | "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + latest "github.com/docker/cagent/pkg/config/v2" |
| 12 | + "github.com/docker/cagent/pkg/environment" |
9 | 13 | ) |
10 | 14 |
|
11 | 15 | func TestAutoRegisterModels(t *testing.T) { |
@@ -188,3 +192,235 @@ func openRoot(t *testing.T, dir string) *os.Root { |
188 | 192 |
|
189 | 193 | return root |
190 | 194 | } |
| 195 | + |
| 196 | +type noEnvProvider struct{} |
| 197 | + |
| 198 | +func (p *noEnvProvider) Get(context.Context, string) string { return "" } |
| 199 | + |
| 200 | +func TestCheckRequiredEnvVars(t *testing.T) { |
| 201 | + tests := []struct { |
| 202 | + yaml string |
| 203 | + expectedMissing []string |
| 204 | + }{ |
| 205 | + { |
| 206 | + yaml: "openai_inline.yaml", |
| 207 | + expectedMissing: []string{"OPENAI_API_KEY"}, |
| 208 | + }, |
| 209 | + { |
| 210 | + yaml: "anthropic_inline.yaml", |
| 211 | + expectedMissing: []string{"ANTHROPIC_API_KEY"}, |
| 212 | + }, |
| 213 | + { |
| 214 | + yaml: "google_inline.yaml", |
| 215 | + expectedMissing: []string{"GOOGLE_API_KEY"}, |
| 216 | + }, |
| 217 | + { |
| 218 | + yaml: "dmr_inline.yaml", |
| 219 | + expectedMissing: []string{}, |
| 220 | + }, |
| 221 | + { |
| 222 | + yaml: "openai_model.yaml", |
| 223 | + expectedMissing: []string{"OPENAI_API_KEY"}, |
| 224 | + }, |
| 225 | + { |
| 226 | + yaml: "anthropic_model.yaml", |
| 227 | + expectedMissing: []string{"ANTHROPIC_API_KEY"}, |
| 228 | + }, |
| 229 | + { |
| 230 | + yaml: "google_model.yaml", |
| 231 | + expectedMissing: []string{"GOOGLE_API_KEY"}, |
| 232 | + }, |
| 233 | + { |
| 234 | + yaml: "dmr_model.yaml", |
| 235 | + expectedMissing: []string{}, |
| 236 | + }, |
| 237 | + { |
| 238 | + yaml: "all.yaml", |
| 239 | + expectedMissing: []string{"ANTHROPIC_API_KEY", "GOOGLE_API_KEY", "OPENAI_API_KEY"}, |
| 240 | + }, |
| 241 | + } |
| 242 | + for _, test := range tests { |
| 243 | + t.Run(test.yaml, func(t *testing.T) { |
| 244 | + t.Parallel() |
| 245 | + |
| 246 | + root := openRoot(t, "testdata/env") |
| 247 | + |
| 248 | + cfg, err := LoadConfig(test.yaml, root) |
| 249 | + require.NoError(t, err) |
| 250 | + |
| 251 | + err = CheckRequiredEnvVars(t.Context(), cfg, &noEnvProvider{}, RuntimeConfig{}) |
| 252 | + |
| 253 | + if len(test.expectedMissing) == 0 { |
| 254 | + require.NoError(t, err) |
| 255 | + } else { |
| 256 | + require.Error(t, err) |
| 257 | + assert.Equal(t, test.expectedMissing, err.(*environment.RequiredEnvError).Missing) |
| 258 | + } |
| 259 | + }) |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +func TestCheckRequiredEnvVarsWithModelGateway(t *testing.T) { |
| 264 | + t.Parallel() |
| 265 | + |
| 266 | + root := openRoot(t, "testdata/env") |
| 267 | + |
| 268 | + cfg, err := LoadConfig("all.yaml", root) |
| 269 | + require.NoError(t, err) |
| 270 | + |
| 271 | + err = CheckRequiredEnvVars(t.Context(), cfg, &noEnvProvider{}, RuntimeConfig{ |
| 272 | + ModelsGateway: "gateway:8080", |
| 273 | + }) |
| 274 | + |
| 275 | + require.NoError(t, err) |
| 276 | +} |
| 277 | + |
| 278 | +func TestApplyModelOverrides(t *testing.T) { |
| 279 | + tests := []struct { |
| 280 | + name string |
| 281 | + agents map[string]latest.AgentConfig |
| 282 | + overrides []string |
| 283 | + expected map[string]string // agent name -> expected model |
| 284 | + expectError bool |
| 285 | + errorMsg string |
| 286 | + }{ |
| 287 | + { |
| 288 | + name: "global override", |
| 289 | + agents: map[string]latest.AgentConfig{ |
| 290 | + "root": {Model: "openai/gpt-4"}, |
| 291 | + "other": {Model: "anthropic/claude-3"}, |
| 292 | + }, |
| 293 | + overrides: []string{"google/gemini-pro"}, |
| 294 | + expected: map[string]string{ |
| 295 | + "root": "google/gemini-pro", |
| 296 | + "other": "google/gemini-pro", |
| 297 | + }, |
| 298 | + }, |
| 299 | + { |
| 300 | + name: "single per-agent override", |
| 301 | + agents: map[string]latest.AgentConfig{ |
| 302 | + "root": {Model: "openai/gpt-4"}, |
| 303 | + "other": {Model: "anthropic/claude-3"}, |
| 304 | + }, |
| 305 | + overrides: []string{"other=google/gemini-pro"}, |
| 306 | + expected: map[string]string{ |
| 307 | + "root": "openai/gpt-4", |
| 308 | + "other": "google/gemini-pro", |
| 309 | + }, |
| 310 | + }, |
| 311 | + { |
| 312 | + name: "multiple separate flags", |
| 313 | + agents: map[string]latest.AgentConfig{ |
| 314 | + "root": {Model: "openai/gpt-4"}, |
| 315 | + "other": {Model: "anthropic/claude-3"}, |
| 316 | + }, |
| 317 | + overrides: []string{"root=openai/gpt-5", "other=anthropic/claude-sonnet-4-0"}, |
| 318 | + expected: map[string]string{ |
| 319 | + "root": "openai/gpt-5", |
| 320 | + "other": "anthropic/claude-sonnet-4-0", |
| 321 | + }, |
| 322 | + }, |
| 323 | + { |
| 324 | + name: "comma-separated format", |
| 325 | + agents: map[string]latest.AgentConfig{ |
| 326 | + "root": {Model: "openai/gpt-4"}, |
| 327 | + "other": {Model: "anthropic/claude-3"}, |
| 328 | + "third": {Model: "google/gemini-pro"}, |
| 329 | + }, |
| 330 | + overrides: []string{"root=openai/gpt-5,other=anthropic/claude-sonnet-4-0"}, |
| 331 | + expected: map[string]string{ |
| 332 | + "root": "openai/gpt-5", |
| 333 | + "other": "anthropic/claude-sonnet-4-0", |
| 334 | + "third": "google/gemini-pro", |
| 335 | + }, |
| 336 | + }, |
| 337 | + { |
| 338 | + name: "mixed formats", |
| 339 | + agents: map[string]latest.AgentConfig{ |
| 340 | + "root": {Model: "openai/gpt-4"}, |
| 341 | + "other": {Model: "anthropic/claude-3"}, |
| 342 | + "third": {Model: "google/gemini-pro"}, |
| 343 | + "reviewer": {Model: "openai/gpt-3.5-turbo"}, |
| 344 | + }, |
| 345 | + overrides: []string{"root=openai/gpt-5,other=anthropic/claude-4", "reviewer=google/gemini-1.5-pro"}, |
| 346 | + expected: map[string]string{ |
| 347 | + "root": "openai/gpt-5", |
| 348 | + "other": "anthropic/claude-4", |
| 349 | + "third": "google/gemini-pro", |
| 350 | + "reviewer": "google/gemini-1.5-pro", |
| 351 | + }, |
| 352 | + }, |
| 353 | + { |
| 354 | + name: "last override wins", |
| 355 | + agents: map[string]latest.AgentConfig{ |
| 356 | + "root": {Model: "openai/gpt-4"}, |
| 357 | + }, |
| 358 | + overrides: []string{"root=openai/gpt-5", "root=anthropic/claude-4"}, |
| 359 | + expected: map[string]string{ |
| 360 | + "root": "anthropic/claude-4", |
| 361 | + }, |
| 362 | + }, |
| 363 | + { |
| 364 | + name: "unknown agent error", |
| 365 | + agents: map[string]latest.AgentConfig{ |
| 366 | + "root": {Model: "openai/gpt-4"}, |
| 367 | + }, |
| 368 | + overrides: []string{"nonexistent=openai/gpt-5"}, |
| 369 | + expectError: true, |
| 370 | + errorMsg: "unknown agent 'nonexistent'", |
| 371 | + }, |
| 372 | + { |
| 373 | + name: "empty model spec error", |
| 374 | + agents: map[string]latest.AgentConfig{ |
| 375 | + "root": {Model: "openai/gpt-4"}, |
| 376 | + }, |
| 377 | + overrides: []string{"root="}, |
| 378 | + expectError: true, |
| 379 | + errorMsg: "empty model specification in override: root=", |
| 380 | + }, |
| 381 | + { |
| 382 | + name: "empty global model spec is skipped", |
| 383 | + agents: map[string]latest.AgentConfig{ |
| 384 | + "root": {Model: "openai/gpt-4"}, |
| 385 | + }, |
| 386 | + overrides: []string{""}, |
| 387 | + expected: map[string]string{ |
| 388 | + "root": "openai/gpt-4", |
| 389 | + }, |
| 390 | + }, |
| 391 | + { |
| 392 | + name: "whitespace handling", |
| 393 | + agents: map[string]latest.AgentConfig{ |
| 394 | + "root": {Model: "openai/gpt-4"}, |
| 395 | + "other": {Model: "anthropic/claude-3"}, |
| 396 | + }, |
| 397 | + overrides: []string{" root = openai/gpt-5 , other = anthropic/claude-4 "}, |
| 398 | + expected: map[string]string{ |
| 399 | + "root": "openai/gpt-5", |
| 400 | + "other": "anthropic/claude-4", |
| 401 | + }, |
| 402 | + }, |
| 403 | + } |
| 404 | + |
| 405 | + for _, tt := range tests { |
| 406 | + t.Run(tt.name, func(t *testing.T) { |
| 407 | + t.Parallel() |
| 408 | + |
| 409 | + cfg := &latest.Config{ |
| 410 | + Agents: tt.agents, |
| 411 | + Models: make(map[string]latest.ModelConfig), |
| 412 | + } |
| 413 | + |
| 414 | + err := ApplyModelOverrides(cfg, tt.overrides) |
| 415 | + |
| 416 | + if tt.expectError { |
| 417 | + require.ErrorContains(t, err, tt.errorMsg) |
| 418 | + } else { |
| 419 | + require.NoError(t, err) |
| 420 | + for agentName, expectedModel := range tt.expected { |
| 421 | + assert.Equal(t, expectedModel, cfg.Agents[agentName].Model) |
| 422 | + } |
| 423 | + } |
| 424 | + }) |
| 425 | + } |
| 426 | +} |
0 commit comments