Elffuss BraidHub

kikocis/continue public

main · 6.2 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import {
3 ContextItemId,
4 ContextItemWithId,
5 RuleWithSource,
6 UserChatMessage,
7} from "../..";
8import { getApplicableRules } from "./getSystemMessageWithRules";
9
10describe("Nested directory rules application", () => {
11 // The rule in nested-folder/rules.md (without globs)
12 const nestedFolderRule: RuleWithSource = {
13 name: "Nested Folder Rule",
14 rule: "HELLO WORLD THIS IS A RULE",
15 source: "rules-block",
16 ruleFile: "manual-testing-sandbox/nested-folder/rules.md",
17 // No globs specified
18 };
19
20 // A global rule for comparison
21 const globalRule: RuleWithSource = {
22 name: "Global Rule",
23 rule: "SOLID Design Principles - Coding Agent Guidelines",
24 source: "rules-block",
25 ruleFile: ".continue/rules.md",
26 };
27
28 it("should apply nested directory rules to files in that directory", () => {
29 // Create a context with a file in the nested directory
30 const nestedFileContext: ContextItemWithId = {
31 id: { providerTitle: "file", itemId: "nested1" } as ContextItemId,
32 uri: {
33 type: "file",
34 value: "manual-testing-sandbox/nested-folder/hellonested.py",
35 },
36 content: 'print("Hello nested")',
37 name: "hellonested.py",
38 description: "Nested file",
39 };
40
41 // Apply rules with the nested file context
42 const applicableRules = getApplicableRules(
43 undefined, // No message needed
44 [nestedFolderRule, globalRule],
45 [nestedFileContext],
46 );
47
48 // Both rules should apply
49 expect(applicableRules).toHaveLength(2);
50 expect(applicableRules.map((r) => r.name)).toContain("Global Rule");
51 expect(applicableRules.map((r) => r.name)).toContain("Nested Folder Rule");
52 });
53
54 it("should also work with file references in messages", () => {
55 // Message with a file reference in the nested directory
56 const messageWithNestedFile: UserChatMessage = {
57 role: "user",
58 content:
59 'Can you explain this file?\n```python manual-testing-sandbox/nested-folder/hellonested.py\nprint("Hello nested")\n```',
60 };
61
62 // Apply rules with the message containing a nested file reference
63 const applicableRules = getApplicableRules(
64 messageWithNestedFile,
65 [nestedFolderRule, globalRule],
66 [],
67 );
68
69 // Both rules should apply
70 expect(applicableRules).toHaveLength(2);
71 expect(applicableRules.map((r) => r.name)).toContain("Global Rule");
72 expect(applicableRules.map((r) => r.name)).toContain("Nested Folder Rule");
73 });
74
75 it("should NOT apply nested directory rules to files outside that directory", () => {
76 // Context with a file outside the nested directory
77 const outsideFileContext: ContextItemWithId = {
78 id: { providerTitle: "file", itemId: "outside1" } as ContextItemId,
79 uri: { type: "file", value: "src/utils/helper.ts" },
80 content: "export const helper = () => {...}",
81 name: "helper.ts",
82 description: "Helper file",
83 };
84
85 // Apply rules with file context outside nested directory
86 const applicableRules = getApplicableRules(
87 undefined,
88 [nestedFolderRule, globalRule],
89 [outsideFileContext],
90 );
91
92 // Only the global rule should be included
93 expect(applicableRules.map((r) => r.name)).toContain("Global Rule");
94 expect(applicableRules.map((r) => r.name)).not.toContain(
95 "Nested Folder Rule",
96 );
97 });
98
99 it("should apply glob patterns relative to rules.md location", () => {
100 // A rule in src/ that targets *.py files
101 const srcPythonRule: RuleWithSource = {
102 name: "Src Python Rule",
103 rule: "Follow Python best practices for src directory",
104 globs: "**/*.py", // This should only match .py files in src/ and subdirectories
105 source: "rules-block",
106 ruleFile: "src/rules.md",
107 };
108
109 // A rule in utils/ that targets *.py files
110 const utilsPythonRule: RuleWithSource = {
111 name: "Utils Python Rule",
112 rule: "Follow Python best practices for utils directory",
113 globs: "**/*.py", // This should only match .py files in utils/ and subdirectories
114 source: "rules-block",
115 ruleFile: "utils/rules.md",
116 };
117
118 // A file in src/
119 const srcPythonFileContext: ContextItemWithId = {
120 id: { providerTitle: "file", itemId: "python1" } as ContextItemId,
121 uri: { type: "file", value: "src/pythonfile.py" },
122 content: 'print("Hello")',
123 name: "pythonfile.py",
124 description: "Python file in src",
125 };
126
127 // A file in utils/
128 const utilsPythonFileContext: ContextItemWithId = {
129 id: { providerTitle: "file", itemId: "python2" } as ContextItemId,
130 uri: { type: "file", value: "utils/pythonfile.py" },
131 content: 'print("Hello")',
132 name: "pythonfile.py",
133 description: "Python file in utils",
134 };
135
136 // A file in manual-testing-sandbox/
137 const manualTestingPythonFileContext: ContextItemWithId = {
138 id: { providerTitle: "file", itemId: "python3" } as ContextItemId,
139 uri: { type: "file", value: "manual-testing-sandbox/pythonfile.py" },
140 content: 'print("Hello")',
141 name: "pythonfile.py",
142 description: "Python file in manual-testing-sandbox",
143 };
144
145 // Apply src rule to src file - should match
146 let applicableRules = getApplicableRules(
147 undefined,
148 [srcPythonRule, utilsPythonRule],
149 [srcPythonFileContext],
150 );
151 expect(applicableRules.map((r) => r.name)).toContain("Src Python Rule");
152 expect(applicableRules.map((r) => r.name)).not.toContain(
153 "Utils Python Rule",
154 );
155
156 // Apply utils rule to utils file - should match
157 applicableRules = getApplicableRules(
158 undefined,
159 [srcPythonRule, utilsPythonRule],
160 [utilsPythonFileContext],
161 );
162 expect(applicableRules.map((r) => r.name)).not.toContain("Src Python Rule");
163 expect(applicableRules.map((r) => r.name)).toContain("Utils Python Rule");
164
165 // Apply both rules to manual-testing-sandbox file - should not match either
166 applicableRules = getApplicableRules(
167 undefined,
168 [srcPythonRule, utilsPythonRule],
169 [manualTestingPythonFileContext],
170 );
171 expect(applicableRules.map((r) => r.name)).not.toContain("Src Python Rule");
172 expect(applicableRules.map((r) => r.name)).not.toContain(
173 "Utils Python Rule",
174 );
175 });
176});