v4-message-actions.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {
  2. TID_V4_COMPOSER_INPUT,
  3. TID_V4_COMPOSER_SEND,
  4. TID_V4_SESSION_PANE,
  5. testId,
  6. } from "@zcode/shared/test-ids";
  7. import { clickTestIdByDom, setInputValueByTestIdDom } from "#e2e/helpers/desktop-app";
  8. import { openV4UserQueryEditInComposer } from "#e2e/helpers/v4-composer-edit";
  9. type MessageAction = "edit" | "fork";
  10. type MessageRole = "user" | "assistant";
  11. // 原因:旧消息 helper 读取 chat-message-id;V4 动作使用 canonical rowId,
  12. // 两者不能互换。定位同时限定主 pane、消息角色和正文,避免点到其他分屏的同名回复。
  13. export function getV4MessageAction(role: MessageRole, action: MessageAction, text: string) {
  14. return browser.execute(
  15. (paneId, expectedRole, actionName, expectedText) => {
  16. const pane = document.querySelector(`[data-testid="${paneId}"]`);
  17. const message = Array.from(pane?.querySelectorAll<HTMLElement>("[data-row-id]") ?? []).find(
  18. (row) =>
  19. row.classList.contains(`group/${expectedRole}-row`) &&
  20. row.innerText.replace(/\u00a0/g, " ").includes(expectedText),
  21. );
  22. const messageId = message?.getAttribute("data-row-id");
  23. if (!messageId) return null;
  24. const actionTestId = `v4-${actionName}-${messageId}`;
  25. const button = pane?.querySelector<HTMLButtonElement>(`[data-testid="${actionTestId}"]`);
  26. return {
  27. messageId,
  28. testId: actionTestId,
  29. exists: Boolean(button),
  30. disabled: button?.disabled ?? false,
  31. ariaDisabled: button?.getAttribute("aria-disabled") === "true",
  32. };
  33. },
  34. testId(TID_V4_SESSION_PANE, "workspace-main"),
  35. role,
  36. action,
  37. text,
  38. );
  39. }
  40. export async function waitForV4MessageAction(
  41. role: MessageRole,
  42. action: MessageAction,
  43. text: string,
  44. ) {
  45. const result: { latest: Awaited<ReturnType<typeof getV4MessageAction>> } = { latest: null };
  46. await browser.waitUntil(
  47. async () => {
  48. const latest = await getV4MessageAction(role, action, text);
  49. result.latest = latest;
  50. return Boolean(latest?.exists && !latest.disabled && !latest.ariaDisabled);
  51. },
  52. { timeout: 30000, timeoutMsg: `V4 ${role} 消息没有可用 ${action} 动作: ${text}` },
  53. );
  54. if (!result.latest) throw new Error(`V4 消息不存在: ${text}`);
  55. return result.latest;
  56. }
  57. /** 通过 composer 编辑态提交指定 user 消息:编辑态下的发送按钮路由到 editUserQuery。 */
  58. export async function editV4UserMessageContaining(currentText: string, nextText: string) {
  59. const action = await waitForV4MessageAction("user", "edit", currentText);
  60. await openV4UserQueryEditInComposer(action.messageId);
  61. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, nextText);
  62. await clickTestIdByDom(TID_V4_COMPOSER_SEND);
  63. return action;
  64. }
  65. export async function forkV4AssistantContaining(text: string) {
  66. const action = await waitForV4MessageAction("assistant", "fork", text);
  67. const button = await $(`[data-testid="${action.testId}"]`);
  68. await button.scrollIntoView();
  69. await button.moveTo();
  70. await button.click();
  71. return action;
  72. }