conversation-session-plan-directory-order.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { TID_CHAT_SUMMARY_PANEL, TID_CHAT_TOOL_CALL_BLOCK, testId } from "@zcode/shared/test-ids";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import {
  4. expandAssistantHistoriesWithContent,
  5. waitForToolCallBlockContaining,
  6. } from "#e2e/helpers/conversation-session-tool";
  7. import {
  8. prepareV4ConversationE2E,
  9. sendV4Prompt,
  10. waitForV4AssistantMessageContaining,
  11. waitForV4UserMessageContaining,
  12. } from "#e2e/helpers/v4-conversation";
  13. import {
  14. getLatestUpstreamToolResultByToolCallId,
  15. waitForUpstreamRequest,
  16. } from "#e2e/helpers/conversation-session-network";
  17. const PROMPT_MARKER = "E2E_PLAN_DIRECTORY_ORDER";
  18. const ENTER_MARKER = "E2E_PLAN_DIRECTORY_ENTER";
  19. const EXIT_MARKER = "E2E_PLAN_DIRECTORY_EXIT";
  20. const PLAN_TITLE = "E2E_PLAN_DIRECTORY_TITLE";
  21. const CONTINUATION_MARKER = "E2E_PLAN_DIRECTORY_CONTINUATION";
  22. describe("conversation session plan directory and original tool order", () => {
  23. after(async () => {
  24. await browser.electron.restoreAllMocks();
  25. await clearAppData();
  26. });
  27. it("keeps a completed plan at its tool row and opens it from the summary directory", async function () {
  28. this.timeout(120_000);
  29. await prepareV4ConversationE2E();
  30. const prompt = [
  31. `${PROMPT_MARKER}: create a plan and request approval.`,
  32. `${ENTER_MARKER}: call EnterPlanMode first.`,
  33. `${EXIT_MARKER}: call ExitPlanMode with a Markdown H1 exactly named ${PLAN_TITLE}.`,
  34. `After approval, continue this same turn with the exact text ${CONTINUATION_MARKER}.`,
  35. ].join(" ");
  36. await sendV4Prompt(prompt);
  37. await waitForV4UserMessageContaining(PROMPT_MARKER);
  38. await waitForUpstreamRequest(
  39. {
  40. includes: [PROMPT_MARKER, ENTER_MARKER, EXIT_MARKER],
  41. excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"],
  42. },
  43. "plan directory initial request",
  44. 60_000,
  45. );
  46. // V4 隐藏内部 EnterPlanMode 行;真实工具结果已回到下一次模型请求才证明模式已进入。
  47. await browser.waitUntil(
  48. async () =>
  49. (await getLatestUpstreamToolResultByToolCallId("toolu_e2e_plan_directory_enter_plan_mode"))
  50. ?.isError === false,
  51. { timeout: 60_000, timeoutMsg: "EnterPlanMode 没有产生成功的真实工具结果" },
  52. );
  53. await waitForUpstreamRequest(
  54. {
  55. includes: [EXIT_MARKER],
  56. excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"],
  57. },
  58. "plan directory ExitPlanMode request",
  59. 60_000,
  60. );
  61. const planBlock = await waitForToolCallBlockContaining(PLAN_TITLE, 60_000);
  62. const toolCallId = planBlock.toolCallId;
  63. if (!toolCallId) {
  64. throw new Error(`ExitPlanMode block 缺少 toolCallId: ${JSON.stringify(planBlock)}`);
  65. }
  66. expect(await hasPlanDirectoryEntry(toolCallId)).toBe(false);
  67. await waitForPlanApprovalDialog(60_000);
  68. await approvePlanWithoutFeedback();
  69. await waitForV4AssistantMessageContaining(CONTINUATION_MARKER);
  70. await expandAssistantHistoriesWithContent();
  71. await waitForPlanBeforeContinuation(toolCallId, CONTINUATION_MARKER, 30_000);
  72. await openPlanDirectoryEntry(toolCallId, 30_000);
  73. const detail = await $(`[data-plan-detail-tool-call-id="${toolCallId}"]`);
  74. await detail.waitForDisplayed({ timeout: 30_000 });
  75. expect(await detail.getText()).toContain(PLAN_TITLE);
  76. });
  77. });
  78. async function hasPlanDirectoryEntry(toolCallId: string): Promise<boolean> {
  79. return browser.execute(
  80. (id) => document.querySelector(`[data-plan-directory-tool-call-id="${id}"]`) !== null,
  81. toolCallId,
  82. );
  83. }
  84. async function waitForPlanApprovalDialog(timeoutMs: number) {
  85. await browser.waitUntil(
  86. async () => {
  87. const body = await $("[data-elicitation-dialog-body='true']");
  88. const footer = await $("[data-elicitation-dialog-footer='true']");
  89. if (!(await body.isExisting()) || !(await footer.isExisting())) return false;
  90. const text = `${await body.getText()}\n${await footer.getText()}`;
  91. return ["Approve", "批准"].some((candidate) => text.includes(candidate));
  92. },
  93. {
  94. timeout: timeoutMs,
  95. timeoutMsg: "没有出现 ExitPlanMode 计划审批弹窗",
  96. },
  97. );
  98. }
  99. async function approvePlanWithoutFeedback() {
  100. const approveButton = await $("//button[contains(., 'Approve') or contains(., '批准')]");
  101. await approveButton.waitForEnabled({ timeout: 30_000 });
  102. await approveButton.click();
  103. }
  104. async function waitForPlanBeforeContinuation(
  105. toolCallId: string,
  106. continuation: string,
  107. timeoutMs: number,
  108. ) {
  109. const planTestId = testId(TID_CHAT_TOOL_CALL_BLOCK, toolCallId);
  110. await browser.waitUntil(
  111. () =>
  112. browser.execute(
  113. (toolTestId, text) => {
  114. const plan = document.querySelector(`[data-testid="${toolTestId}"]`);
  115. const continuationMessage = Array.from(
  116. document.querySelectorAll<HTMLElement>(
  117. '[data-testid="v4-session-pane-workspace-main"] [data-row-id][class~="group/assistant-row"]',
  118. ),
  119. ).find((element) => element.innerText.includes(text));
  120. if (!plan || !continuationMessage) return false;
  121. return Boolean(
  122. plan.compareDocumentPosition(continuationMessage) & Node.DOCUMENT_POSITION_FOLLOWING,
  123. );
  124. },
  125. planTestId,
  126. continuation,
  127. ),
  128. {
  129. timeout: timeoutMs,
  130. timeoutMsg: "ExitPlanMode 计划卡片没有保持在后续 assistant 正文之前",
  131. },
  132. );
  133. }
  134. async function openPlanDirectoryEntry(toolCallId: string, timeoutMs: number) {
  135. const panel = await $(`[data-testid="${TID_CHAT_SUMMARY_PANEL}"]`);
  136. await panel.waitForDisplayed({ timeout: timeoutMs });
  137. // auto 布局在窄列中仍显示胶囊,data-state=expanded 不代表目录项可见。
  138. // 点击真实的展开入口,不能通过 DOM click 绕过隐藏目录的交互条件。
  139. const expand = await panel.$('button[aria-label="展开状态"], button[aria-label="Expand status"]');
  140. if (await expand.isDisplayed()) await expand.click();
  141. const entry = await $(`[data-plan-directory-tool-call-id="${toolCallId}"]`);
  142. await entry.waitForClickable({ timeout: timeoutMs });
  143. await entry.click();
  144. }