Elffuss BraidHub

kikocis/continue public

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