import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { setTimeout as delay } from "node:timers/promises"; import { afterEach, describe, expect, it } from "vitest"; import { writeOfficialPluginRuntimeManifest } from "../src/app/official-plugin-runtime.js"; describe("official plugin runtime manifest", () => { const cleanups: string[] = []; const originalEntrypoint = process.argv[1]; afterEach(async () => { process.argv[1] = originalEntrypoint; await Promise.all(cleanups.splice(0).map((root) => rm(root, { force: true, recursive: true }))); }); it("does not rewrite an unchanged runtime manifest", async () => { const root = await mkdtemp(join(tmpdir(), "zcode-plugin-runtime-manifest-")); cleanups.push(root); const manifestPath = join(root, ".zcode-plugin", "plugin.json"); await writeJson(manifestPath, { mcpServers: { "browser-use": { args: [], command: "old-node", }, }, name: "browser-use", }); process.argv[1] = join(root, "zcode.cjs"); writeOfficialPluginRuntimeManifest({ pluginName: "browser-use", rootPath: root, }); const expectedContents = await readFile(manifestPath, "utf8"); const firstStat = await stat(manifestPath); await delay(30); writeOfficialPluginRuntimeManifest({ pluginName: "browser-use", rootPath: root, }); const secondStat = await stat(manifestPath); expect(await readFile(manifestPath, "utf8")).toBe(expectedContents); expect(secondStat.mtimeMs).toBe(firstStat.mtimeMs); }); it("keeps invalid mcpServers structural errors fatal", async () => { const root = await mkdtemp(join(tmpdir(), "zcode-plugin-runtime-invalid-")); cleanups.push(root); await writeJson(join(root, ".zcode-plugin", "plugin.json"), { mcpServers: "invalid", name: "browser-use", }); process.argv[1] = join(root, "zcode.cjs"); expect(() => writeOfficialPluginRuntimeManifest({ pluginName: "browser-use", rootPath: root, }), ).toThrow("Official plugin manifest has invalid mcpServers."); }); }); async function writeJson(path: string, value: unknown): Promise { await import("node:fs/promises").then(({ mkdir }) => mkdir(dirname(path), { recursive: true })); await writeFile(path, `${JSON.stringify(value, null, 2)}\n`); }