e2ecase-regression.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. import {
  2. TID_CHAT_INPUT,
  3. TID_CHAT_MESSAGES,
  4. TID_CHAT_REASONING_CONTENT,
  5. TID_CHAT_REASONING_TRIGGER,
  6. TID_CHAT_SEND_BUTTON,
  7. TID_LOGIN_TRIGGER,
  8. TID_SETTINGS_BACK_BUTTON,
  9. TID_SETTINGS_PAGE,
  10. TID_SETTINGS_SECTION_NAV,
  11. TID_SIDEBAR_CODING_PLAN_USAGE_BUTTON,
  12. TID_TASK_SETTINGS_BUTTON,
  13. testId,
  14. } from "@zcode/shared";
  15. import AppPage from "./pages/app.page.js";
  16. import {
  17. DEFAULT_WORKSPACE,
  18. clearAppData,
  19. clickTestIdByDom,
  20. waitForDefaultWorkspaceReady,
  21. waitForWorkspaceApp,
  22. } from "./helpers/desktop-app.js";
  23. import {
  24. UPSTREAM_MODEL,
  25. UPSTREAM_THOUGHT_LEVEL,
  26. ensureUpstreamProviderForE2E,
  27. } from "./helpers/upstream-provider.js";
  28. import {
  29. assertUpstreamRequestCapture,
  30. assertUpstreamThoughtLevelCapture,
  31. waitForUpstreamNetworkCapture,
  32. } from "./helpers/upstream-capture.js";
  33. import { sel } from "./helpers/selectors.js";
  34. const PROMPT_RUNNING_ERROR = "a prompt is already running in session";
  35. const MODEL_REQUEST_FAILED_ERROR = "Model request failed";
  36. describe("e2ecase.md 回归", () => {
  37. after(async () => {
  38. await browser.electron.restoreAllMocks();
  39. await clearAppData();
  40. });
  41. it("设置页再次进入时应自动打开上一次打开的分区", async () => {
  42. await waitForDefaultWorkspaceReady(30000);
  43. await openSettingsPage();
  44. await selectSettingsSection("mcp");
  45. await expectSettingsSectionActive("mcp");
  46. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  47. timeout: 15000,
  48. timeoutMsg: "设置页返回按钮没有出现",
  49. });
  50. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  51. await openSettingsPage();
  52. await expectSettingsSectionActive("mcp");
  53. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  54. timeout: 15000,
  55. timeoutMsg: "设置页返回按钮没有出现",
  56. });
  57. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  58. });
  59. it("工作区头像菜单点击使用统计应打开设置页用量分区", async () => {
  60. await waitForDefaultWorkspaceReady(30000);
  61. await openUsageStatsFromProfileMenu();
  62. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  63. await expectSettingsSectionActive("usage");
  64. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  65. timeout: 15000,
  66. timeoutMsg: "设置页返回按钮没有出现",
  67. });
  68. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  69. });
  70. it("设置页头像菜单点击使用统计应停留设置页用量分区", async () => {
  71. await waitForDefaultWorkspaceReady(30000);
  72. await openSettingsPage();
  73. await selectSettingsSection("general");
  74. await expectSettingsSectionActive("general");
  75. await openUsageStatsFromProfileMenu();
  76. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  77. await expect($(sel(TID_SETTINGS_PAGE))).toBeDisplayed();
  78. await expectSettingsSectionActive("usage");
  79. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  80. timeout: 15000,
  81. timeoutMsg: "设置页返回按钮没有出现",
  82. });
  83. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  84. });
  85. it.skip("任务运行中应允许快速发送下一条并保留输入框草稿", async function () {
  86. this.timeout(120000);
  87. await ensureUpstreamProviderForE2E();
  88. await waitForDefaultWorkspaceReady(30000);
  89. const runId = Date.now();
  90. const firstPrompt =
  91. `e2e running first prompt ${runId}. Use brief private reasoning if needed. ` +
  92. "Reply with ten short numbered lines and end with QUEUE_FIRST_DONE.";
  93. const draftPrompt = `e2e draft should stay while running ${runId}`;
  94. await typeChatPrompt(firstPrompt);
  95. await clickChatSend();
  96. await waitForComposerText("", "第一条消息发送后输入框没有清空");
  97. await waitForUserMessageContaining(firstPrompt);
  98. const workingStatus = await waitForWorkingZeroStatus();
  99. await assertWorkingStatusPositionStable(workingStatus.top);
  100. await typeChatPrompt(draftPrompt);
  101. await waitForComposerText(
  102. draftPrompt,
  103. "运行中输入的草稿没有进入输入框",
  104. );
  105. await browser.pause(800);
  106. await waitForComposerText(
  107. draftPrompt,
  108. "运行中输入框草稿被意外清空",
  109. );
  110. await waitForQueueSendButtonReady();
  111. await clickChatSend();
  112. await waitForComposerText("", "队列消息发送后输入框没有清空");
  113. await waitForBodyTextContaining(
  114. draftPrompt,
  115. "第二条消息没有进入待发送队列或消息列表",
  116. );
  117. await assertBodyTextNotContaining(PROMPT_RUNNING_ERROR);
  118. await assertBodyTextNotContaining(MODEL_REQUEST_FAILED_ERROR);
  119. await waitForUserMessageContaining(draftPrompt);
  120. await browser.waitUntil(async () => (await getAssistantMessageCount()) >= 2, {
  121. timeout: 60000,
  122. timeoutMsg: "队列消息没有在第一轮结束后继续生成 assistant 回复",
  123. });
  124. await assertBodyTextNotContaining(PROMPT_RUNNING_ERROR);
  125. await assertBodyTextNotContaining(MODEL_REQUEST_FAILED_ERROR);
  126. });
  127. it.skip("思考内容收起时应先播放高度动画再卸载 DOM", async function () {
  128. this.timeout(90000);
  129. await ensureUpstreamProviderForE2E();
  130. await waitForDefaultWorkspaceReady(30000);
  131. await startNewTask();
  132. const runId = Date.now();
  133. const prompt =
  134. `e2e reasoning collapse ${runId}. Use the configured high thinking depth. ` +
  135. "After reasoning, reply with exactly REASONING_COLLAPSE_DONE.";
  136. await typeChatPrompt(prompt);
  137. await clickChatSend();
  138. const captureRecord = await waitForUpstreamNetworkCapture(prompt);
  139. assertUpstreamRequestCapture(captureRecord, {
  140. expectedText: prompt,
  141. model: UPSTREAM_MODEL,
  142. });
  143. assertUpstreamThoughtLevelCapture(captureRecord, UPSTREAM_THOUGHT_LEVEL);
  144. await $(sel(TID_CHAT_REASONING_TRIGGER)).waitForClickable({
  145. timeout: 45000,
  146. timeoutMsg: "没有渲染思考块触发器",
  147. });
  148. await $(sel(TID_CHAT_REASONING_TRIGGER)).click();
  149. await waitForReasoningContentOpen();
  150. const samples = await monitorReasoningCollapseDuringClick();
  151. const firstExisting = samples.find((sample) => sample.exists && sample.height !== null);
  152. const closedSamples = samples.filter(
  153. (sample) => sample.exists && sample.state === "closed",
  154. );
  155. const firstClosed = closedSamples.find((sample) => sample.height !== null);
  156. if (!firstExisting || !firstClosed) {
  157. throw new Error(
  158. `思考块收起时没有捕获到 closed 动画帧\n${JSON.stringify(samples, null, 2)}`,
  159. );
  160. }
  161. expect(firstExisting.text.trim().length).toBeGreaterThan(0);
  162. expect(firstClosed.text.trim().length).toBeGreaterThan(0);
  163. expect(firstExisting.height ?? 0).toBeGreaterThan(0);
  164. expect(firstClosed.height ?? 0).toBeGreaterThan(0);
  165. expect(firstClosed.height ?? 0).toBeLessThanOrEqual((firstExisting.height ?? 0) + 2);
  166. await waitForReasoningContentUnmounted();
  167. });
  168. it.skip("goal 首发 query 保留命令,标题不包含命令,续跑 system reminder 不显示为用户消息", async function () {
  169. this.timeout(120000);
  170. await ensureUpstreamProviderForE2E();
  171. await waitForDefaultWorkspaceReady(30000);
  172. await startNewTask();
  173. const runId = Date.now();
  174. const objective = `e2e target objective ${runId}`;
  175. await typeChatPrompt(`/goal ${objective}`);
  176. await clickChatSend();
  177. await waitForWorkspaceTitleContaining("target objective");
  178. await assertWorkspaceTitleNotContaining("/goal");
  179. await assertVisibleUserMessagesContaining(`/goal ${objective}`);
  180. await assertVisibleUserMessagesNotContaining("goal-continuation");
  181. await waitForAssistantMessageCountAtLeast(1);
  182. await typeChatPrompt("/goal pause");
  183. await clickChatSend();
  184. await waitForComposerText("", "goal pause 后输入框没有清空");
  185. await typeChatPrompt("/goal resume");
  186. await clickChatSend();
  187. await waitForAssistantMessageCountAtLeast(2);
  188. await assertVisibleUserMessagesNotContaining("/goal resume");
  189. await assertVisibleUserMessagesNotContaining("system-reminder");
  190. await assertVisibleUserMessagesNotContaining("goal-continuation");
  191. });
  192. });
  193. async function openSettingsPage() {
  194. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  195. timeout: 15000,
  196. timeoutMsg: "没有找到设置入口按钮",
  197. });
  198. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  199. }
  200. async function openUsageStatsFromProfileMenu() {
  201. await clickVisibleTestIdByDom(TID_LOGIN_TRIGGER, {
  202. timeout: 15000,
  203. timeoutMsg: "没有找到可见的头像菜单入口",
  204. });
  205. await clickVisibleTestIdByDom(TID_SIDEBAR_CODING_PLAN_USAGE_BUTTON, {
  206. timeout: 15000,
  207. timeoutMsg: "头像菜单没有出现可见的使用统计入口",
  208. });
  209. }
  210. async function selectSettingsSection(section: string) {
  211. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, section), {
  212. timeout: 15000,
  213. timeoutMsg: `设置页没有出现 ${section} 分区入口`,
  214. });
  215. }
  216. async function clickVisibleTestIdByDom(
  217. targetTestId: string,
  218. {
  219. timeout = 10000,
  220. timeoutMsg = `页面没有可见可点击的 test id: ${targetTestId}`,
  221. }: { timeout?: number; timeoutMsg?: string } = {},
  222. ) {
  223. // 修复原因:设置页打开时 workspace DOM 仍保留但不可见,头像菜单和设置页 footer 共享同一 test id。
  224. // E2E 必须点击当前可见入口,否则会误点隐藏 workspace footer,掩盖“设置页菜单返回工作区”的回归。
  225. let latestReason = "not-started";
  226. await browser.waitUntil(
  227. async () => {
  228. const result = (await browser.execute((testIdValue) => {
  229. function isVisible(element: HTMLElement) {
  230. const style = window.getComputedStyle(element);
  231. if (
  232. style.display === "none" ||
  233. style.visibility === "hidden" ||
  234. Number(style.opacity) === 0
  235. ) {
  236. return false;
  237. }
  238. if (element.getClientRects().length === 0) {
  239. return false;
  240. }
  241. return !element.closest('[aria-hidden="true"], [hidden]');
  242. }
  243. const element = Array.from(
  244. document.querySelectorAll<HTMLElement>("[data-testid]"),
  245. ).find((item) => item.dataset.testid === testIdValue && isVisible(item));
  246. if (!element) {
  247. return { clicked: false, reason: "missing-visible" };
  248. }
  249. const disabled =
  250. (element instanceof HTMLButtonElement ||
  251. element instanceof HTMLInputElement ||
  252. element instanceof HTMLSelectElement ||
  253. element instanceof HTMLTextAreaElement) &&
  254. element.disabled;
  255. if (disabled) {
  256. return { clicked: false, reason: "disabled" };
  257. }
  258. // 修复原因:Radix 菜单触发器依赖 pointerdown 激活,单发 element.click() 会让菜单不展开。
  259. function dispatchE2EActivation(target: HTMLElement) {
  260. target.focus();
  261. const pointerEventCtor = window.PointerEvent ?? MouseEvent;
  262. for (const type of ["pointerdown", "mousedown", "pointerup", "mouseup", "click"]) {
  263. const event =
  264. type.startsWith("pointer") && pointerEventCtor === window.PointerEvent
  265. ? new PointerEvent(type, {
  266. bubbles: true,
  267. cancelable: true,
  268. pointerId: 1,
  269. pointerType: "mouse",
  270. isPrimary: true,
  271. })
  272. : new MouseEvent(type, {
  273. bubbles: true,
  274. cancelable: true,
  275. view: window,
  276. });
  277. target.dispatchEvent(event);
  278. }
  279. }
  280. dispatchE2EActivation(element);
  281. return { clicked: true };
  282. }, targetTestId)) as { clicked: boolean; reason?: string };
  283. latestReason = result.reason ?? "clicked";
  284. return result.clicked;
  285. },
  286. {
  287. timeout,
  288. timeoutMsg: `${timeoutMsg}: ${latestReason}`,
  289. },
  290. );
  291. }
  292. async function expectSettingsSectionActive(section: string) {
  293. const navItem = $(sel(testId(TID_SETTINGS_SECTION_NAV, section)));
  294. await browser.waitUntil(async () => (await navItem.getAttribute("aria-current")) === "page", {
  295. timeout: 15000,
  296. timeoutMsg: `设置页没有自动打开上一次的 ${section} 分区`,
  297. });
  298. }
  299. async function typeChatPrompt(prompt: string) {
  300. await AppPage.chatInput.waitForDisplayed({ timeout: 15000 });
  301. await AppPage.chatInput.click();
  302. const result = (await browser.executeAsync(
  303. (inputTestId, text, done) => {
  304. const input = document.querySelector<HTMLElement>(
  305. `[data-testid="${inputTestId}"]`,
  306. );
  307. if (!input) {
  308. done({ actual: "", inserted: false, ok: false, reason: "input-missing" });
  309. return;
  310. }
  311. input.focus();
  312. const selection = window.getSelection();
  313. const range = document.createRange();
  314. range.selectNodeContents(input);
  315. selection?.removeAllRanges();
  316. selection?.addRange(range);
  317. document.execCommand("delete");
  318. // 修复原因:browser.keys() 遇到多行 prompt 会把换行当成 Enter,
  319. // 在 Lexical 输入框里提前触发送信,真实请求只带第一行。逐行插入并显式
  320. // insertLineBreak,才能让 contenteditable 和 Lexical 都保留真实换行。
  321. let inserted = true;
  322. const lines = text.split("\n");
  323. for (const [lineIndex, line] of lines.entries()) {
  324. inserted = document.execCommand("insertText", false, line) && inserted;
  325. if (lineIndex < lines.length - 1) {
  326. inserted = document.execCommand("insertLineBreak") && inserted;
  327. }
  328. }
  329. requestAnimationFrame(() => {
  330. const actual = (input.innerText || input.textContent || "")
  331. .replace(/\u00a0/g, " ")
  332. .trim();
  333. done({
  334. actual,
  335. inserted,
  336. ok: actual === text.trim(),
  337. });
  338. });
  339. },
  340. TID_CHAT_INPUT,
  341. prompt,
  342. )) as { actual: string; inserted: boolean; ok: boolean; reason?: string };
  343. if (!result.ok) {
  344. throw new Error(
  345. `聊天输入框没有写入完整 prompt: ${JSON.stringify(result)}`,
  346. );
  347. }
  348. }
  349. async function clickChatSend() {
  350. await AppPage.chatSendButton.waitForClickable({ timeout: 30000 });
  351. await AppPage.chatSendButton.click();
  352. }
  353. async function startNewTask() {
  354. await AppPage.taskNewButton.waitForClickable({ timeout: 15000 });
  355. await AppPage.taskNewButton.click();
  356. await AppPage.chatInput.waitForDisplayed({ timeout: 15000 });
  357. }
  358. async function waitForQueueSendButtonReady() {
  359. await browser.waitUntil(
  360. async () =>
  361. browser.execute((sendButtonTestId) => {
  362. const button = document.querySelector<HTMLButtonElement>(
  363. `[data-testid="${sendButtonTestId}"]`,
  364. );
  365. return Boolean(button && !button.disabled);
  366. }, TID_CHAT_SEND_BUTTON),
  367. {
  368. timeout: 30000,
  369. timeoutMsg: "任务 streaming 时没有出现可用的队列发送按钮",
  370. },
  371. );
  372. }
  373. async function waitForComposerText(expected: string, timeoutMsg: string) {
  374. await browser.waitUntil(async () => (await getComposerText()) === expected, {
  375. timeout: 10000,
  376. timeoutMsg,
  377. });
  378. }
  379. async function getComposerText() {
  380. return browser.execute((inputTestId) => {
  381. const input = document.querySelector<HTMLElement>(
  382. `[data-testid="${inputTestId}"]`,
  383. );
  384. return (input?.innerText || input?.textContent || "")
  385. .replace(/\u00a0/g, " ")
  386. .trim();
  387. }, TID_CHAT_INPUT);
  388. }
  389. async function waitForUserMessageContaining(text: string) {
  390. await browser.waitUntil(
  391. async () => {
  392. const messages = await browser.execute((messagesTestId) => {
  393. const root = document.querySelector<HTMLElement>(
  394. `[data-testid="${messagesTestId}"]`,
  395. );
  396. return Array.from(root?.querySelectorAll<HTMLElement>(".is-user") ?? [])
  397. .map((element) => element.innerText)
  398. .join("\n");
  399. }, TID_CHAT_MESSAGES);
  400. return messages.includes(text);
  401. },
  402. {
  403. timeout: 60000,
  404. timeoutMsg: `聊天记录中没有出现用户消息: ${text}`,
  405. },
  406. );
  407. }
  408. async function getAssistantMessageCount() {
  409. return AppPage.assistantMessages.length;
  410. }
  411. async function waitForAssistantMessageCountAtLeast(expectedCount: number) {
  412. await browser.waitUntil(async () => (await getAssistantMessageCount()) >= expectedCount, {
  413. timeout: 60000,
  414. timeoutMsg: `assistant 回复数量没有达到 ${expectedCount}`,
  415. });
  416. }
  417. async function waitForWorkingZeroStatus() {
  418. let lastBodyText = "";
  419. await browser.waitUntil(
  420. async () => {
  421. const snapshot = await getWorkingStatusSnapshot();
  422. if (snapshot) {
  423. return true;
  424. }
  425. lastBodyText = await getBodyText();
  426. return false;
  427. },
  428. {
  429. timeout: 15000,
  430. timeoutMsg: `发送消息后没有出现“工作中 0 秒”状态,body=${lastBodyText.slice(0, 1000)}`,
  431. },
  432. );
  433. const snapshot = await getWorkingStatusSnapshot();
  434. if (!snapshot) {
  435. throw new Error("发送消息后没有出现“工作中 0 秒”状态");
  436. }
  437. return snapshot;
  438. }
  439. async function getWorkingStatusSnapshot() {
  440. return browser.execute(() => {
  441. const workingZeroPattern = /工作中\s*0\s*秒|Working for\s*0\s*(s|sec|second)/i;
  442. const candidates = Array.from(
  443. document.querySelectorAll<HTMLElement>(".history-message"),
  444. );
  445. const target = candidates.find((element) =>
  446. workingZeroPattern.test(element.innerText),
  447. );
  448. if (!target) {
  449. return null;
  450. }
  451. const rect = target.getBoundingClientRect();
  452. return {
  453. text: target.innerText,
  454. top: rect.top,
  455. };
  456. });
  457. }
  458. async function assertWorkingStatusPositionStable(initialTop: number) {
  459. await browser.pause(350);
  460. const snapshot = await browser.execute(() => {
  461. const workingPattern = /工作中\s*\d+\s*秒|Working for\s*\d+\s*(s|sec|second)/i;
  462. const target = Array.from(
  463. document.querySelectorAll<HTMLElement>(".history-message"),
  464. ).find((element) => workingPattern.test(element.innerText));
  465. if (!target) {
  466. return null;
  467. }
  468. return {
  469. text: target.innerText,
  470. top: target.getBoundingClientRect().top,
  471. };
  472. });
  473. if (!snapshot) {
  474. throw new Error("工作中状态在稳定性检查期间消失");
  475. }
  476. expect(Math.abs(snapshot.top - initialTop)).toBeLessThanOrEqual(2);
  477. }
  478. async function waitForBodyTextContaining(text: string, timeoutMsg: string) {
  479. await browser.waitUntil(async () => (await getBodyText()).includes(text), {
  480. timeout: 10000,
  481. timeoutMsg,
  482. });
  483. }
  484. async function assertBodyTextNotContaining(text: string) {
  485. expect(await getBodyText()).not.toContain(text);
  486. }
  487. async function getBodyText() {
  488. return browser.execute(() => document.body?.innerText ?? "");
  489. }
  490. async function waitForReasoningContentOpen() {
  491. await browser.waitUntil(
  492. async () =>
  493. browser.execute((contentTestId) => {
  494. const content = document.querySelector<HTMLElement>(
  495. `[data-testid="${contentTestId}"]`,
  496. );
  497. return (
  498. content?.getAttribute("data-state") === "open" &&
  499. content.innerText.trim().length > 0
  500. );
  501. }, TID_CHAT_REASONING_CONTENT),
  502. {
  503. timeout: 15000,
  504. timeoutMsg: "思考内容展开后没有保持 open 状态",
  505. },
  506. );
  507. }
  508. type CollapseSample = {
  509. elapsedMs: number;
  510. exists: boolean;
  511. height: number | null;
  512. state: string | null;
  513. text: string;
  514. };
  515. async function monitorReasoningCollapseDuringClick(): Promise<CollapseSample[]> {
  516. return browser.executeAsync(
  517. (triggerTestId, contentTestId, done) => {
  518. const trigger = document.querySelector<HTMLElement>(
  519. `[data-testid="${triggerTestId}"]`,
  520. );
  521. if (!trigger) {
  522. done([]);
  523. return;
  524. }
  525. const samples: CollapseSample[] = [];
  526. const startedAt = performance.now();
  527. const sample = () => {
  528. const content = document.querySelector<HTMLElement>(
  529. `[data-testid="${contentTestId}"]`,
  530. );
  531. const rect = content?.getBoundingClientRect();
  532. samples.push({
  533. elapsedMs: Math.round(performance.now() - startedAt),
  534. exists: Boolean(content),
  535. height: rect ? rect.height : null,
  536. state: content?.getAttribute("data-state") ?? null,
  537. text: content?.innerText ?? "",
  538. });
  539. if (performance.now() - startedAt < 420) {
  540. requestAnimationFrame(sample);
  541. return;
  542. }
  543. done(samples);
  544. };
  545. trigger.click();
  546. requestAnimationFrame(sample);
  547. },
  548. TID_CHAT_REASONING_TRIGGER,
  549. TID_CHAT_REASONING_CONTENT,
  550. );
  551. }
  552. async function waitForReasoningContentUnmounted() {
  553. await browser.waitUntil(
  554. async () =>
  555. browser.execute((contentTestId) => {
  556. return !document.querySelector(`[data-testid="${contentTestId}"]`);
  557. }, TID_CHAT_REASONING_CONTENT),
  558. {
  559. timeout: 5000,
  560. timeoutMsg: "思考内容收起动画结束后仍未卸载 DOM",
  561. },
  562. );
  563. }
  564. async function waitForWorkspaceTitleContaining(text: string) {
  565. await browser.waitUntil(
  566. async () => (await AppPage.workspaceTitle.getText()).includes(text),
  567. {
  568. timeout: 30000,
  569. timeoutMsg: `任务标题没有包含预期文本: ${text}`,
  570. },
  571. );
  572. }
  573. async function assertWorkspaceTitleNotContaining(text: string) {
  574. expect(await AppPage.workspaceTitle.getText()).not.toContain(text);
  575. }
  576. async function assertVisibleUserMessagesNotContaining(text: string) {
  577. expect(await getVisibleUserMessagesText()).not.toContain(text);
  578. }
  579. async function assertVisibleUserMessagesContaining(text: string) {
  580. expect(await getVisibleUserMessagesText()).toContain(text);
  581. }
  582. async function getVisibleUserMessagesText() {
  583. return browser.execute((messagesTestId) => {
  584. const root = document.querySelector<HTMLElement>(
  585. `[data-testid="${messagesTestId}"]`,
  586. );
  587. return Array.from(root?.querySelectorAll<HTMLElement>(".is-user") ?? [])
  588. .map((element) => element.innerText)
  589. .join("\n");
  590. }, TID_CHAT_MESSAGES);
  591. }