v4-message-actions.ts 2.8 KB

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