|
| 1 | +import * as vscode from "vscode" |
| 2 | + |
| 3 | +vi.mock("fs/promises", async () => { |
| 4 | + const mod = await import("../../__mocks__/fs/promises") |
| 5 | + return (mod as any).default ?? mod |
| 6 | +}) |
| 7 | + |
| 8 | +describe("getStorageBasePath - customStoragePath", () => { |
| 9 | + const defaultPath = "/test/global-storage" |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + vi.clearAllMocks() |
| 13 | + }) |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + vi.restoreAllMocks() |
| 17 | + }) |
| 18 | + |
| 19 | + it("returns the configured custom path when it is writable", async () => { |
| 20 | + const customPath = "/test/storage/path" |
| 21 | + vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ |
| 22 | + get: vi.fn().mockReturnValue(customPath), |
| 23 | + } as any) |
| 24 | + |
| 25 | + const fsPromises = await import("fs/promises") |
| 26 | + const { getStorageBasePath } = await import("../storage") |
| 27 | + |
| 28 | + const result = await getStorageBasePath(defaultPath) |
| 29 | + |
| 30 | + expect(result).toBe(customPath) |
| 31 | + expect((fsPromises as any).mkdir).toHaveBeenCalledWith(customPath, { recursive: true }) |
| 32 | + expect((fsPromises as any).access).toHaveBeenCalledWith(customPath, expect.any(Number)) |
| 33 | + }) |
| 34 | + |
| 35 | + it("falls back to default and shows an error when custom path is not writable", async () => { |
| 36 | + const customPath = "/test/storage/unwritable" |
| 37 | + |
| 38 | + vi.spyOn(vscode.workspace, "getConfiguration").mockReturnValue({ |
| 39 | + get: vi.fn().mockReturnValue(customPath), |
| 40 | + } as any) |
| 41 | + |
| 42 | + const showErrorSpy = vi.spyOn(vscode.window, "showErrorMessage").mockResolvedValue(undefined as any) |
| 43 | + |
| 44 | + const fsPromises = await import("fs/promises") |
| 45 | + const { getStorageBasePath } = await import("../storage") |
| 46 | + |
| 47 | + await (fsPromises as any).mkdir(customPath, { recursive: true }) |
| 48 | + |
| 49 | + const accessMock = (fsPromises as any).access as ReturnType<typeof vi.fn> |
| 50 | + accessMock.mockImplementationOnce(async (p: string) => { |
| 51 | + if (p === customPath) { |
| 52 | + const err: any = new Error("EACCES: permission denied") |
| 53 | + err.code = "EACCES" |
| 54 | + throw err |
| 55 | + } |
| 56 | + return Promise.resolve() |
| 57 | + }) |
| 58 | + |
| 59 | + const result = await getStorageBasePath(defaultPath) |
| 60 | + |
| 61 | + expect(result).toBe(defaultPath) |
| 62 | + expect(showErrorSpy).toHaveBeenCalledTimes(1) |
| 63 | + const firstArg = showErrorSpy.mock.calls[0][0] |
| 64 | + expect(typeof firstArg).toBe("string") |
| 65 | + }) |
| 66 | +}) |
0 commit comments