import { expect, spyOn, test } from "bun:test"; import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { loadPlugins } from "../src/registry.ts"; const dir = join(import.meta.dir, "fixtures/plugins"); test("registry loads echo plugin and skips broken one", async () => { const warn = spyOn(console, "warn").mockImplementation(() => {}); const reg = await loadPlugins([dir, join(dir, "does-not-exist")]); expect(reg.list().map((m) => m.id)).toEqual(["echo"]); expect(reg.get("echo")?.manifest.name).toBe("Echo"); expect(reg.get("broken")).toBeUndefined(); expect(warn.mock.calls.some((c) => String(c[0]).includes("broken"))).toBe(true); await reg.reload(); expect(reg.list()).toHaveLength(1); warn.mockRestore(); }); test("duplicate ids across dirs keep the first", async () => { const warn = spyOn(console, "warn").mockImplementation(() => {}); const reg = await loadPlugins([dir, dir]); expect(reg.list()).toHaveLength(1); warn.mockRestore(); }); test("reload picks up an edited entry file", async () => { const root = await mkdtemp(join(tmpdir(), "sttracker-plugins-")); try { await mkdir(join(root, "hot")); const write = (v: string) => writeFile(join(root, "hot", "index.ts"), `export default { manifest: { id: "hot", name: "Hot", version: "${v}", sdkVersion: 1, mediaTypes: [], capabilities: [], config: [] } };\n`); await write("1.0.0"); const reg = await loadPlugins([root]); expect(reg.get("hot")?.manifest.version).toBe("1.0.0"); await new Promise((r) => setTimeout(r, 5)); await write("2.0.0"); await reg.reload(); expect(reg.get("hot")?.manifest.version).toBe("2.0.0"); } finally { await rm(root, { recursive: true, force: true }); } });