diff --git a/src/services/code-index/__tests__/cache-manager.spec.ts b/src/services/code-index/__tests__/cache-manager.spec.ts index 27408fdf33..e61a92f3cc 100644 --- a/src/services/code-index/__tests__/cache-manager.spec.ts +++ b/src/services/code-index/__tests__/cache-manager.spec.ts @@ -4,6 +4,14 @@ import { createHash } from "crypto" import debounce from "lodash.debounce" import { CacheManager } from "../cache-manager" +// Mock safeWriteJson utility +vitest.mock("../../../utils/safeWriteJson", () => ({ + safeWriteJson: vitest.fn().mockResolvedValue(undefined), +})) + +// Import the mocked version +import { safeWriteJson } from "../../../utils/safeWriteJson" + // Mock vscode vitest.mock("vscode", () => ({ Uri: { @@ -89,7 +97,7 @@ describe("CacheManager", () => { cacheManager.updateHash(filePath, hash) expect(cacheManager.getHash(filePath)).toBe(hash) - expect(vscode.workspace.fs.writeFile).toHaveBeenCalled() + expect(safeWriteJson).toHaveBeenCalled() }) it("should delete hash and trigger save", () => { @@ -100,7 +108,7 @@ describe("CacheManager", () => { cacheManager.deleteHash(filePath) expect(cacheManager.getHash(filePath)).toBeUndefined() - expect(vscode.workspace.fs.writeFile).toHaveBeenCalled() + expect(safeWriteJson).toHaveBeenCalled() }) it("should return shallow copy of hashes", () => { @@ -125,18 +133,16 @@ describe("CacheManager", () => { cacheManager.updateHash(filePath, hash) - expect(vscode.workspace.fs.writeFile).toHaveBeenCalledWith(mockCachePath, expect.any(Uint8Array)) + expect(safeWriteJson).toHaveBeenCalledWith(mockCachePath.fsPath, expect.any(Object)) // Verify the saved data - const savedData = JSON.parse( - Buffer.from((vscode.workspace.fs.writeFile as Mock).mock.calls[0][1]).toString(), - ) + const savedData = (safeWriteJson as Mock).mock.calls[0][1] expect(savedData).toEqual({ [filePath]: hash }) }) it("should handle save errors gracefully", async () => { const consoleErrorSpy = vitest.spyOn(console, "error").mockImplementation(() => {}) - ;(vscode.workspace.fs.writeFile as Mock).mockRejectedValue(new Error("Save failed")) + ;(safeWriteJson as Mock).mockRejectedValue(new Error("Save failed")) cacheManager.updateHash("test.ts", "hash") @@ -153,19 +159,19 @@ describe("CacheManager", () => { it("should clear cache file and reset state", async () => { cacheManager.updateHash("test.ts", "hash") - // Reset the mock to ensure writeFile succeeds for clearCacheFile - ;(vscode.workspace.fs.writeFile as Mock).mockClear() - ;(vscode.workspace.fs.writeFile as Mock).mockResolvedValue(undefined) + // Reset the mock to ensure safeWriteJson succeeds for clearCacheFile + ;(safeWriteJson as Mock).mockClear() + ;(safeWriteJson as Mock).mockResolvedValue(undefined) await cacheManager.clearCacheFile() - expect(vscode.workspace.fs.writeFile).toHaveBeenCalledWith(mockCachePath, Buffer.from("{}")) + expect(safeWriteJson).toHaveBeenCalledWith(mockCachePath.fsPath, {}) expect(cacheManager.getAllHashes()).toEqual({}) }) it("should handle clear errors gracefully", async () => { const consoleErrorSpy = vitest.spyOn(console, "error").mockImplementation(() => {}) - ;(vscode.workspace.fs.writeFile as Mock).mockRejectedValue(new Error("Save failed")) + ;(safeWriteJson as Mock).mockRejectedValue(new Error("Save failed")) await cacheManager.clearCacheFile()