Elffuss BraidHub

kikocis/continue public

51e47455f9be · 4.7 KiBRawPermalink
1import { PackageIdentifier } from "../browser.js";
2import {
3 createMarkdownWithFrontmatter,
4 createRuleMarkdown,
5 sanitizeRuleName,
6} from "./createMarkdownRule.js";
7import { markdownToRule } from "./markdownToRule.js";
8
9// Mock package identifier for testing
10const mockPackageId: PackageIdentifier = {
11 uriType: "file",
12 filePath: "/path/to/file",
13};
14
15describe("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
34describe("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
65Just 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(originalFrontmatter.globs);
89 expect(parsed.alwaysApply).toBe(originalFrontmatter.alwaysApply);
90 expect(parsed.rule).toBe(originalMarkdown);
91 });
92});
93
94describe("createRuleMarkdown", () => {
95 it("should create rule markdown with all options", () => {
96 const result = createRuleMarkdown("Test Rule", "This is the rule content", {
97 description: "Test description",
98 globs: ["*.ts", "*.js"],
99 alwaysApply: true,
100 });
101
102 const parsed = markdownToRule(result, mockPackageId);
103
104 expect(parsed.description).toBe("Test description");
105 expect(parsed.globs).toEqual(["*.ts", "*.js"]);
106 expect(parsed.alwaysApply).toBe(true);
107 expect(parsed.rule).toBe("This is the rule content");
108 });
109
110 it("should create rule markdown with minimal options", () => {
111 const result = createRuleMarkdown("Simple Rule", "Simple content");
112
113 const parsed = markdownToRule(result, mockPackageId);
114
115 expect(parsed.description).toBeUndefined();
116 expect(parsed.globs).toBeUndefined();
117 expect(parsed.alwaysApply).toBeUndefined();
118 expect(parsed.rule).toBe("Simple content");
119 });
120
121 it("should handle string globs", () => {
122 const result = createRuleMarkdown("String Glob Rule", "Content", {
123 globs: "*.py",
124 });
125
126 const parsed = markdownToRule(result, mockPackageId);
127 expect(parsed.globs).toBe("*.py");
128 });
129
130 it("should trim description and globs", () => {
131 const result = createRuleMarkdown("Trim Test", "Content", {
132 description: " spaced description ",
133 globs: " *.ts ",
134 });
135
136 const parsed = markdownToRule(result, mockPackageId);
137 expect(parsed.description).toBe("spaced description");
138 expect(parsed.globs).toBe("*.ts");
139 });
140
141 it("should handle alwaysApply false explicitly", () => {
142 const result = createRuleMarkdown("Always Apply False", "Content", {
143 alwaysApply: false,
144 });
145
146 const parsed = markdownToRule(result, mockPackageId);
147 expect(parsed.alwaysApply).toBe(false);
148 });
149});