| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- import {
- mkdirSync,
- mkdtempSync,
- readFileSync,
- rmSync,
- statSync,
- writeFileSync,
- } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import type { ICommandsService } from "../src/commands/commands.js";
- const originalHome = process.env.HOME;
- const tempRoots: string[] = [];
- function makeTempRoot(): string {
- const root = mkdtempSync(join(tmpdir(), "zcode-commands-"));
- tempRoots.push(root);
- return root;
- }
- function officialPluginRoot(
- home: string,
- pluginName: string,
- version = "0.1.0",
- ): string {
- return join(
- home,
- ".zcode",
- "cli",
- "plugins",
- "cache",
- "zcode-plugins-official",
- pluginName,
- version,
- );
- }
- function marketplacePluginRoot(
- home: string,
- marketplace: string,
- pluginName: string,
- version = "0.0.0",
- ): string {
- return join(
- home,
- ".zcode",
- "cli",
- "plugins",
- "cache",
- marketplace,
- pluginName,
- version,
- );
- }
- function writePluginManifest(
- pluginRoot: string,
- manifest: Record<string, unknown>,
- ): void {
- mkdirSync(join(pluginRoot, ".zcode-plugin"), { recursive: true });
- writeFileSync(
- join(pluginRoot, ".zcode-plugin", "plugin.json"),
- JSON.stringify(manifest, null, 2),
- "utf-8",
- );
- }
- function writeClaudePluginManifest(
- pluginRoot: string,
- manifest: Record<string, unknown>,
- ): void {
- mkdirSync(join(pluginRoot, ".claude-plugin"), { recursive: true });
- writeFileSync(
- join(pluginRoot, ".claude-plugin", "plugin.json"),
- JSON.stringify(manifest, null, 2),
- "utf-8",
- );
- }
- function writeInstalledPluginRecord(
- home: string,
- record: {
- marketplace: string;
- name: string;
- installPath: string;
- version?: string;
- },
- ): void {
- const pluginsRoot = join(home, ".zcode", "cli", "plugins");
- mkdirSync(pluginsRoot, { recursive: true });
- writeFileSync(
- join(pluginsRoot, "installed_plugins.json"),
- JSON.stringify(
- {
- version: 1,
- plugins: [
- {
- id: `${record.name}@${record.marketplace}`,
- name: record.name,
- marketplace: record.marketplace,
- version: record.version ?? "0.0.0",
- installPath: record.installPath,
- installedAt: "2026-06-25T00:00:00.000Z",
- updatedAt: "2026-06-25T00:00:00.000Z",
- scope: "user",
- source: "./plugins/demo",
- },
- ],
- },
- null,
- 2,
- ),
- "utf-8",
- );
- }
- function writeCliConfig(home: string, config: Record<string, unknown>): void {
- const configPath = join(home, ".zcode", "cli", "config.json");
- mkdirSync(join(home, ".zcode", "cli"), { recursive: true });
- writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
- }
- async function createCommandsServiceInEnv(
- home: string,
- ): Promise<ICommandsService> {
- process.env.HOME = home;
- vi.resetModules();
- const mod = await import("../src/commands/commandsService.js");
- return mod.createCommandsService({ isDesktopRuntime: true });
- }
- afterEach(() => {
- process.env.HOME = originalHome;
- vi.resetModules();
- while (tempRoots.length > 0) {
- const root = tempRoots.pop();
- if (root) {
- rmSync(root, { recursive: true, force: true });
- }
- }
- });
- describe("commandsService", () => {
- it("lists user commands and workspace project commands", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- mkdirSync(join(home, ".zcode", "commands"), { recursive: true });
- mkdirSync(join(workspacePath, ".zcode", "commands", "ops"), {
- recursive: true,
- });
- writeFileSync(
- join(home, ".zcode", "commands", "global.md"),
- "---\ndescription: Global command\n---\n\nRun globally",
- "utf-8",
- );
- writeFileSync(
- join(workspacePath, ".zcode", "commands", "ops", "deploy.md"),
- "---\ndescription: Deploy command\nargument-hint: <env>\n---\n\nDeploy project",
- "utf-8",
- );
- const service = await createCommandsServiceInEnv(home);
- const result = await service.list({
- workspacePath,
- workspaceIdentity: "remote:ssh:host:/workspace",
- });
- expect(result.userCommands).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- agentSource: "zcodeAgent",
- id: "zcodeAgent:zcode:global:/global",
- name: "/global",
- scope: "global",
- description: "Global command",
- location: expect.objectContaining({
- source: "zcode",
- scope: "user",
- directoryPath: join(home, ".zcode", "commands"),
- }),
- }),
- expect.objectContaining({
- agentSource: "zcodeAgent",
- id: "zcodeAgent:zcode:project:/ops/deploy",
- name: "/ops/deploy",
- scope: "project",
- projectPath: workspacePath,
- argumentHint: "<env>",
- location: expect.objectContaining({
- source: "zcode",
- scope: "project",
- directoryPath: join(workspacePath, ".zcode", "commands"),
- }),
- }),
- ]),
- );
- expect(result.commands.map((command) => command.name)).toEqual(
- expect.arrayContaining(["/global", "/ops/deploy"]),
- );
- });
- it("returns project commands even when the user commands directory is missing", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- mkdirSync(join(workspacePath, ".zcode", "commands"), { recursive: true });
- mkdirSync(join(workspacePath, ".agents", "commands"), { recursive: true });
- writeFileSync(
- join(workspacePath, ".zcode", "commands", "project-only.md"),
- "Project command",
- "utf-8",
- );
- const service = await createCommandsServiceInEnv(home);
- const result = await service.list({ workspacePath });
- expect(result.userCommands).toEqual([
- expect.objectContaining({
- agentSource: "zcodeAgent",
- id: "zcodeAgent:zcode:project:/project-only",
- name: "/project-only",
- scope: "project",
- location: expect.objectContaining({
- source: "zcode",
- scope: "project",
- directoryPath: join(workspacePath, ".zcode", "commands"),
- }),
- }),
- ]);
- });
- it("reads .zcode command directories first and skips .agents/.claude when zcode has commands", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- mkdirSync(join(home, ".zcode", "commands"), { recursive: true });
- mkdirSync(join(home, ".agents", "commands"), { recursive: true });
- mkdirSync(join(home, ".claude", "commands"), { recursive: true });
- mkdirSync(join(workspacePath, ".zcode", "commands"), { recursive: true });
- mkdirSync(join(workspacePath, ".agents", "commands"), { recursive: true });
- mkdirSync(join(workspacePath, ".claude", "commands"), { recursive: true });
- writeFileSync(
- join(home, ".zcode", "commands", "review.md"),
- "ZCode review",
- "utf-8",
- );
- writeFileSync(
- join(home, ".agents", "commands", "agent.md"),
- "Agents command",
- "utf-8",
- );
- writeFileSync(
- join(home, ".claude", "commands", "review.md"),
- "Claude review",
- "utf-8",
- );
- writeFileSync(
- join(home, ".claude", "commands", "claude.md"),
- "Claude command",
- "utf-8",
- );
- writeFileSync(
- join(workspacePath, ".agents", "commands", "agent.md"),
- "Workspace agents command",
- "utf-8",
- );
- writeFileSync(
- join(workspacePath, ".claude", "commands", "claude-project.md"),
- "Workspace claude command",
- "utf-8",
- );
- writeFileSync(
- join(workspacePath, ".zcode", "commands", "project.md"),
- "ZCode project",
- "utf-8",
- );
- const service = await createCommandsServiceInEnv(home);
- const all = await service.list({ workspacePath });
- const zcodeOnly = await service.list({
- agentSource: "zcodeAgent",
- workspacePath,
- });
- expect(all.userCommands.map((command) => command.id)).toEqual([
- "zcodeAgent:zcode:project:/project",
- "zcodeAgent:zcode:global:/review",
- ]);
- expect(zcodeOnly.userCommands.map((command) => command.id)).toEqual([
- "zcodeAgent:zcode:project:/project",
- "zcodeAgent:zcode:global:/review",
- ]);
- });
- it("writes and opens command files for the selected agent source", async () => {
- const home = makeTempRoot();
- const service = await createCommandsServiceInEnv(home);
- const created = await service.writeCommandFile({
- agentSource: "zcodeAgent",
- config: {
- name: "ship-it",
- prompt: "Ship it",
- description: "Ship command",
- },
- });
- const directory = await service.getPrimaryUserCommandsDirectory({
- agentSource: "zcodeAgent",
- });
- expect(directory.path).toBe(join(home, ".zcode", "commands"));
- expect(created.command).toMatchObject({
- agentSource: "zcodeAgent",
- filePath: join(home, ".zcode", "commands", "ship-it.md"),
- id: "zcodeAgent:zcode:global:/ship-it",
- name: "/ship-it",
- location: expect.objectContaining({
- source: "zcode",
- scope: "user",
- directoryPath: join(home, ".zcode", "commands"),
- }),
- });
- expect(readFileSync(created.command.filePath, "utf-8")).toContain(
- "Ship it",
- );
- });
- it("creates the target commands directory before returning it", async () => {
- const home = makeTempRoot();
- const service = await createCommandsServiceInEnv(home);
- const directory = await service.getPrimaryUserCommandsDirectory();
- expect(directory.path).toBe(join(home, ".zcode", "commands"));
- expect(statSync(directory.path).isDirectory()).toBe(true);
- });
- it("skips .agents/commands when .zcode/commands has any valid command", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- mkdirSync(join(workspacePath, ".zcode", "commands"), { recursive: true });
- mkdirSync(join(workspacePath, ".agents", "commands"), { recursive: true });
- // 同一 scope 中 .zcode 读到命令后,.agents 不再参与,即便包含不同命令名。
- writeFileSync(
- join(workspacePath, ".zcode", "commands", "dup.md"),
- "---\ndescription: from zcode\n---\n\nzcode body",
- "utf-8",
- );
- writeFileSync(
- join(workspacePath, ".agents", "commands", "dup.md"),
- "---\ndescription: from agents\n---\n\nagents body",
- "utf-8",
- );
- writeFileSync(
- join(workspacePath, ".agents", "commands", "agents-only.md"),
- "---\ndescription: agents only\n---\n\nagents only body",
- "utf-8",
- );
- const service = await createCommandsServiceInEnv(home);
- const result = await service.list({ workspacePath });
- const dup = result.userCommands.filter(
- (command) => command.name === "/dup",
- );
- expect(dup).toHaveLength(1);
- expect(dup[0]?.description).toBe("from zcode");
- expect(result.userCommands.map((command) => command.name)).not.toContain(
- "/agents-only",
- );
- });
- it("falls back to .agents/commands when .zcode/commands has no valid command", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- mkdirSync(join(workspacePath, ".zcode", "commands"), { recursive: true });
- mkdirSync(join(workspacePath, ".agents", "commands"), { recursive: true });
- writeFileSync(
- join(workspacePath, ".agents", "commands", "agents-only.md"),
- "---\ndescription: agents only\n---\n\nagents only body",
- "utf-8",
- );
- const service = await createCommandsServiceInEnv(home);
- const result = await service.list({ workspacePath });
- expect(result.userCommands).toEqual([
- expect.objectContaining({
- id: "zcodeAgent:agents:project:/agents-only",
- name: "/agents-only",
- description: "agents only",
- location: expect.objectContaining({
- source: "agents",
- scope: "project",
- directoryPath: join(workspacePath, ".agents", "commands"),
- }),
- }),
- ]);
- });
- it("lists enabled plugin commands separately from user commands", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- const pluginRoot = officialPluginRoot(home, "ios-simulator");
- mkdirSync(join(home, ".zcode", "commands"), { recursive: true });
- mkdirSync(join(pluginRoot, "commands", "ios"), { recursive: true });
- writeFileSync(
- join(home, ".zcode", "commands", "local-review.md"),
- "---\ndescription: Local review\n---\n\nReview locally",
- "utf-8",
- );
- writePluginManifest(pluginRoot, {
- commands: "commands",
- name: "ios-simulator",
- version: "0.1.0",
- });
- writeFileSync(
- join(pluginRoot, "commands", "ios", "dev.md"),
- "---\ndescription: iOS plugin command\nargument-hint: <target>\n---\n\nRun iOS flow",
- "utf-8",
- );
- writeCliConfig(home, {
- plugins: {
- enabledPlugins: {
- "ios-simulator@zcode-plugins-official": true,
- },
- },
- });
- const service = await createCommandsServiceInEnv(home);
- const result = await service.list({ workspacePath });
- expect(result.userCommands.map((command) => command.name)).toEqual([
- "/local-review",
- ]);
- expect(result.pluginCommands).toEqual([
- expect.objectContaining({
- argumentHint: "<target>",
- description: "iOS plugin command",
- enabled: true,
- name: "/ios/dev",
- pluginEnabled: true,
- pluginMarketplace: "zcode-plugins-official",
- pluginName: "ios-simulator",
- source: "plugin",
- }),
- ]);
- expect(result.commands.map((command) => command.name)).toEqual([
- "/local-review",
- "/ios/dev",
- ]);
- });
- it("suppressed 内置插件不贡献任何命令", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- const documentPluginRoot = officialPluginRoot(home, "document-skills");
- const skillCreatorPluginRoot = officialPluginRoot(home, "skill-creator");
- mkdirSync(join(documentPluginRoot, "commands", "doc"), { recursive: true });
- mkdirSync(join(skillCreatorPluginRoot, "commands", "create"), {
- recursive: true,
- });
- writePluginManifest(documentPluginRoot, {
- commands: "commands",
- name: "document-skills",
- version: "0.1.0",
- });
- writePluginManifest(skillCreatorPluginRoot, {
- commands: "commands",
- name: "skill-creator",
- version: "0.1.0",
- });
- writeFileSync(
- join(documentPluginRoot, "commands", "doc", "convert.md"),
- "---\ndescription: Convert doc\n---\n\nConvert document",
- "utf-8",
- );
- writeFileSync(
- join(skillCreatorPluginRoot, "commands", "create", "new.md"),
- "---\ndescription: Create skill\n---\n\nCreate a skill",
- "utf-8",
- );
- // document-skills 被卸载(suppressed),skill-creator 保持默认启用。
- writeCliConfig(home, {
- plugins: {
- suppressedBuiltins: ["document-skills@zcode-plugins-official"],
- },
- });
- const service = await createCommandsServiceInEnv(home);
- const result = await service.list({ workspacePath });
- expect(result.pluginCommands.map((command) => command.name)).toEqual([
- "/create/new",
- ]);
- expect(
- result.pluginCommands.find(
- (command) => command.pluginName === "document-skills",
- ),
- ).toBeUndefined();
- });
- it("lists commands from installed marketplace plugins", async () => {
- const home = makeTempRoot();
- const workspacePath = join(makeTempRoot(), "workspace");
- const pluginRoot = marketplacePluginRoot(
- home,
- "claude-plugins-official",
- "commit-commands",
- );
- mkdirSync(join(pluginRoot, "commands", "git"), { recursive: true });
- writeClaudePluginManifest(pluginRoot, {
- commands: "commands",
- name: "commit-commands",
- version: "1.0.0",
- });
- writeFileSync(
- join(pluginRoot, "commands", "git", "commit.md"),
- "---\ndescription: Commit through plugin\nargument-hint: <message>\n---\n\nCommit changes",
- "utf-8",
- );
- writeInstalledPluginRecord(home, {
- installPath: pluginRoot,
- marketplace: "claude-plugins-official",
- name: "commit-commands",
- });
- writeCliConfig(home, {
- plugins: {
- enabledPlugins: {
- "commit-commands@claude-plugins-official": true,
- },
- },
- });
- const service = await createCommandsServiceInEnv(home);
- const result = await service.list({ workspacePath });
- expect(result.pluginCommands).toEqual([
- expect.objectContaining({
- argumentHint: "<message>",
- description: "Commit through plugin",
- enabled: true,
- name: "/git/commit",
- pluginEnabled: true,
- pluginMarketplace: "claude-plugins-official",
- pluginName: "commit-commands",
- source: "plugin",
- }),
- ]);
- });
- it("stores command disabled overrides in the user zcode config", async () => {
- const home = makeTempRoot();
- const service = await createCommandsServiceInEnv(home);
- const created = await service.writeCommandFile({
- config: {
- name: "review",
- prompt: "Review code",
- },
- });
- await service.setCommandEnabled({
- commandId: created.command.id,
- filePath: created.command.filePath,
- enabled: false,
- });
- const saved = JSON.parse(
- readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
- );
- expect(saved.command[created.command.filePath]).toEqual({ enable: false });
- const disabled = await service.list({});
- expect(disabled.userCommands[0]).toEqual(
- expect.objectContaining({ enabled: false }),
- );
- await service.setCommandEnabled({
- commandId: created.command.id,
- filePath: created.command.filePath,
- enabled: true,
- });
- const enabled = await service.list({});
- const reenabledConfig = JSON.parse(
- readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
- ) as { command?: Record<string, { enable?: boolean }> };
- expect(enabled.userCommands[0]).toEqual(
- expect.objectContaining({ enabled: true }),
- );
- expect(reenabledConfig.command?.[created.command.filePath]).toBeUndefined();
- });
- });
|