Changesets · Branches · Tags · Compare
| Changeset | de41b37de6c35fa44d1929e77ef8539a19675f64ec63f004ccc54e1aec137cd7 |
|---|---|
| Author | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-05-22 10:17 -07:00 |
| Committer | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-05-22 10:17 -07:00 |
| Parents | 4d749ae79aba |
2 files changed
modified core/tools/implementations/createRuleBlock.test.ts +12 −58
| @@ -1,4 +1,4 @@ | ||
| 1 | -import * as YAML from "yaml"; | |
| 1 | +import { parseMarkdownRule } from "../../config/markdown/parseMarkdownRule"; | |
| 2 | 2 | import { createRuleBlockImpl } from "./createRuleBlock"; |
| 3 | 3 | |
| 4 | 4 | // Mock the extras parameter with necessary functions |
| @@ -17,36 +17,6 @@ | ||
| 17 | 17 | jest.clearAllMocks(); |
| 18 | 18 | }); |
| 19 | 19 | |
| 20 | - it("should create a basic rule with name and rule content", async () => { | |
| 21 | - const args = { | |
| 22 | - name: "Test Rule", | |
| 23 | - rule: "Always write tests", | |
| 24 | - }; | |
| 25 | - | |
| 26 | - const result = await createRuleBlockImpl(args, mockExtras as any); | |
| 27 | - | |
| 28 | - // Verify that writeFile was called | |
| 29 | - expect(mockIde.writeFile).toHaveBeenCalled(); | |
| 30 | - | |
| 31 | - // Verify the file name | |
| 32 | - const fileUri = mockIde.writeFile.mock.calls[0][0]; | |
| 33 | - expect(fileUri).toContain("test-rule.md"); | |
| 34 | - | |
| 35 | - // Get the content | |
| 36 | - const fileContent = mockIde.writeFile.mock.calls[0][1]; | |
| 37 | - | |
| 38 | - // Verify the content structure | |
| 39 | - expect(fileContent).toContain("---"); | |
| 40 | - expect(fileContent).toContain("# Test Rule"); | |
| 41 | - expect(fileContent).toContain("Always write tests"); | |
| 42 | - | |
| 43 | - // Should have empty frontmatter - it will be "{}" not "" due to how YAML.stringify works with empty objects | |
| 44 | - const parts = fileContent.split(/^---\s*$/m); | |
| 45 | - expect(parts.length).toBeGreaterThanOrEqual(3); | |
| 46 | - const frontmatterStr = parts[1].trim(); | |
| 47 | - expect(frontmatterStr === "" || frontmatterStr === "{}").toBeTruthy(); | |
| 48 | - }); | |
| 49 | - | |
| 50 | 20 | it("should create a rule with glob pattern", async () => { |
| 51 | 21 | const args = { |
| 52 | 22 | name: "TypeScript Rule", |
| @@ -56,22 +26,16 @@ | ||
| 56 | 26 | |
| 57 | 27 | await createRuleBlockImpl(args, mockExtras as any); |
| 58 | 28 | |
| 59 | - // Get the content | |
| 60 | 29 | const fileContent = mockIde.writeFile.mock.calls[0][1]; |
| 61 | 30 | |
| 62 | - // Parse the frontmatter | |
| 63 | - const parts = fileContent.split(/^---\s*$/m); | |
| 64 | - const frontmatterStr = parts[1].trim(); | |
| 65 | - const frontmatter = YAML.parse(frontmatterStr); | |
| 31 | + const { frontmatter, markdown } = parseMarkdownRule(fileContent); | |
| 66 | 32 | |
| 67 | - // Verify the frontmatter contains globs | |
| 68 | 33 | expect(frontmatter).toEqual({ |
| 69 | 34 | globs: "**/*.{ts,tsx}", |
| 70 | 35 | }); |
| 71 | 36 | |
| 72 | - // Verify the content has the rule | |
| 73 | - expect(fileContent).toContain("# TypeScript Rule"); | |
| 74 | - expect(fileContent).toContain("Use interfaces for object shapes"); | |
| 37 | + expect(markdown).toContain("# TypeScript Rule"); | |
| 38 | + expect(markdown).toContain("Use interfaces for object shapes"); | |
| 75 | 39 | }); |
| 76 | 40 | |
| 77 | 41 | it("should create a filename based on sanitized rule name", async () => { |
| @@ -82,12 +46,11 @@ | ||
| 82 | 46 | |
| 83 | 47 | await createRuleBlockImpl(args, mockExtras as any); |
| 84 | 48 | |
| 85 | - // Check that the filename is sanitized | |
| 86 | 49 | const fileUri = mockIde.writeFile.mock.calls[0][0]; |
| 87 | 50 | expect(fileUri).toContain("special-chracters-spaces.md"); |
| 88 | 51 | }); |
| 89 | 52 | |
| 90 | - it("should include description in frontmatter and content", async () => { | |
| 53 | + it("should create a rule with description pattern", async () => { | |
| 91 | 54 | const args = { |
| 92 | 55 | name: "Description Test", |
| 93 | 56 | rule: "This is the rule content", |
| @@ -96,23 +59,16 @@ | ||
| 96 | 59 | |
| 97 | 60 | await createRuleBlockImpl(args, mockExtras as any); |
| 98 | 61 | |
| 99 | - // Get the content | |
| 100 | 62 | const fileContent = mockIde.writeFile.mock.calls[0][1]; |
| 101 | 63 | |
| 102 | - // Parse the frontmatter | |
| 103 | - const parts = fileContent.split(/^---\s*$/m); | |
| 104 | - const frontmatterStr = parts[1].trim(); | |
| 105 | - const frontmatter = YAML.parse(frontmatterStr); | |
| 64 | + const { frontmatter, markdown } = parseMarkdownRule(fileContent); | |
| 106 | 65 | |
| 107 | - // Verify the frontmatter contains description | |
| 108 | 66 | expect(frontmatter).toEqual({ |
| 109 | 67 | description: "This is a detailed explanation of the rule", |
| 110 | 68 | }); |
| 111 | 69 | |
| 112 | - // Verify the content structure includes description before rule | |
| 113 | - expect(fileContent).toContain("# Description Test"); | |
| 114 | - expect(fileContent).toContain("This is a detailed explanation of the rule"); | |
| 115 | - expect(fileContent).toContain("This is the rule content"); | |
| 70 | + expect(markdown).toContain("# Description Test"); | |
| 71 | + expect(markdown).toContain("This is the rule content"); | |
| 116 | 72 | }); |
| 117 | 73 | |
| 118 | 74 | it("should include both globs and description in frontmatter", async () => { |
| @@ -125,18 +81,16 @@ | ||
| 125 | 81 | |
| 126 | 82 | await createRuleBlockImpl(args, mockExtras as any); |
| 127 | 83 | |
| 128 | - // Get the content | |
| 129 | 84 | const fileContent = mockIde.writeFile.mock.calls[0][1]; |
| 130 | 85 | |
| 131 | - // Parse the frontmatter | |
| 132 | - const parts = fileContent.split(/^---\s*$/m); | |
| 133 | - const frontmatterStr = parts[1].trim(); | |
| 134 | - const frontmatter = YAML.parse(frontmatterStr); | |
| 86 | + const { frontmatter, markdown } = parseMarkdownRule(fileContent); | |
| 135 | 87 | |
| 136 | - // Verify the frontmatter contains both description and globs | |
| 137 | 88 | expect(frontmatter).toEqual({ |
| 138 | 89 | description: "This rule enforces our team standards", |
| 139 | 90 | globs: "**/*.js", |
| 140 | 91 | }); |
| 92 | + | |
| 93 | + expect(markdown).toContain("# Complete Rule"); | |
| 94 | + expect(markdown).toContain("Follow this standard"); | |
| 141 | 95 | }); |
| 142 | 96 | }); |
modified core/tools/implementations/createRuleBlock.ts +1 −1
| @@ -5,8 +5,8 @@ | ||
| 5 | 5 | export interface CreateRuleBlockArgs { |
| 6 | 6 | name: string; |
| 7 | 7 | rule: string; |
| 8 | + description: string; | |
| 8 | 9 | globs?: string; |
| 9 | - description?: string; | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | export const createRuleBlockImpl: ToolImpl = async ( |