import { describe, expect, it } from "vitest"; import { bootLeaks, type ChunkLike } from "./chunkGuard"; const APP = "/r/src/shell/App.tsx"; const chunk = (fileName: string, moduleIds: string[], imports: string[] = [], isEntry = false) => ({ fileName, moduleIds, imports, isEntry }) satisfies ChunkLike; describe("a lazily imported module", () => { const lazy = new Set([APP, "/r/src/modules/plex/PlexBrowse.tsx"]); it("is a leak in the entry chunk, in a chunk the entry imports, or in the App chunk", () => { const P = "/r/src/modules/plex/PlexBrowse.tsx"; for (const chunks of [ [chunk("index.js", ["/r/src/main.tsx", P], [], true)], [chunk("index.js", ["/r/src/main.tsx"], ["shared.js"], true), chunk("shared.js", [P])], [chunk("index.js", ["/r/src/main.tsx"], [], true), chunk("App.js", [APP, P])], [ chunk("index.js", ["/r/src/main.tsx"], [], true), chunk("App.js", [APP], ["s.js"]), chunk("s.js", [P]), ], ]) expect(bootLeaks(chunks, lazy, "src/shell/App.tsx")).toEqual([ `${P} (${chunks[chunks.length - 1]!.fileName})`, ]); }); it("is not a leak in its own chunk, or in one only another lazy chunk loads — and App itself is lazy by design", () => { const chunks = [ chunk("index.js", ["/r/src/main.tsx"], ["react.js"], true), chunk("react.js", ["/r/node_modules/react/index.js"]), chunk("App.js", [APP], ["react.js"]), chunk("PlexBrowse.js", ["/r/src/modules/plex/PlexBrowse.tsx"], ["react.js"]), ]; expect(bootLeaks(chunks, lazy, "src/shell/App.tsx")).toEqual([]); }); it("is checked on Windows paths too", () => { const win = "D:\\r\\src\\modules\\plex\\PlexBrowse.tsx"; const chunks = [chunk("index.js", ["D:\\r\\src\\main.tsx", win], [], true)]; expect(bootLeaks(chunks, new Set([win]), "src/shell/App.tsx")).toHaveLength(1); }); });