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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { 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 conversation plan card", 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 $('[data-status-capsule="plan"]').isExisting()).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 waitForPlanApprovalDialog(timeoutMs: number) {
  79. await browser.waitUntil(
  80. async () => {
  81. const body = await $("[data-elicitation-dialog-body='true']");
  82. const footer = await $("[data-elicitation-dialog-footer='true']");
  83. if (!(await body.isExisting()) || !(await footer.isExisting())) return false;
  84. const text = `${await body.getText()}\n${await footer.getText()}`;
  85. return ["Approve", "批准"].some((candidate) => text.includes(candidate));
  86. },
  87. {
  88. timeout: timeoutMs,
  89. timeoutMsg: "没有出现 ExitPlanMode 计划审批弹窗",
  90. },
  91. );
  92. }
  93. async function approvePlanWithoutFeedback() {
  94. const approveButton = await $("//button[contains(., 'Approve') or contains(., '批准')]");
  95. await approveButton.waitForEnabled({ timeout: 30_000 });
  96. await approveButton.click();
  97. }
  98. async function waitForPlanBeforeContinuation(
  99. toolCallId: string,
  100. continuation: string,
  101. timeoutMs: number,
  102. ) {
  103. const planTestId = testId(TID_CHAT_TOOL_CALL_BLOCK, toolCallId);
  104. await browser.waitUntil(
  105. () =>
  106. browser.execute(
  107. (toolTestId, text) => {
  108. const plan = document.querySelector(`[data-testid="${toolTestId}"]`);
  109. const continuationMessage = Array.from(
  110. document.querySelectorAll<HTMLElement>(
  111. '[data-testid="v4-session-pane-workspace-main"] [data-row-id][class~="group/assistant-row"]',
  112. ),
  113. ).find((element) => element.innerText.includes(text));
  114. if (!plan || !continuationMessage) return false;
  115. return Boolean(
  116. plan.compareDocumentPosition(continuationMessage) & Node.DOCUMENT_POSITION_FOLLOWING,
  117. );
  118. },
  119. planTestId,
  120. continuation,
  121. ),
  122. {
  123. timeout: timeoutMs,
  124. timeoutMsg: "ExitPlanMode 计划卡片没有保持在后续 assistant 正文之前",
  125. },
  126. );
  127. }
  128. async function openPlanDirectoryEntry(toolCallId: string, timeoutMs: number) {
  129. // Plan 胶囊已移除,直接通过对应工具卡打开,保留 toolCallId 身份校验。
  130. const block = $(`[data-testid="${testId(TID_CHAT_TOOL_CALL_BLOCK, toolCallId)}"]`);
  131. const card = block.$('section[role="button"]');
  132. await card.scrollIntoView();
  133. await card.waitForClickable({ timeout: timeoutMs });
  134. await card.click();
  135. }