|
| 1 | +// pnpm --filter roo-cline test utils/__tests__/fileLock.spec.ts |
| 2 | + |
| 3 | +import * as path from "path" |
| 4 | + |
| 5 | +import { acquireFileLock, withFileLock } from "../fileLock" |
| 6 | + |
| 7 | +const { lockMock } = vi.hoisted(() => ({ lockMock: vi.fn() })) |
| 8 | + |
| 9 | +vi.mock("proper-lockfile", () => ({ lock: lockMock, default: { lock: lockMock } })) |
| 10 | + |
| 11 | +interface CapturedLockOptions { |
| 12 | + onCompromised: (error: Error) => void |
| 13 | +} |
| 14 | + |
| 15 | +function requireCapturedLockOptions(): CapturedLockOptions { |
| 16 | + if (!capturedOptions) { |
| 17 | + throw new Error("proper-lockfile options were not captured") |
| 18 | + } |
| 19 | + return capturedOptions |
| 20 | +} |
| 21 | + |
| 22 | +let capturedOptions: CapturedLockOptions | undefined |
| 23 | + |
| 24 | +describe("fileLock", () => { |
| 25 | + const absoluteFilePath = path.resolve("/virtual/dir/target.json") |
| 26 | + let underlyingRelease: ReturnType<typeof vi.fn> |
| 27 | + let consoleError: ReturnType<typeof vi.spyOn> |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + consoleError = vi.spyOn(console, "error").mockImplementation(() => {}) |
| 31 | + underlyingRelease = vi.fn().mockResolvedValue(undefined) |
| 32 | + capturedOptions = undefined |
| 33 | + lockMock.mockImplementation(async (_filePath: string, options: CapturedLockOptions) => { |
| 34 | + capturedOptions = options |
| 35 | + return underlyingRelease |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + consoleError.mockRestore() |
| 41 | + vi.clearAllMocks() |
| 42 | + }) |
| 43 | + |
| 44 | + it("records the compromise without throwing from the renewal callback", () => { |
| 45 | + const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" }) |
| 46 | + |
| 47 | + return acquireFileLock(absoluteFilePath).then((release) => { |
| 48 | + expect(() => requireCapturedLockOptions().onCompromised(compromiseError)).not.toThrow() |
| 49 | + return release().catch(() => {}) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it("rejects the owning operation with the recorded compromise error", async () => { |
| 54 | + const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" }) |
| 55 | + const operation = vi.fn(async () => { |
| 56 | + // Simulate the proper-lockfile renewal timer reporting a |
| 57 | + // compromise while the operation still holds the lock. |
| 58 | + requireCapturedLockOptions().onCompromised(compromiseError) |
| 59 | + return "done" |
| 60 | + }) |
| 61 | + |
| 62 | + await expect(withFileLock(absoluteFilePath, operation)).rejects.toBe(compromiseError) |
| 63 | + expect(operation).toHaveBeenCalledTimes(1) |
| 64 | + // proper-lockfile already marked the lock released, so the wrapper |
| 65 | + // must not call the underlying release after a compromise. |
| 66 | + expect(underlyingRelease).not.toHaveBeenCalled() |
| 67 | + }) |
| 68 | + |
| 69 | + it("keeps the operation error when the operation fails after a compromise", async () => { |
| 70 | + const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" }) |
| 71 | + const operationError = new Error("operation failed") |
| 72 | + const operation = vi.fn(async () => { |
| 73 | + requireCapturedLockOptions().onCompromised(compromiseError) |
| 74 | + throw operationError |
| 75 | + }) |
| 76 | + |
| 77 | + await expect(withFileLock(absoluteFilePath, operation)).rejects.toBe(operationError) |
| 78 | + }) |
| 79 | + |
| 80 | + it("rejects a direct release with the recorded compromise error", async () => { |
| 81 | + const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" }) |
| 82 | + const release = await acquireFileLock(absoluteFilePath) |
| 83 | + |
| 84 | + requireCapturedLockOptions().onCompromised(compromiseError) |
| 85 | + |
| 86 | + await expect(release()).rejects.toBe(compromiseError) |
| 87 | + expect(underlyingRelease).not.toHaveBeenCalled() |
| 88 | + }) |
| 89 | + |
| 90 | + it("releases normally without a compromise", async () => { |
| 91 | + const operation = vi.fn(async () => "done") |
| 92 | + |
| 93 | + await expect(withFileLock(absoluteFilePath, operation)).resolves.toBe("done") |
| 94 | + expect(underlyingRelease).toHaveBeenCalledTimes(1) |
| 95 | + }) |
| 96 | + |
| 97 | + it("keeps reporting success when an unrelated release error occurs", async () => { |
| 98 | + underlyingRelease.mockRejectedValue(new Error("unlock failed")) |
| 99 | + |
| 100 | + await expect( |
| 101 | + withFileLock( |
| 102 | + absoluteFilePath, |
| 103 | + vi.fn(async () => "done"), |
| 104 | + ), |
| 105 | + ).resolves.toBe("done") |
| 106 | + expect(consoleError).toHaveBeenCalled() |
| 107 | + }) |
| 108 | +}) |
0 commit comments