test: update cache-manager tests to use safeWriteJson

Updated the cache-manager tests to properly mock the safeWriteJson utility
instead of expecting vscode.workspace.fs.writeFile calls. This fixes test
failures that occurred after the implementation was changed to use safeWriteJson.

The changes include:
- Adding proper mocking for safeWriteJson
- Updating all test expectations to check for safeWriteJson calls
- Changing how test data is verified to match the new implementation

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>
This commit is contained in:
Eric Wheeler 2025-06-15 17:56:47 -07:00 committed by Daniel Riccio
parent d5f8c5fd5e
commit 4c03145cf9

View file

@ -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()