Elffuss BraidHub

kikocis/continue public

627d7d0d1f85 · 5.2 KiBRawPermalink
1import { markdownToRule } from "@continuedev/config-yaml";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3import { IDE } from "../..";
4import { walkDirs } from "../../indexing/walkDir";
5import { loadCodebaseRules } from "./loadCodebaseRules";
6
7// Mock dependencies
8vi.mock("../../indexing/walkDir", () => ({
9 walkDirs: vi.fn(),
10}));
11
12vi.mock("@continuedev/config-yaml", () => ({
13 markdownToRule: vi.fn(),
14}));
15
16describe("loadCodebaseRules", () => {
17 // Mock IDE with properly typed mock functions
18 const mockIde = {
19 readFile: vi.fn() as unknown as IDE["readFile"] & {
20 mockImplementation: Function;
21 },
22 } as unknown as IDE;
23
24 // Setup test files
25 const mockFiles = [
26 "src/rules.md",
27 "src/redux/rules.md",
28 "src/components/rules.md",
29 "src/utils/helper.ts", // Non-rules file
30 ".continue/rules.md", // This should also be loaded
31 ];
32
33 // Mock rule content
34 const mockRuleContent: Record<string, string> = {
35 "src/rules.md": "# General Rules\nFollow coding standards",
36 "src/redux/rules.md":
37 '---\nglobs: "**/*.{ts,tsx}"\n---\n# Redux Rules\nUse Redux Toolkit',
38 "src/components/rules.md":
39 '---\nglobs: ["**/*.tsx", "**/*.jsx"]\n---\n# Component Rules\nUse functional components',
40 ".continue/rules.md": "# Global Rules\nFollow project guidelines",
41 };
42
43 // Mock converted rules
44 const mockConvertedRules: Record<string, any> = {
45 "src/rules.md": {
46 name: "General Rules",
47 rule: "Follow coding standards",
48 source: "colocated-markdown",
49 ruleFile: "src/rules.md",
50 },
51 "src/redux/rules.md": {
52 name: "Redux Rules",
53 rule: "Use Redux Toolkit",
54 globs: "**/*.{ts,tsx}",
55 source: "colocated-markdown",
56 ruleFile: "src/redux/rules.md",
57 },
58 "src/components/rules.md": {
59 name: "Component Rules",
60 rule: "Use functional components",
61 globs: ["**/*.tsx", "**/*.jsx"],
62 source: "colocated-markdown",
63 ruleFile: "src/components/rules.md",
64 },
65 ".continue/rules.md": {
66 name: "Global Rules",
67 rule: "Follow project guidelines",
68 source: "colocated-markdown",
69 ruleFile: ".continue/rules.md",
70 },
71 };
72
73 beforeEach(() => {
74 // Setup mocks
75 vi.resetAllMocks();
76
77 // Mock walkDirs to return our test files
78 (walkDirs as any).mockResolvedValue(mockFiles);
79
80 // Mock readFile to return content based on path
81 (mockIde.readFile as any).mockImplementation((path: string) => {
82 return Promise.resolve(mockRuleContent[path] || "");
83 });
84
85 // Mock markdownToRule to return converted rules
86 (markdownToRule as any).mockImplementation(
87 (content: string, options: any) => {
88 return mockConvertedRules[options.filePath];
89 },
90 );
91 });
92
93 afterEach(() => {
94 vi.resetAllMocks();
95 });
96 it("should load rules from all rules.md files in the workspace", async () => {
97 const { rules, errors } = await loadCodebaseRules(mockIde);
98
99 // Should find all rules.md files
100 expect(walkDirs).toHaveBeenCalledWith(mockIde);
101
102 // Should read all rules.md files
103 expect(mockIde.readFile).toHaveBeenCalledTimes(4);
104 expect(mockIde.readFile).toHaveBeenCalledWith("src/rules.md");
105 expect(mockIde.readFile).toHaveBeenCalledWith("src/redux/rules.md");
106 expect(mockIde.readFile).toHaveBeenCalledWith("src/components/rules.md");
107 expect(mockIde.readFile).toHaveBeenCalledWith(".continue/rules.md");
108
109 // Should convert all rules
110 expect(markdownToRule).toHaveBeenCalledTimes(4);
111
112 // Should return all rules
113 expect(rules).toHaveLength(4);
114 expect(rules).toContainEqual(mockConvertedRules["src/rules.md"]);
115 expect(rules).toContainEqual(mockConvertedRules["src/redux/rules.md"]);
116 expect(rules).toContainEqual(mockConvertedRules["src/components/rules.md"]);
117 expect(rules).toContainEqual(mockConvertedRules[".continue/rules.md"]);
118
119 // Should not have errors
120 expect(errors).toHaveLength(0);
121 });
122
123 it("should handle errors when reading a rule file", async () => {
124 // Setup mock to throw for a specific file
125 (mockIde.readFile as any).mockImplementation((path: string) => {
126 if (path === "src/redux/rules.md") {
127 return Promise.reject(new Error("Failed to read file"));
128 }
129 return Promise.resolve(mockRuleContent[path] || "");
130 });
131
132 const { rules, errors } = await loadCodebaseRules(mockIde);
133
134 // Should still return other rules
135 expect(rules).toHaveLength(3);
136 expect(rules).toContainEqual(mockConvertedRules["src/rules.md"]);
137 expect(rules).toContainEqual(mockConvertedRules["src/components/rules.md"]);
138 expect(rules).toContainEqual(mockConvertedRules[".continue/rules.md"]);
139
140 // Should have one error
141 expect(errors).toHaveLength(1);
142 expect(errors[0].message).toContain(
143 "Failed to parse colocated rule file src/redux/rules.md",
144 );
145 });
146
147 it("should handle errors when walkDirs fails", async () => {
148 // Setup mock to throw
149 (walkDirs as any).mockRejectedValue(
150 new Error("Failed to walk directories"),
151 );
152 const { rules, errors } = await loadCodebaseRules(mockIde);
153
154 // Should return no rules
155 expect(rules).toHaveLength(0);
156
157 // Should have one error
158 expect(errors).toHaveLength(1);
159 expect(errors[0].message).toContain("Error loading colocated rule files");
160 });
161});