import { readdir, readFile } from "node:fs/promises"; import { afterEach, describe, expect, it, vi, type Mock } from "vitest"; import { loadFeatureManifests } from "./manifest-loader"; // vi.mock is hoisted above the imports by vitest, so the loader binds to these // fakes. We drive the defensive "instanceof Error" fallbacks, which real // fs/JSON.parse rejections (always Error subclasses) can never reach. A default // export is supplied because the loader resolves node:fs/promises through ESM // interop that expects one. vi.mock("node:fs/promises", () => { const readdirFn = vi.fn(); const readFileFn = vi.fn(); return { readdir: readdirFn, readFile: readFileFn, default: { readdir: readdirFn, readFile: readFileFn } }; }); const readdirMock = readdir as unknown as Mock; const readFileMock = readFile as unknown as Mock; describe("loadFeatureManifests non-Error rejections", () => { afterEach(() => { vi.clearAllMocks(); }); it("uses the generic directory message when readdir rejects with a non-Error value", async () => { readdirMock.mockRejectedValueOnce("not-an-error"); const result = await loadFeatureManifests({ rootDir: "/synthetic/root" }); expect(result.ok).toBe(false); expect(result.manifestFiles).toEqual([]); expect(result.diagnostics).toHaveLength(1); expect(result.diagnostics[0]).toMatchObject({ code: "missing_directory", manifestId: null, message: "Failed to read feature manifest directory: Unknown directory read failure.", }); }); it("uses the generic parse message when readFile rejects with a non-Error value", async () => { readdirMock.mockResolvedValueOnce(["candidate.json"]); readFileMock.mockRejectedValueOnce(42); const result = await loadFeatureManifests({ rootDir: "/synthetic/root" }); expect(result.ok).toBe(false); expect(result.manifests).toEqual([]); expect(result.diagnostics).toContainEqual( expect.objectContaining({ code: "invalid_json", manifestId: null, message: "Failed to parse candidate.json: Unknown JSON parse failure.", }), ); }); });