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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // 计划模式列表操作:真实 Tab / Enter / 点击只修改 Composer,发送复用标准 Plan。
  2. import { clearAppData, setInputValueByTestIdDom } from "../helpers/desktop-app.js";
  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 "../helpers/v4-conversation.js";
  17. import { selectUpstreamModel } from "../helpers/upstream-provider.js";
  18. import {
  19. countUpstreamTitleRequestsContaining,
  20. getUpstreamRequestRecordCount,
  21. waitForUpstreamRequest,
  22. } from "../helpers/conversation-session-network.js";
  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. expect(await $(PLAN_OPTION).getText()).toMatch(/计划模式|Plan mode/);
  45. expect(await $(PLAN_OPTION).getText()).not.toContain("/plan");
  46. }
  47. async function assertSelected(expectedText: string) {
  48. await waitForV4ComposerText(expectedText, "选中计划模式后正文不匹配");
  49. await browser.waitUntil(async () => (await getV4ConfigProjection()).planEnabled, {
  50. timeout: 15000,
  51. timeoutMsg: "标准 Plan 标记没有开启",
  52. });
  53. expect(await $(PLAN_OPTION).isExisting()).toBe(false);
  54. expect(
  55. await browser.execute(
  56. (id) => document.activeElement?.getAttribute("data-testid") === id,
  57. TID_V4_COMPOSER_INPUT,
  58. ),
  59. ).toBe(true);
  60. }
  61. async function establishSession() {
  62. await prepareCase();
  63. const titleCount = await countUpstreamTitleRequestsContaining(SEED_PROMPT);
  64. await sendV4Prompt(SEED_PROMPT);
  65. await waitForV4TimelineContaining("V4_PLAN_SHORTCUT_SEED_OK", 45000);
  66. await waitForV4Pane(
  67. (snapshot) =>
  68. !snapshot.canStop && snapshot.sessionId !== "draft" && snapshot.sessionId !== null,
  69. "种子会话未完成",
  70. 45000,
  71. );
  72. await browser.waitUntil(
  73. async () => (await countUpstreamTitleRequestsContaining(SEED_PROMPT)) > titleCount,
  74. { timeout: 30000, timeoutMsg: "种子会话标题请求未完成" },
  75. );
  76. }
  77. async function setViewport(width: number | null) {
  78. await browser.electron.execute(async (electron, viewportWidth) => {
  79. const target = electron.BrowserWindow.getAllWindows().find(
  80. (window) => !window.isDestroyed() && window.isVisible(),
  81. )!;
  82. if (!target.webContents.debugger.isAttached()) target.webContents.debugger.attach("1.3");
  83. await target.webContents.debugger.sendCommand(
  84. viewportWidth ? "Emulation.setDeviceMetricsOverride" : "Emulation.clearDeviceMetricsOverride",
  85. viewportWidth
  86. ? { width: viewportWidth, height: 860, deviceScaleFactor: 1, mobile: false }
  87. : {},
  88. );
  89. }, width);
  90. }
  91. describe("v4 Plan picker action", () => {
  92. after(async () => {
  93. await browser.electron.restoreAllMocks();
  94. await clearAppData();
  95. });
  96. it("draft Tab 开启、Enter 重复选择幂等;Escape 不切模式,不产生消息", async function () {
  97. this.timeout(120000);
  98. await prepareCase();
  99. const before = await getUpstreamRequestRecordCount();
  100. const modeBefore = (await getV4ConfigProjection()).mode;
  101. await enterSearch();
  102. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  103. await browser.keys("Escape");
  104. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  105. await waitForV4ComposerText("/plan", "Escape 应保留输入");
  106. // 改变 token 后重新打开,不能把已取消的候选自动执行。
  107. await enterSearch("/pla");
  108. await browser.keys("Tab");
  109. await assertSelected("");
  110. await enterSearch("/PLAN");
  111. await browser.keys("Enter");
  112. await assertSelected("");
  113. expect((await getV4ConfigProjection()).mode).toBe(modeBefore);
  114. expect(await getV4Messages("user")).toHaveLength(0);
  115. expect(await getUpstreamRequestRecordCount()).toBe(before);
  116. });
  117. it("已有会话点击开启 Plan,保留正文和附件,另行发送走标准 Plan", async function () {
  118. this.timeout(180000);
  119. await establishSession();
  120. await pasteV4ComposerImageAttachment("e2e-plan-picker.png");
  121. await browser.waitUntil(
  122. async () => (await getV4ComposerAttachments())[0]?.uploadStatus === "ready",
  123. { timeout: 30000, timeoutMsg: "附件未 ready" },
  124. );
  125. const attachments = await getV4ComposerAttachments();
  126. const rows = await getV4Messages("user");
  127. const requests = await getUpstreamRequestRecordCount();
  128. await enterSearch(`${TASK_ONE} /plan`);
  129. await $(PLAN_OPTION).click();
  130. await assertSelected(`${TASK_ONE} `);
  131. expect(await getV4ComposerAttachments()).toEqual(attachments);
  132. expect(await getV4Messages("user")).toHaveLength(rows.length);
  133. expect(await getUpstreamRequestRecordCount()).toBe(requests);
  134. await clickV4Send();
  135. await waitForUpstreamRequest(
  136. {
  137. lastUserMessageIncludes: [TASK_ONE, "e2e-plan-picker.png"],
  138. lastUserMessageExcludes: ["/plan"],
  139. includes: ["Plan mode is active."],
  140. },
  141. "标准 Plan 请求没有保留正文与附件",
  142. 60000,
  143. );
  144. await waitForV4TimelineContaining("V4_PLAN_SHORTCUT_TASK_ONE_OK", 60000);
  145. });
  146. it("窄屏 Enter 选择后可用标准关闭按钮退出;原始 /plan 文本不触发模式切换", async function () {
  147. this.timeout(180000);
  148. await establishSession();
  149. try {
  150. await setViewport(390);
  151. await browser.waitUntil(() => browser.execute(() => window.innerWidth === 390), {
  152. timeout: 5000,
  153. timeoutMsg: "手机尺寸 viewport 未生效",
  154. });
  155. await enterSearch();
  156. await browser.keys("Enter");
  157. await assertSelected("");
  158. await $('[data-testid="v4-composer-plan-marker"] button').click();
  159. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  160. const raw = `/plan ${TASK_TWO}`;
  161. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, raw, {
  162. timeout: 15000,
  163. timeoutMsg: "未写入普通文本",
  164. });
  165. await browser.keys("Escape");
  166. await clickV4Send();
  167. await waitForUpstreamRequest(
  168. { lastUserMessageIncludes: [raw] },
  169. "发送时仍错误剥离 /plan",
  170. 60000,
  171. );
  172. await waitForV4TimelineContaining("V4_PLAN_SHORTCUT_TASK_TWO_OK", 60000);
  173. expect((await getV4ConfigProjection()).planEnabled).toBe(false);
  174. } finally {
  175. await setViewport(null);
  176. }
  177. });
  178. });