// @vitest-environment jsdom import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { routeRegistry } from "./registry/store"; import { loadAllPlugins, loadPawApp, resetPawAppLoaderForTests, } from "./usePluginLoader"; const originalCreateObjectUrl = URL.createObjectURL; const originalRevokeObjectUrl = URL.revokeObjectURL; function jsonResponse(value: unknown): Response { return new Response(JSON.stringify(value), { status: 200, headers: { "Content-Type": "application/json" }, }); } function plugin(id: string, type: string) { return { id, name: id, plugin_type: type, frontend_entry: "dist/index.js", }; } describe("frontend plugin loader", () => { beforeEach(() => { resetPawAppLoaderForTests(); routeRegistry.__resetForTests(); vi.restoreAllMocks(); URL.createObjectURL = vi.fn( () => `data:text/javascript,${encodeURIComponent("export default true")}`, ); URL.revokeObjectURL = vi.fn(); }); afterEach(() => { URL.createObjectURL = originalCreateObjectUrl; URL.revokeObjectURL = originalRevokeObjectUrl; delete (globalThis as typeof globalThis & { __registerNotes?: () => void }) .__registerNotes; }); it("loads every installed frontend plugin during startup", async () => { const fetchMock = vi.spyOn(globalThis, "fetch"); fetchMock .mockResolvedValueOnce( jsonResponse([ plugin("global-tools", "frontend"), plugin("notes", "app"), ]), ) .mockImplementation(async () => new Response("export default true")); await expect(loadAllPlugins()).resolves.toEqual({ loaded: 2, failed: [], }); expect(fetchMock).toHaveBeenCalledTimes(3); }); it("loads a newly installed PawApp and exposes its route immediately", async () => { const runtimeGlobal = globalThis as typeof globalThis & { __registerNotes?: () => void; }; runtimeGlobal.__registerNotes = () => { routeRegistry.add("notes", { id: "notes.page", path: "/apps/notes", component: () => null, }); }; URL.createObjectURL = vi.fn( () => `data:text/javascript,${encodeURIComponent( "globalThis.__registerNotes()", )}`, ); const fetchMock = vi.spyOn(globalThis, "fetch"); fetchMock .mockResolvedValueOnce(jsonResponse([plugin("notes", "app")])) .mockResolvedValueOnce(new Response("globalThis.__registerNotes()")); await expect(loadPawApp("notes")).resolves.toBeUndefined(); expect(routeRegistry.snapshot()).toMatchObject([ { id: "notes.page", path: "/apps/notes", source: "notes" }, ]); }); it("deduplicates concurrent loads and allows retry after failure", async () => { const fetchMock = vi.spyOn(globalThis, "fetch"); fetchMock .mockResolvedValueOnce(jsonResponse([plugin("notes", "app")])) .mockResolvedValueOnce(new Response("missing", { status: 503 })); const first = loadPawApp("notes"); expect(loadPawApp("notes")).toBe(first); await expect(first).rejects.toThrow("HTTP 503"); fetchMock .mockResolvedValueOnce(jsonResponse([plugin("notes", "app")])) .mockImplementationOnce(async () => { routeRegistry.add("notes", { id: "notes.page", path: "/apps/notes", component: () => null, }); return new Response("export default true"); }); await expect(loadPawApp("notes")).resolves.toBeUndefined(); }); });