conversation-session-v4-plan-shortcut.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // `/plan` Composer E2E:覆盖 draft 空命令、任务发送、Plan noop 组合和附件拒绝。
  2. import { clearAppData, setInputValueByTestIdDom } from "../helpers/desktop-app.js";
  3. import { TID_V4_COMPOSER_INPUT } from "@zcode/shared";
  4. import {
  5. assertVisibleV4UserMessagesNotContaining,
  6. clickV4Send,
  7. getV4ComposerAttachments,
  8. getV4ConfigProjection,
  9. getV4Messages,
  10. pasteV4ComposerImageAttachment,
  11. prepareV4ConversationE2E,
  12. sendV4Prompt,
  13. startNewV4Draft,
  14. switchV4Mode,
  15. waitForV4ComposerText,
  16. waitForV4Pane,
  17. waitForV4TimelineContaining,
  18. waitForV4UserMessageContaining,
  19. } from "../helpers/v4-conversation.js";
  20. import {
  21. countUpstreamTitleRequestsContaining,
  22. getUpstreamRequestRecordCount,
  23. waitForUpstreamRequest,
  24. } from "../helpers/conversation-session-network.js";
  25. const SEED_PROMPT = "E2E_V4_PLAN_SHORTCUT_SEED 建立已有会话";
  26. const SEED_REPLY = "V4_PLAN_SHORTCUT_SEED_OK";
  27. const TASK_ONE = "E2E_V4_PLAN_SHORTCUT_TASK_ONE 列出实现步骤";
  28. const TASK_ONE_REPLY = "V4_PLAN_SHORTCUT_TASK_ONE_OK";
  29. const TASK_TWO = "E2E_V4_PLAN_SHORTCUT_TASK_TWO 已处于 Plan 继续发送";
  30. const TASK_TWO_REPLY = "V4_PLAN_SHORTCUT_TASK_TWO_OK";
  31. async function waitForPlanMode() {
  32. await browser.waitUntil(async () => (await getV4ConfigProjection()).planEnabled, {
  33. timeout: 30000,
  34. timeoutMsg: "`/plan` 后输入框的 Plan 标记没有开启",
  35. });
  36. }
  37. async function establishBuildSession() {
  38. await prepareV4ConversationE2E();
  39. // 每个组合都从 build 起步,避免前一个 case 的 draft preference 影响当前 mode CAS 断言。
  40. await switchV4Mode("build");
  41. const titleCountBefore = await countUpstreamTitleRequestsContaining(SEED_PROMPT);
  42. await sendV4Prompt(SEED_PROMPT);
  43. await waitForV4TimelineContaining(SEED_REPLY, 45000);
  44. await waitForV4Pane(
  45. (snapshot) =>
  46. !snapshot.canStop && snapshot.sessionId !== "draft" && snapshot.sessionId !== null,
  47. "`/plan` E2E seed 没有回到已有 idle session",
  48. 45000,
  49. );
  50. // 首轮结束后标题请求仍会异步发出;先等待种子会话的标题请求,避免计入空 /plan。
  51. await browser.waitUntil(
  52. async () => (await countUpstreamTitleRequestsContaining(SEED_PROMPT)) > titleCountBefore,
  53. { timeout: 30000, timeoutMsg: "种子会话的标题请求未发出" },
  54. );
  55. }
  56. async function waitForReadyComposerAttachment(filename: string) {
  57. await browser.waitUntil(
  58. async () => {
  59. const attachments = await getV4ComposerAttachments();
  60. // Bug 原因:图片卡按设计只显示缩略图,filename 不保证是可见文本;本 case
  61. // 只有一个附件,数量与 ready 状态才是稳定的发送前契约。
  62. return attachments.length === 1 && attachments[0]?.uploadStatus === "ready";
  63. },
  64. { timeout: 30000, timeoutMsg: `附件没有进入 ready:${filename}` },
  65. );
  66. }
  67. describe("v4 `/plan` Composer shortcut", () => {
  68. after(async () => {
  69. await browser.electron.restoreAllMocks();
  70. await clearAppData();
  71. });
  72. it("draft `/PLAN` 空命令只切换模式,不创建 user row 或 provider request", async function () {
  73. this.timeout(120000);
  74. await prepareV4ConversationE2E();
  75. await startNewV4Draft();
  76. await switchV4Mode("build");
  77. const requestCountBefore = await getUpstreamRequestRecordCount();
  78. await sendV4Prompt(" /PLAN ");
  79. await waitForV4ComposerText("", "空 `/plan` 消费后 composer 没有清空");
  80. await waitForPlanMode();
  81. expect(await getV4Messages("user")).toHaveLength(0);
  82. expect(await getUpstreamRequestRecordCount()).toBe(requestCountBefore);
  83. });
  84. it("existing session `/plan` 空命令只切换模式,不新增 user row 或 provider request", async function () {
  85. this.timeout(120000);
  86. await establishBuildSession();
  87. const userMessagesBefore = await getV4Messages("user");
  88. const requestCountBefore = await getUpstreamRequestRecordCount();
  89. await sendV4Prompt("/plan");
  90. await waitForV4ComposerText("", "已有会话空 `/plan` 消费后 composer 没有清空");
  91. await waitForPlanMode();
  92. expect(await getV4Messages("user")).toHaveLength(userMessagesBefore.length);
  93. expect(await getUpstreamRequestRecordCount()).toBe(requestCountBefore);
  94. });
  95. it("任务命令先切 Plan 再发送正文,且 Plan noop 后仍可继续发送", async function () {
  96. this.timeout(180000);
  97. await establishBuildSession();
  98. await sendV4Prompt(`/plan ${TASK_ONE}`);
  99. await waitForUpstreamRequest(
  100. {
  101. lastUserMessageIncludes: [TASK_ONE],
  102. lastUserMessageExcludes: ["/plan"],
  103. },
  104. "`/plan <task>` provider request 没有只发送任务正文",
  105. 60000,
  106. );
  107. await waitForV4TimelineContaining(TASK_ONE_REPLY, 60000);
  108. await waitForPlanMode();
  109. await waitForV4UserMessageContaining(TASK_ONE);
  110. // 当前 session 已经是 Plan;第二次 shortcut 的 mode CAS 应 noop,但仍需发送普通文本。
  111. await sendV4Prompt(`/plan ${TASK_TWO}`);
  112. await waitForUpstreamRequest(
  113. {
  114. lastUserMessageIncludes: [TASK_TWO],
  115. lastUserMessageExcludes: ["/plan"],
  116. },
  117. "Plan noop 后 `/plan <task>` 没有继续发送正文",
  118. 60000,
  119. );
  120. await waitForV4TimelineContaining(TASK_TWO_REPLY, 60000);
  121. await waitForV4UserMessageContaining(TASK_TWO);
  122. await assertVisibleV4UserMessagesNotContaining("/plan");
  123. });
  124. it("附件拒绝时保留 `/plan <task>` 草稿、附件和当前模式", async function () {
  125. this.timeout(120000);
  126. await prepareV4ConversationE2E();
  127. await startNewV4Draft();
  128. await switchV4Mode("build");
  129. const modeBefore = (await getV4ConfigProjection()).mode;
  130. const requestCountBefore = await getUpstreamRequestRecordCount();
  131. const attachmentName = "e2e-plan-shortcut-rejected.png";
  132. const draft = "/plan plan-shortcut-attachment-reject";
  133. await pasteV4ComposerImageAttachment(attachmentName);
  134. await waitForReadyComposerAttachment(attachmentName);
  135. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, draft, {
  136. timeout: 15000,
  137. timeoutMsg: "附件拒绝 case 没有写入 `/plan` 草稿",
  138. });
  139. await waitForV4ComposerText(draft, "附件拒绝 case 没有写入 `/plan` 草稿");
  140. await clickV4Send();
  141. await waitForV4ComposerText(draft, "附件拒绝后 composer 草稿没有保留");
  142. expect((await getV4ConfigProjection()).mode).toBe(modeBefore);
  143. expect(await getUpstreamRequestRecordCount()).toBe(requestCountBefore);
  144. expect(await getV4ComposerAttachments()).toEqual(
  145. expect.arrayContaining([
  146. expect.objectContaining({
  147. uploadStatus: "ready",
  148. }),
  149. ]),
  150. );
  151. });
  152. });