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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // 计划模式列表操作:真实 Tab / Enter / 点击只修改 Composer,发送复用标准 Plan。
  2. import { clearAppData, setInputValueByTestIdDom } from "#e2e/helpers/desktop-app";
  3. import { TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  4. import {
  5. clickV4Send,
  6. getV4ComposerAttachments,
  7. getV4ConfigProjection,
  8. getV4Messages,
  9. pasteV4ComposerImageAttachment,
  10. prepareV4ConversationE2E,
  11. sendV4Prompt,
  12. switchV4Mode,
  13. waitForV4ComposerText,
  14. waitForV4Pane,
  15. waitForV4TimelineContaining,
  16. } from "#e2e/helpers/v4-conversation";
  17. import { selectUpstreamModel } from "#e2e/helpers/upstream-provider";
  18. import {
  19. countUpstreamTitleRequestsContaining,
  20. getUpstreamRequestRecordCount,
  21. waitForUpstreamRequest,
  22. } from "#e2e/helpers/conversation-session-network";
  23. const SEED_PROMPT = "E2E_V4_PLAN_SHORTCUT_SEED 建立已有会话";
  24. const TASK_ONE = "E2E_V4_PLAN_SHORTCUT_TASK_ONE 列出实现步骤";
  25. const TASK_TWO = "E2E_V4_PLAN_SHORTCUT_TASK_TWO 普通文本";
  26. const PLAN_OPTION = '[data-option-id="app-slash:plan"]';
  27. let shellReady = false;
  28. async function prepareCase() {
  29. // 首次测试之后默认项目已切换,复用 provider,避免重复等待启动项目。
  30. await prepareV4ConversationE2E({ skipShellReady: shellReady, skipProvider: shellReady });
  31. if (shellReady) await selectUpstreamModel();
  32. shellReady = true;
  33. if ((await getV4ConfigProjection()).planEnabled) {
  34. await $('[data-testid="v4-composer-plan-marker"] button').click();
  35. }
  36. await switchV4Mode("build");
  37. }
  38. async function enterSearch(text = "/plan") {
  39. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, text, {
  40. timeout: 15000,
  41. timeoutMsg: "未写入 Plan 搜索词",
  42. });
  43. await $(PLAN_OPTION).waitForDisplayed({ timeout: 15000 });
  44. const optionText = await $(PLAN_OPTION).getText();
  45. expect(optionText).toMatch(/计划模式|Plan mode/);
  46. // 面板选项展示本地化名称与描述;`/plan` 只是搜索词,不作为命令名展示
  47. // (见 docs/plan-slash-command.md,8f13cd8415 已恢复本地化 label)。
  48. expect(optionText).not.toContain("/plan");
  49. }
  50. async function assertSelected(expectedText: string) {
  51. await waitForV4ComposerText(expectedText, "选中计划模式后正文不匹配");
  52. await browser.waitUntil(async () => (await getV4ConfigProjection()).planEnabled, {
  53. timeout: 15000,
  54. timeoutMsg: "标准 Plan 标记没有开启",
  55. });
  56. expect(await $(PLAN_OPTION).isExisting()).toBe(false);
  57. expect(
  58. await browser.execute(
  59. (id) => document.activeElement?.getAttribute("data-testid") === id,
  60. TID_V4_COMPOSER_INPUT,
  61. ),
  62. ).toBe(true);
  63. }
  64. async function establishSession() {
  65. await prepareCase();
  66. const titleCount = await countUpstreamTitleRequestsContaining(SEED_PROMPT);
  67. await sendV4Prompt(SEED_PROMPT);
  68. await waitForV4TimelineContaining("V4_PLAN_SHORTCUT_SEED_OK", 45000);
  69. await waitForV4Pane(
  70. (snapshot) =>
  71. !snapshot.canStop && snapshot.sessionId !== "draft" && snapshot.sessionId !== null,
  72. "种子会话未完成",
  73. 45000,
  74. );
  75. await browser.waitUntil(
  76. async () => (await countUpstreamTitleRequestsContaining(SEED_PROMPT)) > titleCount,
  77. { timeout: 30000, timeoutMsg: "种子会话标题请求未完成" },
  78. );
  79. }
  80. async function setViewport(width: number | null) {
  81. await browser.electron.execute(async (electron, viewportWidth) => {
  82. const target = electron.BrowserWindow.getAllWindows().find(
  83. (window) => !window.isDestroyed() && window.isVisible(),
  84. )!;
  85. if (!target.webContents.debugger.isAttached()) target.webContents.debugger.attach("1.3");
  86. await target.webContents.debugger.sendCommand(
  87. viewportWidth ? "Emulation.setDeviceMetricsOverride" : "Emulation.clearDeviceMetricsOverride",
  88. viewportWidth
  89. ? { width: viewportWidth, height: 860, deviceScaleFactor: 1, mobile: false }
  90. : {},
  91. );
  92. }, width);
  93. }
  94. describe("v4 Plan picker action", () => {
  95. after(async () => {
  96. await browser.electron.restoreAllMocks();
  97. await clearAppData();
  98. });
  99. it("draft Tab 开启、Enter 重复选择幂等;Escape 不切模式,不产生消息", async function () {
  100. this.timeout(120000);
  101. await prepareCase();
  102. const before = await getUpstreamRequestRecordCount();
  103. const modeBefore = (await getV4ConfigProjection()).mode;
  104. await enterSearch();
  105. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  106. await browser.keys("Escape");
  107. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  108. await waitForV4ComposerText("/plan", "Escape 应保留输入");
  109. // 改变 token 后重新打开,不能把已取消的候选自动执行。
  110. await enterSearch("/pla");
  111. await browser.keys("Tab");
  112. await assertSelected("");
  113. await enterSearch("/PLAN");
  114. await browser.keys("Enter");
  115. await assertSelected("");
  116. expect((await getV4ConfigProjection()).mode).toBe(modeBefore);
  117. expect(await getV4Messages("user")).toHaveLength(0);
  118. expect(await getUpstreamRequestRecordCount()).toBe(before);
  119. });
  120. it("已有会话点击开启 Plan,保留正文和附件,另行发送走标准 Plan", async function () {
  121. this.timeout(180000);
  122. await establishSession();
  123. await pasteV4ComposerImageAttachment("e2e-plan-picker.png");
  124. await browser.waitUntil(
  125. async () => (await getV4ComposerAttachments())[0]?.uploadStatus === "ready",
  126. { timeout: 30000, timeoutMsg: "附件未 ready" },
  127. );
  128. const attachments = await getV4ComposerAttachments();
  129. const rows = await getV4Messages("user");
  130. const requests = await getUpstreamRequestRecordCount();
  131. await enterSearch(`${TASK_ONE} /plan`);
  132. await $(PLAN_OPTION).click();
  133. await assertSelected(`${TASK_ONE} `);
  134. expect(await getV4ComposerAttachments()).toEqual(attachments);
  135. expect(await getV4Messages("user")).toHaveLength(rows.length);
  136. expect(await getUpstreamRequestRecordCount()).toBe(requests);
  137. await clickV4Send();
  138. await waitForUpstreamRequest(
  139. {
  140. lastUserMessageIncludes: [TASK_ONE, "e2e-plan-picker.png"],
  141. lastUserMessageExcludes: ["/plan"],
  142. includes: ["Plan mode is active."],
  143. },
  144. "标准 Plan 请求没有保留正文与附件",
  145. 60000,
  146. );
  147. await waitForV4TimelineContaining("V4_PLAN_SHORTCUT_TASK_ONE_OK", 60000);
  148. });
  149. it("窄屏 Enter 选择后可用标准关闭按钮退出;原始 /plan 文本不触发模式切换", async function () {
  150. this.timeout(180000);
  151. await establishSession();
  152. try {
  153. await setViewport(390);
  154. await browser.waitUntil(() => browser.execute(() => window.innerWidth === 390), {
  155. timeout: 5000,
  156. timeoutMsg: "手机尺寸 viewport 未生效",
  157. });
  158. await enterSearch();
  159. await browser.keys("Enter");
  160. await assertSelected("");
  161. await $('[data-testid="v4-composer-plan-marker"] button').click();
  162. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  163. const raw = `/plan ${TASK_TWO}`;
  164. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, raw, {
  165. timeout: 15000,
  166. timeoutMsg: "未写入普通文本",
  167. });
  168. await browser.keys("Escape");
  169. await clickV4Send();
  170. await waitForUpstreamRequest(
  171. { lastUserMessageIncludes: [raw] },
  172. "发送时仍错误剥离 /plan",
  173. 60000,
  174. );
  175. await waitForV4TimelineContaining("V4_PLAN_SHORTCUT_TASK_TWO_OK", 60000);
  176. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  177. } finally {
  178. await setViewport(null);
  179. }
  180. });
  181. });