Elffuss BraidHub

kikocis/continue public

68dbf00e2c2f · 8.2 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import { RuleWithSource } from "../..";
3import { shouldApplyRule } from "./getSystemMessageWithRules";
4import { RulePolicies } from "./types";
5
6describe("Rule colocation glob matching", () => {
7 // This test file demonstrates the expected behavior after our fix
8
9 it("should restrict rules by their directory when no globs specified", () => {
10 // Rule in a nested directory with no globs - should only apply to files in that directory
11 const componentRule: RuleWithSource = {
12 name: "Components Rule",
13 rule: "Use functional components with hooks",
14 source: "rules-block",
15 ruleFile: "src/components/rules.md",
16 // No explicit globs - should only apply to files in src/components/ directory
17 };
18
19 // Files in the same directory - should match
20 const matchingFiles = [
21 "src/components/Button.tsx",
22 "src/components/Form.jsx",
23 ];
24 expect(shouldApplyRule(componentRule, matchingFiles)).toBe(true);
25
26 // Files outside the directory - should NOT match
27 const nonMatchingFiles = ["src/utils/helpers.ts", "src/redux/slice.ts"];
28 expect(shouldApplyRule(componentRule, nonMatchingFiles)).toBe(false);
29 });
30
31 it("should combine directory restriction with explicit globs", () => {
32 // Rule with explicit globs in a nested directory
33 const tsxComponentRule: RuleWithSource = {
34 name: "TSX Components Rule",
35 rule: "Use TypeScript with React components",
36 globs: "**/*.tsx", // Only .tsx files
37 source: "rules-block",
38 ruleFile: "src/components/rules.md",
39 // Should only apply to .tsx files in src/components/ directory
40 };
41
42 // TSX files in the same directory - should match
43 const matchingFiles = [
44 "src/components/Button.tsx",
45 "src/components/Form.tsx",
46 ];
47 expect(shouldApplyRule(tsxComponentRule, matchingFiles)).toBe(true);
48
49 // Non-TSX files in the same directory - should NOT match
50 const nonMatchingExtension = ["src/components/OldButton.jsx"];
51 expect(shouldApplyRule(tsxComponentRule, nonMatchingExtension)).toBe(false);
52
53 // TSX files outside the directory - should NOT match
54 const nonMatchingDir = ["src/pages/Home.tsx", "src/App.tsx"];
55 expect(shouldApplyRule(tsxComponentRule, nonMatchingDir)).toBe(false);
56 });
57
58 it("should apply root-level rules to all files", () => {
59 // Rule at the root level
60 const rootRule: RuleWithSource = {
61 name: "Root Rule",
62 rule: "Follow project standards",
63 source: "rules-block",
64 ruleFile: ".continue/rules.md",
65 // No restriction, should apply to all files
66 };
67
68 // Files in various directories - should all match
69 const files = [
70 "src/components/Button.tsx",
71 "src/redux/slice.ts",
72 "src/utils/helpers.ts",
73 ];
74 expect(shouldApplyRule(rootRule, files)).toBe(true);
75 });
76
77 it("should respect alwaysApply override regardless of directory", () => {
78 // Rule with alwaysApply: true
79 const alwaysApplyRule: RuleWithSource = {
80 name: "Always Apply Rule",
81 rule: "Follow these guidelines always",
82 alwaysApply: true,
83 source: "rules-block",
84 ruleFile: "src/specific/rules.md",
85 // Should apply to all files regardless of directory
86 };
87
88 // Files in various directories - should all match due to alwaysApply: true
89 const files = [
90 "src/components/Button.tsx",
91 "src/redux/slice.ts",
92 "src/utils/helpers.ts",
93 ];
94 expect(shouldApplyRule(alwaysApplyRule, files)).toBe(true);
95
96 // Rule with alwaysApply: false and no globs
97 const neverApplyRule: RuleWithSource = {
98 name: "Never Apply Rule",
99 rule: "This rule should never apply",
100 alwaysApply: false,
101 source: "rules-block",
102 ruleFile: "src/specific/rules.md",
103 // Should never apply since alwaysApply is false and there are no globs
104 };
105
106 expect(shouldApplyRule(neverApplyRule, files)).toBe(false);
107 });
108
109 it("should support complex directory + glob combinations", () => {
110 // Rule with complex glob pattern in a nested directory
111 const testExclusionRule: RuleWithSource = {
112 name: "Test Exclusion Rule",
113 rule: "Apply to TS files but not test files",
114 globs: ["**/*.ts", "!**/*.test.ts", "!**/*.spec.ts"],
115 source: "rules-block",
116 ruleFile: "src/utils/rules.md",
117 // Should only apply to non-test TS files in src/utils/
118 };
119
120 // Regular TS file in utils - should match
121 expect(shouldApplyRule(testExclusionRule, ["src/utils/helpers.ts"])).toBe(
122 true,
123 );
124
125 // Test TS file in utils - should NOT match due to negative glob
126 expect(
127 shouldApplyRule(testExclusionRule, ["src/utils/helpers.test.ts"]),
128 ).toBe(false);
129
130 // Regular TS file outside utils - should NOT match due to directory restriction
131 expect(shouldApplyRule(testExclusionRule, ["src/models/user.ts"])).toBe(
132 false,
133 );
134 });
135});
136
137describe("Rule policies", () => {
138 const componentRule: RuleWithSource = {
139 name: "Components Rule",
140 rule: "Use functional components with hooks",
141 source: "rules-block",
142 ruleFile: "src/components/rules.md",
143 };
144
145 const testFiles = ["src/components/Button.tsx"];
146 const nonMatchingFiles = ["src/utils/helpers.ts"];
147
148 it("should never apply rules with 'off' policy regardless of file paths", () => {
149 const rulePolicies: RulePolicies = {
150 "Components Rule": "off",
151 };
152
153 // Should not apply even to matching files
154 expect(shouldApplyRule(componentRule, testFiles, rulePolicies)).toBe(false);
155
156 // Rule with alwaysApply: true should still be overridden by 'off' policy
157 const alwaysApplyRule: RuleWithSource = {
158 name: "Always Apply Rule",
159 rule: "This rule would normally always apply",
160 alwaysApply: true,
161 source: "rules-block",
162 ruleFile: "src/components/rules.md",
163 };
164
165 const offPolicies: RulePolicies = {
166 "Always Apply Rule": "off",
167 };
168
169 expect(shouldApplyRule(alwaysApplyRule, testFiles, offPolicies)).toBe(
170 false,
171 );
172 });
173
174 it("should apply 'on' policy rules based on normal matching logic", () => {
175 const rulePolicies: RulePolicies = {
176 "Components Rule": "on",
177 };
178
179 // Should apply to matching files
180 expect(shouldApplyRule(componentRule, testFiles, rulePolicies)).toBe(true);
181
182 // Should not apply to non-matching files
183 expect(shouldApplyRule(componentRule, nonMatchingFiles, rulePolicies)).toBe(
184 false,
185 );
186 });
187
188 it("should use default behavior when no policy is specified", () => {
189 // Empty policies object should use default behavior
190 const rulePolicies: RulePolicies = {};
191
192 // Should apply to matching files
193 expect(shouldApplyRule(componentRule, testFiles, rulePolicies)).toBe(true);
194
195 // Should not apply to non-matching files
196 expect(shouldApplyRule(componentRule, nonMatchingFiles, rulePolicies)).toBe(
197 false,
198 );
199 });
200
201 it("should handle policy interaction with global rules", () => {
202 // Root level rule which would normally apply globally
203 const rootRule: RuleWithSource = {
204 name: "Root Rule",
205 rule: "Follow project standards",
206 source: "rules-block",
207 ruleFile: ".continue/rules.md",
208 };
209
210 // Off policy should override even global rules
211 const offPolicies: RulePolicies = {
212 "Root Rule": "off",
213 };
214
215 expect(shouldApplyRule(rootRule, testFiles, offPolicies)).toBe(false);
216
217 // On policy should maintain global rule behavior
218 const onPolicies: RulePolicies = {
219 "Root Rule": "on",
220 };
221
222 expect(shouldApplyRule(rootRule, testFiles, onPolicies)).toBe(true);
223 });
224
225 it("should prioritize 'off' policy over alwaysApply and directory restrictions", () => {
226 // Create rule with multiple matching criteria
227 const complexRule: RuleWithSource = {
228 name: "Complex Rule",
229 rule: "This rule has complex matching logic",
230 alwaysApply: true,
231 globs: "**/*.ts",
232 source: "rules-block",
233 ruleFile: "src/utils/rules.md",
234 };
235
236 // Off policy should win over everything
237 const offPolicies: RulePolicies = {
238 "Complex Rule": "off",
239 };
240
241 // Even with matching files and alwaysApply: true, off policy wins
242 expect(
243 shouldApplyRule(complexRule, ["src/utils/test.ts"], offPolicies),
244 ).toBe(false);
245 });
246});