Elffuss BraidHub

kikocis/continue public

198ebda6cfd2 · 4.8 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import { RuleWithSource } from "../..";
3import { shouldApplyRule } from "./getSystemMessageWithRules";
4
5describe("Rule colocation glob matching", () => {
6 // This test file demonstrates the expected behavior after our fix
7
8 it("should restrict rules by their directory when no globs specified", () => {
9 // Rule in a nested directory with no globs - should only apply to files in that directory
10 const componentRule: RuleWithSource = {
11 name: "Components Rule",
12 rule: "Use functional components with hooks",
13 source: "rules-block",
14 ruleFile: "src/components/rules.md",
15 // No explicit globs - should only apply to files in src/components/ directory
16 };
17
18 // Files in the same directory - should match
19 const matchingFiles = [
20 "src/components/Button.tsx",
21 "src/components/Form.jsx",
22 ];
23 expect(shouldApplyRule(componentRule, matchingFiles)).toBe(true);
24
25 // Files outside the directory - should NOT match
26 const nonMatchingFiles = ["src/utils/helpers.ts", "src/redux/slice.ts"];
27 expect(shouldApplyRule(componentRule, nonMatchingFiles)).toBe(false);
28 });
29
30 it("should combine directory restriction with explicit globs", () => {
31 // Rule with explicit globs in a nested directory
32 const tsxComponentRule: RuleWithSource = {
33 name: "TSX Components Rule",
34 rule: "Use TypeScript with React components",
35 globs: "**/*.tsx", // Only .tsx files
36 source: "rules-block",
37 ruleFile: "src/components/rules.md",
38 // Should only apply to .tsx files in src/components/ directory
39 };
40
41 // TSX files in the same directory - should match
42 const matchingFiles = [
43 "src/components/Button.tsx",
44 "src/components/Form.tsx",
45 ];
46 expect(shouldApplyRule(tsxComponentRule, matchingFiles)).toBe(true);
47
48 // Non-TSX files in the same directory - should NOT match
49 const nonMatchingExtension = ["src/components/OldButton.jsx"];
50 expect(shouldApplyRule(tsxComponentRule, nonMatchingExtension)).toBe(false);
51
52 // TSX files outside the directory - should NOT match
53 const nonMatchingDir = ["src/pages/Home.tsx", "src/App.tsx"];
54 expect(shouldApplyRule(tsxComponentRule, nonMatchingDir)).toBe(false);
55 });
56
57 it("should apply root-level rules to all files", () => {
58 // Rule at the root level
59 const rootRule: RuleWithSource = {
60 name: "Root Rule",
61 rule: "Follow project standards",
62 source: "rules-block",
63 ruleFile: ".continue/rules.md",
64 // No restriction, should apply to all files
65 };
66
67 // Files in various directories - should all match
68 const files = [
69 "src/components/Button.tsx",
70 "src/redux/slice.ts",
71 "src/utils/helpers.ts",
72 ];
73 expect(shouldApplyRule(rootRule, files)).toBe(true);
74 });
75
76 it("should respect alwaysApply override regardless of directory", () => {
77 // Rule with alwaysApply: true
78 const alwaysApplyRule: RuleWithSource = {
79 name: "Always Apply Rule",
80 rule: "Follow these guidelines always",
81 alwaysApply: true,
82 source: "rules-block",
83 ruleFile: "src/specific/rules.md",
84 // Should apply to all files regardless of directory
85 };
86
87 // Files in various directories - should all match due to alwaysApply: true
88 const files = [
89 "src/components/Button.tsx",
90 "src/redux/slice.ts",
91 "src/utils/helpers.ts",
92 ];
93 expect(shouldApplyRule(alwaysApplyRule, files)).toBe(true);
94
95 // Rule with alwaysApply: false and no globs
96 const neverApplyRule: RuleWithSource = {
97 name: "Never Apply Rule",
98 rule: "This rule should never apply",
99 alwaysApply: false,
100 source: "rules-block",
101 ruleFile: "src/specific/rules.md",
102 // Should never apply since alwaysApply is false and there are no globs
103 };
104
105 expect(shouldApplyRule(neverApplyRule, files)).toBe(false);
106 });
107
108 it("should support complex directory + glob combinations", () => {
109 // Rule with complex glob pattern in a nested directory
110 const testExclusionRule: RuleWithSource = {
111 name: "Test Exclusion Rule",
112 rule: "Apply to TS files but not test files",
113 globs: ["**/*.ts", "!**/*.test.ts", "!**/*.spec.ts"],
114 source: "rules-block",
115 ruleFile: "src/utils/rules.md",
116 // Should only apply to non-test TS files in src/utils/
117 };
118
119 // Regular TS file in utils - should match
120 expect(shouldApplyRule(testExclusionRule, ["src/utils/helpers.ts"])).toBe(
121 true,
122 );
123
124 // Test TS file in utils - should NOT match due to negative glob
125 expect(
126 shouldApplyRule(testExclusionRule, ["src/utils/helpers.test.ts"]),
127 ).toBe(false);
128
129 // Regular TS file outside utils - should NOT match due to directory restriction
130 expect(shouldApplyRule(testExclusionRule, ["src/models/user.ts"])).toBe(
131 false,
132 );
133 });
134});