import { createResolveFsOptions } from "../../src/resolver/resolve"; // Mock filesystem interface interface MockFs { stat: jest.Mock; readFile: jest.Mock; realpath: jest.Mock; } describe("Resolver", () => { let mockFs: MockFs; beforeEach(() => { mockFs = { stat: jest.fn(), readFile: jest.fn(), realpath: jest.fn(), }; }); describe("createResolveFsOptions", () => { it("should return an object with readFile, isFile, isDirectory, and realpath functions", () => { const options = createResolveFsOptions(mockFs as any); expect(options).toHaveProperty("readFile"); expect(options).toHaveProperty("isFile"); expect(options).toHaveProperty("isDirectory"); expect(options).toHaveProperty("realpath"); expect(typeof options.readFile).toBe("function"); expect(typeof options.isFile).toBe("function"); expect(typeof options.isDirectory).toBe("function"); expect(typeof options.realpath).toBe("function"); }); }); describe("readFileCb", () => { it("should call callback with file contents on success", (done) => { const fileContent = "test file content"; mockFs.readFile.mockResolvedValue(fileContent); const options = createResolveFsOptions(mockFs as any); options.readFile("/test/file.txt", (err: any, data: any) => { expect(err).toBeUndefined(); expect(data).toBe(fileContent); expect(mockFs.readFile).toHaveBeenCalledWith("/test/file.txt", "utf8"); done(); }); }); it("should call callback with error when readFile fails", (done) => { const error = new Error("Read error"); mockFs.readFile.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.readFile("/test/missing.txt", (err: any) => { expect(err).toBe(error); expect(mockFs.readFile).toHaveBeenCalledWith( "/test/missing.txt", "utf8", ); done(); }); }); it("should handle ENOENT errors", (done) => { const error: NodeJS.ErrnoException = new Error("File not found"); error.code = "ENOENT"; mockFs.readFile.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.readFile("/test/nonexistent.txt", (err: any) => { expect(err).toBe(error); done(); }); }); it("should handle empty file content", (done) => { mockFs.readFile.mockResolvedValue(""); const options = createResolveFsOptions(mockFs as any); options.readFile("/test/empty.txt", (err: any, data: any) => { expect(err).toBeUndefined(); expect(data).toBe(""); done(); }); }); it("should handle rejection with string instead of Error", (done) => { mockFs.readFile.mockRejectedValue("string error"); const options = createResolveFsOptions(mockFs as any); options.readFile("/test/file.txt", (err: any) => { expect(err).toBe("string error"); done(); }); }); }); describe("isFile", () => { it("should call callback with true when path is a file", (done) => { const mockStats = { isFile: () => true, isDirectory: () => false, }; mockFs.stat.mockResolvedValue(mockStats); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/file.txt", (err: any, result: any) => { expect(err).toBeUndefined(); expect(result).toBe(true); expect(mockFs.stat).toHaveBeenCalledWith("/test/file.txt"); done(); }); }); it("should call callback with false when path is not a file", (done) => { const mockStats = { isFile: () => false, isDirectory: () => true, }; mockFs.stat.mockResolvedValue(mockStats); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/directory", (err: any, result: any) => { expect(err).toBeUndefined(); expect(result).toBe(false); done(); }); }); it("should call callback with false when file does not exist (ENOENT)", (done) => { const error: NodeJS.ErrnoException = new Error("File not found"); error.code = "ENOENT"; mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/missing.txt", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBe(false); done(); }); }); it("should call callback with false for ENOTDIR error", (done) => { const error: NodeJS.ErrnoException = new Error("Not a directory"); error.code = "ENOTDIR"; mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/invalid/path", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBe(false); done(); }); }); it("should propagate non-ENOENT/ENOTDIR errors", (done) => { const error: NodeJS.ErrnoException = new Error("Permission denied"); error.code = "EACCES"; mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/restricted.txt", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBeInstanceOf(Error); done(); }); }); it("should handle null error objects", (done) => { mockFs.stat.mockRejectedValue(null); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/file.txt", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBeNull(); done(); }); }); it("should handle errors without code property", (done) => { const error = new Error("Unknown error"); mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/file.txt", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBeInstanceOf(Error); done(); }); }); }); describe("isDirectory", () => { it("should call callback with true when path is a directory", (done) => { const mockStats = { isFile: () => false, isDirectory: () => true, }; mockFs.stat.mockResolvedValue(mockStats); const options = createResolveFsOptions(mockFs as any); options.isDirectory("/test/directory", (err: any, result: any) => { expect(err).toBeUndefined(); expect(result).toBe(true); expect(mockFs.stat).toHaveBeenCalledWith("/test/directory"); done(); }); }); it("should call callback with false when path is not a directory", (done) => { const mockStats = { isFile: () => true, isDirectory: () => false, }; mockFs.stat.mockResolvedValue(mockStats); const options = createResolveFsOptions(mockFs as any); options.isDirectory("/test/file.txt", (err: any, result: any) => { expect(err).toBeUndefined(); expect(result).toBe(false); done(); }); }); it("should call callback with false when directory does not exist (ENOENT)", (done) => { const error: NodeJS.ErrnoException = new Error("Directory not found"); error.code = "ENOENT"; mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isDirectory("/test/missing", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBe(false); done(); }); }); it("should call callback with false for ENOTDIR error", (done) => { const error: NodeJS.ErrnoException = new Error("Not a directory"); error.code = "ENOTDIR"; mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isDirectory("/test/invalid/path", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBe(false); done(); }); }); it("should propagate non-ENOENT/ENOTDIR errors", (done) => { const error: NodeJS.ErrnoException = new Error("Permission denied"); error.code = "EACCES"; mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isDirectory("/test/restricted", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBeInstanceOf(Error); done(); }); }); }); describe("realpath", () => { it("should call callback with resolved path on success", (done) => { const resolvedPath = "/real/path/to/file.txt"; mockFs.realpath.mockResolvedValue(resolvedPath); const options = createResolveFsOptions(mockFs as any); options.realpath("/test/symlink.txt", (err: any, result: any) => { expect(err).toBeUndefined(); expect(result).toBe(resolvedPath); expect(mockFs.realpath).toHaveBeenCalledWith("/test/symlink.txt"); done(); }); }); it("should call callback with original path when ENOENT error occurs", (done) => { const error: NodeJS.ErrnoException = new Error("File not found"); error.code = "ENOENT"; mockFs.realpath.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.realpath("/test/missing.txt", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBe("/test/missing.txt"); done(); }); }); it("should propagate non-ENOENT errors", (done) => { const error: NodeJS.ErrnoException = new Error("Permission denied"); error.code = "EACCES"; mockFs.realpath.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.realpath("/test/restricted.txt", (err: any) => { expect(err).toBe(error); done(); }); }); it("should handle symlinks correctly", (done) => { const symlinkPath = "/test/link"; const targetPath = "/test/target/file.txt"; mockFs.realpath.mockResolvedValue(targetPath); const options = createResolveFsOptions(mockFs as any); options.realpath(symlinkPath, (err: any, result: any) => { expect(err).toBeUndefined(); expect(result).toBe(targetPath); done(); }); }); it("should handle circular symlinks gracefully", (done) => { const error: NodeJS.ErrnoException = new Error("Too many symbolic links"); error.code = "ELOOP"; mockFs.realpath.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.realpath("/test/circular", (err: any) => { expect(err).toBe(error); done(); }); }); }); describe("Integration tests", () => { it("should handle multiple operations in sequence", async () => { const fileContent = "content"; const mockStats = { isFile: () => true, isDirectory: () => false }; const resolvedPath = "/real/path"; mockFs.readFile.mockResolvedValue(fileContent); mockFs.stat.mockResolvedValue(mockStats); mockFs.realpath.mockResolvedValue(resolvedPath); const options = createResolveFsOptions(mockFs as any); await new Promise((resolve) => { options.isFile("/test/file.txt", (err: any, isFile: any) => { expect(isFile).toBe(true); resolve(); }); }); await new Promise((resolve) => { options.readFile("/test/file.txt", (err: any, data: any) => { expect(data).toBe(fileContent); resolve(); }); }); await new Promise((resolve) => { options.realpath("/test/file.txt", (err: any, path: any) => { expect(path).toBe(resolvedPath); resolve(); }); }); }); it("should handle different filesystem implementations", () => { const customFs = { stat: jest .fn() .mockResolvedValue({ isFile: () => true, isDirectory: () => false }), readFile: jest.fn().mockResolvedValue("custom content"), realpath: jest.fn().mockResolvedValue("/custom/path"), }; const options = createResolveFsOptions(customFs as any); expect(options.readFile).toBeDefined(); expect(options.isFile).toBeDefined(); expect(options.isDirectory).toBeDefined(); expect(options.realpath).toBeDefined(); }); }); describe("Error handling edge cases", () => { it("should handle null error objects", (done) => { mockFs.stat.mockRejectedValue(null); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/file.txt", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBeNull(); done(); }); }); it("should handle errors without code property", (done) => { const error = new Error("Unknown error"); mockFs.stat.mockRejectedValue(error); const options = createResolveFsOptions(mockFs as any); options.isFile("/test/file.txt", (err: any, result: any) => { expect(err).toBeNull(); expect(result).toBeInstanceOf(Error); done(); }); }); it("should handle rejection with string instead of Error", (done) => { mockFs.readFile.mockRejectedValue("string error"); const options = createResolveFsOptions(mockFs as any); options.readFile("/test/file.txt", (err: any) => { expect(err).toBe("string error"); done(); }); }); }); });