import fs from "fs"; import nodeFsAdapter from "../../src/node-fs-adapter"; // Mock the fs module jest.mock("fs", () => { const actualFs = jest.requireActual("fs"); return { ...actualFs, promises: { stat: jest.fn(), readFile: jest.fn(), realpath: jest.fn(), mkdir: jest.fn(), writeFile: jest.fn(), readdir: jest.fn(), }, realpathSync: jest.fn(), }; }); describe("Node FS Adapter", () => { beforeEach(() => { jest.clearAllMocks(); }); describe("access", () => { it("should return true when file exists", async () => { const mockStats = { isFile: () => true, isDirectory: () => false, size: 100, }; (fs.promises.stat as jest.Mock).mockResolvedValue(mockStats); const result = await nodeFsAdapter.access("/test/file.txt"); expect(result).toBe(true); expect(fs.promises.stat).toHaveBeenCalledWith("/test/file.txt"); }); it("should return false when file does not exist", async () => { (fs.promises.stat as jest.Mock).mockRejectedValue( new Error("ENOENT: no such file or directory"), ); const result = await nodeFsAdapter.access("/test/nonexistent.txt"); expect(result).toBe(false); expect(fs.promises.stat).toHaveBeenCalledWith("/test/nonexistent.txt"); }); it("should return false on any error", async () => { (fs.promises.stat as jest.Mock).mockRejectedValue( new Error("Permission denied"), ); const result = await nodeFsAdapter.access("/test/restricted.txt"); expect(result).toBe(false); }); }); describe("readFile", () => { it("should read file as string when encoding is specified", async () => { const fileContent = "Hello, World!"; (fs.promises.readFile as jest.Mock).mockResolvedValue(fileContent); const result = await nodeFsAdapter.readFile("/test/file.txt", "utf8"); expect(result).toBe(fileContent); expect(fs.promises.readFile).toHaveBeenCalledWith("/test/file.txt", { encoding: "utf-8", }); }); it("should read file as buffer when encoding is not specified", async () => { const fileBuffer = new Uint8Array([72, 101, 108, 108, 111]); (fs.promises.readFile as jest.Mock).mockResolvedValue(fileBuffer); const result = await nodeFsAdapter.readFile("/test/file.bin"); expect(result).toBe(fileBuffer); expect(fs.promises.readFile).toHaveBeenCalledWith( "/test/file.bin", undefined, ); }); it("should propagate read errors", async () => { (fs.promises.readFile as jest.Mock).mockRejectedValue( new Error("ENOENT: no such file or directory"), ); await expect(nodeFsAdapter.readFile("/test/missing.txt")).rejects.toThrow( "ENOENT", ); }); }); describe("realpath", () => { it("should resolve the real path", async () => { const realPath = "/real/path/to/file.txt"; (fs.promises.realpath as jest.Mock).mockResolvedValue(realPath); const result = await nodeFsAdapter.realpath("/symlink/file.txt"); expect(result).toBe(realPath); expect(fs.promises.realpath).toHaveBeenCalledWith("/symlink/file.txt"); }); it("should propagate realpath errors", async () => { (fs.promises.realpath as jest.Mock).mockRejectedValue( new Error("ENOENT: no such file or directory"), ); await expect(nodeFsAdapter.realpath("/nonexistent/path")).rejects.toThrow( "ENOENT", ); }); }); describe("realpathSync", () => { it("should resolve the real path synchronously", () => { const realPath = "/real/path/to/file.txt"; const mockRealpathSync = fs.realpathSync as jest.MockedFunction< typeof fs.realpathSync >; mockRealpathSync.mockReturnValue(realPath); const result = nodeFsAdapter.realpathSync("/symlink/file.txt"); expect(result).toBe(realPath); expect(fs.realpathSync).toHaveBeenCalledWith("/symlink/file.txt"); }); it("should propagate realpathSync errors", () => { const mockRealpathSync = fs.realpathSync as jest.MockedFunction< typeof fs.realpathSync >; mockRealpathSync.mockImplementation(() => { throw new Error("ENOENT: no such file or directory"); }); expect(() => nodeFsAdapter.realpathSync("/nonexistent/path")).toThrow( "ENOENT", ); }); }); describe("stat", () => { it("should return file stats", async () => { const mockStats = { isFile: () => true, isDirectory: () => false, isSymbolicLink: () => false, size: 1024, mode: 0o644, mtimeMs: Date.now(), ctimeMs: Date.now(), ino: 123456, uid: 1000, gid: 1000, dev: 1, }; (fs.promises.stat as jest.Mock).mockResolvedValue(mockStats); const result = await nodeFsAdapter.stat("/test/file.txt"); expect(result).toBeDefined(); expect(fs.promises.stat).toHaveBeenCalledWith("/test/file.txt"); }); it("should propagate stat errors", async () => { (fs.promises.stat as jest.Mock).mockRejectedValue( new Error("ENOENT: no such file or directory"), ); await expect(nodeFsAdapter.stat("/test/missing.txt")).rejects.toThrow( "ENOENT", ); }); }); describe("mkdir", () => { it("should create directory", async () => { (fs.promises.mkdir as jest.Mock).mockResolvedValue(undefined); await nodeFsAdapter.mkdir("/test/newdir"); expect(fs.promises.mkdir).toHaveBeenCalledWith("/test/newdir", undefined); }); it("should create directory recursively", async () => { (fs.promises.mkdir as jest.Mock).mockResolvedValue(undefined); await nodeFsAdapter.mkdir("/test/deep/nested/dir", { recursive: true }); expect(fs.promises.mkdir).toHaveBeenCalledWith("/test/deep/nested/dir", { recursive: true, }); }); it("should propagate mkdir errors", async () => { (fs.promises.mkdir as jest.Mock).mockRejectedValue( new Error("EEXIST: file already exists"), ); await expect(nodeFsAdapter.mkdir("/test/existing")).rejects.toThrow( "EEXIST", ); }); }); describe("writeFile", () => { it("should write string to file", async () => { (fs.promises.writeFile as jest.Mock).mockResolvedValue(undefined); await nodeFsAdapter.writeFile("/test/file.txt", "Hello, World!"); expect(fs.promises.writeFile).toHaveBeenCalledWith( "/test/file.txt", "Hello, World!", undefined, ); }); it("should write string to file with encoding", async () => { (fs.promises.writeFile as jest.Mock).mockResolvedValue(undefined); await nodeFsAdapter.writeFile("/test/file.txt", "Hello, World!", "utf8"); expect(fs.promises.writeFile).toHaveBeenCalledWith( "/test/file.txt", "Hello, World!", "utf8", ); }); it("should write buffer to file", async () => { const buffer = new Uint8Array([72, 101, 108, 108, 111]); (fs.promises.writeFile as jest.Mock).mockResolvedValue(undefined); await nodeFsAdapter.writeFile("/test/file.bin", buffer); expect(fs.promises.writeFile).toHaveBeenCalledWith( "/test/file.bin", buffer, undefined, ); }); it("should propagate writeFile errors", async () => { (fs.promises.writeFile as jest.Mock).mockRejectedValue( new Error("EACCES: permission denied"), ); await expect( nodeFsAdapter.writeFile("/test/readonly.txt", "data"), ).rejects.toThrow("EACCES"); }); }); describe("promises.lstat", () => { it("should return lstat information", async () => { const mockStats = { isFile: () => false, isDirectory: () => false, isSymbolicLink: () => true, size: 0, }; (fs.promises.stat as jest.Mock).mockResolvedValue(mockStats); const result = await nodeFsAdapter.promises!.lstat!("/test/symlink"); expect(result).toBe(mockStats); expect(fs.promises.stat).toHaveBeenCalledWith("/test/symlink"); }); }); describe("promises.readdir", () => { it("should read directory with file types", async () => { const mockEntries = [ { name: "file1.txt", isFile: () => true, isDirectory: () => false }, { name: "dir1", isFile: () => false, isDirectory: () => true }, ]; (fs.promises.readdir as jest.Mock).mockResolvedValue(mockEntries); const result = await nodeFsAdapter.promises!.readdir!("/test/dir", { withFileTypes: true, }); expect(result).toBe(mockEntries); expect(fs.promises.readdir).toHaveBeenCalledWith("/test/dir", { withFileTypes: true, }); }); it("should propagate readdir errors", async () => { (fs.promises.readdir as jest.Mock).mockRejectedValue( new Error("ENOENT: no such file or directory"), ); await expect( nodeFsAdapter.promises!.readdir!("/test/missing", { withFileTypes: true, }), ).rejects.toThrow("ENOENT"); }); }); describe("promises.readlink", () => { it("should resolve symlink path", async () => { const targetPath = "/real/path/to/target"; (fs.promises.realpath as jest.Mock).mockResolvedValue(targetPath); const result = await nodeFsAdapter.promises!.readlink!("/test/symlink"); expect(result).toBe(targetPath); expect(fs.promises.realpath).toHaveBeenCalledWith("/test/symlink"); }); }); describe("promises.realpath", () => { it("should resolve the real path", async () => { const realPath = "/real/path/to/file.txt"; (fs.promises.realpath as jest.Mock).mockResolvedValue(realPath); const result = await nodeFsAdapter.promises!.realpath!("/symlink/file.txt"); expect(result).toBe(realPath); expect(fs.promises.realpath).toHaveBeenCalledWith("/symlink/file.txt"); }); }); describe("integration", () => { it("should implement all required IsomorphicRollupFs methods", () => { expect(nodeFsAdapter.access).toBeDefined(); expect(nodeFsAdapter.readFile).toBeDefined(); expect(nodeFsAdapter.realpath).toBeDefined(); expect(nodeFsAdapter.realpathSync).toBeDefined(); expect(nodeFsAdapter.stat).toBeDefined(); expect(nodeFsAdapter.mkdir).toBeDefined(); expect(nodeFsAdapter.writeFile).toBeDefined(); expect(nodeFsAdapter.promises).toBeDefined(); expect(nodeFsAdapter.promises?.lstat).toBeDefined(); expect(nodeFsAdapter.promises?.readdir).toBeDefined(); expect(nodeFsAdapter.promises?.readlink).toBeDefined(); expect(nodeFsAdapter.promises?.realpath).toBeDefined(); }); it("should have correct method types", () => { expect(typeof nodeFsAdapter.access).toBe("function"); expect(typeof nodeFsAdapter.readFile).toBe("function"); expect(typeof nodeFsAdapter.realpath).toBe("function"); expect(typeof nodeFsAdapter.realpathSync).toBe("function"); expect(typeof nodeFsAdapter.stat).toBe("function"); expect(typeof nodeFsAdapter.mkdir).toBe("function"); expect(typeof nodeFsAdapter.writeFile).toBe("function"); expect(typeof nodeFsAdapter.promises?.lstat).toBe("function"); expect(typeof nodeFsAdapter.promises?.readdir).toBe("function"); expect(typeof nodeFsAdapter.promises?.readlink).toBe("function"); expect(typeof nodeFsAdapter.promises?.realpath).toBe("function"); }); }); });