| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701 |
- import { mkdtemp, mkdir, writeFile, rm, rename, stat } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { expect, it, vi } from "vitest";
- import { activateDirectoryAtomically } from "../src/plugins/atomic-directory.js";
- import { createMcpConfigurationLoader } from "../src/mcp/config-loader.js";
- import { createDynamicMcpConfigSource } from "../src/mcp/config-source.js";
- /** 直接使用加载器的用例按配置源的方式采纳每次结果。 */
- async function accept(loader: ReturnType<typeof createMcpConfigurationLoader>) {
- const loaded = await loader.load();
- loaded.commit();
- return loaded;
- }
- it.each(["config", "legacy"])(
- "keeps valid settings with MCP warnings on initial load and refresh (%s)",
- async (format) => {
- const root = await mkdtemp(join(tmpdir(), "mcp-config-warning-"));
- const config = join(root, "config.json");
- const legacy = join(root, ".mcp.json");
- const plugin = join(root, "plugin");
- try {
- await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(plugin, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "example", mcpServers: { demo: { command: "node" } } }),
- );
- const loader = createMcpConfigurationLoader({
- configPaths: [config],
- userConfigPath: config,
- fallbackPaths: format === "legacy" ? { [config]: legacy } : undefined,
- pluginStorageRoot: join(root, "plugins"),
- pluginRoots: [],
- workingDirectory: root,
- });
- for (const enabled of [false, true]) {
- const name = enabled ? "updated" : "initial";
- const servers = { [name]: { command: "node" }, broken: { command: 123 } };
- await writeFile(
- config,
- JSON.stringify({
- features: { mcp: enabled },
- plugins: { enabled, dirs: [plugin] },
- ...(format === "config" ? { mcp: { servers } } : {}),
- }),
- );
- if (format === "legacy") await writeFile(legacy, JSON.stringify({ mcpServers: servers }));
- const { configuration } = await accept(loader);
- expect(configuration.enabled).toBe(enabled);
- expect(Object.keys(configuration.servers).sort()).toEqual(
- enabled ? ["plugin:example:demo", name].sort() : [name],
- );
- expect(configuration.servers[name]).toMatchObject({ type: "stdio", command: "node" });
- }
- const { configuration: valid } = await accept(loader);
- // warning 的结果也是有效快照;后续整体解析失败不能回退到更早配置。
- for (const invalid of ["{ invalid", JSON.stringify({ features: { mcp: "invalid" } })]) {
- await writeFile(config, invalid);
- await expect(accept(loader)).resolves.toMatchObject({ configuration: valid });
- }
- if (format === "legacy") {
- await writeFile(legacy, "{ invalid");
- await expect(accept(loader)).resolves.toMatchObject({ configuration: valid });
- }
- await rm(config);
- await rm(legacy, { force: true });
- await expect(accept(loader)).resolves.toMatchObject({
- configuration: { enabled: true, servers: {} },
- });
- } finally {
- await rm(root, { recursive: true, force: true });
- }
- },
- );
- it("publishes external atomic config replacement", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-config-"));
- const file = join(root, "config.json");
- await writeFile(file, JSON.stringify({ mcp: { servers: {} } }));
- const source = await createDynamicMcpConfigSource({
- configPaths: [file],
- userConfigPath: file,
- pluginStorageRoot: join(root, "plugins"),
- workingDirectory: root,
- pluginRoots: [],
- });
- try {
- const changed = Promise.withResolvers<void>();
- source.subscribe(() => {
- if (source.getSnapshot().servers.test) changed.resolve();
- });
- await writeFile(
- `${file}.tmp`,
- JSON.stringify({ mcp: { servers: { test: { type: "stdio", command: "node" } } } }),
- );
- await rename(`${file}.tmp`, file);
- await changed.promise;
- expect(source.getSnapshot().servers.test?.type).toBe("stdio");
- } finally {
- source.close();
- await rm(root, { recursive: true, force: true });
- }
- }, 5000);
- it("reloads an installed plugin on each install record commit and removes it on uninstall", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-plugin-"));
- const config = join(root, "config.json");
- const plugins = join(root, "plugins");
- const plugin = join(root, "plugin");
- await mkdir(plugins);
- await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(plugin, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "example", mcpServers: "./.mcp.json" }),
- );
- await writeFile(
- join(plugin, ".mcp.json"),
- JSON.stringify({ mcpServers: { demo: { command: "node", args: ["server.js"] } } }),
- );
- await writeFile(
- config,
- JSON.stringify({ plugins: { enabledPlugins: { "example@test": true } } }),
- );
- const source = await createDynamicMcpConfigSource({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: plugins,
- pluginRoots: [],
- workingDirectory: root,
- });
- // 安装和更新流程在替换插件目录后写安装记录(updatedAt 每次不同);配置源只监听这个提交点。
- const commit = (updatedAt: string) =>
- writeFile(
- join(plugins, "installed_plugins.json"),
- JSON.stringify({
- version: 1,
- plugins: [
- {
- id: "example@test",
- name: "example",
- marketplace: "test",
- version: "1.0.0",
- installPath: plugin,
- installedAt: "2026-01-01T00:00:00Z",
- updatedAt,
- scope: "user",
- },
- ],
- }),
- );
- try {
- const loaded = Promise.withResolvers<void>();
- const removed = Promise.withResolvers<void>();
- let installed = false;
- source.subscribe(() => {
- if (source.getSnapshot().servers["plugin:example:demo"]) {
- installed = true;
- loaded.resolve();
- } else if (installed) removed.resolve();
- });
- await commit("2026-01-01T00:00:00Z");
- await loaded.promise;
- expect(source.getSnapshot().servers["plugin:example:demo"]).toMatchObject({ command: "node" });
- const declared = Promise.withResolvers<void>();
- const refreshed = Promise.withResolvers<void>();
- source.subscribe(() => {
- const server = source.getSnapshot().servers["plugin:example:demo"];
- if (server?.type === "stdio" && server.args?.[0] === "declared.js") declared.resolve();
- if (server?.type === "stdio" && server.args?.[0] === "updated.js") refreshed.resolve();
- });
- const extra = join(plugin, "extra.json");
- await writeFile(
- extra,
- JSON.stringify({ mcpServers: { demo: { command: "node", args: ["declared.js"] } } }),
- );
- await writeFile(
- join(plugin, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "example", mcpServers: "./extra.json" }),
- );
- await commit("2026-01-02T00:00:00Z");
- await declared.promise;
- await writeFile(
- extra,
- JSON.stringify({ mcpServers: { demo: { command: "node", args: ["updated.js"] } } }),
- );
- await commit("2026-01-03T00:00:00Z");
- await refreshed.promise;
- await writeFile(
- join(plugins, "installed_plugins.json"),
- JSON.stringify({ version: 1, plugins: [] }),
- );
- await removed.promise;
- expect(source.getSnapshot().servers).toEqual({});
- } finally {
- source.close();
- await rm(root, { recursive: true, force: true });
- }
- });
- it("keeps an explicit server map and retains a malformed install inventory", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-explicit-"));
- const config = join(root, "config.json");
- const manifest = join(root, "plugin", ".zcode-plugin", "plugin.json");
- const inventory = join(root, "installed_plugins.json");
- await mkdir(join(root, "plugin", ".zcode-plugin"), { recursive: true });
- await writeFile(
- manifest,
- JSON.stringify({ name: "example", mcpServers: { demo: { command: "node" } } }),
- );
- await writeFile(
- inventory,
- JSON.stringify({ plugins: { "example@test": [{ installPath: join(root, "plugin") }] } }),
- );
- await writeFile(
- config,
- JSON.stringify({ plugins: { enabledPlugins: { "example@test": true } } }),
- );
- const explicit = { pinned: { type: "stdio" as const, command: "node", args: ["pinned.js"] } };
- const source = await createDynamicMcpConfigSource({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- explicitServers: explicit,
- });
- try {
- expect(Object.keys(source.getSnapshot().servers).sort()).toEqual([
- "pinned",
- "plugin:example:demo",
- ]);
- const changed = Promise.withResolvers<void>();
- source.subscribe(() => {
- if (!source.getSnapshot().enabled) changed.resolve();
- });
- await writeFile(inventory, JSON.stringify({ plugins: "broken" }));
- await writeFile(
- config,
- JSON.stringify({
- features: { mcp: false },
- plugins: { enabledPlugins: { "example@test": true } },
- mcp: { servers: { unexpected: { command: "node" } } },
- }),
- );
- await changed.promise;
- expect(Object.keys(source.getSnapshot().servers).sort()).toEqual([
- "pinned",
- "plugin:example:demo",
- ]);
- expect(source.getSnapshot().servers.pinned).toEqual(explicit.pinned);
- } finally {
- source.close();
- await rm(root, { recursive: true, force: true });
- }
- });
- it("retains an invalid source while applying a different valid source and detects directory recreation", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-config-sources-"));
- const directory = join(root, "workspace");
- await mkdir(directory);
- const user = join(root, "config.json");
- const project = join(directory, "config.json");
- await writeFile(
- user,
- JSON.stringify({ mcp: { servers: { user: { type: "stdio", command: "node" } } } }),
- );
- const source = await createDynamicMcpConfigSource({
- configPaths: [user, project],
- userConfigPath: user,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- });
- try {
- const first = Promise.withResolvers<void>();
- const next = Promise.withResolvers<void>();
- source.subscribe(() => {
- if (source.getSnapshot().servers.one) first.resolve();
- if (source.getSnapshot().servers.two) next.resolve();
- });
- await writeFile(user, "{ invalid");
- await writeFile(
- project,
- JSON.stringify({ mcp: { servers: { one: { type: "stdio", command: "node" } } } }),
- );
- await first.promise;
- expect(source.getSnapshot().servers.user).toBeDefined();
- const replacement = join(root, "replacement");
- await mkdir(replacement);
- await writeFile(
- join(replacement, "config.json"),
- JSON.stringify({ mcp: { servers: { two: { type: "stdio", command: "node" } } } }),
- );
- await rename(directory, join(root, "old"));
- await rename(replacement, directory);
- await next.promise;
- expect(Object.keys(source.getSnapshot().servers).sort()).toEqual(["two", "user"]);
- } finally {
- source.close();
- await rm(root, { recursive: true, force: true });
- }
- });
- it("does not reuse a plugin result after its installation path is replaced", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-plugin-replacement-"));
- const config = join(root, "config.json");
- const inventory = join(root, "installed_plugins.json");
- const pluginId = "example@test";
- const record = (version: string) => ({
- id: pluginId,
- name: "example",
- marketplace: "test",
- version,
- installPath: join(root, version),
- installedAt: "2026-01-01T00:00:00Z",
- scope: "user",
- });
- await writeFile(config, JSON.stringify({ plugins: { enabledPlugins: { [pluginId]: true } } }));
- for (const version of ["v1", "v2"]) {
- await mkdir(join(root, version, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(root, version, ".zcode-plugin", "plugin.json"),
- JSON.stringify({
- name: "example",
- mcpServers: {
- demo: version === "v1" ? { command: "node", args: ["old.js"] } : { command: 123 },
- },
- }),
- );
- }
- await writeFile(inventory, JSON.stringify({ version: 1, plugins: [record("v1")] }));
- const source = await createDynamicMcpConfigSource({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- });
- try {
- expect(source.getSnapshot().servers["plugin:example:demo"]).toMatchObject({ args: ["old.js"] });
- await writeFile(inventory, JSON.stringify({ version: 1, plugins: [record("v2")] }));
- await vi.waitFor(() => expect(source.getSnapshot().servers).toEqual({}));
- } finally {
- source.close();
- await rm(root, { recursive: true, force: true });
- }
- });
- it("isolates malformed plugin server and installation entries", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-entry-isolation-"));
- const plugin = join(root, "plugin");
- const config = join(root, "config.json");
- try {
- await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(plugin, ".zcode-plugin", "plugin.json"),
- JSON.stringify({
- name: "example",
- mcpServers: { good: { command: "node" }, bad: { command: 42 } },
- }),
- );
- await writeFile(
- config,
- JSON.stringify({ plugins: { enabledPlugins: { "example@test": true } } }),
- );
- await writeFile(
- join(root, "installed_plugins.json"),
- JSON.stringify({
- version: 1,
- plugins: [
- {
- id: "example@test",
- name: "example",
- marketplace: "test",
- version: "1",
- installPath: plugin,
- installedAt: "2026-01-01",
- scope: "user",
- },
- { id: "broken" },
- ],
- }),
- );
- const loader = createMcpConfigurationLoader({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- });
- expect(Object.keys((await accept(loader)).configuration.servers)).toEqual([
- "plugin:example:good",
- ]);
- // 有效的空声明必须收敛,不能继续回退到旧插件目录。
- await writeFile(
- join(plugin, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "example", mcpServers: {} }),
- );
- expect((await accept(loader)).configuration.servers).toEqual({});
- } finally {
- await rm(root, { recursive: true, force: true });
- }
- });
- it("does not fail config loading when an optional filesystem workspace is missing", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-missing-workspace-"));
- const config = join(root, "config.json");
- try {
- await writeFile(
- config,
- JSON.stringify({
- mcp: {
- servers: {
- filesystem: { command: "npx", args: ["@modelcontextprotocol/server-filesystem", root] },
- other: { command: "node" },
- },
- },
- }),
- );
- const loader = createMcpConfigurationLoader({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: join(root, "missing"),
- });
- const { configuration } = await accept(loader);
- expect(Object.keys(configuration.servers)).toEqual(["filesystem", "other"]);
- expect(configuration.servers.filesystem).toMatchObject({
- args: ["@modelcontextprotocol/server-filesystem", root],
- });
- } finally {
- await rm(root, { recursive: true, force: true });
- }
- });
- it("publishes the final value of consecutive atomic replacements", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-consecutive-replace-"));
- const config = join(root, "config.json");
- await writeFile(config, "{}");
- const source = await createDynamicMcpConfigSource({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: join(root, "plugins"),
- pluginRoots: [],
- workingDirectory: root,
- });
- try {
- for (const command of ["first", "second", "third"]) {
- const observed = Promise.withResolvers<void>();
- const unsubscribe = source.subscribe(() => {
- if (
- source.getSnapshot().servers.demo?.type === "stdio" &&
- source.getSnapshot().servers.demo.command === command
- )
- observed.resolve();
- });
- await writeFile(`${config}.tmp`, JSON.stringify({ mcp: { servers: { demo: { command } } } }));
- await rename(`${config}.tmp`, config);
- try {
- await observed.promise;
- } finally {
- unsubscribe();
- }
- }
- } finally {
- source.close();
- await rm(root, { recursive: true, force: true });
- }
- }, 5000);
- it("reads only the committed plugin generation during an atomic activation", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-atomic-plugin-"));
- const plugin = join(root, "plugin");
- const authority = join(root, "installed_plugins.json");
- const config = join(root, "config.json");
- let activation: Awaited<ReturnType<typeof activateDirectoryAtomically>> | undefined;
- try {
- const manifest = (command: string) =>
- JSON.stringify({ name: "example", mcpServers: { demo: { command } } });
- await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
- await writeFile(join(plugin, ".zcode-plugin", "plugin.json"), manifest("old"));
- const record = {
- id: "example@test",
- name: "example",
- marketplace: "test",
- version: "1",
- installPath: plugin,
- installedAt: "2026-01-01",
- scope: "user",
- };
- await writeFile(authority, JSON.stringify({ version: 1, plugins: [record] }));
- await writeFile(
- config,
- JSON.stringify({ plugins: { enabledPlugins: { "example@test": true } } }),
- );
- const loader = createMcpConfigurationLoader({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- });
- const stable = (await accept(loader)).configuration;
- activation = await activateDirectoryAtomically({
- targetPath: plugin,
- authorityPath: authority,
- prepare: async (path) => {
- await mkdir(join(path, ".zcode-plugin"), { recursive: true });
- await writeFile(join(path, ".zcode-plugin", "plugin.json"), manifest("new"));
- },
- });
- expect((await accept(loader)).configuration).toEqual(stable);
- const cold = createMcpConfigurationLoader({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- });
- expect((await cold.load()).configuration.servers).toEqual({});
- await writeFile(
- authority,
- JSON.stringify({
- version: 1,
- plugins: [{ ...record, cacheTransactionId: activation.transactionId }],
- }),
- );
- expect((await accept(loader)).configuration.servers["plugin:example:demo"]).toMatchObject({
- command: "new",
- env: expect.objectContaining({ ZCODE_PLUGIN_ROOT: plugin }),
- });
- } finally {
- await activation?.finalize();
- await rm(root, { recursive: true, force: true });
- }
- });
- it.each(["directory", "isolation"])("prepares each plugin independently (%s)", async (mode) => {
- const root = await mkdtemp(join(tmpdir(), "mcp-plugin-preparation-"));
- const config = join(root, "config.json");
- const plugin = join(root, "plugin");
- const manifest = join(plugin, ".zcode-plugin", "plugin.json");
- try {
- await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
- await writeFile(
- manifest,
- JSON.stringify({
- name: "example",
- mcpServers: { demo: { command: "node", cwd: "${ZCODE_PLUGIN_DATA}" } },
- }),
- );
- await writeFile(
- config,
- JSON.stringify({
- plugins: { dirs: [plugin] },
- mcp: { servers: { old: { command: "node" } } },
- }),
- );
- const loader = createMcpConfigurationLoader({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: join(root, "plugins"),
- pluginRoots: [],
- workingDirectory: root,
- });
- const initial = await accept(loader);
- const published = initial.configuration.servers["plugin:example:demo"];
- expect(published?.type).toBe("stdio");
- if (published?.type !== "stdio") throw new Error("missing plugin server");
- if (mode === "directory") {
- expect(
- await stat(published.cwd!)
- .then((s) => s.isDirectory())
- .catch(() => false),
- ).toBe(true);
- return;
- }
- await writeFile(
- manifest,
- JSON.stringify({
- name: "example",
- userConfig: { broken: null },
- mcpServers: { demo: { command: "node" } },
- }),
- );
- await writeFile(
- config,
- JSON.stringify({
- plugins: { dirs: [plugin] },
- mcp: { servers: { fresh: { command: "node" } } },
- }),
- );
- expect(Object.keys((await accept(loader)).configuration.servers)).toEqual(["fresh"]);
- } finally {
- await rm(root, { recursive: true, force: true });
- }
- });
- it("holds stable execution paths during standalone activation and observes its commit marker", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-plugin-commit-"));
- const config = join(root, "config.json");
- const plugin = join(root, "plugin");
- const manifest = (command: string) =>
- JSON.stringify({ name: "example", mcpServers: { demo: { command } } });
- let activation: Awaited<ReturnType<typeof activateDirectoryAtomically>> | undefined;
- let source: Awaited<ReturnType<typeof createDynamicMcpConfigSource>> | undefined;
- try {
- await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
- await writeFile(join(plugin, ".zcode-plugin", "plugin.json"), manifest("old"));
- const settings = { plugins: { dirs: [plugin] }, mcp: { servers: {} } };
- await writeFile(config, JSON.stringify(settings));
- source = await createDynamicMcpConfigSource({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- });
- const stable = source.getSnapshot().servers["plugin:example:demo"];
- activation = await activateDirectoryAtomically({
- targetPath: plugin,
- prepare: async (path) => {
- await mkdir(join(path, ".zcode-plugin"), { recursive: true });
- await writeFile(join(path, ".zcode-plugin", "plugin.json"), manifest("new"));
- },
- });
- const unrelated = Promise.withResolvers<void>();
- source.subscribe(() => {
- if (source!.getSnapshot().servers.unrelated) unrelated.resolve();
- });
- await writeFile(
- config,
- JSON.stringify({ ...settings, mcp: { servers: { unrelated: { command: "node" } } } }),
- );
- await unrelated.promise;
- expect(source.getSnapshot().servers["plugin:example:demo"]).toEqual(stable);
- const committed = Promise.withResolvers<void>();
- source.subscribe(() => {
- const server = source!.getSnapshot().servers["plugin:example:demo"];
- if (server?.type === "stdio" && server.command === "new") committed.resolve();
- });
- await activation.finalize();
- activation = undefined;
- await committed.promise;
- expect(source.getSnapshot().servers["plugin:example:demo"]).toMatchObject({
- env: { ZCODE_PLUGIN_ROOT: plugin },
- });
- } finally {
- source?.close();
- await activation?.rollback();
- await rm(root, { recursive: true, force: true });
- }
- }, 5000);
- it("does not retain a disabled plugin during activation and restores only the rolled-back generation", async () => {
- const root = await mkdtemp(join(tmpdir(), "mcp-plugin-rollback-"));
- const config = join(root, "config.json");
- const plugin = join(root, "plugin");
- let activation: Awaited<ReturnType<typeof activateDirectoryAtomically>> | undefined;
- try {
- await mkdir(join(plugin, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(plugin, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "example", mcpServers: { demo: { command: "old" } } }),
- );
- const settings = { plugins: { dirs: [plugin] } };
- await writeFile(config, JSON.stringify(settings));
- const loader = createMcpConfigurationLoader({
- configPaths: [config],
- userConfigPath: config,
- pluginStorageRoot: root,
- pluginRoots: [],
- workingDirectory: root,
- });
- const stable = (await accept(loader)).configuration;
- activation = await activateDirectoryAtomically({
- targetPath: plugin,
- prepare: async (path) => {
- await mkdir(join(path, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(path, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "example", mcpServers: { demo: { command: "new" } } }),
- );
- },
- });
- await writeFile(config, JSON.stringify({ plugins: { ...settings.plugins, enabled: false } }));
- expect((await accept(loader)).configuration.servers).toEqual({});
- await writeFile(config, JSON.stringify(settings));
- expect((await accept(loader)).configuration.servers).toEqual({});
- await activation.rollback();
- activation = undefined;
- expect((await accept(loader)).configuration).toEqual(stable);
- } finally {
- await activation?.rollback();
- await rm(root, { recursive: true, force: true });
- }
- });
|