| 1 | import { parseMarkdownRule } from "../../config/markdown/parseMarkdownRule"; |
| 2 | import { createRuleBlockImpl } from "./createRuleBlock"; |
| 3 | |
| 4 | // Mock the extras parameter with necessary functions |
| 5 | const mockIde = { |
| 6 | getWorkspaceDirs: jest.fn().mockResolvedValue(["/"]), |
| 7 | writeFile: jest.fn().mockResolvedValue(undefined), |
| 8 | openFile: jest.fn().mockResolvedValue(undefined), |
| 9 | }; |
| 10 | |
| 11 | const mockExtras = { |
| 12 | ide: mockIde, |
| 13 | }; |
| 14 | |
| 15 | describe("createRuleBlockImpl", () => { |
| 16 | beforeEach(() => { |
| 17 | jest.clearAllMocks(); |
| 18 | }); |
| 19 | |
| 20 | it("should create a rule with glob pattern", async () => { |
| 21 | const args = { |
| 22 | name: "TypeScript Rule", |
| 23 | rule: "Use interfaces for object shapes", |
| 24 | globs: "**/*.{ts,tsx}", |
| 25 | }; |
| 26 | |
| 27 | await createRuleBlockImpl(args, mockExtras as any); |
| 28 | |
| 29 | const fileContent = mockIde.writeFile.mock.calls[0][1]; |
| 30 | |
| 31 | const { frontmatter, markdown } = parseMarkdownRule(fileContent); |
| 32 | |
| 33 | expect(frontmatter).toEqual({ |
| 34 | globs: "**/*.{ts,tsx}", |
| 35 | }); |
| 36 | |
| 37 | expect(markdown).toContain("# TypeScript Rule"); |
| 38 | expect(markdown).toContain("Use interfaces for object shapes"); |
| 39 | }); |
| 40 | |
| 41 | it("should create a filename based on sanitized rule name", async () => { |
| 42 | const args = { |
| 43 | name: "Special Ch@racters & Spaces", |
| 44 | rule: "Handle special characters", |
| 45 | }; |
| 46 | |
| 47 | await createRuleBlockImpl(args, mockExtras as any); |
| 48 | |
| 49 | const fileUri = mockIde.writeFile.mock.calls[0][0]; |
| 50 | expect(fileUri).toContain("special-chracters-spaces.md"); |
| 51 | }); |
| 52 | |
| 53 | it("should create a rule with description pattern", async () => { |
| 54 | const args = { |
| 55 | name: "Description Test", |
| 56 | rule: "This is the rule content", |
| 57 | description: "This is a detailed explanation of the rule", |
| 58 | }; |
| 59 | |
| 60 | await createRuleBlockImpl(args, mockExtras as any); |
| 61 | |
| 62 | const fileContent = mockIde.writeFile.mock.calls[0][1]; |
| 63 | |
| 64 | const { frontmatter, markdown } = parseMarkdownRule(fileContent); |
| 65 | |
| 66 | expect(frontmatter).toEqual({ |
| 67 | description: "This is a detailed explanation of the rule", |
| 68 | }); |
| 69 | |
| 70 | expect(markdown).toContain("# Description Test"); |
| 71 | expect(markdown).toContain("This is the rule content"); |
| 72 | }); |
| 73 | |
| 74 | it("should include both globs and description in frontmatter", async () => { |
| 75 | const args = { |
| 76 | name: "Complete Rule", |
| 77 | rule: "Follow this standard", |
| 78 | description: "This rule enforces our team standards", |
| 79 | globs: "**/*.js", |
| 80 | }; |
| 81 | |
| 82 | await createRuleBlockImpl(args, mockExtras as any); |
| 83 | |
| 84 | const fileContent = mockIde.writeFile.mock.calls[0][1]; |
| 85 | |
| 86 | const { frontmatter, markdown } = parseMarkdownRule(fileContent); |
| 87 | |
| 88 | expect(frontmatter).toEqual({ |
| 89 | description: "This rule enforces our team standards", |
| 90 | globs: "**/*.js", |
| 91 | }); |
| 92 | |
| 93 | expect(markdown).toContain("# Complete Rule"); |
| 94 | expect(markdown).toContain("Follow this standard"); |
| 95 | }); |
| 96 | |
| 97 | it("should create a rule with alwaysApply set to false", async () => { |
| 98 | const args = { |
| 99 | name: "Conditional Rule", |
| 100 | rule: "This rule should not always be applied", |
| 101 | alwaysApply: false, |
| 102 | }; |
| 103 | |
| 104 | await createRuleBlockImpl(args, mockExtras as any); |
| 105 | |
| 106 | const fileContent = mockIde.writeFile.mock.calls[0][1]; |
| 107 | |
| 108 | const { frontmatter } = parseMarkdownRule(fileContent); |
| 109 | |
| 110 | expect(frontmatter).toEqual({ |
| 111 | alwaysApply: false, |
| 112 | }); |
| 113 | }); |
| 114 | }); |