commands.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { createNodeCustomCommandAdapter } from "../src/commands/index.js";
  6. describe("NodeCustomCommandAdapter", () => {
  7. it("discovers project custom commands with frontmatter", async () => {
  8. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  9. const commandDir = join(dir, ".zcode", "commands", "frontend");
  10. try {
  11. await mkdir(commandDir, { recursive: true });
  12. await writeFile(
  13. join(commandDir, "review.md"),
  14. [
  15. "---",
  16. "description: Review frontend changes",
  17. "argument-hint: [scope]",
  18. "allowed-tools: Read, Grep",
  19. "skills: frontend-review",
  20. "---",
  21. "",
  22. "Review $ARGUMENTS.",
  23. ].join("\n"),
  24. );
  25. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  26. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  27. expect(outcome.commands.map((command) => command.name)).toEqual(["frontend:review"]);
  28. expect(outcome.commands[0]?.description).toBe("Review frontend changes");
  29. expect(outcome.commands[0]?.argumentHint).toBe("[scope]");
  30. expect(outcome.commands[0]?.allowedTools).toEqual(["Read", "Grep"]);
  31. expect(outcome.commands[0]?.skills).toEqual(["frontend-review"]);
  32. } finally {
  33. await rm(dir, { force: true, recursive: true });
  34. }
  35. });
  36. it("filters out commands disabled via config (enable:false path)", async () => {
  37. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  38. const commandDir = join(dir, ".zcode", "commands");
  39. const disabledPath = join(commandDir, "disabled.md");
  40. try {
  41. await mkdir(commandDir, { recursive: true });
  42. await writeFile(
  43. join(commandDir, "kept.md"),
  44. ["---", "description: Stays available", "---", "", "Body."].join("\n"),
  45. );
  46. await writeFile(
  47. disabledPath,
  48. ["---", "description: Should be filtered", "---", "", "Body."].join("\n"),
  49. );
  50. const adapter = createNodeCustomCommandAdapter({
  51. homeDirectory: join(dir, "home"),
  52. disabledPaths: [disabledPath],
  53. });
  54. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  55. // 仅保留未被禁用的命令;load 复用 discover 也应拒绝被禁用项
  56. expect(outcome.commands.map((command) => command.name)).toEqual(["kept"]);
  57. await expect(
  58. adapter.loadCommand({ name: "disabled", workingDirectory: dir }),
  59. ).rejects.toThrow(/not found/);
  60. } finally {
  61. await rm(dir, { force: true, recursive: true });
  62. }
  63. });
  64. it("loads custom command body without frontmatter", async () => {
  65. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  66. const commandDir = join(dir, ".zcode", "commands");
  67. try {
  68. await mkdir(commandDir, { recursive: true });
  69. await writeFile(
  70. join(commandDir, "test.md"),
  71. ["---", "description: Run tests", "---", "", "Run tests for $1."].join("\n"),
  72. );
  73. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  74. const loaded = await adapter.loadCommand({ name: "test", workingDirectory: dir });
  75. expect(loaded.content).toBe("Run tests for $1.");
  76. expect(loaded.metadata.source).toBe("zcode");
  77. expect(loaded.truncated).toBe(false);
  78. } finally {
  79. await rm(dir, { force: true, recursive: true });
  80. }
  81. });
  82. it("discovers command names with underscores", async () => {
  83. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  84. const commandDir = join(dir, ".zcode", "commands");
  85. try {
  86. await mkdir(commandDir, { recursive: true });
  87. await writeCommand(commandDir, "clean_gone", "Clean gone git branches");
  88. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  89. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  90. expect(outcome.commands.map((command) => command.name)).toEqual(["clean_gone"]);
  91. expect(outcome.diagnostics).toEqual([]);
  92. } finally {
  93. await rm(dir, { force: true, recursive: true });
  94. }
  95. });
  96. it("keeps the higher-priority duplicate command", async () => {
  97. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  98. const projectDir = join(dir, ".zcode", "commands");
  99. const userDir = join(dir, "home", ".zcode", "commands");
  100. try {
  101. await mkdir(projectDir, { recursive: true });
  102. await mkdir(userDir, { recursive: true });
  103. await writeCommand(projectDir, "review", "Project review");
  104. await writeCommand(userDir, "review", "User review");
  105. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  106. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  107. expect(outcome.commands).toHaveLength(1);
  108. expect(outcome.commands[0]?.description).toBe("User review");
  109. expect(outcome.diagnostics.map((diagnostic) => diagnostic.code)).toContain(
  110. "custom_command_duplicate_name",
  111. );
  112. } finally {
  113. await rm(dir, { force: true, recursive: true });
  114. }
  115. });
  116. it("discovers commands under .agents/commands", async () => {
  117. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  118. const commandDir = join(dir, ".agents", "commands");
  119. try {
  120. await mkdir(commandDir, { recursive: true });
  121. await writeCommand(commandDir, "agents-cmd", "Lives under .agents/commands");
  122. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  123. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  124. expect(outcome.commands.map((command) => command.name)).toEqual(["agents-cmd"]);
  125. expect(outcome.commands[0]?.source).toBe("agents");
  126. } finally {
  127. await rm(dir, { force: true, recursive: true });
  128. }
  129. });
  130. it("merges .zcode/commands and .agents/commands with .zcode winning duplicates", async () => {
  131. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  132. const zcodeDir = join(dir, ".zcode", "commands");
  133. const agentsDir = join(dir, ".agents", "commands");
  134. try {
  135. await mkdir(zcodeDir, { recursive: true });
  136. await mkdir(agentsDir, { recursive: true });
  137. await writeCommand(zcodeDir, "dup", "From .zcode");
  138. await writeCommand(agentsDir, "dup", "From .agents");
  139. await writeCommand(agentsDir, "agents-only", "Agents only");
  140. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  141. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  142. expect(outcome.commands.map((command) => command.name)).toEqual(["agents-only", "dup"]);
  143. expect(outcome.commands.map((command) => command.description)).toEqual([
  144. "Agents only",
  145. "From .zcode",
  146. ]);
  147. expect(outcome.diagnostics.map((diagnostic) => diagnostic.code)).toContain(
  148. "custom_command_duplicate_name",
  149. );
  150. } finally {
  151. await rm(dir, { force: true, recursive: true });
  152. }
  153. });
  154. it("discovers commands imported as file symlinks", async () => {
  155. // 回归测试:外部 agent 导入默认 importMode="symlink",命令文件是指向源文件的软链。
  156. // 旧逻辑只检查 entry.isFile() 会漏掉 symlink(readdir 报告为 isSymbolicLink())。
  157. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  158. const sourceDir = join(dir, "source");
  159. const commandDir = join(dir, ".zcode", "commands");
  160. try {
  161. await mkdir(sourceDir, { recursive: true });
  162. await mkdir(commandDir, { recursive: true });
  163. const sourceFile = join(sourceDir, "commit.md");
  164. await writeFile(
  165. sourceFile,
  166. ["---", "description: Commit staged changes", "---", "", "Commit $ARGUMENTS."].join("\n"),
  167. );
  168. await symlink(sourceFile, join(commandDir, "commit.md"));
  169. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  170. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  171. expect(outcome.commands.map((command) => command.name)).toEqual(["commit"]);
  172. expect(outcome.commands[0]?.description).toBe("Commit staged changes");
  173. const loaded = await adapter.loadCommand({ name: "commit", workingDirectory: dir });
  174. expect(loaded.content).toBe("Commit $ARGUMENTS.");
  175. } finally {
  176. await rm(dir, { force: true, recursive: true });
  177. }
  178. });
  179. it("discovers commands inside a symlinked directory", async () => {
  180. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  181. const sourceDir = join(dir, "source-commands");
  182. const commandRoot = join(dir, ".zcode", "commands");
  183. try {
  184. await mkdir(sourceDir, { recursive: true });
  185. await mkdir(commandRoot, { recursive: true });
  186. await writeCommand(sourceDir, "deploy", "Deploy the app");
  187. // 目录级 symlink:stat 跟随链接后应识别为目录并递归扫描
  188. await symlink(sourceDir, join(commandRoot, "ops"), "dir");
  189. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  190. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  191. expect(outcome.commands.map((command) => command.name)).toEqual(["ops:deploy"]);
  192. } finally {
  193. await rm(dir, { force: true, recursive: true });
  194. }
  195. });
  196. it("skips dangling command symlinks without failing the scan", async () => {
  197. const dir = await mkdtemp(join(tmpdir(), "zcode-commands-"));
  198. const commandDir = join(dir, ".zcode", "commands");
  199. try {
  200. await mkdir(commandDir, { recursive: true });
  201. await writeCommand(commandDir, "kept", "Real command");
  202. await symlink(join(dir, "does-not-exist.md"), join(commandDir, "broken.md"));
  203. const adapter = createNodeCustomCommandAdapter({ homeDirectory: join(dir, "home") });
  204. const outcome = await adapter.discoverCommands({ workingDirectory: dir });
  205. expect(outcome.commands.map((command) => command.name)).toEqual(["kept"]);
  206. } finally {
  207. await rm(dir, { force: true, recursive: true });
  208. }
  209. });
  210. });
  211. async function writeCommand(dir: string, name: string, description: string): Promise<void> {
  212. await writeFile(
  213. join(dir, `${name}.md`),
  214. ["---", `description: ${description}`, "---", "", "# Body"].join("\n"),
  215. );
  216. }
  217. it("附加目录命令使用目录限定名并保留原 cwd 命令", async () => {
  218. const root = await mkdtemp(join(tmpdir(), "zcode-additional-commands-"));
  219. try {
  220. const main = join(root, "frontend"),
  221. extra = join(root, "backend");
  222. for (const dir of [main, extra]) {
  223. await mkdir(join(dir, ".zcode", "commands"), { recursive: true });
  224. await writeFile(
  225. join(dir, ".zcode", "commands", "review.md"),
  226. `---\ndescription: review\n---\n${dir}`,
  227. );
  228. }
  229. const adapter = createNodeCustomCommandAdapter({
  230. homeDirectory: join(root, "home"),
  231. additionalDirectories: [extra],
  232. });
  233. expect(
  234. (await adapter.discoverCommands({ workingDirectory: main })).commands
  235. .map((c) => c.name)
  236. .sort(),
  237. ).toEqual(["backend/review", "review"]);
  238. expect(
  239. (await adapter.loadCommand({ workingDirectory: main, name: "backend/review" })).content,
  240. ).toContain(extra);
  241. } finally {
  242. await rm(root, { recursive: true, force: true });
  243. }
  244. });