| 1 | import { |
| 2 | convertMarkdownRuleToContinueRule, |
| 3 | parseMarkdownRule, |
| 4 | } from "./parseMarkdownRule"; |
| 5 | |
| 6 | describe("parseMarkdownRule", () => { |
| 7 | it("should correctly parse markdown with YAML frontmatter", () => { |
| 8 | const content = `--- |
| 9 | globs: "**/test/**/*.kt" |
| 10 | --- |
| 11 | |
| 12 | # Test Rule |
| 13 | |
| 14 | This is a test rule.`; |
| 15 | |
| 16 | const result = parseMarkdownRule(content); |
| 17 | expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" }); |
| 18 | expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule."); |
| 19 | }); |
| 20 | |
| 21 | it("should handle missing frontmatter", () => { |
| 22 | const content = `# Test Rule |
| 23 | |
| 24 | This is a test rule without frontmatter.`; |
| 25 | |
| 26 | const result = parseMarkdownRule(content); |
| 27 | expect(result.frontmatter).toEqual({}); |
| 28 | expect(result.markdown).toBe(content); |
| 29 | }); |
| 30 | |
| 31 | it("should handle empty frontmatter", () => { |
| 32 | const content = `--- |
| 33 | --- |
| 34 | |
| 35 | # Test Rule |
| 36 | |
| 37 | This is a test rule with empty frontmatter.`; |
| 38 | |
| 39 | const result = parseMarkdownRule(content); |
| 40 | |
| 41 | // Log exact strings for debugging |
| 42 | console.log("Actual:", JSON.stringify(result.markdown)); |
| 43 | console.log( |
| 44 | "Expected:", |
| 45 | JSON.stringify( |
| 46 | "# Test Rule\n\nThis is a test rule with empty frontmatter.", |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | expect(result.frontmatter).toEqual({}); |
| 51 | expect(result.markdown).toBe( |
| 52 | "# Test Rule\n\nThis is a test rule with empty frontmatter.", |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | it("should handle frontmatter with whitespace", () => { |
| 57 | const content = `--- |
| 58 | globs: "**/test/**/*.kt" |
| 59 | --- |
| 60 | |
| 61 | # Test Rule |
| 62 | |
| 63 | This is a test rule.`; |
| 64 | |
| 65 | const result = parseMarkdownRule(content); |
| 66 | expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" }); |
| 67 | expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule."); |
| 68 | }); |
| 69 | |
| 70 | it("should handle Windows line endings (CRLF)", () => { |
| 71 | // Using \r\n for CRLF line endings |
| 72 | const content = `---\r |
| 73 | globs: "**/test/**/*.kt"\r |
| 74 | ---\r |
| 75 | \r |
| 76 | # Test Rule\r |
| 77 | \r |
| 78 | This is a test rule.`; |
| 79 | |
| 80 | const result = parseMarkdownRule(content); |
| 81 | expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" }); |
| 82 | // The result should be normalized to \n |
| 83 | expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule."); |
| 84 | }); |
| 85 | |
| 86 | it("should handle malformed frontmatter", () => { |
| 87 | const content = `--- |
| 88 | globs: - "**/test/**/*.kt" |
| 89 | invalid: yaml: content |
| 90 | --- |
| 91 | |
| 92 | # Test Rule |
| 93 | |
| 94 | This is a test rule.`; |
| 95 | |
| 96 | // Should treat as only markdown when frontmatter is malformed |
| 97 | const result = parseMarkdownRule(content); |
| 98 | expect(result.frontmatter).toEqual({}); |
| 99 | expect(result.markdown).toBe(content); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("convertMarkdownRuleToContinueRule", () => { |
| 104 | it("should convert markdown with frontmatter to a rule", () => { |
| 105 | const content = `--- |
| 106 | globs: "**/test/**/*.kt" |
| 107 | name: Custom Name |
| 108 | --- |
| 109 | |
| 110 | # Test Rule |
| 111 | |
| 112 | This is a test rule.`; |
| 113 | |
| 114 | const result = convertMarkdownRuleToContinueRule( |
| 115 | "/path/to/rule.md", |
| 116 | content, |
| 117 | ); |
| 118 | expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); |
| 119 | expect(result.globs).toBe("**/test/**/*.kt"); |
| 120 | expect(result.name).toBe("Custom Name"); |
| 121 | expect(result.source).toBe("rules-block"); |
| 122 | expect(result.ruleFile).toBe("/path/to/rule.md"); |
| 123 | }); |
| 124 | |
| 125 | it("should use the first heading as name if not in frontmatter", () => { |
| 126 | const content = `--- |
| 127 | globs: "**/test/**/*.kt" |
| 128 | --- |
| 129 | |
| 130 | # Test Rule Title |
| 131 | |
| 132 | This is a test rule.`; |
| 133 | |
| 134 | const result = convertMarkdownRuleToContinueRule( |
| 135 | "/path/to/rule.md", |
| 136 | content, |
| 137 | ); |
| 138 | expect(result.name).toBe("Test Rule Title"); |
| 139 | }); |
| 140 | |
| 141 | it("should use filename as name if no heading or frontmatter name", () => { |
| 142 | const content = `--- |
| 143 | globs: "**/test/**/*.kt" |
| 144 | --- |
| 145 | |
| 146 | This is a test rule without a heading.`; |
| 147 | |
| 148 | const result = convertMarkdownRuleToContinueRule( |
| 149 | "/path/to/custom-rule.md", |
| 150 | content, |
| 151 | ); |
| 152 | expect(result.name).toBe("custom-rule"); |
| 153 | }); |
| 154 | }); |