Elffuss BraidHub

kikocis/continue public

d0cd3fef8227 · 4.7 KiBRawPermalink
1import { PackageIdentifier } from "../browser.js";
2import { markdownToRule } from "./markdownToRule.js";
3
4describe("markdownToRule", () => {
5 // Use a mock PackageIdentifier for testing
6 const mockId: PackageIdentifier = {
7 uriType: "file",
8 filePath: "/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("**/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("**/test/**/*.kt");
38 expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
39 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
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).toBeUndefined();
49 expect(result.rule).toBe(content);
50 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
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).toBeUndefined();
63 expect(result.rule).toBe(
64 "# Test Rule\n\nThis is a test rule with empty frontmatter.",
65 );
66 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
67 });
68
69 it("should handle frontmatter with whitespace", () => {
70 const content = `---
71globs: "**/test/**/*.kt"
72---
73
74# Test Rule
75
76This is a test rule.`;
77
78 const result = markdownToRule(content, mockId);
79 expect(result.globs).toBe("**/test/**/*.kt");
80 expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
81 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
82 });
83
84 it("should handle Windows line endings (CRLF)", () => {
85 // Using \r\n for CRLF line endings
86 const content = `---\r
87globs: "**/test/**/*.kt"\r
88---\r
89\r
90# Test Rule\r
91\r
92This is a test rule.`;
93
94 const result = markdownToRule(content, mockId);
95 expect(result.globs).toBe("**/test/**/*.kt");
96 // The result should be normalized to \n
97 expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
98 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
99 });
100
101 it("should handle malformed frontmatter", () => {
102 const content = `---
103globs: - "**/test/**/*.kt"
104invalid: yaml: content
105---
106
107# Test Rule
108
109This is a test rule.`;
110
111 // Should treat as only markdown when frontmatter is malformed
112 const result = markdownToRule(content, mockId);
113 expect(result.globs).toBeUndefined();
114 expect(result.rule).toBe(content);
115 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
116 });
117
118 it("should use packageIdentifierToDisplayName if no heading or frontmatter name", () => {
119 const content = `---
120globs: "**/test/**/*.kt"
121---
122
123# Test Rule Title
124
125This is a test rule.`;
126
127 const result = markdownToRule(content, mockId);
128 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
129 });
130
131 it("should use packageIdentifierToDisplayName if no heading or frontmatter name (no heading)", () => {
132 const content = `---
133globs: "**/test/**/*.kt"
134---
135
136This is a test rule without a heading.`;
137
138 const result = markdownToRule(content, mockId);
139 expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result
140 });
141
142 it("should include description from frontmatter", () => {
143 const content = `---
144globs: "**/test/**/*.kt"
145name: Test Rule
146description: This is a rule description from frontmatter
147---
148
149# Test Rule
150
151This is the content of the rule.`;
152
153 const result = markdownToRule(content, mockId);
154 expect(result.description).toBe(
155 "This is a rule description from frontmatter",
156 );
157 });
158
159 it("should include alwaysApply from frontmatter", () => {
160 const content = `---
161globs: "**/test/**/*.kt"
162name: Test Rule
163alwaysApply: false
164---
165
166# Test Rule
167
168This is a rule with alwaysApply explicitly set to false.`;
169
170 const result = markdownToRule(content, mockId);
171 expect(result.alwaysApply).toBe(false);
172 });
173});