@@ -2,7 +2,6 @@ import { markdownToRule } from "@continuedev/config-yaml";
22import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
33import { IDE } from "../.." ;
44import { walkDirs } from "../../indexing/walkDir" ;
5- import { findUriInDirs , getUriPathBasename } from "../../util/uri" ;
65import { loadCodebaseRules } from "./loadCodebaseRules" ;
76
87// Mock dependencies
@@ -14,14 +13,10 @@ vi.mock("@continuedev/config-yaml", () => ({
1413 markdownToRule : vi . fn ( ) ,
1514} ) ) ;
1615
17- vi . mock ( "../../util/uri" , ( ) => ( {
18- findUriInDirs : vi . fn ( ) ,
19- getUriPathBasename : vi . fn ( ) ,
20- } ) ) ;
21-
2216describe ( "loadCodebaseRules" , ( ) => {
2317 // Mock IDE with properly typed mock functions
2418 const mockIde = {
19+ fileExists : vi . fn ( ) . mockImplementation ( ( ) => true ) ,
2520 readFile : vi . fn ( ) as unknown as IDE [ "readFile" ] & {
2621 mockImplementation : Function ;
2722 } ,
@@ -30,52 +25,45 @@ describe("loadCodebaseRules", () => {
3025 } ,
3126 } as unknown as IDE ;
3227
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-
4228 // Mock rule content
4329 const mockRuleContent : Record < string , string > = {
44- "src/rules.md" : "# General Rules\nFollow coding standards" ,
45- "src/redux/rules.md" :
30+ "file:///workspace/src/rules.md" :
31+ "# General Rules\nFollow coding standards" ,
32+ "file:///workspace/src/redux/rules.md" :
4633 '---\nglobs: "**/*.{ts,tsx}"\n---\n# Redux Rules\nUse Redux Toolkit' ,
47- "src/components/rules.md" :
34+ "file:///workspace/ src/components/rules.md" :
4835 '---\nglobs: ["**/*.tsx", "**/*.jsx"]\n---\n# Component Rules\nUse functional components' ,
49- ".continue/rules.md" : "# Global Rules\nFollow project guidelines" ,
36+ "file:///workspace/.continue/rules.md" :
37+ "# Global Rules\nFollow project guidelines" ,
5038 } ;
5139
5240 // Mock converted rules
5341 const mockConvertedRules : Record < string , any > = {
54- "src/rules.md" : {
42+ "file:///workspace/ src/rules.md" : {
5543 name : "General Rules" ,
5644 rule : "Follow coding standards" ,
5745 source : "colocated-markdown" ,
58- ruleFile : "src/rules.md" ,
46+ sourceFile : "file:///workspace/ src/rules.md" ,
5947 } ,
60- "src/redux/rules.md" : {
48+ "file:///workspace/ src/redux/rules.md" : {
6149 name : "Redux Rules" ,
6250 rule : "Use Redux Toolkit" ,
6351 globs : "**/*.{ts,tsx}" ,
6452 source : "colocated-markdown" ,
65- ruleFile : "src/redux/rules.md" ,
53+ sourceFile : "file:///workspace/ src/redux/rules.md" ,
6654 } ,
67- "src/components/rules.md" : {
55+ "file:///workspace/ src/components/rules.md" : {
6856 name : "Component Rules" ,
6957 rule : "Use functional components" ,
7058 globs : [ "**/*.tsx" , "**/*.jsx" ] ,
7159 source : "colocated-markdown" ,
72- ruleFile : "src/components/rules.md" ,
60+ sourceFile : "file:///workspace/ src/components/rules.md" ,
7361 } ,
74- ".continue/rules.md" : {
62+ "file:///workspace/ .continue/rules.md" : {
7563 name : "Global Rules" ,
7664 rule : "Follow project guidelines" ,
7765 source : "colocated-markdown" ,
78- ruleFile : ".continue/rules.md" ,
66+ sourceFile : "file:///workspace/ .continue/rules.md" ,
7967 } ,
8068 } ;
8169
@@ -84,20 +72,13 @@ describe("loadCodebaseRules", () => {
8472 vi . resetAllMocks ( ) ;
8573
8674 // Mock walkDirs to return our test files
87- ( walkDirs as any ) . mockResolvedValue ( mockFiles ) ;
75+ ( walkDirs as any ) . mockResolvedValue ( [
76+ ...Object . keys ( mockRuleContent ) ,
77+ "file:///workspace/src/utils/helper.ts" , // Non-rules file
78+ ] ) ;
8879
8980 // 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- } ) ;
81+ ( mockIde . getWorkspaceDirs as any ) . mockResolvedValue ( [ "file:///workspace" ] ) ;
10182
10283 // Mock readFile to return content based on path
10384 ( mockIde . readFile as any ) . mockImplementation ( ( path : string ) => {
@@ -123,20 +104,36 @@ describe("loadCodebaseRules", () => {
123104
124105 // Should read all rules.md files
125106 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" ) ;
107+ expect ( mockIde . readFile ) . toHaveBeenCalledWith (
108+ "file:///workspace/src/rules.md" ,
109+ ) ;
110+ expect ( mockIde . readFile ) . toHaveBeenCalledWith (
111+ "file:///workspace/src/redux/rules.md" ,
112+ ) ;
113+ expect ( mockIde . readFile ) . toHaveBeenCalledWith (
114+ "file:///workspace/src/components/rules.md" ,
115+ ) ;
116+ expect ( mockIde . readFile ) . toHaveBeenCalledWith (
117+ "file:///workspace/.continue/rules.md" ,
118+ ) ;
130119
131120 // Should convert all rules
132121 expect ( markdownToRule ) . toHaveBeenCalledTimes ( 4 ) ;
133122
134123 // Should return all rules
135124 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" ] ) ;
125+ expect ( rules ) . toContainEqual (
126+ mockConvertedRules [ "file:///workspace/src/rules.md" ] ,
127+ ) ;
128+ expect ( rules ) . toContainEqual (
129+ mockConvertedRules [ "file:///workspace/src/redux/rules.md" ] ,
130+ ) ;
131+ expect ( rules ) . toContainEqual (
132+ mockConvertedRules [ "file:///workspace/src/components/rules.md" ] ,
133+ ) ;
134+ expect ( rules ) . toContainEqual (
135+ mockConvertedRules [ "file:///workspace/.continue/rules.md" ] ,
136+ ) ;
140137
141138 // Should not have errors
142139 expect ( errors ) . toHaveLength ( 0 ) ;
@@ -145,7 +142,7 @@ describe("loadCodebaseRules", () => {
145142 it ( "should handle errors when reading a rule file" , async ( ) => {
146143 // Setup mock to throw for a specific file
147144 ( mockIde . readFile as any ) . mockImplementation ( ( path : string ) => {
148- if ( path === "src/redux/rules.md" ) {
145+ if ( path === "file:///workspace/ src/redux/rules.md" ) {
149146 return Promise . reject ( new Error ( "Failed to read file" ) ) ;
150147 }
151148 return Promise . resolve ( mockRuleContent [ path ] || "" ) ;
@@ -155,14 +152,20 @@ describe("loadCodebaseRules", () => {
155152
156153 // Should still return other rules
157154 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" ] ) ;
155+ expect ( rules ) . toContainEqual (
156+ mockConvertedRules [ "file:///workspace/src/rules.md" ] ,
157+ ) ;
158+ expect ( rules ) . toContainEqual (
159+ mockConvertedRules [ "file:///workspace/src/components/rules.md" ] ,
160+ ) ;
161+ expect ( rules ) . toContainEqual (
162+ mockConvertedRules [ "file:///workspace/.continue/rules.md" ] ,
163+ ) ;
161164
162165 // Should have one error
163166 expect ( errors ) . toHaveLength ( 1 ) ;
164167 expect ( errors [ 0 ] . message ) . toContain (
165- "Failed to parse colocated rule file src/redux/rules.md" ,
168+ "Failed to parse colocated rule file file:///workspace/ src/redux/rules.md: Failed to read file " ,
166169 ) ;
167170 } ) ;
168171
0 commit comments