Elffuss BraidHub

kikocis/continue public

8eb3ae7b8d16 · 9.7 KiBRawPermalink
1import { PackageIdentifier } from "../browser.js";
2import { getRuleName, markdownToRule } from "./markdownToRule.js";
3
4describe("markdownToRule", () => {
5 // Use a mock PackageIdentifier for testing
6 const mockId: PackageIdentifier = {
7 uriType: "file",
8 fileUri: "/path/to/file",
9 };
10
11 it("should convert markdown with frontmatter to a rule", () => {
12 const content = `---
13globs: "**/test/**/*.kt"
14name: Custom Name
15---
16
17# Test Rule
18
19This is a test rule.`;
20
21 const result = markdownToRule(content, mockId);
22 expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
23 expect(result.globs).toBe("/path/to/**/test/**/*.kt");
24 expect(result.name).toBe("Custom Name");
25 });
26
27 it("should correctly parse markdown with YAML frontmatter", () => {
28 const content = `---
29globs: "**/test/**/*.kt"
30---
31
32# Test Rule
33
34This is a test rule.`;
35
36 const result = markdownToRule(content, mockId);
37 expect(result.globs).toBe("/path/to/**/test/**/*.kt");
38 expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
39 expect(result.name).toBe("to/file"); // Should use last two path segments
40 });
41
42 it("should handle missing frontmatter", () => {
43 const content = `# Test Rule
44
45This is a test rule without frontmatter.`;
46
47 const result = markdownToRule(content, mockId);
48 expect(result.globs).toBe("/path/to/**/*");
49 expect(result.rule).toBe(content);
50 expect(result.name).toBe("to/file"); // Should use last two path segments
51 });
52
53 it("should handle empty frontmatter", () => {
54 const content = `---
55---
56
57# Test Rule
58
59This is a test rule with empty frontmatter.`;
60
61 const result = markdownToRule(content, mockId);
62 expect(result.globs).toBe("/path/to/**/*");
63 expect(result.rule).toBe(
64 "# Test Rule\n\nThis is a test rule with empty frontmatter.",
65 );
66 expect(result.name).toBe("to/file"); // Should use last two path segments
67 });
68
69 describe("glob patterns", () => {
70 // we want to ensure the following glob changes happen
71 // *.ts -> path/to/**/*.ts
72 // **/subdir/** -> path/to/**/**/subdir/** OR path/to/**/subdir/**
73 // myfile -> path/to/**/myfile (any depth including 0)
74 // mydir/ -> path/to/**/mydir/**
75 // **abc** -> path/to/**abc**
76 // *xyz* -> path/to/**/*xyz*
77
78 it("should match glob pattern for file extensions", () => {
79 const content = `---
80globs: "*.ts"
81name: glob pattern testing
82---
83
84 # Test Rule
85
86 This is a test rule.`;
87
88 const result = markdownToRule(content, mockId);
89 expect(result.globs).toBe("/path/to/**/*.ts");
90 });
91
92 it("should match glob pattern for dotfiles", () => {
93 const content = `---
94globs: ".gitignore"
95name: glob pattern testing
96---
97
98 # Test Rule
99
100 This is a test rule.`;
101
102 const result = markdownToRule(content, mockId);
103 expect(result.globs).toBe("/path/to/**/.gitignore");
104 });
105
106 it("should also work in root as base directory", () => {
107 const content = `---
108globs: "src/**/Dockerfile"
109name: glob pattern testing
110---
111
112 # Test Rule
113
114 This is a test rule.`;
115
116 const result = markdownToRule(content, {
117 uriType: "file",
118 fileUri: "/",
119 });
120 expect(result.globs).toBe("/**/src/**/Dockerfile");
121 });
122
123 it("should match for multiple globs", () => {
124 const content = `---
125globs: ["**/nested/**/deeper/**/*.rs", ".zshrc1", "**abc**", "*xyz*"]
126name: glob pattern testing
127---
128
129# Test Rule
130
131This is a test rule.`;
132
133 const result = markdownToRule(content, mockId);
134 expect(result.globs).toEqual([
135 "/path/to/**/nested/**/deeper/**/*.rs",
136 "/path/to/**/.zshrc1",
137 "/path/to/**abc**",
138 "/path/to/**/*xyz*",
139 ]);
140 });
141
142 it("should not prepend when inside .continue", () => {
143 const content = `---
144globs: ".git"
145name: glob pattern testing
146---
147
148 # Test Rule
149
150 This is a test rule.`;
151
152 const result = markdownToRule(content, {
153 uriType: "file",
154 fileUri: "/Documents/myproject/.continue/rules/rule1.md",
155 });
156 expect(result.globs).toBe(".git");
157 });
158 });
159
160 it("should handle frontmatter with whitespace", () => {
161 const content = `---
162globs: "**/test/**/*.kt"
163---
164
165# Test Rule
166
167This is a test rule.`;
168
169 const result = markdownToRule(content, mockId);
170 expect(result.globs).toBe("/path/to/**/test/**/*.kt");
171 expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
172 expect(result.name).toBe("to/file"); // Should use last two path segments
173 });
174
175 it("should handle Windows line endings (CRLF)", () => {
176 // Using \r\n for CRLF line endings
177 const content = `---\r
178globs: "**/test/**/*.kt"\r
179---\r
180\r
181# Test Rule\r
182\r
183This is a test rule.`;
184
185 const result = markdownToRule(content, mockId);
186 expect(result.globs).toBe("/path/to/**/test/**/*.kt");
187 // The result should be normalized to \n
188 expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
189 expect(result.name).toBe("to/file"); // Should use last two path segments
190 });
191
192 it("should handle malformed frontmatter", () => {
193 const content = `---
194globs: - "**/test/**/*.kt"
195invalid: yaml: content
196---
197
198# Test Rule
199
200This is a test rule.`;
201
202 // Should treat as only markdown when frontmatter is malformed
203 const result = markdownToRule(content, mockId);
204 expect(result.globs).toBe("/path/to/**/*");
205 expect(result.rule).toBe(content);
206 expect(result.name).toBe("to/file"); // Should use last two path segments
207 });
208
209 it("should use packageIdentifierToDisplayName if no heading or frontmatter name", () => {
210 const content = `---
211globs: "**/test/**/*.kt"
212---
213
214# Test Rule Title
215
216This is a test rule.`;
217
218 const result = markdownToRule(content, mockId);
219 expect(result.name).toBe("to/file"); // Should use last two path segments
220 });
221
222 it("should use packageIdentifierToDisplayName if no heading or frontmatter name (no heading)", () => {
223 const content = `---
224globs: "**/test/**/*.kt"
225---
226
227This is a test rule without a heading.`;
228
229 const result = markdownToRule(content, mockId);
230 expect(result.name).toBe("to/file"); // Should use last two path segments
231 });
232
233 it("should include description from frontmatter", () => {
234 const content = `---
235globs: "**/test/**/*.kt"
236name: Test Rule
237description: This is a rule description from frontmatter
238---
239
240# Test Rule
241
242This is the content of the rule.`;
243
244 const result = markdownToRule(content, mockId);
245 expect(result.description).toBe(
246 "This is a rule description from frontmatter",
247 );
248 });
249
250 it("should include alwaysApply from frontmatter", () => {
251 const content = `---
252globs: "**/test/**/*.kt"
253name: Test Rule
254alwaysApply: false
255---
256
257# Test Rule
258
259This is a rule with alwaysApply explicitly set to false.`;
260
261 const result = markdownToRule(content, mockId);
262 expect(result.alwaysApply).toBe(false);
263 });
264});
265
266describe("getRuleName", () => {
267 it("should return frontmatter name when provided", () => {
268 const frontmatter = { name: "Custom Rule Name" };
269 const id: PackageIdentifier = {
270 uriType: "file",
271 fileUri: "/path/to/my-rule.md",
272 };
273
274 const result = getRuleName(frontmatter, id);
275 expect(result).toBe("Custom Rule Name");
276 });
277
278 it("should return last two path segments for file identifier when no name", () => {
279 const frontmatter = {};
280 const id: PackageIdentifier = {
281 uriType: "file",
282 fileUri: "/long/path/to/rules/my-rule.md",
283 };
284
285 const result = getRuleName(frontmatter, id);
286 expect(result).toBe("rules/my-rule.md");
287 });
288
289 it("should handle file paths with backslashes", () => {
290 const frontmatter = {};
291 const id: PackageIdentifier = {
292 uriType: "file",
293 fileUri: "C:\\long\\path\\to\\rules\\my-rule.md",
294 };
295
296 const result = getRuleName(frontmatter, id);
297 expect(result).toBe("rules/my-rule.md");
298 });
299
300 it("should handle mixed forward and back slashes", () => {
301 const frontmatter = {};
302 const id: PackageIdentifier = {
303 uriType: "file",
304 fileUri: "/path/to\\rules/my-rule.md",
305 };
306
307 const result = getRuleName(frontmatter, id);
308 expect(result).toBe("rules/my-rule.md");
309 });
310
311 it("should handle single segment path", () => {
312 const frontmatter = {};
313 const id: PackageIdentifier = {
314 uriType: "file",
315 fileUri: "my-rule.md",
316 };
317
318 const result = getRuleName(frontmatter, id);
319 expect(result).toBe("my-rule.md");
320 });
321
322 it("should handle two segment path", () => {
323 const frontmatter = {};
324 const id: PackageIdentifier = {
325 uriType: "file",
326 fileUri: "rules/my-rule.md",
327 };
328
329 const result = getRuleName(frontmatter, id);
330 expect(result).toBe("rules/my-rule.md");
331 });
332
333 it("should return display name for non-file identifiers", () => {
334 const frontmatter = {};
335 const id: PackageIdentifier = {
336 uriType: "slug",
337 fullSlug: {
338 ownerSlug: "test-owner",
339 packageSlug: "my-rule-package",
340 versionSlug: "v1.0.0",
341 },
342 };
343
344 const result = getRuleName(frontmatter, id);
345 expect(result).toBe("my-rule-package"); // packageIdentifierToDisplayName returns just the packageSlug
346 });
347
348 it("should prioritize frontmatter name over file path", () => {
349 const frontmatter = { name: "Override Name" };
350 const id: PackageIdentifier = {
351 uriType: "file",
352 fileUri: "/very/long/path/to/rules/original-name.md",
353 };
354
355 const result = getRuleName(frontmatter, id);
356 expect(result).toBe("Override Name");
357 });
358
359 it("should handle empty string name in frontmatter by falling back to path", () => {
360 const frontmatter = { name: "" };
361 const id: PackageIdentifier = {
362 uriType: "file",
363 fileUri: "/path/to/rules/my-rule.md",
364 };
365
366 const result = getRuleName(frontmatter, id);
367 expect(result).toBe("rules/my-rule.md"); // Empty string is falsy, so falls back to path logic
368 });
369});