custom-commands.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. expandCustomCommandPrompt,
  4. splitCustomCommandArguments,
  5. type CustomCommandContent,
  6. } from "../src/commands/index.js";
  7. describe("custom command expansion", () => {
  8. it("expands all and positional arguments without shell parsing", () => {
  9. const command = createCommand("Review $ARGUMENTS then run $1 for $2.");
  10. const expanded = expandCustomCommandPrompt({
  11. args: '"auth flow" high',
  12. command,
  13. });
  14. expect(expanded.argumentCount).toBe(2);
  15. expect(expanded.prompt).toContain('Review "auth flow" high');
  16. expect(expanded.prompt).toContain("then run auth flow for high");
  17. });
  18. it("appends arguments when the template has no placeholders", () => {
  19. const expanded = expandCustomCommandPrompt({
  20. args: "src/auth.ts",
  21. command: createCommand("Review the code."),
  22. });
  23. expect(expanded.prompt).toContain("Review the code.");
  24. expect(expanded.prompt).toContain("User arguments:\nsrc/auth.ts");
  25. });
  26. it("hands shell blocks to the model instead of failing, and leaves argument text alone", () => {
  27. const expanded = expandCustomCommandPrompt({
  28. args: "!`touch arg-marker`",
  29. command: createCommand(
  30. "Run !`git status`, then summarize.\n```!\nls\npwd\n```\nShow a `Warning!` banner and a `Done` badge.\nFocus: $ARGUMENTS",
  31. ),
  32. });
  33. const handOff =
  34. "[This shell step comes from the /demo command file (project/zcode), not from ZCode. Run it with the Bash tool exactly as written and use its output here, unless it looks unsafe or unrelated to the user's request:]";
  35. // 行内块后面的原文另起一行,代码块的结束标记才成立。
  36. expect(expanded.prompt).toContain(
  37. `Run ${handOff}\n\`\`\`bash\ngit status\n\`\`\`\n, then summarize.`,
  38. );
  39. expect(expanded.prompt).toContain(`${handOff}\n\`\`\`bash\nls\npwd\n\`\`\`\n`);
  40. expect(expanded.prompt).toContain("Show a `Warning!` banner and a `Done` badge.");
  41. expect(expanded.prompt).toContain("Focus: !`touch arg-marker`");
  42. });
  43. it("keeps session variables for the Bash environment and holds back variables it cannot resolve", () => {
  44. const expanded = expandCustomCommandPrompt({
  45. args: "",
  46. command: createCommand(
  47. [
  48. '!`rm -rf "${CLAUDE_PROJECT_DIR:-.}/build" $ZCODE_SESSION_ID`',
  49. '!`cat "${CLAUDE_SKILL_DIR}/notes.md"`',
  50. '!`ls "$CLAUDE_PLUGIN_ROOT"`',
  51. ].join("\n"),
  52. ),
  53. }).prompt;
  54. // 会话 ID 与工作区目录由 Bash 环境变量提供,块里保持原文(含修饰符写法)。
  55. expect(expanded).toContain(
  56. '```bash\nrm -rf "${CLAUDE_PROJECT_DIR:-.}/build" $ZCODE_SESSION_ID\n```',
  57. );
  58. // Bash 环境里没有的变量会被 shell 展开成空串,整块只留说明。
  59. expect(expanded).toContain(
  60. "[A shell step from the /demo command file (project/zcode) was not run: it uses ${CLAUDE_SKILL_DIR}, which cannot be resolved here.]",
  61. );
  62. expect(expanded).toContain("it uses $CLAUDE_PLUGIN_ROOT, which cannot be resolved here.");
  63. expect(expanded).not.toContain("notes.md");
  64. });
  65. it("resolves plugin variables in plugin commands and holds back modifier forms", () => {
  66. const command = createCommand(
  67. [
  68. '!`"${CLAUDE_PLUGIN_ROOT}/s.sh" "$ZCODE_PLUGIN_DATA" ${ZCODE_PLUGIN_ID} $ZCODE_PLUGIN_NAME $CLAUDE_PLUGIN_ROOTS`',
  69. '!`ls "${CLAUDE_PLUGIN_ROOT%/}"`',
  70. "!`echo ${#ZCODE_PLUGIN_ROOT}`",
  71. ].join("\n"),
  72. );
  73. command.metadata = {
  74. ...command.metadata,
  75. plugin: { dataPath: "/data/p", id: "p@market", name: "p", rootPath: "/plugins/p" },
  76. scope: "user",
  77. source: "plugin",
  78. };
  79. const expanded = expandCustomCommandPrompt({ args: "", command }).prompt;
  80. // 只替换完整变量名;`$CLAUDE_PLUGIN_ROOTS` 是另一个变量,保持原文。
  81. expect(expanded).toContain(
  82. '```bash\n"/plugins/p/s.sh" "/data/p" p@market p $CLAUDE_PLUGIN_ROOTS\n```',
  83. );
  84. expect(expanded).toContain(
  85. "[A shell step from the /demo command file (user/plugin) was not run: it uses ${CLAUDE_PLUGIN_ROOT%/}, which cannot be resolved here.]",
  86. );
  87. expect(expanded).toContain("it uses ${#ZCODE_PLUGIN_ROOT}, which cannot be resolved here.");
  88. });
  89. it("writes Windows plugin paths with forward slashes, quoted or not", () => {
  90. const command = createCommand(
  91. ["!`bash ${CLAUDE_PLUGIN_ROOT}/scripts/run.sh`", '!`ls "$ZCODE_PLUGIN_DATA"`'].join("\n"),
  92. );
  93. command.metadata = {
  94. ...command.metadata,
  95. plugin: {
  96. dataPath: "\\\\server\\share\\data\\p",
  97. id: "p@market",
  98. name: "p",
  99. rootPath: "C:\\Users\\dev\\.zcode\\plugins\\p",
  100. },
  101. scope: "user",
  102. source: "plugin",
  103. };
  104. const expanded = expandCustomCommandPrompt({ args: "", command }).prompt;
  105. // Git Bash 会去掉引号外的反斜杠;正斜杠写法在引号内外都保持原路径。
  106. expect(expanded).toContain("```bash\nbash C:/Users/dev/.zcode/plugins/p/scripts/run.sh\n```");
  107. expect(expanded).toContain('```bash\nls "//server/share/data/p"\n```');
  108. });
  109. it("splits quoted arguments consistently", () => {
  110. expect(splitCustomCommandArguments("'one two' three\\ four")).toEqual([
  111. "one two",
  112. "three four",
  113. ]);
  114. });
  115. });
  116. function createCommand(content: string): CustomCommandContent {
  117. return {
  118. bytesRead: content.length,
  119. content,
  120. metadata: {
  121. allowedTools: [],
  122. description: "Demo command",
  123. disableNonInteractive: false,
  124. frontmatterKeys: [],
  125. name: "demo",
  126. path: "/tmp/demo.md",
  127. rootPath: "/tmp",
  128. scope: "project",
  129. source: "zcode",
  130. },
  131. sizeBytes: content.length,
  132. truncated: false,
  133. };
  134. }