protocol-slash-commands.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 type { ExecutionPort, ExecutionResult } from "@zcode/contracts";
  6. import { resolveZCodeBuiltinPromptCommand } from "../src/builtin-prompt-command.js";
  7. import { resolveZCodeCustomCommandPrompt } from "../src/custom-command-prompt.js";
  8. describe("protocol slash command surface", () => {
  9. it("expands prompt custom commands without taking over reserved builtins", async () => {
  10. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-custom-command-prompt-"));
  11. const workspace = join(tempRoot, "workspace");
  12. const home = join(tempRoot, "home");
  13. const userCommands = join(home, ".zcode", "commands");
  14. try {
  15. await mkdir(workspace, { recursive: true });
  16. await mkdir(userCommands, { recursive: true });
  17. await writeCommand(userCommands, "review", "Review changes", "Review $ARGUMENTS.");
  18. await writeCommand(userCommands, "init", "Custom init", "Should not expand.");
  19. await writeCommand(userCommands, "model", "Reserved model", "Should not expand.");
  20. await writeCommand(userCommands, "plan", "Reserved Plan action", "Should not expand.");
  21. const options = {
  22. env: { ZCODE_ENV: "test" },
  23. homeDirectory: home,
  24. skipUserConfig: true,
  25. workingDirectory: workspace,
  26. };
  27. const prompt = await resolveZCodeCustomCommandPrompt("/review src packages", options);
  28. expect(prompt).toContain("Run custom command /review.");
  29. expect(prompt).toContain("Command source: user/zcode.");
  30. expect(prompt).toContain("Review src packages.");
  31. await expect(resolveZCodeCustomCommandPrompt("/model glm-5", options)).resolves.toBe(
  32. undefined,
  33. );
  34. await expect(resolveZCodeCustomCommandPrompt("/plan task", options)).resolves.toBe(undefined);
  35. expect(resolveZCodeBuiltinPromptCommand("/plan task", { workingDirectory: workspace })).toBe(
  36. undefined,
  37. );
  38. await expect(resolveZCodeCustomCommandPrompt("/init", options)).resolves.toBe(undefined);
  39. await expect(resolveZCodeCustomCommandPrompt("please /review", options)).resolves.toBe(
  40. undefined,
  41. );
  42. } finally {
  43. await rm(tempRoot, { force: true, recursive: true });
  44. }
  45. });
  46. // DWG-03:灰度关闭时 `/workflow` 不得展开。目录侧已经剃掉了它,但用户仍可手打命令名,
  47. // 两条路径必须给出同一个结论;同时 `workflow` 是保留名,自定义命令解析对它一律不展开,
  48. // 用户或插件的同名命令既顶替不了内置语义,也绕不过灰度门。
  49. it("expands builtin /workflow through the builtin resolver only, gated by the feature flag", async () => {
  50. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-builtin-workflow-"));
  51. const workspace = join(tempRoot, "workspace");
  52. const home = join(tempRoot, "home");
  53. const userCommands = join(home, ".zcode", "commands");
  54. try {
  55. await mkdir(workspace, { recursive: true });
  56. await mkdir(userCommands, { recursive: true });
  57. await writeCommand(userCommands, "workflow", "Shadowing workflow", "Should not expand.");
  58. const options = {
  59. env: { ZCODE_ENV: "test" },
  60. homeDirectory: home,
  61. skipUserConfig: true,
  62. workingDirectory: workspace,
  63. };
  64. const expanded = resolveZCodeBuiltinPromptCommand("/workflow ship it", {
  65. dynamicWorkflowEnabled: true,
  66. workingDirectory: workspace,
  67. });
  68. expect(expanded).toContain("Required skills: `dynamic-workflows`.");
  69. expect(expanded).toContain("ship it");
  70. expect(expanded).not.toContain("Should not expand.");
  71. // 缺席(TUI / headless)等于开启;显式 false 才挡下。
  72. expect(
  73. resolveZCodeBuiltinPromptCommand("/workflow ship it", { workingDirectory: workspace }),
  74. ).toContain("ship it");
  75. expect(
  76. resolveZCodeBuiltinPromptCommand("/workflow ship it", {
  77. dynamicWorkflowEnabled: false,
  78. workingDirectory: workspace,
  79. }),
  80. ).toBe(undefined);
  81. await expect(resolveZCodeCustomCommandPrompt("/workflow ship it", options)).resolves.toBe(
  82. undefined,
  83. );
  84. } finally {
  85. await rm(tempRoot, { force: true, recursive: true });
  86. }
  87. });
  88. it("expands builtin /init into the workspace AGENTS.md prompt", async () => {
  89. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-builtin-init-"));
  90. const workspace = join(tempRoot, "workspace");
  91. try {
  92. await mkdir(workspace, { recursive: true });
  93. const prompt = resolveZCodeBuiltinPromptCommand("/init include pnpm scripts", {
  94. workingDirectory: workspace,
  95. });
  96. expect(prompt).toContain("built-in /init command");
  97. expect(prompt).toContain("AGENTS.md");
  98. expect(prompt).toContain(join(workspace, "AGENTS.md"));
  99. expect(prompt).toContain(join(workspace, ".zcode", "AGENTS.md"));
  100. expect(prompt).toContain(join(workspace, ".agents", "AGENTS.md"));
  101. expect(prompt).toContain("they already have an instructions file");
  102. expect(prompt).not.toContain("Do not create CLAUDE.md or AGNENTS.md");
  103. expect(prompt).toContain("include pnpm scripts");
  104. expect(resolveZCodeBuiltinPromptCommand("/review", { workingDirectory: workspace })).toBe(
  105. undefined,
  106. );
  107. expect(
  108. resolveZCodeBuiltinPromptCommand("please /init", { workingDirectory: workspace }),
  109. ).toBe(undefined);
  110. } finally {
  111. await rm(tempRoot, { force: true, recursive: true });
  112. }
  113. });
  114. it("fails shell-expanded custom commands that request unavailable contexts", async () => {
  115. const tempRoot = await mkdtemp(join(tmpdir(), "zcode-command-shell-missing-context-"));
  116. const workspace = join(tempRoot, "workspace");
  117. const home = join(tempRoot, "home");
  118. const userCommands = join(home, ".zcode", "commands");
  119. let executionCalled = false;
  120. const executionPort: ExecutionPort = {
  121. async run() {
  122. executionCalled = true;
  123. return createExecutionResult("should not run");
  124. },
  125. };
  126. try {
  127. await mkdir(userCommands, { recursive: true });
  128. await writeCommand(
  129. userCommands,
  130. "skill-context",
  131. "Needs skill context",
  132. ["```!", 'echo "${CLAUDE_SKILL_DIR}"', "```"].join("\n"),
  133. );
  134. await writeCommand(
  135. userCommands,
  136. "session-context",
  137. "Needs session context",
  138. ["```!", 'echo "${CLAUDE_SESSION_ID}"', "```"].join("\n"),
  139. );
  140. const options = {
  141. env: { ZCODE_ENV: "test" },
  142. executionPort,
  143. homeDirectory: home,
  144. skipUserConfig: true,
  145. workingDirectory: workspace,
  146. };
  147. await expect(resolveZCodeCustomCommandPrompt("/skill-context", options)).rejects.toThrow(
  148. "Custom command /skill-context variable requires a skill context: CLAUDE_SKILL_DIR",
  149. );
  150. await expect(resolveZCodeCustomCommandPrompt("/session-context", options)).rejects.toThrow(
  151. "Custom command /session-context variable requires a runtime session context: CLAUDE_SESSION_ID",
  152. );
  153. expect(executionCalled).toBe(false);
  154. } finally {
  155. await rm(tempRoot, { force: true, recursive: true });
  156. }
  157. });
  158. });
  159. async function writeCommand(
  160. directory: string,
  161. name: string,
  162. description: string,
  163. body: string,
  164. extraFrontmatter: string[] = [],
  165. ): Promise<void> {
  166. await writeFile(
  167. join(directory, `${name}.md`),
  168. ["---", `description: ${description}`, ...extraFrontmatter, "---", "", body].join("\n"),
  169. );
  170. }
  171. function createExecutionResult(stdout: string): ExecutionResult {
  172. const now = new Date();
  173. return {
  174. status: "completed",
  175. exitCode: 0,
  176. stdout: { text: stdout, bytes: stdout.length, truncated: false },
  177. stderr: { text: "", bytes: 0, truncated: false },
  178. durationMs: 1,
  179. timedOut: false,
  180. cancelled: false,
  181. startedAt: now,
  182. completedAt: now,
  183. };
  184. }