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

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