fix: load codebase rules tests: use URIs
This commit is contained in:
parent
47f0fb2e4a
commit
9f92dbdc8d
1 changed files with 55 additions and 52 deletions
|
|
@ -2,7 +2,6 @@ import { markdownToRule } from "@continuedev/config-yaml";
|
|||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { IDE } from "../..";
|
||||
import { walkDirs } from "../../indexing/walkDir";
|
||||
import { findUriInDirs, getUriPathBasename } from "../../util/uri";
|
||||
import { loadCodebaseRules } from "./loadCodebaseRules";
|
||||
|
||||
// Mock dependencies
|
||||
|
|
@ -14,14 +13,10 @@ vi.mock("@continuedev/config-yaml", () => ({
|
|||
markdownToRule: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../../util/uri", () => ({
|
||||
findUriInDirs: vi.fn(),
|
||||
getUriPathBasename: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("loadCodebaseRules", () => {
|
||||
// Mock IDE with properly typed mock functions
|
||||
const mockIde = {
|
||||
fileExists: vi.fn().mockImplementation(() => true),
|
||||
readFile: vi.fn() as unknown as IDE["readFile"] & {
|
||||
mockImplementation: Function;
|
||||
},
|
||||
|
|
@ -30,52 +25,45 @@ describe("loadCodebaseRules", () => {
|
|||
},
|
||||
} as unknown as IDE;
|
||||
|
||||
// Setup test files
|
||||
const mockFiles = [
|
||||
"src/rules.md",
|
||||
"src/redux/rules.md",
|
||||
"src/components/rules.md",
|
||||
"src/utils/helper.ts", // Non-rules file
|
||||
".continue/rules.md", // This should also be loaded
|
||||
];
|
||||
|
||||
// Mock rule content
|
||||
const mockRuleContent: Record<string, string> = {
|
||||
"src/rules.md": "# General Rules\nFollow coding standards",
|
||||
"src/redux/rules.md":
|
||||
"file:///workspace/src/rules.md":
|
||||
"# General Rules\nFollow coding standards",
|
||||
"file:///workspace/src/redux/rules.md":
|
||||
'---\nglobs: "**/*.{ts,tsx}"\n---\n# Redux Rules\nUse Redux Toolkit',
|
||||
"src/components/rules.md":
|
||||
"file:///workspace/src/components/rules.md":
|
||||
'---\nglobs: ["**/*.tsx", "**/*.jsx"]\n---\n# Component Rules\nUse functional components',
|
||||
".continue/rules.md": "# Global Rules\nFollow project guidelines",
|
||||
"file:///workspace/.continue/rules.md":
|
||||
"# Global Rules\nFollow project guidelines",
|
||||
};
|
||||
|
||||
// Mock converted rules
|
||||
const mockConvertedRules: Record<string, any> = {
|
||||
"src/rules.md": {
|
||||
"file:///workspace/src/rules.md": {
|
||||
name: "General Rules",
|
||||
rule: "Follow coding standards",
|
||||
source: "colocated-markdown",
|
||||
sourceFile: "src/rules.md",
|
||||
sourceFile: "file:///workspace/src/rules.md",
|
||||
},
|
||||
"src/redux/rules.md": {
|
||||
"file:///workspace/src/redux/rules.md": {
|
||||
name: "Redux Rules",
|
||||
rule: "Use Redux Toolkit",
|
||||
globs: "**/*.{ts,tsx}",
|
||||
source: "colocated-markdown",
|
||||
sourceFile: "src/redux/rules.md",
|
||||
sourceFile: "file:///workspace/src/redux/rules.md",
|
||||
},
|
||||
"src/components/rules.md": {
|
||||
"file:///workspace/src/components/rules.md": {
|
||||
name: "Component Rules",
|
||||
rule: "Use functional components",
|
||||
globs: ["**/*.tsx", "**/*.jsx"],
|
||||
source: "colocated-markdown",
|
||||
sourceFile: "src/components/rules.md",
|
||||
sourceFile: "file:///workspace/src/components/rules.md",
|
||||
},
|
||||
".continue/rules.md": {
|
||||
"file:///workspace/.continue/rules.md": {
|
||||
name: "Global Rules",
|
||||
rule: "Follow project guidelines",
|
||||
source: "colocated-markdown",
|
||||
sourceFile: ".continue/rules.md",
|
||||
sourceFile: "file:///workspace/.continue/rules.md",
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -84,20 +72,13 @@ describe("loadCodebaseRules", () => {
|
|||
vi.resetAllMocks();
|
||||
|
||||
// Mock walkDirs to return our test files
|
||||
(walkDirs as any).mockResolvedValue(mockFiles);
|
||||
(walkDirs as any).mockResolvedValue([
|
||||
...Object.keys(mockRuleContent),
|
||||
"file:///workspace/src/utils/helper.ts", // Non-rules file
|
||||
]);
|
||||
|
||||
// Mock getWorkspaceDirs to return a workspace directory
|
||||
(mockIde.getWorkspaceDirs as any).mockResolvedValue(["/workspace"]);
|
||||
|
||||
// Mock getUriPathBasename to return just the filename
|
||||
(getUriPathBasename as any).mockImplementation((path: string) => {
|
||||
return path.split("/").pop() || "";
|
||||
});
|
||||
|
||||
// Mock findUriInDirs to return relative path
|
||||
(findUriInDirs as any).mockImplementation((uri: string) => {
|
||||
return { relativePathOrBasename: uri };
|
||||
});
|
||||
(mockIde.getWorkspaceDirs as any).mockResolvedValue(["file:///workspace"]);
|
||||
|
||||
// Mock readFile to return content based on path
|
||||
(mockIde.readFile as any).mockImplementation((path: string) => {
|
||||
|
|
@ -123,20 +104,36 @@ describe("loadCodebaseRules", () => {
|
|||
|
||||
// Should read all rules.md files
|
||||
expect(mockIde.readFile).toHaveBeenCalledTimes(4);
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith("src/rules.md");
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith("src/redux/rules.md");
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith("src/components/rules.md");
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith(".continue/rules.md");
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith(
|
||||
"file:///workspace/src/rules.md",
|
||||
);
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith(
|
||||
"file:///workspace/src/redux/rules.md",
|
||||
);
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith(
|
||||
"file:///workspace/src/components/rules.md",
|
||||
);
|
||||
expect(mockIde.readFile).toHaveBeenCalledWith(
|
||||
"file:///workspace/.continue/rules.md",
|
||||
);
|
||||
|
||||
// Should convert all rules
|
||||
expect(markdownToRule).toHaveBeenCalledTimes(4);
|
||||
|
||||
// Should return all rules
|
||||
expect(rules).toHaveLength(4);
|
||||
expect(rules).toContainEqual(mockConvertedRules["src/rules.md"]);
|
||||
expect(rules).toContainEqual(mockConvertedRules["src/redux/rules.md"]);
|
||||
expect(rules).toContainEqual(mockConvertedRules["src/components/rules.md"]);
|
||||
expect(rules).toContainEqual(mockConvertedRules[".continue/rules.md"]);
|
||||
expect(rules).toContainEqual(
|
||||
mockConvertedRules["file:///workspace/src/rules.md"],
|
||||
);
|
||||
expect(rules).toContainEqual(
|
||||
mockConvertedRules["file:///workspace/src/redux/rules.md"],
|
||||
);
|
||||
expect(rules).toContainEqual(
|
||||
mockConvertedRules["file:///workspace/src/components/rules.md"],
|
||||
);
|
||||
expect(rules).toContainEqual(
|
||||
mockConvertedRules["file:///workspace/.continue/rules.md"],
|
||||
);
|
||||
|
||||
// Should not have errors
|
||||
expect(errors).toHaveLength(0);
|
||||
|
|
@ -145,7 +142,7 @@ describe("loadCodebaseRules", () => {
|
|||
it("should handle errors when reading a rule file", async () => {
|
||||
// Setup mock to throw for a specific file
|
||||
(mockIde.readFile as any).mockImplementation((path: string) => {
|
||||
if (path === "src/redux/rules.md") {
|
||||
if (path === "file:///workspace/src/redux/rules.md") {
|
||||
return Promise.reject(new Error("Failed to read file"));
|
||||
}
|
||||
return Promise.resolve(mockRuleContent[path] || "");
|
||||
|
|
@ -155,14 +152,20 @@ describe("loadCodebaseRules", () => {
|
|||
|
||||
// Should still return other rules
|
||||
expect(rules).toHaveLength(3);
|
||||
expect(rules).toContainEqual(mockConvertedRules["src/rules.md"]);
|
||||
expect(rules).toContainEqual(mockConvertedRules["src/components/rules.md"]);
|
||||
expect(rules).toContainEqual(mockConvertedRules[".continue/rules.md"]);
|
||||
expect(rules).toContainEqual(
|
||||
mockConvertedRules["file:///workspace/src/rules.md"],
|
||||
);
|
||||
expect(rules).toContainEqual(
|
||||
mockConvertedRules["file:///workspace/src/components/rules.md"],
|
||||
);
|
||||
expect(rules).toContainEqual(
|
||||
mockConvertedRules["file:///workspace/.continue/rules.md"],
|
||||
);
|
||||
|
||||
// Should have one error
|
||||
expect(errors).toHaveLength(1);
|
||||
expect(errors[0].message).toContain(
|
||||
"Failed to parse colocated rule file src/redux/rules.md",
|
||||
"Failed to parse colocated rule file file:///workspace/src/redux/rules.md: Failed to read file",
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue