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