import type { Plugin, PluginContext } from "rollup"; import webFsLoader from "../../src/web-fs-loader"; describe("Web FS Loader plugin", () => { const createMockContext = () => { const mockStat = jest.fn(); const mockReadFile = jest.fn(); return { fs: { stat: mockStat, readFile: mockReadFile, }, mockStat, mockReadFile, }; }; beforeEach(() => { jest.clearAllMocks(); }); describe("plugin creation", () => { it("should have resolveId hook", () => { const plugin = webFsLoader() as Plugin; expect(plugin.resolveId).toBeDefined(); expect(typeof plugin.resolveId).toBe("object"); }); it("should have load hook", () => { const plugin = webFsLoader() as Plugin; expect(plugin.load).toBeDefined(); expect(typeof plugin.load).toBe("object"); }); it("should have post order for resolveId", () => { const plugin = webFsLoader() as Plugin; expect(plugin.resolveId).toMatchObject({ order: "post", }); }); it("should have post order for load", () => { const plugin = webFsLoader() as Plugin; expect(plugin.load).toMatchObject({ order: "post", }); }); }); describe("resolveId hook", () => { it("should return importee when file exists", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/module.js"; const importer = "/src/index.js"; context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const result = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, importer, { attributes: {}, isEntry: false, }, ) : null; expect(result).toBe(importee); expect(context.mockStat).toHaveBeenCalledWith(importee); }); it("should throw error when file does not exist", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/missing.js"; const importer = "/src/index.js"; context.mockStat.mockRejectedValue(new Error("ENOENT: file not found")); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; await expect( resolveIdHook && typeof resolveIdHook.handler === "function" ? resolveIdHook.handler.call( context as unknown as PluginContext, importee, importer, { attributes: {}, isEntry: false, }, ) : Promise.resolve(null), ).rejects.toThrow( `Could not resolve import "${importee}" from "${importer}"`, ); expect(context.mockStat).toHaveBeenCalledWith(importee); }); it("should throw error with correct message format", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "./utils"; const importer = "/app/src/main.js"; context.mockStat.mockRejectedValue(new Error("File not found")); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; await expect( resolveIdHook && typeof resolveIdHook.handler === "function" ? resolveIdHook.handler.call( context as unknown as PluginContext, importee, importer, { attributes: {}, isEntry: false, }, ) : Promise.resolve(null), ).rejects.toThrow( 'Could not resolve import "./utils" from "/app/src/main.js"', ); }); it("should handle stat rejections gracefully", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); context.mockStat.mockRejectedValue(new Error("Permission denied")); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; await expect( resolveIdHook && typeof resolveIdHook.handler === "function" ? resolveIdHook.handler.call( context as unknown as PluginContext, "/restricted.js", "/src/index.js", { attributes: {}, isEntry: false, }, ) : Promise.resolve(null), ).rejects.toThrow("Could not resolve import"); }); it("should resolve absolute paths", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/absolute/path/to/module.js"; context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const result = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, undefined, { attributes: {}, isEntry: false, }, ) : null; expect(result).toBe(importee); }); it("should resolve relative paths", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "./utils/helper.js"; const importer = "/src/index.js"; context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const result = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, importer, { attributes: {}, isEntry: false, }, ) : null; expect(result).toBe(importee); expect(context.mockStat).toHaveBeenCalledWith(importee); }); it("should handle entry point resolution", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/main.js"; context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const result = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, undefined, { attributes: {}, isEntry: true, }, ) : null; expect(result).toBe(importee); }); }); describe("load hook", () => { it("should load file content as string", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const id = "/src/module.js"; const fileContent = "export const value = 42;"; context.mockReadFile.mockResolvedValue(fileContent); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; const result = loadHook && typeof loadHook.handler === "function" ? await loadHook.handler.call(context as unknown as PluginContext, id) : null; expect(result).toBe(fileContent); expect(context.mockReadFile).toHaveBeenCalledWith(id, { encoding: "utf8", }); }); it("should load different file types", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const files = [ { id: "/src/main.ts", content: "const x: number = 1;" }, { id: "/src/styles.css", content: ".class { color: red; }" }, { id: "/src/data.json", content: '{"key": "value"}' }, ]; for (const file of files) { context.mockReadFile.mockResolvedValue(file.content); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; const result = loadHook && typeof loadHook.handler === "function" ? await loadHook.handler.call( context as unknown as PluginContext, file.id, ) : null; expect(result).toBe(file.content); expect(context.mockReadFile).toHaveBeenCalledWith(file.id, { encoding: "utf8", }); } }); it("should always use utf8 encoding", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const id = "/src/file.js"; context.mockReadFile.mockResolvedValue("file content"); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; if (loadHook && typeof loadHook.handler === "function") { await loadHook.handler.call(context as unknown as PluginContext, id); } expect(context.mockReadFile).toHaveBeenCalledWith(id, { encoding: "utf8", }); }); it("should handle empty files", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const id = "/src/empty.js"; context.mockReadFile.mockResolvedValue(""); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; const result = loadHook && typeof loadHook.handler === "function" ? await loadHook.handler.call(context as unknown as PluginContext, id) : null; expect(result).toBe(""); }); it("should handle large files", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const id = "/src/large.js"; const largeContent = "x".repeat(10000); context.mockReadFile.mockResolvedValue(largeContent); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; const result = loadHook && typeof loadHook.handler === "function" ? await loadHook.handler.call(context as unknown as PluginContext, id) : null; expect(result).toBe(largeContent); expect(result).toHaveLength(10000); }); it("should propagate readFile errors", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const id = "/src/error.js"; context.mockReadFile.mockRejectedValue( new Error("EACCES: permission denied"), ); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; await expect( loadHook && typeof loadHook.handler === "function" ? loadHook.handler.call(context as unknown as PluginContext, id) : Promise.resolve(null), ).rejects.toThrow("EACCES: permission denied"); }); }); describe("integration", () => { it("should handle complete workflow: resolve then load", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/module.js"; const importer = "/src/index.js"; const fileContent = "export const value = 42;"; // Resolve context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const resolvedId = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, importer, { attributes: {}, isEntry: false, }, ) : null; expect(resolvedId).toBe(importee); // Load context.mockReadFile.mockResolvedValue(fileContent); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; const loadedContent = loadHook && typeof loadHook.handler === "function" ? await loadHook.handler.call( context as unknown as PluginContext, resolvedId as string, ) : null; expect(loadedContent).toBe(fileContent); }); it("should handle multiple files in sequence", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const files = [ { path: "/src/a.js", content: "export const a = 1;" }, { path: "/src/b.js", content: "export const b = 2;" }, { path: "/src/c.js", content: "export const c = 3;" }, ]; for (const file of files) { // Resolve context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const resolvedId = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, file.path, "/src/index.js", { attributes: {}, isEntry: false, }, ) : null; expect(resolvedId).toBe(file.path); // Load context.mockReadFile.mockResolvedValue(file.content); const loadHook = typeof plugin.load === "object" && plugin.load !== null ? plugin.load : null; const loadedContent = loadHook && typeof loadHook.handler === "function" ? await loadHook.handler.call( context as unknown as PluginContext, resolvedId as string, ) : null; expect(loadedContent).toBe(file.content); } }); it("should maintain hook order as post", () => { const plugin = webFsLoader() as Plugin; const resolveIdHook = typeof plugin.resolveId === "object" ? plugin.resolveId : null; const loadHook = typeof plugin.load === "object" ? plugin.load : null; expect(resolveIdHook).toMatchObject({ order: "post" }); expect(loadHook).toMatchObject({ order: "post" }); }); }); describe("edge cases", () => { it("should handle undefined importer", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/entry.js"; context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const result = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, undefined, { attributes: {}, isEntry: true, }, ) : null; expect(result).toBe(importee); }); it("should throw meaningful error for undefined importer", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/missing.js"; context.mockStat.mockRejectedValue(new Error("Not found")); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; await expect( resolveIdHook && typeof resolveIdHook.handler === "function" ? resolveIdHook.handler.call( context as unknown as PluginContext, importee, undefined, { attributes: {}, isEntry: false, }, ) : Promise.resolve(null), ).rejects.toThrow( `Could not resolve import "${importee}" from "undefined"`, ); }); it("should handle special characters in paths", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/file with spaces.js"; context.mockStat.mockResolvedValue({ isFile: () => true }); context.mockReadFile.mockResolvedValue("content"); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const result = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, "/src/index.js", { attributes: {}, isEntry: false, }, ) : null; expect(result).toBe(importee); }); it("should handle paths with unicode characters", async () => { const plugin = webFsLoader() as Plugin; const context = createMockContext(); const importee = "/src/文件.js"; context.mockStat.mockResolvedValue({ isFile: () => true }); const resolveIdHook = typeof plugin.resolveId === "object" && plugin.resolveId !== null ? plugin.resolveId : null; const result = resolveIdHook && typeof resolveIdHook.handler === "function" ? await resolveIdHook.handler.call( context as unknown as PluginContext, importee, "/src/index.js", { attributes: {}, isEntry: false, }, ) : null; expect(result).toBe(importee); }); }); });