| 1 | import { PackageIdentifier } from "../browser.js"; |
| 2 | import { getRuleName, markdownToRule } from "./markdownToRule.js"; |
| 3 | |
| 4 | describe("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 = `--- |
| 13 | globs: "**/test/**/*.kt" |
| 14 | name: Custom Name |
| 15 | --- |
| 16 | |
| 17 | # Test Rule |
| 18 | |
| 19 | This 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 = `--- |
| 29 | globs: "**/test/**/*.kt" |
| 30 | --- |
| 31 | |
| 32 | # Test Rule |
| 33 | |
| 34 | This 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("to/file"); // Should use last two path segments |
| 40 | }); |
| 41 | |
| 42 | it("should handle missing frontmatter", () => { |
| 43 | const content = `# Test Rule |
| 44 | |
| 45 | This 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("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 | |
| 59 | This 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("to/file"); // Should use last two path segments |
| 67 | }); |
| 68 | |
| 69 | it("should handle frontmatter with whitespace", () => { |
| 70 | const content = `--- |
| 71 | globs: "**/test/**/*.kt" |
| 72 | --- |
| 73 | |
| 74 | # Test Rule |
| 75 | |
| 76 | This 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("to/file"); // Should use last two path segments |
| 82 | }); |
| 83 | |
| 84 | it("should handle Windows line endings (CRLF)", () => { |
| 85 | // Using \r\n for CRLF line endings |
| 86 | const content = `---\r |
| 87 | globs: "**/test/**/*.kt"\r |
| 88 | ---\r |
| 89 | \r |
| 90 | # Test Rule\r |
| 91 | \r |
| 92 | This 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("to/file"); // Should use last two path segments |
| 99 | }); |
| 100 | |
| 101 | it("should handle malformed frontmatter", () => { |
| 102 | const content = `--- |
| 103 | globs: - "**/test/**/*.kt" |
| 104 | invalid: yaml: content |
| 105 | --- |
| 106 | |
| 107 | # Test Rule |
| 108 | |
| 109 | This 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("to/file"); // Should use last two path segments |
| 116 | }); |
| 117 | |
| 118 | it("should use packageIdentifierToDisplayName if no heading or frontmatter name", () => { |
| 119 | const content = `--- |
| 120 | globs: "**/test/**/*.kt" |
| 121 | --- |
| 122 | |
| 123 | # Test Rule Title |
| 124 | |
| 125 | This is a test rule.`; |
| 126 | |
| 127 | const result = markdownToRule(content, mockId); |
| 128 | expect(result.name).toBe("to/file"); // Should use last two path segments |
| 129 | }); |
| 130 | |
| 131 | it("should use packageIdentifierToDisplayName if no heading or frontmatter name (no heading)", () => { |
| 132 | const content = `--- |
| 133 | globs: "**/test/**/*.kt" |
| 134 | --- |
| 135 | |
| 136 | This is a test rule without a heading.`; |
| 137 | |
| 138 | const result = markdownToRule(content, mockId); |
| 139 | expect(result.name).toBe("to/file"); // Should use last two path segments |
| 140 | }); |
| 141 | |
| 142 | it("should include description from frontmatter", () => { |
| 143 | const content = `--- |
| 144 | globs: "**/test/**/*.kt" |
| 145 | name: Test Rule |
| 146 | description: This is a rule description from frontmatter |
| 147 | --- |
| 148 | |
| 149 | # Test Rule |
| 150 | |
| 151 | This 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 = `--- |
| 161 | globs: "**/test/**/*.kt" |
| 162 | name: Test Rule |
| 163 | alwaysApply: false |
| 164 | --- |
| 165 | |
| 166 | # Test Rule |
| 167 | |
| 168 | This is a rule with alwaysApply explicitly set to false.`; |
| 169 | |
| 170 | const result = markdownToRule(content, mockId); |
| 171 | expect(result.alwaysApply).toBe(false); |
| 172 | }); |
| 173 | }); |
| 174 | |
| 175 | describe("getRuleName", () => { |
| 176 | it("should return frontmatter name when provided", () => { |
| 177 | const frontmatter = { name: "Custom Rule Name" }; |
| 178 | const id: PackageIdentifier = { |
| 179 | uriType: "file", |
| 180 | filePath: "/path/to/my-rule.md", |
| 181 | }; |
| 182 | |
| 183 | const result = getRuleName(frontmatter, id); |
| 184 | expect(result).toBe("Custom Rule Name"); |
| 185 | }); |
| 186 | |
| 187 | it("should return last two path segments for file identifier when no name", () => { |
| 188 | const frontmatter = {}; |
| 189 | const id: PackageIdentifier = { |
| 190 | uriType: "file", |
| 191 | filePath: "/long/path/to/rules/my-rule.md", |
| 192 | }; |
| 193 | |
| 194 | const result = getRuleName(frontmatter, id); |
| 195 | expect(result).toBe("rules/my-rule.md"); |
| 196 | }); |
| 197 | |
| 198 | it("should handle file paths with backslashes", () => { |
| 199 | const frontmatter = {}; |
| 200 | const id: PackageIdentifier = { |
| 201 | uriType: "file", |
| 202 | filePath: "C:\\long\\path\\to\\rules\\my-rule.md", |
| 203 | }; |
| 204 | |
| 205 | const result = getRuleName(frontmatter, id); |
| 206 | expect(result).toBe("rules/my-rule.md"); |
| 207 | }); |
| 208 | |
| 209 | it("should handle mixed forward and back slashes", () => { |
| 210 | const frontmatter = {}; |
| 211 | const id: PackageIdentifier = { |
| 212 | uriType: "file", |
| 213 | filePath: "/path/to\\rules/my-rule.md", |
| 214 | }; |
| 215 | |
| 216 | const result = getRuleName(frontmatter, id); |
| 217 | expect(result).toBe("rules/my-rule.md"); |
| 218 | }); |
| 219 | |
| 220 | it("should handle single segment path", () => { |
| 221 | const frontmatter = {}; |
| 222 | const id: PackageIdentifier = { |
| 223 | uriType: "file", |
| 224 | filePath: "my-rule.md", |
| 225 | }; |
| 226 | |
| 227 | const result = getRuleName(frontmatter, id); |
| 228 | expect(result).toBe("my-rule.md"); |
| 229 | }); |
| 230 | |
| 231 | it("should handle two segment path", () => { |
| 232 | const frontmatter = {}; |
| 233 | const id: PackageIdentifier = { |
| 234 | uriType: "file", |
| 235 | filePath: "rules/my-rule.md", |
| 236 | }; |
| 237 | |
| 238 | const result = getRuleName(frontmatter, id); |
| 239 | expect(result).toBe("rules/my-rule.md"); |
| 240 | }); |
| 241 | |
| 242 | it("should return display name for non-file identifiers", () => { |
| 243 | const frontmatter = {}; |
| 244 | const id: PackageIdentifier = { |
| 245 | uriType: "slug", |
| 246 | fullSlug: { |
| 247 | ownerSlug: "test-owner", |
| 248 | packageSlug: "my-rule-package", |
| 249 | versionSlug: "v1.0.0", |
| 250 | }, |
| 251 | }; |
| 252 | |
| 253 | const result = getRuleName(frontmatter, id); |
| 254 | expect(result).toBe("my-rule-package"); // packageIdentifierToDisplayName returns just the packageSlug |
| 255 | }); |
| 256 | |
| 257 | it("should prioritize frontmatter name over file path", () => { |
| 258 | const frontmatter = { name: "Override Name" }; |
| 259 | const id: PackageIdentifier = { |
| 260 | uriType: "file", |
| 261 | filePath: "/very/long/path/to/rules/original-name.md", |
| 262 | }; |
| 263 | |
| 264 | const result = getRuleName(frontmatter, id); |
| 265 | expect(result).toBe("Override Name"); |
| 266 | }); |
| 267 | |
| 268 | it("should handle empty string name in frontmatter by falling back to path", () => { |
| 269 | const frontmatter = { name: "" }; |
| 270 | const id: PackageIdentifier = { |
| 271 | uriType: "file", |
| 272 | filePath: "/path/to/rules/my-rule.md", |
| 273 | }; |
| 274 | |
| 275 | const result = getRuleName(frontmatter, id); |
| 276 | expect(result).toBe("rules/my-rule.md"); // Empty string is falsy, so falls back to path logic |
| 277 | }); |
| 278 | }); |