|
7 | 7 | "strings" |
8 | 8 | "testing" |
9 | 9 |
|
| 10 | + "github.com/spf13/viper" |
10 | 11 | "gorm.io/driver/sqlite" |
11 | 12 | "gorm.io/gorm" |
12 | 13 | gormlogger "gorm.io/gorm/logger" |
@@ -280,6 +281,52 @@ func TestSearchCommand_RejectsNonPositiveLimit(t *testing.T) { |
280 | 281 | } |
281 | 282 | } |
282 | 283 |
|
| 284 | +func TestSearchCommand_NamespaceFromConfig(t *testing.T) { |
| 285 | + viper.Reset() |
| 286 | + defer viper.Reset() |
| 287 | + |
| 288 | + deps, stdout, stderr, _ := setupSearchTest(t) |
| 289 | + |
| 290 | + var gotNS string |
| 291 | + deps.SearchReader = &spySearchBackend{queryFn: func(ctx context.Context, query string, queryLimit int) ([]graph.Node, error) { |
| 292 | + gotNS = requestctx.FromContext(ctx) |
| 293 | + return nil, nil |
| 294 | + }} |
| 295 | + |
| 296 | + // Config selects namespace=backend; no --namespace flag is passed. |
| 297 | + viper.Set("namespace", "backend") |
| 298 | + |
| 299 | + if err := executeCmd(deps, stdout, stderr, "search", "hello"); err != nil { |
| 300 | + t.Fatalf("search: %v", err) |
| 301 | + } |
| 302 | + if gotNS != "backend" { |
| 303 | + t.Fatalf("expected config namespace 'backend', got %q", gotNS) |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +func TestSearchCommand_FlagOverridesConfigNamespace(t *testing.T) { |
| 308 | + viper.Reset() |
| 309 | + defer viper.Reset() |
| 310 | + |
| 311 | + deps, stdout, stderr, _ := setupSearchTest(t) |
| 312 | + |
| 313 | + var gotNS string |
| 314 | + deps.SearchReader = &spySearchBackend{queryFn: func(ctx context.Context, query string, queryLimit int) ([]graph.Node, error) { |
| 315 | + gotNS = requestctx.FromContext(ctx) |
| 316 | + return nil, nil |
| 317 | + }} |
| 318 | + |
| 319 | + // Explicit --namespace must win over the config value. |
| 320 | + viper.Set("namespace", "backend") |
| 321 | + |
| 322 | + if err := executeCmd(deps, stdout, stderr, "--namespace", "frontend", "search", "hello"); err != nil { |
| 323 | + t.Fatalf("search: %v", err) |
| 324 | + } |
| 325 | + if gotNS != "frontend" { |
| 326 | + t.Fatalf("expected flag namespace 'frontend' to override config, got %q", gotNS) |
| 327 | + } |
| 328 | +} |
| 329 | + |
283 | 330 | func TestSearchCommand_UsesCommandContext(t *testing.T) { |
284 | 331 | deps, stdout, stderr, _ := setupSearchTest(t) |
285 | 332 | ctx, cancel := context.WithCancel(context.Background()) |
|
0 commit comments