|
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | 6 | "os" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 | ) |
9 | 10 |
|
@@ -525,3 +526,121 @@ func TestProfileMaxContext_Unknown(t *testing.T) { |
525 | 526 | t.Errorf("LookupProfile for unknown model = %v, want nil", p) |
526 | 527 | } |
527 | 528 | } |
| 529 | + |
| 530 | + |
| 531 | +// ── Project File (AGENTS.md) Tests ─────────────────────────────────── |
| 532 | + |
| 533 | +func TestLoadProjectFile_Missing(t *testing.T) { |
| 534 | + // No AGENTS.md in current dir — should return empty |
| 535 | + content := LoadProjectFile() |
| 536 | + if content != "" { |
| 537 | + t.Errorf("LoadProjectFile() with no file = %q, want empty", content) |
| 538 | + } |
| 539 | +} |
| 540 | + |
| 541 | +func TestLoadProjectFile_WithFile(t *testing.T) { |
| 542 | + dir := t.TempDir() |
| 543 | + cwd, _ := os.Getwd() |
| 544 | + os.Chdir(dir) |
| 545 | + defer os.Chdir(cwd) |
| 546 | + |
| 547 | + if err := os.WriteFile("AGENTS.md", []byte("This project uses Go 1.24."), 0644); err != nil { |
| 548 | + t.Fatal(err) |
| 549 | + } |
| 550 | + |
| 551 | + content := LoadProjectFile() |
| 552 | + if content != "This project uses Go 1.24." { |
| 553 | + t.Errorf("LoadProjectFile() = %q, want %q", content, "This project uses Go 1.24.") |
| 554 | + } |
| 555 | +} |
| 556 | + |
| 557 | +func TestLoadProjectFile_TrimsWhitespace(t *testing.T) { |
| 558 | + dir := t.TempDir() |
| 559 | + cwd, _ := os.Getwd() |
| 560 | + os.Chdir(dir) |
| 561 | + defer os.Chdir(cwd) |
| 562 | + |
| 563 | + if err := os.WriteFile("AGENTS.md", []byte(" \n project instructions \n "), 0644); err != nil { |
| 564 | + t.Fatal(err) |
| 565 | + } |
| 566 | + |
| 567 | + content := LoadProjectFile() |
| 568 | + if content != "project instructions" { |
| 569 | + t.Errorf("LoadProjectFile() = %q, want %q", content, "project instructions") |
| 570 | + } |
| 571 | +} |
| 572 | + |
| 573 | +func TestNew_ProjectFileAppended(t *testing.T) { |
| 574 | + dir := t.TempDir() |
| 575 | + cwd, _ := os.Getwd() |
| 576 | + os.Chdir(dir) |
| 577 | + defer os.Chdir(cwd) |
| 578 | + |
| 579 | + if err := os.WriteFile("AGENTS.md", []byte("Use tabs, not spaces."), 0644); err != nil { |
| 580 | + t.Fatal(err) |
| 581 | + } |
| 582 | + |
| 583 | + cfg := Config{ |
| 584 | + APIKey: "sk-test", |
| 585 | + SystemMessage: "You are a bot.", |
| 586 | + } |
| 587 | + agent, err := New(cfg) |
| 588 | + if err != nil { |
| 589 | + t.Fatal(err) |
| 590 | + } |
| 591 | + if !strings.Contains(agent.config.SystemMessage, "Use tabs, not spaces.") { |
| 592 | + t.Errorf("SystemMessage should contain AGENTS.md content, got: %q", agent.config.SystemMessage) |
| 593 | + } |
| 594 | + if !strings.Contains(agent.config.SystemMessage, "Project Instructions") { |
| 595 | + t.Errorf("SystemMessage should have 'Project Instructions' header, got: %q", agent.config.SystemMessage) |
| 596 | + } |
| 597 | + if !strings.Contains(agent.config.SystemMessage, "You are a bot.") { |
| 598 | + t.Errorf("SystemMessage should keep original content, got: %q", agent.config.SystemMessage) |
| 599 | + } |
| 600 | +} |
| 601 | + |
| 602 | +func TestNew_ProjectFileWithNoOriginalSystem(t *testing.T) { |
| 603 | + dir := t.TempDir() |
| 604 | + cwd, _ := os.Getwd() |
| 605 | + os.Chdir(dir) |
| 606 | + defer os.Chdir(cwd) |
| 607 | + |
| 608 | + if err := os.WriteFile("AGENTS.md", []byte("Just these instructions."), 0644); err != nil { |
| 609 | + t.Fatal(err) |
| 610 | + } |
| 611 | + |
| 612 | + cfg := Config{ |
| 613 | + APIKey: "sk-test", |
| 614 | + } |
| 615 | + agent, err := New(cfg) |
| 616 | + if err != nil { |
| 617 | + t.Fatal(err) |
| 618 | + } |
| 619 | + if agent.config.SystemMessage != "# Project Instructions\n\nJust these instructions." { |
| 620 | + t.Errorf("SystemMessage = %q, want 'Project Instructions' + content", agent.config.SystemMessage) |
| 621 | + } |
| 622 | +} |
| 623 | + |
| 624 | +func TestNew_NoProjectFileOptOut(t *testing.T) { |
| 625 | + dir := t.TempDir() |
| 626 | + cwd, _ := os.Getwd() |
| 627 | + os.Chdir(dir) |
| 628 | + defer os.Chdir(cwd) |
| 629 | + |
| 630 | + if err := os.WriteFile("AGENTS.md", []byte("Should not appear."), 0644); err != nil { |
| 631 | + t.Fatal(err) |
| 632 | + } |
| 633 | + |
| 634 | + cfg := Config{ |
| 635 | + APIKey: "sk-test", |
| 636 | + SystemMessage: "Only this.", |
| 637 | + NoProjectFile: true, |
| 638 | + } |
| 639 | + agent, err := New(cfg) |
| 640 | + if err != nil { |
| 641 | + t.Fatal(err) |
| 642 | + } |
| 643 | + if agent.config.SystemMessage != "Only this." { |
| 644 | + t.Errorf("SystemMessage = %q, want original only (no project file)", agent.config.SystemMessage) |
| 645 | + } |
| 646 | +} |
0 commit comments