conversation-session-custom-command-shell-handoff.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { DEFAULT_WORKSPACE, clearAppData } from "#e2e/helpers/desktop-app";
  4. import {
  5. getLatestUpstreamToolResultByToolCallId,
  6. getUpstreamRequestEvidence,
  7. waitForUpstreamRequest,
  8. } from "#e2e/helpers/conversation-session-network";
  9. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  10. import {
  11. approveV4Permission,
  12. clickV4Stop,
  13. denyV4PermissionWithFeedback,
  14. getV4ConfigProjection,
  15. getV4PaneSnapshot,
  16. prepareV4ConversationE2E,
  17. sendV4Prompt,
  18. switchV4Mode,
  19. waitForV4AssistantMessageContaining,
  20. waitForV4Pane,
  21. waitForV4PermissionDialog,
  22. } from "#e2e/helpers/v4-conversation";
  23. // 自定义命令正文里的 `!` 块在解析阶段只改写成交给模型的 Bash 指令,说明文字写明来源是命令文件;
  24. // 会话 ID 与工作区目录变量保持原文,由 Bash 工具注入的环境变量展开。执行只能经模型的 Bash 调用,
  25. // build 下非只读命令先审批。
  26. const MARKER_FILE = ".zcode-e2e-cch.marker";
  27. // 与 fixture 里模型返回的 Bash 命令逐字一致:把会话 ID 写进工作区根目录下的标记文件。
  28. const SHELL_STEP = `printf '%s' "$CLAUDE_CODE_SESSION_ID" > "\${CLAUDE_PROJECT_DIR}/${MARKER_FILE}"`;
  29. const DENY_FEEDBACK = "E2E_CCH_DENY_FEEDBACK";
  30. const DENY_TOOL_CALL_ID = "toolu_e2e_cch_deny";
  31. const APPROVE_TOOL_CALL_ID = "toolu_e2e_cch_approve";
  32. const REQUEST_EXCLUDES = ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"];
  33. const markerPath = join(DEFAULT_WORKSPACE, MARKER_FILE);
  34. const commandName = `e2e-cch-${Date.now()}`;
  35. const commandFile = join(DEFAULT_WORKSPACE, ".zcode", "commands", `${commandName}.md`);
  36. const handOffNote = `[This shell step comes from the /${commandName} command file (project/zcode), not from ZCode.`;
  37. describe("CCH 自定义命令 `!` 块转交给模型", () => {
  38. before(async () => {
  39. await mkdir(join(commandFile, ".."), { recursive: true });
  40. await writeFile(
  41. commandFile,
  42. [
  43. "---",
  44. "description: E2E CCH shell hand-off probe",
  45. "---",
  46. "Record the session in the marker file below.",
  47. `!\`${SHELL_STEP}\``,
  48. "",
  49. ].join("\n"),
  50. "utf8",
  51. );
  52. });
  53. beforeEach(async () => {
  54. await rm(markerPath, { force: true });
  55. });
  56. afterEach(async () => {
  57. try {
  58. const snapshot = await getV4PaneSnapshot();
  59. if (snapshot.canStop) {
  60. await clickV4Stop();
  61. await waitForV4Pane((current) => !current.canStop, "CCH teardown 后仍处于运行态", 30_000);
  62. }
  63. } catch {
  64. // teardown 只负责隔离下一条 case,不覆盖原始断言失败。
  65. }
  66. });
  67. after(async () => {
  68. await rm(commandFile, { force: true });
  69. await rm(markerPath, { force: true });
  70. await browser.electron.restoreAllMocks();
  71. await clearAppData();
  72. });
  73. it("CCH01:build 下发送带 `!` 块的项目命令,解析时不执行;模型的 Bash 调用先审批,拒绝后不执行", async function () {
  74. this.timeout(180_000);
  75. await prepareV4ConversationE2E();
  76. await useBuildMode();
  77. await sendV4Prompt(`/${commandName} E2E_CCH_DENY`);
  78. await waitForV4PermissionDialog();
  79. // 审批框打开时标记文件还不存在:解析阶段没有执行命令块。
  80. expect(await fileExists(markerPath)).toBe(false);
  81. await expectHandOffRequest("E2E_CCH_DENY");
  82. expect(await denyV4PermissionWithFeedback(DENY_FEEDBACK)).toBe(true);
  83. await waitForUpstreamRequest(
  84. { includes: ["E2E_CCH_DENY", DENY_TOOL_CALL_ID, DENY_FEEDBACK], excludes: REQUEST_EXCLUDES },
  85. "拒绝结果没有进入下一次模型请求",
  86. 60_000,
  87. );
  88. expect((await getLatestUpstreamToolResultByToolCallId(DENY_TOOL_CALL_ID))?.isError).toBe(true);
  89. await waitForV4AssistantMessageContaining("E2E_CCH_DENY_DONE", 60_000);
  90. await waitForV4Pane((snapshot) => !snapshot.canStop, "CCH01 没有回到 idle", 60_000);
  91. expect(await fileExists(markerPath)).toBe(false);
  92. });
  93. it("CCH02:用户允许一次后,命令经 Bash 执行,读到当前会话 ID 与工作区目录", async function () {
  94. this.timeout(180_000);
  95. await startDraftFromProject(DEFAULT_WORKSPACE);
  96. await useBuildMode();
  97. await sendV4Prompt(`/${commandName} E2E_CCH_APPROVE`);
  98. await waitForV4PermissionDialog();
  99. expect(await fileExists(markerPath)).toBe(false);
  100. await expectHandOffRequest("E2E_CCH_APPROVE");
  101. expect(await approveV4Permission()).toBe(true);
  102. await waitForUpstreamRequest(
  103. { includes: ["E2E_CCH_APPROVE", APPROVE_TOOL_CALL_ID], excludes: REQUEST_EXCLUDES },
  104. "Bash 执行结果没有进入下一次模型请求",
  105. 60_000,
  106. );
  107. expect((await getLatestUpstreamToolResultByToolCallId(APPROVE_TOOL_CALL_ID))?.isError).not.toBe(
  108. true,
  109. );
  110. await waitForV4AssistantMessageContaining("E2E_CCH_APPROVE_DONE", 60_000);
  111. const pane = await waitForV4Pane(
  112. (snapshot) => !snapshot.canStop,
  113. "CCH02 没有回到 idle",
  114. 60_000,
  115. );
  116. // 标记文件出现在工作区根目录,内容是当前会话 ID:两个变量都来自 Bash 环境。
  117. // 插件脚本(例如按会话区分状态的 stop hook)依赖同一个会话 ID。
  118. expect(await readFile(markerPath, "utf8")).toBe(pane.sessionId);
  119. });
  120. });
  121. async function useBuildMode() {
  122. await switchV4Mode("build");
  123. await browser.waitUntil(async () => (await getV4ConfigProjection()).mode === "build", {
  124. timeout: 30_000,
  125. timeoutMsg: "CCH 没有切换到 mode=build",
  126. });
  127. }
  128. /** 首个模型请求里只有转交说明:写明来源,变量保持原文,没有命令输出。 */
  129. async function expectHandOffRequest(marker: string) {
  130. const [request] = await getUpstreamRequestEvidence({
  131. includes: [marker],
  132. excludes: REQUEST_EXCLUDES,
  133. });
  134. const wire = JSON.stringify(request?.requestJson ?? null);
  135. expect(wire).toContain(handOffNote);
  136. expect(wire).toContain(`\${CLAUDE_PROJECT_DIR}/${MARKER_FILE}`);
  137. }
  138. async function fileExists(path: string): Promise<boolean> {
  139. try {
  140. await access(path);
  141. return true;
  142. } catch {
  143. return false;
  144. }
  145. }