Files
João Salavisa 1a3c134f1a feat(plugins): ship the plugin system without a feature flag (#2086)
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 08:59:34 +01:00

72 lines
2.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { StoreApi } from "zustand";
import type { AppState } from "@/lib/state/store";
import type { ActivePlugin } from "./types";
const hostMocks = vi.hoisted(() => ({
installPluginGlobal: vi.fn(),
loadPlugins: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("./host", () => ({
installPluginGlobal: hostMocks.installPluginGlobal,
loadPlugins: hostMocks.loadPlugins,
}));
import { bootPlugins } from "./boot";
function fakeStore(): StoreApi<AppState> {
return {} as StoreApi<AppState>;
}
function plugin(id: string): ActivePlugin {
return { id, name: id, bundleUrl: `/api/plugins/${id}/bundle` };
}
describe("bootPlugins", () => {
beforeEach(() => {
hostMocks.installPluginGlobal.mockClear();
hostMocks.loadPlugins.mockClear();
});
afterEach(() => vi.restoreAllMocks());
it("does nothing when the boot payload has no plugins", () => {
bootPlugins({ plugins: [] }, fakeStore());
bootPlugins({}, fakeStore());
expect(hostMocks.installPluginGlobal).not.toHaveBeenCalled();
expect(hostMocks.loadPlugins).not.toHaveBeenCalled();
});
it("installs the global and loads boot payload plugins", () => {
const store = fakeStore();
bootPlugins({ plugins: [plugin("a")] }, store);
expect(hostMocks.installPluginGlobal).toHaveBeenCalledTimes(1);
expect(hostMocks.loadPlugins).toHaveBeenCalledTimes(1);
expect(hostMocks.loadPlugins).toHaveBeenCalledWith([plugin("a")], expect.any(Function));
});
it("is idempotent across repeated calls for the same store", () => {
const store = fakeStore();
const payload = { plugins: [plugin("a")] };
bootPlugins(payload, store);
bootPlugins(payload, store);
bootPlugins(payload, store);
expect(hostMocks.installPluginGlobal).toHaveBeenCalledTimes(1);
expect(hostMocks.loadPlugins).toHaveBeenCalledTimes(1);
});
it("boots independently for distinct store instances", () => {
const payload = { plugins: [plugin("a")] };
bootPlugins(payload, fakeStore());
bootPlugins(payload, fakeStore());
expect(hostMocks.loadPlugins).toHaveBeenCalledTimes(2);
});
});