v4-composer-edit.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Composer 编辑态 E2E 原语(docs/ui/composer-edit-mode.md)。
  2. // 编辑最后一条消息与编辑排队消息都复用底部 composer:状态读 TID_V4_COMPOSER_EDIT_BAR 的 data-*,
  3. // 原位置占位读 TID_V4_EDIT_IN_COMPOSER_HINT / TID_V4_QUEUE_ITEM_EDIT_LOCK。
  4. // 本文件只依赖 desktop-app 与 test-ids,供 v4-conversation.ts 复用而不形成循环依赖。
  5. import {
  6. TID_CONFIRM_DIALOG_CONFIRM,
  7. TID_V4_COMPOSER,
  8. TID_V4_COMPOSER_EDIT_APPLY_QUEUE_DIALOG,
  9. TID_V4_COMPOSER_EDIT_BAR,
  10. TID_V4_COMPOSER_EDIT_CANCEL,
  11. TID_V4_COMPOSER_EDIT_SWITCH_DIALOG,
  12. TID_V4_EDIT,
  13. TID_V4_EDIT_IN_COMPOSER_HINT,
  14. TID_V4_QUEUE_ITEM_EDIT,
  15. TID_V4_QUEUE_ITEM_EDIT_LOCK,
  16. TID_V4_QUEUE_ITEM_RELEASE_EDIT,
  17. testId,
  18. } from "@zcode/shared/test-ids";
  19. import { clickTestIdByDom } from "./desktop-app.js";
  20. export type V4ComposerEditTargetKind = "userQuery" | "queueItem";
  21. export interface V4ComposerEditState {
  22. kind: V4ComposerEditTargetKind | null;
  23. targetId: string | null;
  24. state: "editing" | "invalid" | null;
  25. invalidReason: string | null;
  26. text: string;
  27. hasCancel: boolean;
  28. }
  29. /** 读取 composer 编辑态状态条;不在编辑态时返回 null。 */
  30. export function getV4ComposerEditState(): Promise<V4ComposerEditState | null> {
  31. return browser.execute(
  32. (barTestId, cancelTestId) => {
  33. const bar = document.querySelector<HTMLElement>(`[data-testid="${barTestId}"]`);
  34. if (!bar) return null;
  35. const kind = bar.getAttribute("data-edit-target-kind");
  36. const state = bar.getAttribute("data-edit-state");
  37. return {
  38. kind: kind === "userQuery" || kind === "queueItem" ? kind : null,
  39. targetId: bar.getAttribute("data-edit-target-id"),
  40. state: state === "editing" || state === "invalid" ? state : null,
  41. invalidReason: bar.getAttribute("data-edit-invalid-reason"),
  42. text: (bar.innerText || "").replace(/\u00a0/g, " ").trim(),
  43. hasCancel: Boolean(bar.querySelector(`[data-testid="${cancelTestId}"]`)),
  44. };
  45. },
  46. TID_V4_COMPOSER_EDIT_BAR,
  47. TID_V4_COMPOSER_EDIT_CANCEL,
  48. );
  49. }
  50. export async function waitForV4ComposerEditState(
  51. predicate: (state: V4ComposerEditState | null) => boolean,
  52. timeoutMsg: string,
  53. timeout = 15000,
  54. ): Promise<V4ComposerEditState | null> {
  55. let latest: V4ComposerEditState | null = null;
  56. try {
  57. await browser.waitUntil(
  58. async () => {
  59. latest = await getV4ComposerEditState();
  60. return predicate(latest);
  61. },
  62. { timeout, timeoutMsg },
  63. );
  64. } catch (error) {
  65. throw new Error(`${timeoutMsg}: ${JSON.stringify(latest)}`, { cause: error });
  66. }
  67. return latest;
  68. }
  69. /** 等待 composer 退出编辑态(状态条消失)。 */
  70. export async function waitForV4ComposerEditClosed(timeoutMsg: string, timeout = 15000) {
  71. await waitForV4ComposerEditState((state) => state === null, timeoutMsg, timeout);
  72. }
  73. /** 点击状态条「取消」。编辑态不支持 Esc 取消,这里只走按钮。 */
  74. export async function clickV4ComposerEditCancel() {
  75. await clickTestIdByDom(TID_V4_COMPOSER_EDIT_CANCEL, {
  76. timeout: 15000,
  77. timeoutMsg: "composer 编辑态没有可点击的取消按钮",
  78. });
  79. }
  80. /** 编辑中切换目标且已有改动时,确认丢弃并切换。 */
  81. export async function confirmV4ComposerEditSwitch() {
  82. await clickTestIdByDom(TID_CONFIRM_DIALOG_CONFIRM, {
  83. timeout: 15000,
  84. timeoutMsg: "composer 编辑态切换确认弹窗没有出现确认按钮",
  85. });
  86. }
  87. /** 编辑提交后「是否也应用于所有排队消息」弹窗出现,返回其可见文本。 */
  88. export async function waitForV4ComposerEditApplyQueueDialog(timeout = 15000): Promise<string> {
  89. let text = "";
  90. await browser.waitUntil(
  91. async () => {
  92. text = await browser.execute((dialogTestId) => {
  93. const dialog = document.querySelector<HTMLElement>(`[data-testid="${dialogTestId}"]`);
  94. return dialog ? (dialog.innerText || "").trim() : "";
  95. }, TID_V4_COMPOSER_EDIT_APPLY_QUEUE_DIALOG);
  96. return text.length > 0;
  97. },
  98. { timeout, timeoutMsg: "编辑提交后没有出现「应用于所有排队消息」弹窗" },
  99. );
  100. return text;
  101. }
  102. /** 选「应用到排队消息」(弹窗确认按钮)。 */
  103. export async function confirmV4ComposerEditApplyQueue() {
  104. await clickTestIdByDom(TID_CONFIRM_DIALOG_CONFIRM, {
  105. timeout: 15000,
  106. timeoutMsg: "「应用于所有排队消息」弹窗没有出现确认按钮",
  107. });
  108. }
  109. export function hasV4ComposerEditSwitchDialog(): Promise<boolean> {
  110. return browser.execute(
  111. (dialogTestId) => Boolean(document.querySelector(`[data-testid="${dialogTestId}"]`)),
  112. TID_V4_COMPOSER_EDIT_SWITCH_DIALOG,
  113. );
  114. }
  115. /** 被编辑 user 行的占位与编辑入口状态。 */
  116. export function getV4UserRowEditHint(
  117. rowId: string,
  118. ): Promise<{ hint: boolean; hintText: string; editAction: boolean }> {
  119. return browser.execute(
  120. (hintTestId, editTestId) => {
  121. const hint = document.querySelector<HTMLElement>(`[data-testid="${hintTestId}"]`);
  122. return {
  123. hint: Boolean(hint),
  124. hintText: (hint?.innerText || "").trim(),
  125. editAction: Boolean(document.querySelector(`[data-testid="${editTestId}"]`)),
  126. };
  127. },
  128. testId(TID_V4_EDIT_IN_COMPOSER_HINT, rowId),
  129. testId(TID_V4_EDIT, rowId),
  130. );
  131. }
  132. export async function clickV4UserRowEditHint(rowId: string) {
  133. await clickTestIdByDom(testId(TID_V4_EDIT_IN_COMPOSER_HINT, rowId), {
  134. timeout: 15000,
  135. timeoutMsg: `user 行没有「正在下方编辑」占位: rowId=${rowId}`,
  136. });
  137. }
  138. export interface V4QueueItemEditLockState {
  139. exists: boolean;
  140. locked: boolean;
  141. owner: "self" | "other" | null;
  142. lockText: string;
  143. releasable: boolean;
  144. editAction: boolean;
  145. deleteAction: boolean;
  146. sendNowAction: boolean;
  147. }
  148. /** 读取 queue 项的编辑锁投影与操作入口。 */
  149. export function getV4QueueItemEditLock(queueItemId: string): Promise<V4QueueItemEditLockState> {
  150. return browser.execute(
  151. (id, lockTestId, releaseTestId) => {
  152. const item = document.querySelector<HTMLElement>(`li[data-queue-item-id="${id}"]`);
  153. const lock = document.querySelector<HTMLElement>(`[data-testid="${lockTestId}"]`);
  154. const owner = lock?.getAttribute("data-edit-lock-owner");
  155. const enabled = (testIdValue: string) => {
  156. const button = document.querySelector<HTMLButtonElement>(`[data-testid="${testIdValue}"]`);
  157. return Boolean(
  158. button && !button.disabled && button.getAttribute("aria-disabled") !== "true",
  159. );
  160. };
  161. return {
  162. exists: Boolean(item),
  163. locked: item?.getAttribute("data-edit-locked") === "true",
  164. owner: owner === "self" || owner === "other" ? owner : null,
  165. lockText: (lock?.innerText || "").trim(),
  166. releasable: enabled(releaseTestId),
  167. editAction: enabled(`v4-queue-item-edit-${id}`),
  168. deleteAction: enabled(`v4-queue-item-delete-${id}`),
  169. sendNowAction: enabled(`v4-queue-item-send-now-${id}`),
  170. };
  171. },
  172. queueItemId,
  173. testId(TID_V4_QUEUE_ITEM_EDIT_LOCK, queueItemId),
  174. testId(TID_V4_QUEUE_ITEM_RELEASE_EDIT, queueItemId),
  175. );
  176. }
  177. export async function waitForV4QueueItemEditLock(
  178. queueItemId: string,
  179. predicate: (state: V4QueueItemEditLockState) => boolean,
  180. timeoutMsg: string,
  181. timeout = 30000,
  182. ): Promise<V4QueueItemEditLockState> {
  183. let latest: V4QueueItemEditLockState | null = null;
  184. try {
  185. await browser.waitUntil(
  186. async () => {
  187. latest = await getV4QueueItemEditLock(queueItemId);
  188. return predicate(latest);
  189. },
  190. { timeout, timeoutMsg },
  191. );
  192. } catch (error) {
  193. throw new Error(`${timeoutMsg}: ${JSON.stringify(latest)}`, { cause: error });
  194. }
  195. if (!latest) throw new Error(`${timeoutMsg}: queue 项编辑锁状态为空`);
  196. return latest;
  197. }
  198. /** 其他窗口或设备持有锁时,手动「解除」。 */
  199. export async function clickV4QueueItemReleaseEdit(queueItemId: string) {
  200. await clickTestIdByDom(testId(TID_V4_QUEUE_ITEM_RELEASE_EDIT, queueItemId), {
  201. timeout: 15000,
  202. timeoutMsg: `queue 项没有可点击的解除按钮: ${queueItemId}`,
  203. });
  204. }
  205. /**
  206. * 点击 latest user query 的编辑入口,等待 composer 进入 userQuery 编辑态,返回 rowId。
  207. * 修复原因(沿用行内编辑时期的根因):compact success 会先投影成功 marker,再清理 active
  208. * work;两帧之间旧 edit action 可能短暂可见后被 canEdit 收口撤下。这里只在入口仍存在时
  209. * 重试点击,成功判据是状态条进入 editing,而不是“点到了按钮”。
  210. */
  211. export async function openLatestV4UserQueryEditInComposer(timeout = 15000): Promise<string> {
  212. let lastClickedRowId: string | null = null;
  213. let lastClickedAt = 0;
  214. let rowId: string | null = null;
  215. await browser.waitUntil(
  216. async () => {
  217. const state = await getV4ComposerEditState();
  218. if (state?.kind === "userQuery" && state.state === "editing" && state.targetId) {
  219. rowId = state.targetId;
  220. return true;
  221. }
  222. const actionRowId = await browser.execute((editPrefix) => {
  223. const pattern = new RegExp(`^${editPrefix}-(\\d+)$`);
  224. const action = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]"))
  225. .filter((candidate) => pattern.test(candidate.dataset.testid ?? ""))
  226. .at(-1);
  227. return pattern.exec(action?.dataset.testid ?? "")?.[1] ?? null;
  228. }, TID_V4_EDIT);
  229. if (!actionRowId) return false;
  230. const now = Date.now();
  231. if (lastClickedRowId !== actionRowId || now - lastClickedAt >= 500) {
  232. lastClickedRowId = actionRowId;
  233. lastClickedAt = now;
  234. await clickTestIdByDom(testId(TID_V4_EDIT, actionRowId), {
  235. timeout: 2000,
  236. timeoutMsg: `v4 编辑按钮在 projection 收口时消失: rowId=${actionRowId}`,
  237. }).catch(() => undefined);
  238. }
  239. return false;
  240. },
  241. { timeout, interval: 100, timeoutMsg: "latest user query 没有进入 composer 编辑态" },
  242. );
  243. if (!rowId) throw new Error("latest user query 编辑态缺少 rowId");
  244. return rowId;
  245. }
  246. /** 点击 user 行编辑入口(按 rowId),等待 composer 进入该行的编辑态。 */
  247. export async function openV4UserQueryEditInComposer(rowId: string, timeout = 15000) {
  248. await clickTestIdByDom(testId(TID_V4_EDIT, rowId), {
  249. timeout,
  250. timeoutMsg: `user 行没有可点击的编辑按钮: rowId=${rowId}`,
  251. });
  252. await waitForV4ComposerEditState(
  253. (state) => state?.kind === "userQuery" && state.targetId === rowId && state.state === "editing",
  254. `user 行没有进入 composer 编辑态: rowId=${rowId}`,
  255. timeout,
  256. );
  257. }
  258. /** 点击 queue 项编辑入口,等待 lock accepted 后 composer 进入 queueItem 编辑态。 */
  259. export async function openV4QueueItemEditInComposer(queueItemId: string, timeout = 30000) {
  260. await clickTestIdByDom(testId(TID_V4_QUEUE_ITEM_EDIT, queueItemId), {
  261. timeout: 15000,
  262. timeoutMsg: `queue 项没有可点击的编辑按钮: ${queueItemId}`,
  263. });
  264. await waitForV4ComposerEditState(
  265. (state) =>
  266. state?.kind === "queueItem" && state.targetId === queueItemId && state.state === "editing",
  267. `queue 项没有进入 composer 编辑态: ${queueItemId}`,
  268. timeout,
  269. );
  270. }
  271. /** 删除 composer 中第 index 个附件(编辑态与普通草稿共用同一附件列表)。 */
  272. export async function removeV4ComposerAttachmentAt(index: number) {
  273. const removed = await browser.execute(
  274. (composerTestId, targetIndex) => {
  275. const composer = document.querySelector<HTMLElement>(`[data-testid="${composerTestId}"]`);
  276. const buttons = Array.from(
  277. composer?.querySelectorAll<HTMLButtonElement>("[data-composer-attachment-remove]") ?? [],
  278. );
  279. const button = buttons[targetIndex];
  280. button?.click();
  281. return Boolean(button);
  282. },
  283. TID_V4_COMPOSER,
  284. index,
  285. );
  286. if (!removed) throw new Error(`composer 没有第 ${index} 个附件删除按钮`);
  287. }