Skip to content

Commit 77d2c4f

Browse files
committed
Fix Windows path normalization in readFileTool test
- Add normalizePath helper to extract relative paths from absolute Windows paths - Use normalizePath consistently across all path resolution and file operation mocks - This ensures Windows absolute paths like D:\git\...\test\valid.txt are correctly normalized to test/valid.txt for comparison with expected relative paths - Fixes validation order test that was failing on Windows but passing on Linux
1 parent 3620246 commit 77d2c4f

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/core/tools/__tests__/readFileTool.spec.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,14 @@ describe("read_file tool XML output structure", () => {
10201020
})
10211021

10221022
it("should handle errors in multiple file entries independently", async () => {
1023+
// Helper function to normalize paths for cross-platform compatibility
1024+
const normalizePath = (filePath: string): string => {
1025+
const normalized = filePath.replace(/\\/g, "/")
1026+
// Extract the relative path part (e.g., "test/valid.txt" from any absolute path)
1027+
const match = normalized.match(/test\/(valid|invalid)\.txt$/)
1028+
return match ? `test/${match[1]}.txt` : normalized
1029+
}
1030+
10231031
// Setup
10241032
const validPath = "test/valid.txt"
10251033
const invalidPath = "test/invalid.txt"
@@ -1030,8 +1038,9 @@ describe("read_file tool XML output structure", () => {
10301038
const normalizedInvalidPath = "/test/invalid.txt"
10311039

10321040
mockedPathResolve.mockImplementation((_: string, filePath: string) => {
1033-
if (filePath === validPath) return normalizedValidPath
1034-
if (filePath === invalidPath) return normalizedInvalidPath
1041+
const normalizedInput = normalizePath(filePath)
1042+
if (normalizedInput === validPath) return normalizedValidPath
1043+
if (normalizedInput === invalidPath) return normalizedInvalidPath
10351044
return filePath
10361045
})
10371046

@@ -1059,28 +1068,26 @@ describe("read_file tool XML output structure", () => {
10591068

10601069
// Mock file operations to track operation order
10611070
mockedCountFileLines.mockImplementation((filePath: string) => {
1062-
const normalizedPath = filePath.replace(/\\/g, "/")
1063-
const relPath = normalizedPath === normalizedValidPath ? validPath : invalidPath
1064-
validationOrder.push(`countLines:${relPath}`)
1065-
if (normalizedPath === normalizedValidPath || normalizedPath.endsWith(validPath)) {
1071+
const normalizedInput = normalizePath(filePath)
1072+
validationOrder.push(`countLines:${normalizedInput}`)
1073+
if (normalizedInput === validPath) {
10661074
return Promise.resolve(1)
10671075
}
10681076
throw new Error("File not found")
10691077
})
10701078

10711079
mockedIsBinaryFile.mockImplementation((filePath: string) => {
1072-
const normalizedPath = filePath.replace(/\\/g, "/")
1073-
const relPath = normalizedPath === normalizedValidPath ? validPath : invalidPath
1074-
validationOrder.push(`isBinary:${relPath}`)
1075-
if (normalizedPath === normalizedValidPath || normalizedPath.endsWith(validPath)) {
1080+
const normalizedInput = normalizePath(filePath)
1081+
validationOrder.push(`isBinary:${normalizedInput}`)
1082+
if (normalizedInput === validPath) {
10761083
return Promise.resolve(false)
10771084
}
10781085
throw new Error("File not found")
10791086
})
10801087

10811088
mockedExtractTextFromFile.mockImplementation((filePath: string) => {
1082-
const normalizedPath = filePath.replace(/\\/g, "/")
1083-
if (normalizedPath === normalizedValidPath || normalizedPath.endsWith(validPath)) {
1089+
const normalizedInput = normalizePath(filePath)
1090+
if (normalizedInput === validPath) {
10841091
validationOrder.push(`extract:${validPath}`)
10851092
return Promise.resolve(numberedContent)
10861093
}

0 commit comments

Comments
 (0)