|
| 1 | +package context_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + appcontext "github.com/CrowdStrike/codestrike/internal/context" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDiscoverCursorContext_ReadsAgentsMdAndAlwaysApplyRules(t *testing.T) { |
| 13 | + dir := t.TempDir() |
| 14 | + writeFile(t, dir, "AGENTS.md", "# Project Instructions\nUse snake_case.\n") |
| 15 | + |
| 16 | + rulesDir := filepath.Join(dir, ".cursor", "rules") |
| 17 | + if err := os.MkdirAll(rulesDir, 0755); err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + writeFile(t, rulesDir, "always.mdc", "---\nalwaysApply: true\n---\n\nAlways follow this rule.\n") |
| 21 | + writeFile(t, rulesDir, "no-frontmatter-fields.mdc", "---\ndescription: agent decides\n---\n\nAgent-decided rule body.\n") |
| 22 | + |
| 23 | + got := appcontext.DiscoverCursorContext(dir) |
| 24 | + |
| 25 | + if !strings.Contains(got, "### AGENTS.md") || !strings.Contains(got, "Use snake_case.") { |
| 26 | + t.Errorf("expected AGENTS.md content in output, got: %q", got) |
| 27 | + } |
| 28 | + if !strings.Contains(got, "### .cursor/rules/always.mdc") || !strings.Contains(got, "Always follow this rule.") { |
| 29 | + t.Errorf("expected always.mdc content in output, got: %q", got) |
| 30 | + } |
| 31 | + if !strings.Contains(got, "### .cursor/rules/no-frontmatter-fields.mdc") || !strings.Contains(got, "Agent-decided rule body.") { |
| 32 | + t.Errorf("expected no-frontmatter-fields.mdc content in output, got: %q", got) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestDiscoverCursorContext_SkipsGlobScopedAndDisabledRules(t *testing.T) { |
| 37 | + dir := t.TempDir() |
| 38 | + rulesDir := filepath.Join(dir, ".cursor", "rules") |
| 39 | + if err := os.MkdirAll(rulesDir, 0755); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + writeFile(t, rulesDir, "scoped.mdc", "---\nglobs: \"**/*.tsx\"\nalwaysApply: false\n---\n\nOnly for tsx files.\n") |
| 43 | + writeFile(t, rulesDir, "disabled.mdc", "---\nalwaysApply: false\n---\n\nManual only.\n") |
| 44 | + writeFile(t, rulesDir, "not-a-rule.md", "not an mdc file\n") |
| 45 | + |
| 46 | + got := appcontext.DiscoverCursorContext(dir) |
| 47 | + |
| 48 | + if strings.Contains(got, "scoped.mdc") || strings.Contains(got, "Only for tsx files.") { |
| 49 | + t.Errorf("expected glob-scoped rule to be excluded, got: %q", got) |
| 50 | + } |
| 51 | + if strings.Contains(got, "disabled.mdc") || strings.Contains(got, "Manual only.") { |
| 52 | + t.Errorf("expected alwaysApply:false rule to be excluded, got: %q", got) |
| 53 | + } |
| 54 | + if strings.Contains(got, "not-a-rule") { |
| 55 | + t.Errorf("expected non-.mdc file to be ignored, got: %q", got) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestDiscoverCursorContext_MissingDirReturnsEmptyNoError(t *testing.T) { |
| 60 | + dir := t.TempDir() |
| 61 | + |
| 62 | + got := appcontext.DiscoverCursorContext(dir) |
| 63 | + |
| 64 | + if got != "" { |
| 65 | + t.Errorf("expected empty result for repo with no AGENTS.md/.cursor/rules, got: %q", got) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestDiscoverCursorContext_SkipsFileWithoutFrontmatter(t *testing.T) { |
| 70 | + dir := t.TempDir() |
| 71 | + rulesDir := filepath.Join(dir, ".cursor", "rules") |
| 72 | + if err := os.MkdirAll(rulesDir, 0755); err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + writeFile(t, rulesDir, "plain.mdc", "Just plain text, no frontmatter delimiters.\n") |
| 76 | + |
| 77 | + got := appcontext.DiscoverCursorContext(dir) |
| 78 | + |
| 79 | + if got != "" { |
| 80 | + t.Errorf("expected file without frontmatter to be skipped, got: %q", got) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func writeFile(t *testing.T, dir, name, content string) { |
| 85 | + t.Helper() |
| 86 | + if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0600); err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | +} |
0 commit comments