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