conversation-session-queue.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import {
  2. TID_CHAT_QUEUE_DRAG_HANDLE,
  3. TID_CHAT_QUEUE_EDIT_BUTTON,
  4. TID_CHAT_QUEUE_EDIT_INPUT,
  5. TID_CHAT_QUEUE_EDIT_SAVE_BUTTON,
  6. TID_CHAT_QUEUE_ITEM,
  7. TID_CHAT_QUEUE_ITEM_CONTENT,
  8. TID_CHAT_QUEUE_PANEL,
  9. TID_CHAT_QUEUE_REMOVE_BUTTON,
  10. TID_CHAT_QUEUE_SEND_NOW_BUTTON,
  11. TID_V4_QUEUE,
  12. TID_V4_QUEUE_ITEM,
  13. TID_V4_QUEUE_ITEM_DELETE,
  14. TID_V4_QUEUE_ITEM_EDIT,
  15. TID_V4_QUEUE_ITEM_SEND_NOW,
  16. } from "@zcode/shared/test-ids";
  17. import { clickTestIdByDom, setInputValueByTestIdDom } from "./desktop-app.js";
  18. export interface QueueItemSnapshot {
  19. content: string;
  20. id: string;
  21. index: number;
  22. kind: string | null;
  23. status: string | null;
  24. }
  25. export async function clickFirstQueueSendNow() {
  26. const items = await getQueueItems();
  27. const first = items[0];
  28. if (!first) {
  29. throw new Error("队列为空,无法点击立即发送");
  30. }
  31. await clickQueueSendNow(first.id);
  32. return first;
  33. }
  34. export async function clickQueueSendNow(promptId: string) {
  35. const items = await getQueueItems();
  36. const target = items.find((item) => item.id === promptId);
  37. if (!target) {
  38. throw new Error(
  39. `队列中不存在可立即发送的项 ${promptId}; latest=${JSON.stringify(items)}`,
  40. );
  41. }
  42. const buttonTestId = await resolveQueueActionTestId(
  43. target.id,
  44. TID_CHAT_QUEUE_SEND_NOW_BUTTON,
  45. TID_V4_QUEUE_ITEM_SEND_NOW,
  46. );
  47. await clickTestIdByDom(buttonTestId, {
  48. timeout: 15000,
  49. timeoutMsg: `队列项 ${target.id} 的立即发送按钮没有出现`,
  50. });
  51. return target;
  52. }
  53. export async function clickQueueEdit(promptId: string) {
  54. const buttonTestId = await resolveQueueActionTestId(
  55. promptId,
  56. TID_CHAT_QUEUE_EDIT_BUTTON,
  57. TID_V4_QUEUE_ITEM_EDIT,
  58. );
  59. await clickTestIdByDom(buttonTestId, {
  60. timeout: 15000,
  61. timeoutMsg: `队列项 ${promptId} 的编辑按钮没有出现`,
  62. });
  63. }
  64. export async function setQueueEditInputText(promptId: string, content: string) {
  65. await setInputValueByTestIdDom(
  66. `${TID_CHAT_QUEUE_EDIT_INPUT}-${promptId}`,
  67. content,
  68. {
  69. timeout: 15000,
  70. timeoutMsg: `队列项 ${promptId} 的编辑输入框没有出现`,
  71. },
  72. );
  73. }
  74. export async function clickQueueEditSave(promptId: string) {
  75. await clickTestIdByDom(`${TID_CHAT_QUEUE_EDIT_SAVE_BUTTON}-${promptId}`, {
  76. timeout: 15000,
  77. timeoutMsg: `队列项 ${promptId} 的编辑保存按钮没有出现`,
  78. });
  79. }
  80. export async function clickQueueRemove(promptId: string) {
  81. const buttonTestId = await resolveQueueActionTestId(
  82. promptId,
  83. TID_CHAT_QUEUE_REMOVE_BUTTON,
  84. TID_V4_QUEUE_ITEM_DELETE,
  85. );
  86. await clickTestIdByDom(buttonTestId, {
  87. timeout: 15000,
  88. timeoutMsg: `队列项 ${promptId} 的删除按钮没有出现`,
  89. });
  90. }
  91. export async function dragQueueItemOnto(
  92. sourcePromptId: string,
  93. targetPromptId: string,
  94. ) {
  95. const positions = (await browser.execute(
  96. (dragHandleTestId, targetItemTestId) => {
  97. const dragHandle = document.querySelector<HTMLElement>(
  98. `[data-testid="${dragHandleTestId}"]`,
  99. );
  100. const targetItem = document.querySelector<HTMLElement>(
  101. `[data-testid="${targetItemTestId}"]`,
  102. );
  103. if (!dragHandle || !targetItem) {
  104. return {
  105. ok: false,
  106. reason: "missing-element",
  107. hasDragHandle: Boolean(dragHandle),
  108. hasTargetItem: Boolean(targetItem),
  109. };
  110. }
  111. const dragRect = dragHandle.getBoundingClientRect();
  112. const targetRect = targetItem.getBoundingClientRect();
  113. return {
  114. ok: true,
  115. startX: Math.round(dragRect.left + dragRect.width / 2),
  116. startY: Math.round(dragRect.top + dragRect.height / 2),
  117. endX: Math.round(targetRect.left + targetRect.width / 2),
  118. endY: Math.round(targetRect.top + targetRect.height / 2),
  119. };
  120. },
  121. `${TID_CHAT_QUEUE_DRAG_HANDLE}-${sourcePromptId}`,
  122. `${TID_CHAT_QUEUE_ITEM}-${targetPromptId}`,
  123. )) as
  124. | { ok: true; startX: number; startY: number; endX: number; endY: number }
  125. | {
  126. ok: false;
  127. reason: string;
  128. hasDragHandle: boolean;
  129. hasTargetItem: boolean;
  130. };
  131. if (!positions.ok) {
  132. throw new Error(`队列项拖拽元素不存在: ${JSON.stringify(positions)}`);
  133. }
  134. await browser.performActions([
  135. {
  136. id: "queue-drag-pointer",
  137. type: "pointer",
  138. parameters: { pointerType: "mouse" },
  139. actions: [
  140. {
  141. type: "pointerMove",
  142. duration: 0,
  143. x: positions.startX,
  144. y: positions.startY,
  145. },
  146. { type: "pointerDown", button: 0 },
  147. { type: "pause", duration: 80 },
  148. {
  149. type: "pointerMove",
  150. duration: 120,
  151. x: positions.startX,
  152. y: positions.startY - 10,
  153. },
  154. {
  155. type: "pointerMove",
  156. duration: 260,
  157. x: positions.endX,
  158. y: positions.endY,
  159. },
  160. { type: "pointerUp", button: 0 },
  161. ],
  162. },
  163. ]);
  164. await browser.releaseActions();
  165. // 修复原因:dnd-kit 会在拖拽结束后屏蔽紧随其后的 click,避免 pointerup
  166. // 被误判成一次普通点击。WebDriver pointer actions 在这里不会稳定补发浏览器合成 click,
  167. // 因此测试里先用一次空白 click 消耗该屏蔽,后续按钮点击才等价于真实用户拖完再点。
  168. await browser.execute(() => {
  169. document.body.dispatchEvent(
  170. new MouseEvent("click", {
  171. bubbles: true,
  172. cancelable: true,
  173. view: window,
  174. }),
  175. );
  176. });
  177. await browser.pause(50);
  178. }
  179. export async function waitForQueueOrderContaining(
  180. expectedTokens: readonly string[],
  181. timeoutMsg?: string,
  182. ) {
  183. let latest: QueueItemSnapshot[] = [];
  184. try {
  185. await browser.waitUntil(
  186. async () => {
  187. latest = await getQueueItems();
  188. if (latest.length < expectedTokens.length) {
  189. return false;
  190. }
  191. return expectedTokens.every((token, index) =>
  192. latest[index]?.content.includes(token),
  193. );
  194. },
  195. {
  196. timeout: 30000,
  197. timeoutMsg:
  198. timeoutMsg ?? `队列顺序不符合预期 ${JSON.stringify(expectedTokens)}`,
  199. },
  200. );
  201. } catch (error) {
  202. // 修复原因:WebdriverIO 的 timeoutMsg 是等待开始时固定的字符串,
  203. // 队列枚举失败时必须补采最终 UI 队列,才能判断是产品状态错了还是测试动作没命中。
  204. latest = await getQueueItems();
  205. throw new Error(
  206. `${timeoutMsg ?? `队列顺序不符合预期 ${JSON.stringify(expectedTokens)}`}; latest=${JSON.stringify(latest)}`,
  207. {
  208. cause: error,
  209. },
  210. );
  211. }
  212. return latest;
  213. }
  214. export async function waitForQueueCount(count: number, timeoutMsg?: string) {
  215. let latest: QueueItemSnapshot[] = [];
  216. try {
  217. await browser.waitUntil(
  218. async () => {
  219. latest = await getQueueItems();
  220. return latest.length === count;
  221. },
  222. {
  223. timeout: 30000,
  224. timeoutMsg: timeoutMsg ?? `队列数量没有变为 ${count}`,
  225. },
  226. );
  227. } catch (error) {
  228. // 修复原因同 waitForQueueOrderContaining:失败证据必须来自最后一帧队列 DOM。
  229. latest = await getQueueItems();
  230. throw new Error(
  231. `${timeoutMsg ?? `队列数量没有变为 ${count}`}; latest=${JSON.stringify(latest)}`,
  232. {
  233. cause: error,
  234. },
  235. );
  236. }
  237. return latest;
  238. }
  239. export function getQueueItems(): Promise<QueueItemSnapshot[]> {
  240. return browser.execute(
  241. (
  242. queueItemPrefix,
  243. queueContentPrefix,
  244. queuePanelTestId,
  245. v4QueueItemPrefix,
  246. v4QueuePanelTestId,
  247. ) => {
  248. const panel =
  249. document.querySelector<HTMLElement>(
  250. `[data-testid="${v4QueuePanelTestId}"]`,
  251. ) ??
  252. document.querySelector<HTMLElement>(
  253. `[data-testid="${queuePanelTestId}"]`,
  254. );
  255. if (!panel) {
  256. return [];
  257. }
  258. const activeItemPrefix =
  259. panel.getAttribute("data-testid") === v4QueuePanelTestId
  260. ? v4QueueItemPrefix
  261. : queueItemPrefix;
  262. return Array.from(
  263. panel.querySelectorAll<HTMLElement>(
  264. `li[data-testid^="${activeItemPrefix}-"][data-queue-item-id]`,
  265. ),
  266. ).map((element, index) => {
  267. const id = element.getAttribute("data-queue-item-id") ?? "";
  268. const legacyContent = element.querySelector<HTMLElement>(
  269. `[data-testid="${queueContentPrefix}-${id}"]`,
  270. );
  271. const v4Content = element.querySelector<HTMLElement>("span[title]");
  272. const rawKind = element.getAttribute("data-kind");
  273. return {
  274. // 修复原因:V4 队列迁移后不再渲染 chat-queue-item-content,正文保存在
  275. // title span;旧 helper 把真实 queue 读成空,导致 guide/verifier 与 send-now
  276. // E2E 都误报丢消息。这里统一成历史 helper 的 text/goal/compact 术语。
  277. content: (
  278. legacyContent?.innerText ??
  279. v4Content?.getAttribute("title") ??
  280. v4Content?.innerText ??
  281. ""
  282. )
  283. .replace(/\u00a0/g, " ")
  284. .trim(),
  285. id,
  286. index,
  287. kind:
  288. rawKind === "sendText"
  289. ? "text"
  290. : rawKind === "sendGoalCommand"
  291. ? "goal"
  292. : rawKind,
  293. status:
  294. element.getAttribute("data-status") ??
  295. element.getAttribute("data-dispatch-state"),
  296. };
  297. });
  298. },
  299. TID_CHAT_QUEUE_ITEM,
  300. TID_CHAT_QUEUE_ITEM_CONTENT,
  301. TID_CHAT_QUEUE_PANEL,
  302. TID_V4_QUEUE_ITEM,
  303. TID_V4_QUEUE,
  304. );
  305. }
  306. async function resolveQueueActionTestId(
  307. promptId: string,
  308. legacyPrefix: string,
  309. v4Prefix: string,
  310. ) {
  311. const testIdValue = await browser.execute(
  312. (itemId, legacyActionPrefix, v4ActionPrefix) => {
  313. const v4TestId = `${v4ActionPrefix}-${itemId}`;
  314. if (document.querySelector(`[data-testid="${v4TestId}"]`)) {
  315. return v4TestId;
  316. }
  317. const legacyTestId = `${legacyActionPrefix}-${itemId}`;
  318. if (document.querySelector(`[data-testid="${legacyTestId}"]`)) {
  319. return legacyTestId;
  320. }
  321. return v4TestId;
  322. },
  323. promptId,
  324. legacyPrefix,
  325. v4Prefix,
  326. );
  327. return testIdValue;
  328. }
  329. export async function waitForQueueContaining(text: string) {
  330. let latest: QueueItemSnapshot[] = [];
  331. try {
  332. await browser.waitUntil(
  333. async () => {
  334. latest = await getQueueItems();
  335. return latest.some((item) => item.content.includes(text));
  336. },
  337. {
  338. timeout: 30000,
  339. timeoutMsg: `队列中没有出现 ${text}`,
  340. },
  341. );
  342. } catch (error) {
  343. latest = await getQueueItems();
  344. throw new Error(
  345. `队列中没有出现 ${text}; latest=${JSON.stringify(latest)}`,
  346. {
  347. cause: error,
  348. },
  349. );
  350. }
  351. }