Elffuss BraidHub

kikocis/continue public

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