Elffuss BraidHub

kikocis/continue public

main · 14.0 KiBRawPermalink
1import path from "path";
2import { beforeEach, describe, expect, it } from "vitest";
3import {
4 ContextItemId,
5 ContextItemWithId,
6 RuleWithSource,
7 UserChatMessage,
8} from "../..";
9
10import { getApplicableRules } from "../../llm/rules/getSystemMessageWithRules";
11
12describe("Rule Colocation Application", () => {
13 // Create a set of rules in different directories
14 const rules: RuleWithSource[] = [
15 // Root level rule - should apply everywhere
16 {
17 name: "Root Rule",
18 rule: "Follow project standards",
19 source: "colocated-markdown",
20 ruleFile: ".continue/rules.md",
21 },
22
23 // Nested directory rule without globs - should only apply to files in that directory
24 {
25 name: "React Components Rule",
26 rule: "Use functional components with hooks",
27 source: "colocated-markdown",
28 ruleFile: "src/components/rules.md",
29 // No explicit globs - should implicitly only apply to files in that directory
30 },
31
32 // Nested directory rule with explicit globs - should apply to matching files only
33 {
34 name: "Redux Rule",
35 rule: "Use Redux Toolkit for state management",
36 globs: "src/redux/**/*.{ts,tsx}",
37 source: "colocated-markdown",
38 ruleFile: "src/redux/rules.md",
39 },
40
41 // Directory rule with specific file extension glob
42 {
43 name: "TypeScript Components Rule",
44 rule: "Use TypeScript with React components",
45 globs: "**/*.tsx", // Only apply to .tsx files
46 source: "colocated-markdown",
47 ruleFile: "src/components/rules.md",
48 },
49
50 // Rule for a specific subdirectory with its own glob
51 {
52 name: "API Utils Rule",
53 rule: "Follow API utility conventions",
54 globs: "**/*.ts", // Only TypeScript files in this directory
55 source: "colocated-markdown",
56 ruleFile: "src/utils/api/rules.md",
57 },
58 ];
59
60 // Mock user message and context for various scenarios
61 let userMessageWithComponentFile: UserChatMessage;
62 let userMessageWithReduxFile: UserChatMessage;
63 let userMessageWithRootFile: UserChatMessage;
64 let userMessageWithApiUtilFile: UserChatMessage;
65 let userMessageWithComponentJsxFile: UserChatMessage;
66
67 let componentTsxContextItem: ContextItemWithId;
68 let componentJsxContextItem: ContextItemWithId;
69 let reduxContextItem: ContextItemWithId;
70 let rootContextItem: ContextItemWithId;
71 let apiUtilContextItem: ContextItemWithId;
72 let otherUtilContextItem: ContextItemWithId;
73
74 beforeEach(() => {
75 // Setup user messages with different code blocks
76 userMessageWithComponentFile = {
77 role: "user",
78 content:
79 "Can you help me with this component file?\n```tsx src/components/Button.tsx\nexport const Button = () => {...}\n```",
80 };
81
82 userMessageWithComponentJsxFile = {
83 role: "user",
84 content:
85 "Can you help me with this JSX component file?\n```jsx src/components/OldButton.jsx\nexport const OldButton = () => {...}\n```",
86 };
87
88 userMessageWithReduxFile = {
89 role: "user",
90 content:
91 'Can you help with this redux slice?\n```ts src/redux/userSlice.ts\nimport { createSlice } from "@reduxjs/toolkit";\n```',
92 };
93
94 userMessageWithRootFile = {
95 role: "user",
96 content:
97 "Can you help with this utility file?\n```ts src/utils/helpers.ts\nexport const formatDate = (date) => {...}\n```",
98 };
99
100 userMessageWithApiUtilFile = {
101 role: "user",
102 content:
103 "Can you help with this API utility file?\n```ts src/utils/api/requests.ts\nexport const fetchData = () => {...}\n```",
104 };
105
106 // Setup context items
107 componentTsxContextItem = {
108 id: { providerTitle: "file", itemId: "context1" } as ContextItemId,
109 uri: { type: "file", value: "src/components/Button.tsx" },
110 content: "export const Button = () => {...}",
111 name: "Button.tsx",
112 description: "Component file",
113 };
114
115 componentJsxContextItem = {
116 id: { providerTitle: "file", itemId: "context1b" } as ContextItemId,
117 uri: { type: "file", value: "src/components/OldButton.jsx" },
118 content: "export const OldButton = () => {...}",
119 name: "OldButton.jsx",
120 description: "Component file",
121 };
122
123 reduxContextItem = {
124 id: { providerTitle: "file", itemId: "context2" } as ContextItemId,
125 uri: { type: "file", value: "src/redux/userSlice.ts" },
126 content: 'import { createSlice } from "@reduxjs/toolkit";',
127 name: "userSlice.ts",
128 description: "Redux slice",
129 };
130
131 rootContextItem = {
132 id: { providerTitle: "file", itemId: "context3" } as ContextItemId,
133 uri: { type: "file", value: "src/utils/helpers.ts" },
134 content: "export const formatDate = (date) => {...}",
135 name: "helpers.ts",
136 description: "Utility file",
137 };
138
139 apiUtilContextItem = {
140 id: { providerTitle: "file", itemId: "context4" } as ContextItemId,
141 uri: { type: "file", value: "src/utils/api/requests.ts" },
142 content: "export const fetchData = () => {...}",
143 name: "requests.ts",
144 description: "API utility file",
145 };
146
147 otherUtilContextItem = {
148 id: { providerTitle: "file", itemId: "context5" } as ContextItemId,
149 uri: { type: "file", value: "src/utils/format.ts" },
150 content: "export const formatCurrency = (amount) => {...}",
151 name: "format.ts",
152 description: "Formatting utility",
153 };
154 });
155
156 describe("Basic directory-specific rule application", () => {
157 it("should apply root rules to all files", () => {
158 // Test with component file
159 let applicableRules = getApplicableRules(
160 userMessageWithComponentFile,
161 rules,
162 [componentTsxContextItem],
163 );
164 expect(applicableRules.map((r) => r.name)).toContain("Root Rule");
165
166 // Test with redux file
167 applicableRules = getApplicableRules(userMessageWithReduxFile, rules, [
168 reduxContextItem,
169 ]);
170 expect(applicableRules.map((r) => r.name)).toContain("Root Rule");
171
172 // Test with root-level file
173 applicableRules = getApplicableRules(userMessageWithRootFile, rules, [
174 rootContextItem,
175 ]);
176 expect(applicableRules.map((r) => r.name)).toContain("Root Rule");
177 });
178 });
179
180 describe("Directory-specific rule application with implied globs", () => {
181 it("should only apply component rules to files in the component directory when no globs specified", () => {
182 // Create a rule without explicit globs but with a file path in the components directory
183 const impliedComponentRule: RuleWithSource = {
184 name: "Implied Components Rule",
185 rule: "Use React component best practices",
186 source: "colocated-markdown",
187 ruleFile: "src/components/rules.md",
188 // No explicit globs - should infer from directory
189 };
190
191 // Test with component file - should apply the rule
192 let applicableRules = getApplicableRules(
193 userMessageWithComponentFile,
194 [impliedComponentRule],
195 [componentTsxContextItem],
196 );
197 expect(applicableRules.map((r) => r.name)).toContain(
198 "Implied Components Rule",
199 );
200
201 // Test with redux file - should NOT apply the component rule
202 // This is failing currently - we need to fix the implementation
203 applicableRules = getApplicableRules(
204 userMessageWithReduxFile,
205 [impliedComponentRule],
206 [reduxContextItem],
207 );
208
209 // THIS WILL FAIL - Current implementation doesn't restrict by directory
210 expect(applicableRules.map((r) => r.name)).not.toContain(
211 "Implied Components Rule",
212 );
213
214 // Test with root-level file - should NOT apply the component rule
215 // This is failing currently - we need to fix the implementation
216 applicableRules = getApplicableRules(
217 userMessageWithRootFile,
218 [impliedComponentRule],
219 [rootContextItem],
220 );
221
222 // THIS WILL FAIL - Current implementation doesn't restrict by directory
223 expect(applicableRules.map((r) => r.name)).not.toContain(
224 "Implied Components Rule",
225 );
226 });
227 });
228
229 describe("Combined directory and glob pattern matching", () => {
230 it("should respect directory + glob pattern when both are present", () => {
231 // Create a rule with explicit glob in a nested directory
232 const typescriptComponentRule: RuleWithSource = {
233 name: "TypeScript Component Rule",
234 rule: "Use TypeScript with React components",
235 globs: "**/*.tsx", // Only apply to .tsx files
236 source: "colocated-markdown",
237 ruleFile: "src/components/rules.md",
238 };
239
240 // Test with TSX component file - should apply
241 let applicableRules = getApplicableRules(
242 userMessageWithComponentFile,
243 [typescriptComponentRule],
244 [componentTsxContextItem],
245 );
246 expect(applicableRules.map((r) => r.name)).toContain(
247 "TypeScript Component Rule",
248 );
249
250 // Test with JSX component file - should NOT apply (wrong extension)
251 // This test is likely to pass even with current implementation since the glob is explicit
252 applicableRules = getApplicableRules(
253 userMessageWithComponentJsxFile,
254 [typescriptComponentRule],
255 [componentJsxContextItem],
256 );
257 expect(applicableRules.map((r) => r.name)).not.toContain(
258 "TypeScript Component Rule",
259 );
260
261 // Test with TS file outside components directory - should NOT apply
262 // This test will fail because current implementation doesn't consider directory boundaries
263 applicableRules = getApplicableRules(
264 userMessageWithReduxFile,
265 [typescriptComponentRule],
266 [reduxContextItem],
267 );
268
269 // THIS WILL FAIL - Current impl only checks file extension, not directory
270 expect(applicableRules.map((r) => r.name)).not.toContain(
271 "TypeScript Component Rule",
272 );
273 });
274 });
275
276 describe("Nested directory rules with globs", () => {
277 it("should apply API utils rule only to files in that directory matching the glob", () => {
278 // Create a rule for a specific subdirectory with its own glob
279 const apiUtilsRule: RuleWithSource = {
280 name: "API Utils Rule",
281 rule: "Follow API utility conventions",
282 globs: "**/*.ts", // Only TypeScript files in this directory
283 source: "colocated-markdown",
284 ruleFile: "src/utils/api/rules.md",
285 };
286
287 // Test with file in the API utils directory - should apply
288 let applicableRules = getApplicableRules(
289 userMessageWithApiUtilFile,
290 [apiUtilsRule],
291 [apiUtilContextItem],
292 );
293 expect(applicableRules.map((r) => r.name)).toContain("API Utils Rule");
294
295 // Test with TS file in general utils directory - should NOT apply
296 // This test will fail because current implementation doesn't consider directory boundaries
297 applicableRules = getApplicableRules(
298 userMessageWithRootFile,
299 [apiUtilsRule],
300 [rootContextItem],
301 );
302
303 // THIS WILL FAIL - Current impl only checks file extension, not directory
304 expect(applicableRules.map((r) => r.name)).not.toContain(
305 "API Utils Rule",
306 );
307 });
308 });
309
310 describe("Rule application inference strategy", () => {
311 it("should infer directory-specific glob patterns from rule file location", () => {
312 // This test will propose the expected behavior for the feature:
313 // When a rules.md file is colocated in a directory without explicit globs,
314 // it should automatically create an implicit glob pattern for that directory.
315
316 function createRuleWithAutomaticGlobInference(
317 ruleFilePath: string,
318 ): RuleWithSource {
319 const directory = path.dirname(ruleFilePath);
320 // The expected behavior would be to create an implicit glob like this:
321 const expectedGlob = `${directory}/**/*`;
322
323 return {
324 name: `Inferred Rule for ${directory}`,
325 rule: `Follow standards for ${directory}`,
326 source: "colocated-markdown",
327 ruleFile: ruleFilePath,
328 // In a fixed implementation, these globs would be automatically inferred
329 // globs: expectedGlob,
330 };
331 }
332
333 // Create rules for different directories
334 const modelsRule = createRuleWithAutomaticGlobInference(
335 "src/models/rules.md",
336 );
337 const servicesRule = createRuleWithAutomaticGlobInference(
338 "src/services/rules.md",
339 );
340
341 // Create context items for different files
342 const modelFileContext: ContextItemWithId = {
343 id: { providerTitle: "file", itemId: "models1" } as ContextItemId,
344 uri: { type: "file", value: "src/models/user.ts" },
345 content: "export interface User {...}",
346 name: "user.ts",
347 description: "User model",
348 };
349
350 const serviceFileContext: ContextItemWithId = {
351 id: { providerTitle: "file", itemId: "services1" } as ContextItemId,
352 uri: { type: "file", value: "src/services/auth.ts" },
353 content: "export const login = () => {...}",
354 name: "auth.ts",
355 description: "Auth service",
356 };
357
358 // Test with model file - should apply only the models rule
359 const applicableModelsRules = getApplicableRules(
360 undefined, // No user message needed
361 [modelsRule, servicesRule],
362 [modelFileContext],
363 );
364
365 // These assertions will fail with current implementation
366 // but represent the desired behavior
367 expect(applicableModelsRules.map((r) => r.name)).toContain(
368 "Inferred Rule for src/models",
369 );
370 expect(applicableModelsRules.map((r) => r.name)).not.toContain(
371 "Inferred Rule for src/services",
372 );
373
374 // Test with service file - should apply only the services rule
375 const applicableServicesRules = getApplicableRules(
376 undefined, // No user message needed
377 [modelsRule, servicesRule],
378 [serviceFileContext],
379 );
380
381 // These assertions will fail with current implementation
382 // but represent the desired behavior
383 expect(applicableServicesRules.map((r) => r.name)).not.toContain(
384 "Inferred Rule for src/models",
385 );
386 expect(applicableServicesRules.map((r) => r.name)).toContain(
387 "Inferred Rule for src/services",
388 );
389 });
390 });
391});