| 1 | import { |
| 2 | createMarkdownWithFrontmatter, |
| 3 | createRuleFilePath, |
| 4 | createRuleMarkdown, |
| 5 | sanitizeRuleName, |
| 6 | } from "./createMarkdownRule"; |
| 7 | import { parseMarkdownRule } from "./parseMarkdownRule"; |
| 8 | |
| 9 | describe("sanitizeRuleName", () => { |
| 10 | it("should sanitize rule names for filenames", () => { |
| 11 | expect(sanitizeRuleName("My Test Rule")).toBe("my-test-rule"); |
| 12 | expect(sanitizeRuleName("Rule with @#$% chars")).toBe("rule-with-chars"); |
| 13 | expect(sanitizeRuleName("Multiple spaces")).toBe("multiple-spaces"); |
| 14 | expect(sanitizeRuleName("UPPERCASE-rule")).toBe("uppercase-rule"); |
| 15 | expect(sanitizeRuleName("already-sanitized")).toBe("already-sanitized"); |
| 16 | }); |
| 17 | |
| 18 | it("should handle empty and edge case inputs", () => { |
| 19 | expect(sanitizeRuleName("")).toBe(""); |
| 20 | expect(sanitizeRuleName(" ")).toBe(""); |
| 21 | expect(sanitizeRuleName("123")).toBe("123"); |
| 22 | expect(sanitizeRuleName("rule-with-numbers-123")).toBe( |
| 23 | "rule-with-numbers-123", |
| 24 | ); |
| 25 | }); |
| 26 | }); |
| 27 | |
| 28 | describe("createRuleFilePath", () => { |
| 29 | it("should create correct rule file path", () => { |
| 30 | const result = createRuleFilePath("/workspace", "My Test Rule"); |
| 31 | expect(result).toBe("/workspace/.continue/rules/my-test-rule.md"); |
| 32 | }); |
| 33 | |
| 34 | it("should handle special characters in rule name", () => { |
| 35 | const result = createRuleFilePath("/home/user", "Rule with @#$% chars"); |
| 36 | expect(result).toBe("/home/user/.continue/rules/rule-with-chars.md"); |
| 37 | }); |
| 38 | |
| 39 | it("should handle edge case rule names", () => { |
| 40 | const result = createRuleFilePath("/test", " Multiple Spaces "); |
| 41 | expect(result).toBe("/test/.continue/rules/multiple-spaces.md"); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe("createMarkdownWithFrontmatter", () => { |
| 46 | it("should create properly formatted markdown with frontmatter", () => { |
| 47 | const frontmatter = { |
| 48 | name: "Test Rule", |
| 49 | description: "A test rule", |
| 50 | globs: "*.ts", |
| 51 | }; |
| 52 | const markdown = "# Test Rule\n\nThis is a test rule."; |
| 53 | |
| 54 | const result = createMarkdownWithFrontmatter(frontmatter, markdown); |
| 55 | |
| 56 | // The exact quote style doesn't matter as long as it parses correctly |
| 57 | expect(result).toContain("name: Test Rule"); |
| 58 | expect(result).toContain("description: A test rule"); |
| 59 | expect(result).toContain("globs:"); |
| 60 | expect(result).toContain("*.ts"); |
| 61 | expect(result).toContain("---\n\n# Test Rule\n\nThis is a test rule."); |
| 62 | }); |
| 63 | |
| 64 | it("should handle empty frontmatter", () => { |
| 65 | const frontmatter = {}; |
| 66 | const markdown = "# Simple Rule\n\nJust markdown content."; |
| 67 | |
| 68 | const result = createMarkdownWithFrontmatter(frontmatter, markdown); |
| 69 | |
| 70 | const expected = `--- |
| 71 | {} |
| 72 | --- |
| 73 | |
| 74 | # Simple Rule |
| 75 | |
| 76 | Just markdown content.`; |
| 77 | |
| 78 | expect(result).toBe(expected); |
| 79 | }); |
| 80 | |
| 81 | it("should create content that can be parsed back correctly", () => { |
| 82 | const originalFrontmatter = { |
| 83 | name: "Roundtrip Test", |
| 84 | description: "Testing roundtrip parsing", |
| 85 | globs: ["*.js", "*.ts"], |
| 86 | alwaysApply: true, |
| 87 | }; |
| 88 | const originalMarkdown = |
| 89 | "# Roundtrip Test\n\nThis should parse back correctly."; |
| 90 | |
| 91 | const created = createMarkdownWithFrontmatter( |
| 92 | originalFrontmatter, |
| 93 | originalMarkdown, |
| 94 | ); |
| 95 | const parsed = parseMarkdownRule(created); |
| 96 | |
| 97 | expect(parsed.frontmatter).toEqual(originalFrontmatter); |
| 98 | expect(parsed.markdown).toBe(originalMarkdown); |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | describe("createRuleMarkdown", () => { |
| 103 | it("should create rule markdown with all options", () => { |
| 104 | const result = createRuleMarkdown("Test Rule", "This is the rule content", { |
| 105 | description: "Test description", |
| 106 | globs: ["*.ts", "*.js"], |
| 107 | alwaysApply: true, |
| 108 | }); |
| 109 | |
| 110 | const parsed = parseMarkdownRule(result); |
| 111 | |
| 112 | expect(parsed.frontmatter.description).toBe("Test description"); |
| 113 | expect(parsed.frontmatter.globs).toEqual(["*.ts", "*.js"]); |
| 114 | expect(parsed.frontmatter.alwaysApply).toBe(true); |
| 115 | expect(parsed.markdown).toBe("# Test Rule\n\nThis is the rule content"); |
| 116 | }); |
| 117 | |
| 118 | it("should create rule markdown with minimal options", () => { |
| 119 | const result = createRuleMarkdown("Simple Rule", "Simple content"); |
| 120 | |
| 121 | const parsed = parseMarkdownRule(result); |
| 122 | |
| 123 | expect(parsed.frontmatter.description).toBeUndefined(); |
| 124 | expect(parsed.frontmatter.globs).toBeUndefined(); |
| 125 | expect(parsed.frontmatter.alwaysApply).toBeUndefined(); |
| 126 | expect(parsed.markdown).toBe("# Simple Rule\n\nSimple content"); |
| 127 | }); |
| 128 | |
| 129 | it("should handle string globs", () => { |
| 130 | const result = createRuleMarkdown("String Glob Rule", "Content", { |
| 131 | globs: "*.py", |
| 132 | }); |
| 133 | |
| 134 | const parsed = parseMarkdownRule(result); |
| 135 | expect(parsed.frontmatter.globs).toBe("*.py"); |
| 136 | }); |
| 137 | |
| 138 | it("should trim description and globs", () => { |
| 139 | const result = createRuleMarkdown("Trim Test", "Content", { |
| 140 | description: " spaced description ", |
| 141 | globs: " *.ts ", |
| 142 | }); |
| 143 | |
| 144 | const parsed = parseMarkdownRule(result); |
| 145 | expect(parsed.frontmatter.description).toBe("spaced description"); |
| 146 | expect(parsed.frontmatter.globs).toBe("*.ts"); |
| 147 | }); |
| 148 | |
| 149 | it("should handle alwaysApply false explicitly", () => { |
| 150 | const result = createRuleMarkdown("Always Apply False", "Content", { |
| 151 | alwaysApply: false, |
| 152 | }); |
| 153 | |
| 154 | const parsed = parseMarkdownRule(result); |
| 155 | expect(parsed.frontmatter.alwaysApply).toBe(false); |
| 156 | }); |
| 157 | }); |