protocol-slash-commands.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { mkdir, mkdtemp, rm, 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 { resolveZCodeBuiltinPromptCommand } from "../src/builtin-prompt-command.js";
  6. import { resolveZCodeCustomCommandPrompt } from "../src/custom-command-prompt.js";
  7. describe("protocol slash command surface", () => {
  8. it("expands prompt custom commands without taking over reserved builtins", async () => {
  9. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-custom-command-prompt-"));
  10. const workspace = join(tempRoot, "workspace");
  11. const home = join(tempRoot, "home");
  12. const userCommands = join(home, ".zcode", "commands");
  13. try {
  14. await mkdir(workspace, { recursive: true });
  15. await mkdir(userCommands, { recursive: true });
  16. await writeCommand(userCommands, "review", "Review changes", "Review $ARGUMENTS.");
  17. await writeCommand(userCommands, "init", "Custom init", "Should not expand.");
  18. await writeCommand(userCommands, "model", "Reserved model", "Should not expand.");
  19. await writeCommand(userCommands, "plan", "Reserved Plan action", "Should not expand.");
  20. const options = {
  21. env: { ZCODE_ENV: "test" },
  22. homeDirectory: home,
  23. skipUserConfig: true,
  24. workingDirectory: workspace,
  25. };
  26. const prompt = await resolveZCodeCustomCommandPrompt("/review src packages", options);
  27. expect(prompt).toContain("Run custom command /review.");
  28. expect(prompt).toContain("Command source: user/zcode.");
  29. expect(prompt).toContain("Review src packages.");
  30. await expect(resolveZCodeCustomCommandPrompt("/model glm-5", options)).resolves.toBe(
  31. undefined,
  32. );
  33. await expect(resolveZCodeCustomCommandPrompt("/plan task", options)).resolves.toBe(undefined);
  34. expect(resolveZCodeBuiltinPromptCommand("/plan task", { workingDirectory: workspace })).toBe(
  35. undefined,
  36. );
  37. await expect(resolveZCodeCustomCommandPrompt("/init", options)).resolves.toBe(undefined);
  38. await expect(resolveZCodeCustomCommandPrompt("please /review", options)).resolves.toBe(
  39. undefined,
  40. );
  41. } finally {
  42. await rm(tempRoot, { force: true, recursive: true });
  43. }
  44. });
  45. // DWG-03:灰度关闭时 `/workflow` 不得展开。目录侧已经剃掉了它,但用户仍可手打命令名,
  46. // 两条路径必须给出同一个结论;同时 `workflow` 是保留名,自定义命令解析对它一律不展开,
  47. // 用户或插件的同名命令既顶替不了内置语义,也绕不过灰度门。
  48. it("expands builtin /workflow through the builtin resolver only, gated by the feature flag", async () => {
  49. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-builtin-workflow-"));
  50. const workspace = join(tempRoot, "workspace");
  51. const home = join(tempRoot, "home");
  52. const userCommands = join(home, ".zcode", "commands");
  53. try {
  54. await mkdir(workspace, { recursive: true });
  55. await mkdir(userCommands, { recursive: true });
  56. await writeCommand(userCommands, "workflow", "Shadowing workflow", "Should not expand.");
  57. const options = {
  58. env: { ZCODE_ENV: "test" },
  59. homeDirectory: home,
  60. skipUserConfig: true,
  61. workingDirectory: workspace,
  62. };
  63. const expanded = resolveZCodeBuiltinPromptCommand("/workflow ship it", {
  64. dynamicWorkflowEnabled: true,
  65. workingDirectory: workspace,
  66. });
  67. expect(expanded).toContain("Required skills: `dynamic-workflows`.");
  68. expect(expanded).toContain("ship it");
  69. expect(expanded).not.toContain("Should not expand.");
  70. // 缺席(TUI / headless)等于开启;显式 false 才挡下。
  71. expect(
  72. resolveZCodeBuiltinPromptCommand("/workflow ship it", { workingDirectory: workspace }),
  73. ).toContain("ship it");
  74. expect(
  75. resolveZCodeBuiltinPromptCommand("/workflow ship it", {
  76. dynamicWorkflowEnabled: false,
  77. workingDirectory: workspace,
  78. }),
  79. ).toBe(undefined);
  80. await expect(resolveZCodeCustomCommandPrompt("/workflow ship it", options)).resolves.toBe(
  81. undefined,
  82. );
  83. } finally {
  84. await rm(tempRoot, { force: true, recursive: true });
  85. }
  86. });
  87. it("expands builtin /init into the workspace AGENTS.md prompt", async () => {
  88. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-builtin-init-"));
  89. const workspace = join(tempRoot, "workspace");
  90. try {
  91. await mkdir(workspace, { recursive: true });
  92. const prompt = resolveZCodeBuiltinPromptCommand("/init include pnpm scripts", {
  93. workingDirectory: workspace,
  94. });
  95. expect(prompt).toContain("built-in /init command");
  96. expect(prompt).toContain("AGENTS.md");
  97. expect(prompt).toContain(join(workspace, "AGENTS.md"));
  98. expect(prompt).toContain(join(workspace, ".zcode", "AGENTS.md"));
  99. expect(prompt).toContain(join(workspace, ".agents", "AGENTS.md"));
  100. expect(prompt).toContain("they already have an instructions file");
  101. expect(prompt).not.toContain("Do not create CLAUDE.md or AGNENTS.md");
  102. expect(prompt).toContain("include pnpm scripts");
  103. expect(resolveZCodeBuiltinPromptCommand("/review", { workingDirectory: workspace })).toBe(
  104. undefined,
  105. );
  106. expect(
  107. resolveZCodeBuiltinPromptCommand("please /init", { workingDirectory: workspace }),
  108. ).toBe(undefined);
  109. } finally {
  110. await rm(tempRoot, { force: true, recursive: true });
  111. }
  112. });
  113. it("hands plugin shell blocks to the model with the plugin root resolved", async () => {
  114. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-plugin-command-shell-"));
  115. const workspace = join(tempRoot, "workspace");
  116. const pluginRoot = join(tempRoot, "ralph-loop");
  117. const commandsRoot = join(pluginRoot, "commands");
  118. const pluginDataRoot = join(tempRoot, "plugins");
  119. const configPath = join(tempRoot, "config.json");
  120. try {
  121. await mkdir(commandsRoot, { recursive: true });
  122. await mkdir(join(pluginRoot, ".claude-plugin"), { recursive: true });
  123. await writeFile(
  124. join(pluginRoot, ".claude-plugin", "plugin.json"),
  125. JSON.stringify({ name: "ralph-loop", version: "1.0.0" }),
  126. );
  127. await writeCommand(
  128. commandsRoot,
  129. "ralph-loop",
  130. "Start Ralph",
  131. [
  132. "Before",
  133. "```!",
  134. 'echo "${CLAUDE_PLUGIN_ROOT}/scripts/setup-ralph-loop.sh" "$ARGUMENTS" "$CLAUDE_PROJECT_DIR"',
  135. "```",
  136. "After",
  137. ].join("\n"),
  138. );
  139. await writeFile(
  140. configPath,
  141. JSON.stringify({
  142. plugins: {
  143. dirs: [pluginRoot],
  144. enabled: true,
  145. },
  146. storage: {
  147. dir: pluginDataRoot,
  148. },
  149. }),
  150. );
  151. // 项目配置会剥离 plugins.dirs(工作区不能引入插件根目录),测试插件只能经用户配置加载;
  152. // homeDirectory 指向临时目录,不读本机用户命令。
  153. const prompt = await resolveZCodeCustomCommandPrompt("/ralph-loop build demo", {
  154. env: { ZCODE_ENV: "test" },
  155. homeDirectory: join(tempRoot, "home"),
  156. userConfigPath: configPath,
  157. workingDirectory: workspace,
  158. });
  159. expect(prompt).not.toContain("```!");
  160. expect(prompt).toContain(`the /ralph-loop command file (user/plugin), not from ZCode.`);
  161. // 命令文本自带 "/scripts/...",期望值不能用 join() 拼平台分隔符;工作区变量由 Bash 环境提供,保持原文。
  162. // Windows 上插件路径改写成正斜杠,期望值按同一规则换算;POSIX 路径不受影响。
  163. expect(prompt).toContain(
  164. `\`\`\`bash\necho "${pluginRoot.replaceAll("\\", "/")}/scripts/setup-ralph-loop.sh" "build demo" "$CLAUDE_PROJECT_DIR"\n\`\`\`\n`,
  165. );
  166. } finally {
  167. await rm(tempRoot, { force: true, recursive: true });
  168. }
  169. });
  170. });
  171. async function writeCommand(
  172. directory: string,
  173. name: string,
  174. description: string,
  175. body: string,
  176. extraFrontmatter: string[] = [],
  177. ): Promise<void> {
  178. await writeFile(
  179. join(directory, `${name}.md`),
  180. ["---", `description: ${description}`, ...extraFrontmatter, "---", "", body].join("\n"),
  181. );
  182. }