Elffuss BraidHub

kikocis/continue public

main · 10.9 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 it("should include invokable from frontmatter when true", () => {
266 const content = `---
267globs: "**/test/**/*.kt"
268name: Test Rule
269invokable: true
270---
271
272# Test Rule
273
274This is an invokable rule.`;
275
276 const result = markdownToRule(content, mockId);
277 expect(result.invokable).toBe(true);
278 });
279
280 it("should include invokable from frontmatter when false", () => {
281 const content = `---
282globs: "**/test/**/*.kt"
283name: Test Rule
284invokable: false
285---
286
287# Test Rule
288
289This is a non-invokable rule.`;
290
291 const result = markdownToRule(content, mockId);
292 expect(result.invokable).toBe(false);
293 });
294
295 it("should handle invokable with alwaysApply together", () => {
296 const content = `---
297alwaysApply: true
298invokable: true
299name: Test Rule
300---
301
302# Test Rule
303
304This is a rule with both alwaysApply and invokable.`;
305
306 const result = markdownToRule(content, mockId);
307 expect(result.alwaysApply).toBe(true);
308 expect(result.invokable).toBe(true);
309 });
310
311 it("should not include invokable when not in frontmatter", () => {
312 const content = `---
313globs: "**/test/**/*.kt"
314name: Test Rule
315---
316
317# Test Rule
318
319This is a rule without invokable property.`;
320
321 const result = markdownToRule(content, mockId);
322 expect(result.invokable).toBeUndefined();
323 });
324});
325
326describe("getRuleName", () => {
327 it("should return frontmatter name when provided", () => {
328 const frontmatter = { name: "Custom Rule Name" };
329 const id: PackageIdentifier = {
330 uriType: "file",
331 fileUri: "/path/to/my-rule.md",
332 };
333
334 const result = getRuleName(frontmatter, id);
335 expect(result).toBe("Custom Rule Name");
336 });
337
338 it("should return last two path segments for file identifier when no name", () => {
339 const frontmatter = {};
340 const id: PackageIdentifier = {
341 uriType: "file",
342 fileUri: "/long/path/to/rules/my-rule.md",
343 };
344
345 const result = getRuleName(frontmatter, id);
346 expect(result).toBe("rules/my-rule.md");
347 });
348
349 it("should handle file paths with backslashes", () => {
350 const frontmatter = {};
351 const id: PackageIdentifier = {
352 uriType: "file",
353 fileUri: "C:\\long\\path\\to\\rules\\my-rule.md",
354 };
355
356 const result = getRuleName(frontmatter, id);
357 expect(result).toBe("rules/my-rule.md");
358 });
359
360 it("should handle mixed forward and back slashes", () => {
361 const frontmatter = {};
362 const id: PackageIdentifier = {
363 uriType: "file",
364 fileUri: "/path/to\\rules/my-rule.md",
365 };
366
367 const result = getRuleName(frontmatter, id);
368 expect(result).toBe("rules/my-rule.md");
369 });
370
371 it("should handle single segment path", () => {
372 const frontmatter = {};
373 const id: PackageIdentifier = {
374 uriType: "file",
375 fileUri: "my-rule.md",
376 };
377
378 const result = getRuleName(frontmatter, id);
379 expect(result).toBe("my-rule.md");
380 });
381
382 it("should handle two segment path", () => {
383 const frontmatter = {};
384 const id: PackageIdentifier = {
385 uriType: "file",
386 fileUri: "rules/my-rule.md",
387 };
388
389 const result = getRuleName(frontmatter, id);
390 expect(result).toBe("rules/my-rule.md");
391 });
392
393 it("should return display name for non-file identifiers", () => {
394 const frontmatter = {};
395 const id: PackageIdentifier = {
396 uriType: "slug",
397 fullSlug: {
398 ownerSlug: "test-owner",
399 packageSlug: "my-rule-package",
400 versionSlug: "v1.0.0",
401 },
402 };
403
404 const result = getRuleName(frontmatter, id);
405 expect(result).toBe("my-rule-package"); // packageIdentifierToDisplayName returns just the packageSlug
406 });
407
408 it("should prioritize frontmatter name over file path", () => {
409 const frontmatter = { name: "Override Name" };
410 const id: PackageIdentifier = {
411 uriType: "file",
412 fileUri: "/very/long/path/to/rules/original-name.md",
413 };
414
415 const result = getRuleName(frontmatter, id);
416 expect(result).toBe("Override Name");
417 });
418
419 it("should handle empty string name in frontmatter by falling back to path", () => {
420 const frontmatter = { name: "" };
421 const id: PackageIdentifier = {
422 uriType: "file",
423 fileUri: "/path/to/rules/my-rule.md",
424 };
425
426 const result = getRuleName(frontmatter, id);
427 expect(result).toBe("rules/my-rule.md"); // Empty string is falsy, so falls back to path logic
428 });
429});