| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694 |
- import { existsSync, mkdtempSync, symlinkSync } from "node:fs";
- import { chmod, mkdir, readFile, realpath, stat, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join, resolve } from "node:path";
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
- import { createPluginSyncService } from "../src/plugin-sync/pluginSyncService.js";
- function makeHome() {
- return mkdtempSync(join(tmpdir(), "zcode-plugin-sync-home-"));
- }
- async function writeZcodeUserConfig(
- home: string,
- value: Record<string, unknown>,
- ): Promise<string> {
- const configPath = join(home, ".zcode", "cli", "config.json");
- await mkdir(join(home, ".zcode", "cli"), { recursive: true });
- await writeFile(configPath, `${JSON.stringify(value, null, 2)}\n`, "utf-8");
- return configPath;
- }
- async function readZcodeUserConfig(home: string): Promise<Record<string, unknown>> {
- const raw = await readFile(join(home, ".zcode", "cli", "config.json"), "utf-8");
- return JSON.parse(raw) as Record<string, unknown>;
- }
- async function writeInlinePlugin(
- root: string,
- directoryName: string,
- manifest: Record<string, unknown> = {},
- ): Promise<string> {
- const pluginRoot = join(root, directoryName);
- await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(pluginRoot, ".zcode-plugin", "plugin.json"),
- `${JSON.stringify({
- name: directoryName,
- description: `${directoryName} description`,
- version: "1.0.0",
- ...manifest,
- }, null, 2)}\n`,
- "utf-8",
- );
- await mkdir(join(pluginRoot, "skills", "demo"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "demo", "SKILL.md"), "---\nname: demo\n---\n");
- return pluginRoot;
- }
- async function readJsonFile(filePath: string): Promise<Record<string, unknown>> {
- return JSON.parse(await readFile(filePath, "utf-8")) as Record<string, unknown>;
- }
- describe("plugin sync service", () => {
- let originalHome: string | undefined;
- beforeEach(() => {
- originalHome = process.env.HOME;
- });
- afterEach(() => {
- process.env.HOME = originalHome;
- });
- it("lists user inline plugins from zcode plugins.dirs as local candidates", async () => {
- const home = makeHome();
- process.env.HOME = home;
- const pluginRoot = await writeInlinePlugin(join(home, "external"), "remote-tools", {
- name: "remote-tools",
- mcpServers: { docs: { command: "node", args: ["server.js"] } },
- commands: ["commands"],
- hooks: [{ event: "PreToolUse", command: "echo hook" }],
- });
- await writeZcodeUserConfig(home, {
- plugins: {
- dirs: [pluginRoot],
- enabledPlugins: {
- "remote-tools@inline": false,
- },
- },
- });
- const service = createPluginSyncService();
- const result = await service.listLocalUserPluginCandidates();
- const canonicalPluginRoot = await realpath(pluginRoot);
- expect(result.candidates).toMatchObject([
- {
- name: "remote-tools",
- pluginId: "remote-tools@inline",
- directoryName: "remote-tools",
- description: "remote-tools description",
- version: "1.0.0",
- path: canonicalPluginRoot,
- enabled: false,
- componentTypes: expect.arrayContaining(["commands", "hooks", "mcp", "skills"]),
- },
- ]);
- expect(result.maxArchiveBytes).toBeGreaterThan(0);
- });
- it("checks remote plugin and config directory write access before sync", async () => {
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const service = createPluginSyncService();
- const result = await service.checkRemoteUserPluginWriteAccess();
- expect(result).toMatchObject({
- ok: true,
- path: [
- join(remoteHome, ".zcode", "plugins"),
- join(remoteHome, ".zcode", "cli"),
- ].join(", "),
- });
- });
- it("reports remote plugin write preflight failure when plugin root is not a directory", async () => {
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- await mkdir(join(remoteHome, ".zcode"), { recursive: true });
- await writeFile(join(remoteHome, ".zcode", "plugins"), "not a directory");
- const service = createPluginSyncService();
- const result = await service.checkRemoteUserPluginWriteAccess();
- expect(result.ok).toBe(false);
- expect(result.path).toBe(join(remoteHome, ".zcode", "plugins"));
- expect(result.error).toBeTruthy();
- });
- it("does not list marketplace installed plugin cache records as V1 candidates", async () => {
- const home = makeHome();
- process.env.HOME = home;
- const cachePluginRoot = await writeInlinePlugin(
- join(home, ".zcode", "cli", "plugins", "cache", "market", "cached", "1.0.0"),
- "cached",
- { name: "cached" },
- );
- await mkdir(join(home, ".zcode", "cli", "plugins"), { recursive: true });
- await writeFile(
- join(home, ".zcode", "cli", "plugins", "installed_plugins.json"),
- `${JSON.stringify({
- plugins: [
- {
- id: "cached@market",
- name: "cached",
- marketplace: "market",
- version: "1.0.0",
- installPath: cachePluginRoot,
- },
- ],
- })}\n`,
- "utf-8",
- );
- const service = createPluginSyncService();
- const result = await service.listLocalUserPluginCandidates();
- expect(result.candidates).toEqual([]);
- });
- it("imports selected inline plugins into the remote user plugin root and registers plugins.dirs", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
- name: "remote-tools",
- });
- await writeZcodeUserConfig(localHome, {
- plugins: {
- dirs: [pluginRoot],
- enabledPlugins: {
- "remote-tools@inline": false,
- },
- },
- });
- const localService = createPluginSyncService();
- const candidates = await localService.listLocalUserPluginCandidates();
- const exported = await localService.exportPluginsArchive({
- pluginIds: candidates.candidates.map((candidate) => candidate.id),
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const remoteService = createPluginSyncService();
- const result = await remoteService.importPluginsArchive({
- archive: exported.archive,
- overwrite: false,
- });
- expect(result.results).toMatchObject([
- {
- name: "remote-tools",
- pluginId: "remote-tools@inline",
- directoryName: "remote-tools",
- status: "synced",
- },
- ]);
- const remotePluginRoot = join(remoteHome, ".zcode", "plugins", "remote-tools");
- expect(existsSync(join(remotePluginRoot, ".zcode-plugin", "plugin.json"))).toBe(true);
- const remoteConfig = await readZcodeUserConfig(remoteHome);
- expect(remoteConfig).toMatchObject({
- plugins: {
- dirs: [resolve(remotePluginRoot)],
- enabledPlugins: {
- "remote-tools@inline": false,
- },
- },
- });
- });
- it("preserves executable file permissions when importing inline plugins", async () => {
- if (process.platform === "win32") {
- return;
- }
- const localHome = makeHome();
- process.env.HOME = localHome;
- const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
- name: "remote-tools",
- commands: ["commands"],
- });
- await mkdir(join(pluginRoot, "commands"), { recursive: true });
- const localCommandPath = join(pluginRoot, "commands", "run.sh");
- await writeFile(localCommandPath, "#!/bin/sh\necho ok\n", "utf-8");
- await chmod(localCommandPath, 0o755);
- await writeZcodeUserConfig(localHome, { plugins: { dirs: [pluginRoot] } });
- const localService = createPluginSyncService();
- const candidates = await localService.listLocalUserPluginCandidates();
- const exported = await localService.exportPluginsArchive({
- pluginIds: candidates.candidates.map((candidate) => candidate.id),
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const remoteService = createPluginSyncService();
- await remoteService.importPluginsArchive({
- archive: exported.archive,
- overwrite: false,
- });
- const remoteCommandPath = join(
- remoteHome,
- ".zcode",
- "plugins",
- "remote-tools",
- "commands",
- "run.sh",
- );
- expect((await stat(remoteCommandPath)).mode & 0o777).toBe(0o755);
- });
- it("rolls back an inline plugin directory when config registration fails", async () => {
- if (process.platform === "win32") {
- return;
- }
- const localHome = makeHome();
- process.env.HOME = localHome;
- const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
- name: "remote-tools",
- });
- await writeZcodeUserConfig(localHome, { plugins: { dirs: [pluginRoot] } });
- const localService = createPluginSyncService();
- const candidates = await localService.listLocalUserPluginCandidates();
- const exported = await localService.exportPluginsArchive({
- pluginIds: candidates.candidates.map((candidate) => candidate.id),
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- await writeZcodeUserConfig(remoteHome, {});
- const remoteConfigDir = join(remoteHome, ".zcode", "cli");
- const remotePluginRoot = join(remoteHome, ".zcode", "plugins", "remote-tools");
- const remoteService = createPluginSyncService();
- await chmod(remoteConfigDir, 0o555);
- const failed = await remoteService.importPluginsArchive({
- archive: exported.archive,
- overwrite: false,
- });
- await chmod(remoteConfigDir, 0o755);
- expect(failed.results).toMatchObject([
- {
- pluginId: "remote-tools@inline",
- status: "failed",
- },
- ]);
- expect(existsSync(remotePluginRoot)).toBe(false);
- const retried = await remoteService.importPluginsArchive({
- archive: exported.archive,
- overwrite: false,
- });
- expect(retried.results).toMatchObject([
- {
- pluginId: "remote-tools@inline",
- status: "synced",
- path: remotePluginRoot,
- },
- ]);
- const remoteConfig = await readZcodeUserConfig(remoteHome);
- expect(remoteConfig).toMatchObject({
- plugins: {
- dirs: [resolve(remotePluginRoot)],
- },
- });
- });
- it("skips remote import when the same plugin id already exists in remote plugins.dirs", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
- name: "remote-tools",
- });
- await writeZcodeUserConfig(localHome, { plugins: { dirs: [pluginRoot] } });
- const localService = createPluginSyncService();
- const candidates = await localService.listLocalUserPluginCandidates();
- const exported = await localService.exportPluginsArchive({
- pluginIds: candidates.candidates.map((candidate) => candidate.id),
- });
- const remoteHome = makeHome();
- const existingRemoteRoot = await writeInlinePlugin(
- join(remoteHome, "existing"),
- "different-dir",
- { name: "remote-tools" },
- );
- await writeZcodeUserConfig(remoteHome, { plugins: { dirs: [existingRemoteRoot] } });
- process.env.HOME = remoteHome;
- const remoteService = createPluginSyncService();
- const result = await remoteService.importPluginsArchive({
- archive: exported.archive,
- overwrite: false,
- });
- expect(result.results).toMatchObject([
- {
- pluginId: "remote-tools@inline",
- status: "skipped",
- },
- ]);
- const remoteConfig = await readZcodeUserConfig(remoteHome);
- expect(remoteConfig).toMatchObject({ plugins: { dirs: [existingRemoteRoot] } });
- });
- it("reports remote statuses by target directory and plugin id", async () => {
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const existingRemoteRoot = await writeInlinePlugin(join(remoteHome, "existing"), "known", {
- name: "known",
- });
- await writeInlinePlugin(join(remoteHome, ".zcode", "plugins"), "known", {
- name: "other-known",
- });
- await writeZcodeUserConfig(remoteHome, { plugins: { dirs: [existingRemoteRoot] } });
- const service = createPluginSyncService();
- const result = await service.listRemoteUserPluginStatuses({
- plugins: [
- { pluginId: "known@inline", directoryName: "missing-target" },
- { pluginId: "new@inline", directoryName: "known" },
- { pluginId: "new@inline", directoryName: "new" },
- ],
- });
- expect(result.statuses).toMatchObject([
- { pluginId: "known@inline", directoryName: "missing-target", exists: true },
- { pluginId: "new@inline", directoryName: "known", exists: true },
- { pluginId: "new@inline", directoryName: "new", exists: false },
- ]);
- });
- it("rejects plugin archives containing symlinks", async () => {
- if (process.platform === "win32") {
- return;
- }
- const home = makeHome();
- process.env.HOME = home;
- const pluginRoot = await writeInlinePlugin(join(home, "external"), "remote-tools", {
- name: "remote-tools",
- });
- await writeFile(join(home, "secret.txt"), "secret");
- symlinkSync(join(home, "secret.txt"), join(pluginRoot, "secret-link.txt"));
- await writeZcodeUserConfig(home, { plugins: { dirs: [pluginRoot] } });
- const service = createPluginSyncService();
- const candidates = await service.listLocalUserPluginCandidates();
- await expect(
- service.exportPluginsArchive({
- pluginIds: candidates.candidates.map((candidate) => candidate.id),
- }),
- ).rejects.toThrow("unsupported plugin archive source");
- });
- it("exports and imports a mirrored directory marketplace source", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const marketplaceRoot = join(localHome, "team-market");
- await writeInlinePlugin(marketplaceRoot, "formatter", { name: "formatter" });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- `${JSON.stringify({
- name: "team-market",
- plugins: [
- {
- name: "formatter",
- version: "1.0.0",
- source: "./formatter",
- },
- ],
- }, null, 2)}\n`,
- "utf-8",
- );
- const localService = createPluginSyncService();
- const exported = await localService.exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: { source: "directory", path: marketplaceRoot },
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const remoteService = createPluginSyncService();
- const imported = await remoteService.importMarketplaceSourceArchive({
- archive: exported.archive,
- overwrite: false,
- });
- expect(imported).toMatchObject({
- marketplaceId: "team-market",
- status: "synced",
- });
- expect(imported.path.startsWith(join(remoteHome, ".zcode", "plugins", "marketplace-sources"))).toBe(true);
- const mirroredManifest = await readJsonFile(join(imported.path, "marketplace.json"));
- expect(mirroredManifest).toMatchObject({
- name: "team-market",
- metadata: {
- pluginRoot: "plugins",
- },
- plugins: [
- {
- name: "formatter",
- source: "./formatter",
- },
- ],
- });
- expect(
- existsSync(join(imported.path, "plugins", "formatter", ".zcode-plugin", "plugin.json")),
- ).toBe(true);
- });
- it("rejects absolute marketplace plugin source paths outside the marketplace root", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const marketplaceRoot = join(localHome, "team-market");
- const externalPluginRoot = await writeInlinePlugin(join(localHome, "external"), "formatter", {
- name: "formatter",
- });
- await mkdir(marketplaceRoot, { recursive: true });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- `${JSON.stringify({
- name: "team-market",
- plugins: [
- {
- name: "formatter",
- source: externalPluginRoot,
- },
- ],
- }, null, 2)}\n`,
- "utf-8",
- );
- await expect(
- createPluginSyncService().exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: { source: "directory", path: marketplaceRoot },
- }),
- ).rejects.toThrow("unsafe marketplace plugin source path");
- });
- it("rejects mirrored marketplace plugin source directories without a plugin manifest", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const marketplaceRoot = join(localHome, "team-market");
- await mkdir(join(marketplaceRoot, "formatter"), { recursive: true });
- await writeFile(join(marketplaceRoot, "formatter", "README.md"), "not a plugin\n", "utf-8");
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- `${JSON.stringify({
- name: "team-market",
- plugins: [
- {
- name: "formatter",
- source: "./formatter",
- },
- ],
- }, null, 2)}\n`,
- "utf-8",
- );
- await expect(
- createPluginSyncService().exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: { source: "directory", path: marketplaceRoot },
- }),
- ).rejects.toThrow("local marketplace plugin source is missing plugin manifest");
- });
- it("changes mirrored marketplace source directory when local plugin contents change", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const marketplaceRoot = join(localHome, "team-market");
- const pluginRoot = await writeInlinePlugin(marketplaceRoot, "formatter", {
- name: "formatter",
- });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- `${JSON.stringify({
- name: "team-market",
- plugins: [{ name: "formatter", source: "./formatter" }],
- }, null, 2)}\n`,
- "utf-8",
- );
- const service = createPluginSyncService();
- const before = await service.exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: { source: "directory", path: marketplaceRoot },
- });
- await writeFile(join(pluginRoot, "skills", "demo", "SKILL.md"), "---\nname: demo\n---\nnew");
- const after = await service.exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: { source: "directory", path: marketplaceRoot },
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const remoteService = createPluginSyncService();
- const beforeImport = await remoteService.importMarketplaceSourceArchive({
- archive: before.archive,
- overwrite: false,
- });
- const afterImport = await remoteService.importMarketplaceSourceArchive({
- archive: after.archive,
- overwrite: false,
- });
- expect(afterImport.path).not.toBe(beforeImport.path);
- });
- it("mirrors same-marketplace dependencies with the selected marketplace plugin", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const marketplaceRoot = join(localHome, "team-market");
- await writeInlinePlugin(marketplaceRoot, "formatter", { name: "formatter" });
- await writeInlinePlugin(marketplaceRoot, "helper", { name: "helper" });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- `${JSON.stringify({
- name: "team-market",
- plugins: [
- { name: "formatter", dependencies: ["helper"], source: "./formatter" },
- { name: "helper", source: "./helper" },
- ],
- }, null, 2)}\n`,
- "utf-8",
- );
- const exported = await createPluginSyncService().exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: { source: "directory", path: marketplaceRoot },
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const imported = await createPluginSyncService().importMarketplaceSourceArchive({
- archive: exported.archive,
- overwrite: false,
- });
- const mirroredManifest = await readJsonFile(join(imported.path, "marketplace.json"));
- expect(exported.pluginNames).toEqual(["formatter", "helper"]);
- expect(mirroredManifest.plugins).toMatchObject([
- { name: "formatter", dependencies: ["helper"], source: "./formatter" },
- { name: "helper", source: "./helper" },
- ]);
- expect(existsSync(join(imported.path, "plugins", "helper"))).toBe(true);
- });
- it("exports a file marketplace source with plugin paths relative to the marketplace file", async () => {
- const localHome = makeHome();
- process.env.HOME = localHome;
- const marketplaceRoot = join(localHome, "team-market");
- await writeInlinePlugin(marketplaceRoot, "formatter", { name: "formatter" });
- const marketplaceFile = join(marketplaceRoot, "marketplace.json");
- await writeFile(
- marketplaceFile,
- `${JSON.stringify({
- name: "team-market",
- plugins: [{ name: "formatter", source: "./formatter" }],
- }, null, 2)}\n`,
- "utf-8",
- );
- const exported = await createPluginSyncService().exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: { source: "file", path: marketplaceFile },
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const imported = await createPluginSyncService().importMarketplaceSourceArchive({
- archive: exported.archive,
- overwrite: false,
- });
- expect(existsSync(join(imported.path, "plugins", "formatter"))).toBe(true);
- });
- it("exports a settings marketplace source without local cache fallback when entries are remote", async () => {
- const home = makeHome();
- process.env.HOME = home;
- const service = createPluginSyncService();
- const exported = await service.exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: {
- source: "settings",
- marketplace: {
- name: "team-market",
- plugins: [
- {
- name: "formatter",
- version: "1.0.0",
- source: { source: "github", repo: "example/formatter-plugin" },
- },
- ],
- },
- },
- });
- const remoteHome = makeHome();
- process.env.HOME = remoteHome;
- const imported = await createPluginSyncService().importMarketplaceSourceArchive({
- archive: exported.archive,
- overwrite: false,
- });
- const mirroredManifest = await readJsonFile(join(imported.path, "marketplace.json"));
- expect(mirroredManifest).toMatchObject({
- name: "team-market",
- plugins: [
- {
- name: "formatter",
- source: { source: "github", repo: "example/formatter-plugin" },
- },
- ],
- });
- expect(existsSync(join(imported.path, "plugins"))).toBe(false);
- });
- it("rejects settings marketplace entries whose local source root cannot be reconstructed", async () => {
- const home = makeHome();
- process.env.HOME = home;
- const service = createPluginSyncService();
- await expect(
- service.exportMarketplaceSourceArchive({
- marketplaceId: "team-market",
- pluginNames: ["formatter"],
- source: {
- source: "settings",
- marketplace: {
- name: "team-market",
- plugins: [
- {
- name: "formatter",
- source: "./formatter",
- },
- ],
- },
- },
- }),
- ).rejects.toThrow("cannot mirror local marketplace plugin source");
- });
- });
|