|
4 | 4 | "os" |
5 | 5 | "os/exec" |
6 | 6 | "path/filepath" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 | ) |
9 | 10 |
|
@@ -234,3 +235,185 @@ func TestMultipleBootstrapFiles(t *testing.T) { |
234 | 235 | t.Errorf("expected 2 bootstrap files, got %d", len(files)) |
235 | 236 | } |
236 | 237 | } |
| 238 | + |
| 239 | +func TestSelectorFiltering(t *testing.T) { |
| 240 | + // Build the binary |
| 241 | + binaryPath := filepath.Join(t.TempDir(), "coding-agent-context") |
| 242 | + cmd := exec.Command("go", "build", "-o", binaryPath, ".") |
| 243 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 244 | + t.Fatalf("failed to build binary: %v\n%s", err, output) |
| 245 | + } |
| 246 | + |
| 247 | + // Create a temporary directory structure |
| 248 | + tmpDir := t.TempDir() |
| 249 | + contextDir := filepath.Join(tmpDir, ".coding-agent-context") |
| 250 | + memoriesDir := filepath.Join(contextDir, "memories") |
| 251 | + promptsDir := filepath.Join(contextDir, "prompts") |
| 252 | + outputDir := filepath.Join(tmpDir, "output") |
| 253 | + |
| 254 | + if err := os.MkdirAll(memoriesDir, 0755); err != nil { |
| 255 | + t.Fatalf("failed to create memories dir: %v", err) |
| 256 | + } |
| 257 | + if err := os.MkdirAll(promptsDir, 0755); err != nil { |
| 258 | + t.Fatalf("failed to create prompts dir: %v", err) |
| 259 | + } |
| 260 | + |
| 261 | + // Create memory files with different frontmatter |
| 262 | + if err := os.WriteFile(filepath.Join(memoriesDir, "prod.md"), []byte("---\nenv: production\nlanguage: go\n---\n# Production\nProd content\n"), 0644); err != nil { |
| 263 | + t.Fatalf("failed to write memory file: %v", err) |
| 264 | + } |
| 265 | + if err := os.WriteFile(filepath.Join(memoriesDir, "dev.md"), []byte("---\nenv: development\nlanguage: python\n---\n# Development\nDev content\n"), 0644); err != nil { |
| 266 | + t.Fatalf("failed to write memory file: %v", err) |
| 267 | + } |
| 268 | + if err := os.WriteFile(filepath.Join(memoriesDir, "test.md"), []byte("---\nenv: test\nlanguage: go\n---\n# Test\nTest content\n"), 0644); err != nil { |
| 269 | + t.Fatalf("failed to write memory file: %v", err) |
| 270 | + } |
| 271 | + // Create a file without frontmatter (should be included by default) |
| 272 | + if err := os.WriteFile(filepath.Join(memoriesDir, "nofm.md"), []byte("---\n---\n# No Frontmatter\nNo FM content\n"), 0644); err != nil { |
| 273 | + t.Fatalf("failed to write memory file: %v", err) |
| 274 | + } |
| 275 | + |
| 276 | + // Create a prompt file |
| 277 | + if err := os.WriteFile(filepath.Join(promptsDir, "test-task.md"), []byte("---\n---\n# Test Task\n"), 0644); err != nil { |
| 278 | + t.Fatalf("failed to write prompt file: %v", err) |
| 279 | + } |
| 280 | + |
| 281 | + // Test 1: Include by env=production |
| 282 | + cmd = exec.Command(binaryPath, "-d", contextDir, "-o", outputDir, "-s", "env=production", "test-task") |
| 283 | + cmd.Dir = tmpDir |
| 284 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 285 | + t.Fatalf("failed to run binary: %v\n%s", err, output) |
| 286 | + } |
| 287 | + |
| 288 | + promptOutput := filepath.Join(outputDir, "prompt.md") |
| 289 | + content, err := os.ReadFile(promptOutput) |
| 290 | + if err != nil { |
| 291 | + t.Fatalf("failed to read prompt output: %v", err) |
| 292 | + } |
| 293 | + contentStr := string(content) |
| 294 | + if !strings.Contains(contentStr, "Prod content") { |
| 295 | + t.Errorf("Expected production content in output") |
| 296 | + } |
| 297 | + if strings.Contains(contentStr, "Dev content") { |
| 298 | + t.Errorf("Did not expect development content in output") |
| 299 | + } |
| 300 | + if strings.Contains(contentStr, "Test content") { |
| 301 | + t.Errorf("Did not expect test content in output") |
| 302 | + } |
| 303 | + // File without env key should be included (key missing is allowed) |
| 304 | + if !strings.Contains(contentStr, "No FM content") { |
| 305 | + t.Errorf("Expected no frontmatter content in output (missing key should be allowed)") |
| 306 | + } |
| 307 | + |
| 308 | + // Clean output for next test |
| 309 | + os.RemoveAll(outputDir) |
| 310 | + |
| 311 | + // Test 2: Include by language=go (should include prod and test, and nofm) |
| 312 | + cmd = exec.Command(binaryPath, "-d", contextDir, "-o", outputDir, "-s", "language=go", "test-task") |
| 313 | + cmd.Dir = tmpDir |
| 314 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 315 | + t.Fatalf("failed to run binary: %v\n%s", err, output) |
| 316 | + } |
| 317 | + |
| 318 | + content, err = os.ReadFile(promptOutput) |
| 319 | + if err != nil { |
| 320 | + t.Fatalf("failed to read prompt output: %v", err) |
| 321 | + } |
| 322 | + contentStr = string(content) |
| 323 | + if !strings.Contains(contentStr, "Prod content") { |
| 324 | + t.Errorf("Expected production content in output") |
| 325 | + } |
| 326 | + if strings.Contains(contentStr, "Dev content") { |
| 327 | + t.Errorf("Did not expect development content in output") |
| 328 | + } |
| 329 | + if !strings.Contains(contentStr, "Test content") { |
| 330 | + t.Errorf("Expected test content in output") |
| 331 | + } |
| 332 | + if !strings.Contains(contentStr, "No FM content") { |
| 333 | + t.Errorf("Expected no frontmatter content in output (missing key should be allowed)") |
| 334 | + } |
| 335 | + |
| 336 | + // Clean output for next test |
| 337 | + os.RemoveAll(outputDir) |
| 338 | + |
| 339 | + // Test 3: Exclude by env=production (should include dev and test, and nofm) |
| 340 | + cmd = exec.Command(binaryPath, "-d", contextDir, "-o", outputDir, "-S", "env=production", "test-task") |
| 341 | + cmd.Dir = tmpDir |
| 342 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 343 | + t.Fatalf("failed to run binary: %v\n%s", err, output) |
| 344 | + } |
| 345 | + |
| 346 | + content, err = os.ReadFile(promptOutput) |
| 347 | + if err != nil { |
| 348 | + t.Fatalf("failed to read prompt output: %v", err) |
| 349 | + } |
| 350 | + contentStr = string(content) |
| 351 | + if strings.Contains(contentStr, "Prod content") { |
| 352 | + t.Errorf("Did not expect production content in output") |
| 353 | + } |
| 354 | + if !strings.Contains(contentStr, "Dev content") { |
| 355 | + t.Errorf("Expected development content in output") |
| 356 | + } |
| 357 | + if !strings.Contains(contentStr, "Test content") { |
| 358 | + t.Errorf("Expected test content in output") |
| 359 | + } |
| 360 | + if !strings.Contains(contentStr, "No FM content") { |
| 361 | + t.Errorf("Expected no frontmatter content in output (missing key should be allowed)") |
| 362 | + } |
| 363 | + |
| 364 | + // Clean output for next test |
| 365 | + os.RemoveAll(outputDir) |
| 366 | + |
| 367 | + // Test 4: Multiple includes env=production language=go (should include only prod and nofm) |
| 368 | + cmd = exec.Command(binaryPath, "-d", contextDir, "-o", outputDir, "-s", "env=production", "-s", "language=go", "test-task") |
| 369 | + cmd.Dir = tmpDir |
| 370 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 371 | + t.Fatalf("failed to run binary: %v\n%s", err, output) |
| 372 | + } |
| 373 | + |
| 374 | + content, err = os.ReadFile(promptOutput) |
| 375 | + if err != nil { |
| 376 | + t.Fatalf("failed to read prompt output: %v", err) |
| 377 | + } |
| 378 | + contentStr = string(content) |
| 379 | + if !strings.Contains(contentStr, "Prod content") { |
| 380 | + t.Errorf("Expected production content in output") |
| 381 | + } |
| 382 | + if strings.Contains(contentStr, "Dev content") { |
| 383 | + t.Errorf("Did not expect development content in output") |
| 384 | + } |
| 385 | + if strings.Contains(contentStr, "Test content") { |
| 386 | + t.Errorf("Did not expect test content in output") |
| 387 | + } |
| 388 | + if !strings.Contains(contentStr, "No FM content") { |
| 389 | + t.Errorf("Expected no frontmatter content in output (missing key should be allowed)") |
| 390 | + } |
| 391 | + |
| 392 | + // Clean output for next test |
| 393 | + os.RemoveAll(outputDir) |
| 394 | + |
| 395 | + // Test 5: Mix of include and exclude -s env=production -S language=python (should include only prod with go) |
| 396 | + cmd = exec.Command(binaryPath, "-d", contextDir, "-o", outputDir, "-s", "env=production", "-S", "language=python", "test-task") |
| 397 | + cmd.Dir = tmpDir |
| 398 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 399 | + t.Fatalf("failed to run binary: %v\n%s", err, output) |
| 400 | + } |
| 401 | + |
| 402 | + content, err = os.ReadFile(promptOutput) |
| 403 | + if err != nil { |
| 404 | + t.Fatalf("failed to read prompt output: %v", err) |
| 405 | + } |
| 406 | + contentStr = string(content) |
| 407 | + if !strings.Contains(contentStr, "Prod content") { |
| 408 | + t.Errorf("Expected production content in output") |
| 409 | + } |
| 410 | + if strings.Contains(contentStr, "Dev content") { |
| 411 | + t.Errorf("Did not expect development content in output") |
| 412 | + } |
| 413 | + if strings.Contains(contentStr, "Test content") { |
| 414 | + t.Errorf("Did not expect test content in output") |
| 415 | + } |
| 416 | + if !strings.Contains(contentStr, "No FM content") { |
| 417 | + t.Errorf("Expected no frontmatter content in output (missing keys should be allowed)") |
| 418 | + } |
| 419 | +} |
0 commit comments