Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/core/prompts/__tests__/custom-system-prompt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ vi.mock("../../../utils/fs", () => ({
createDirectoriesForFile: vi.fn().mockResolvedValue([]),
}))

// Import path module to add toPosix to String prototype
import "../../../utils/path"
import { SYSTEM_PROMPT } from "../system"
import { defaultModeSlug, modes } from "../../../shared/modes"
import * as vscode from "vscode"
import * as fs from "fs/promises"
import { toPosix } from "./utils"

// Get the mocked fs module
const mockedFs = vi.mocked(fs)
Expand Down Expand Up @@ -121,7 +122,11 @@ describe("File-Based Custom System Prompt", () => {
const fileCustomSystemPrompt = "Custom system prompt from file"
// When called with utf-8 encoding, return a string
mockedFs.readFile.mockImplementation((filePath, options) => {
if (toPosix(filePath).includes(`.roo/system-prompt-${defaultModeSlug}`) && options === "utf-8") {
// Convert filePath to string and normalize path separators
const normalizedPath =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we extract this duplicated normalization logic into a helper function within the test file? I see the same code on lines 168-169. Something like:

const normalizeFilePath = (filePath: string | Buffer): string => {
  return typeof filePath === "string" 
    ? filePath.replace(/\/g, "/") 
    : filePath.toString('utf-8').replace(/\/g, "/")
}

Also, should we specify the encoding when calling toString() on a Buffer for better type safety?

typeof filePath === "string" ? filePath.replace(/\\/g, "/") : filePath.toString().replace(/\\/g, "/")

if (normalizedPath.includes(`.roo/system-prompt-${defaultModeSlug}`) && options === "utf-8") {
return Promise.resolve(fileCustomSystemPrompt)
}
return Promise.reject({ code: "ENOENT" })
Expand Down Expand Up @@ -159,7 +164,11 @@ describe("File-Based Custom System Prompt", () => {
// Mock the readFile to return content from a file
const fileCustomSystemPrompt = "Custom system prompt from file"
mockedFs.readFile.mockImplementation((filePath, options) => {
if (toPosix(filePath).includes(`.roo/system-prompt-${defaultModeSlug}`) && options === "utf-8") {
// Convert filePath to string and normalize path separators
const normalizedPath =
typeof filePath === "string" ? filePath.replace(/\\/g, "/") : filePath.toString().replace(/\\/g, "/")

if (normalizedPath.includes(`.roo/system-prompt-${defaultModeSlug}`) && options === "utf-8") {
return Promise.resolve(fileCustomSystemPrompt)
}
return Promise.reject({ code: "ENOENT" })
Expand Down
7 changes: 4 additions & 3 deletions src/core/prompts/__tests__/responses-rooignore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import type { Mock } from "vitest"

// Import path module to add toPosix to String prototype
import "../../../utils/path"
import { formatResponse } from "../responses"
import { RooIgnoreController, LOCK_TEXT_SYMBOL } from "../../ignore/RooIgnoreController"
import { fileExistsAtPath } from "../../../utils/fs"
import * as fs from "fs/promises"
import { toPosix } from "./utils"

// Mock dependencies
vi.mock("../../../utils/fs")
Expand Down Expand Up @@ -86,7 +87,7 @@ describe("RooIgnore Response Formatting", () => {
return (
!filePath.includes("node_modules") &&
!filePath.includes(".git") &&
!toPosix(filePath).includes("secrets/")
!filePath.toString().toPosix().includes("secrets/")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional? I notice we're still using filePath.toString().toPosix() here, but in custom-system-prompt.spec.ts we replaced all toPosix() calls with inline normalization. This inconsistency might cause the same Windows test failures in this file. Should we apply the same inline normalization approach here?

Suggested change
!filePath.toString().toPosix().includes("secrets/")
const normalizedPath = typeof filePath === "string" ? filePath.replace(/\/g, "/") : filePath.toString().replace(/\/g, "/")
return (
!filePath.includes("node_modules") &&
!filePath.includes(".git") &&
!normalizedPath.includes("secrets/")
)

)
})

Expand Down Expand Up @@ -130,7 +131,7 @@ describe("RooIgnore Response Formatting", () => {
return (
!filePath.includes("node_modules") &&
!filePath.includes(".git") &&
!toPosix(filePath).includes("secrets/")
!filePath.toString().toPosix().includes("secrets/")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here - we're still using filePath.toString().toPosix() which could fail on Windows. For consistency with the fix in custom-system-prompt.spec.ts, should we use inline normalization here too?

)
})

Expand Down
9 changes: 2 additions & 7 deletions src/core/prompts/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
import * as fs from "fs/promises"
import { PathLike } from "fs"

// Make a path take a unix-like form. Useful for making path comparisons.
export function toPosix(filePath: PathLike | fs.FileHandle) {
return filePath.toString().toPosix()
}
// This file is no longer needed as we're using String.prototype.toPosix() directly
// from src/utils/path module which is imported in the test files
Loading