conversation-session-task-title-marquee.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import {
  2. TID_TASK_ITEM,
  3. TID_WORKSPACE_HEADER,
  4. TID_WORKSPACE_PATH,
  5. TID_WORKSPACE_TITLE,
  6. testId,
  7. } from "@zcode/shared/test-ids";
  8. import { DEFAULT_WORKSPACE, clearAppData, waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  9. import { ensureProjectExpanded } from "#e2e/helpers/project-sidebar";
  10. import {
  11. TASK_TITLE_MARQUEE_LONG_ID,
  12. TASK_TITLE_MARQUEE_PINNED_ID,
  13. TASK_TITLE_MARQUEE_SHORT_ID,
  14. } from "#e2e/helpers/task-title-marquee-fixture";
  15. const CASE_TIMEOUT_MS = 180_000;
  16. interface TitleSnapshot {
  17. animationCount: number;
  18. copyCount: number;
  19. duplicateAriaHidden: string | null;
  20. maskImage: string;
  21. nativeTitle: string | null;
  22. overflow: boolean;
  23. rowWidth: number;
  24. titleWidth: number;
  25. trackWidth: number;
  26. }
  27. describe("TTL:Task 名称溢出走马灯", () => {
  28. before(async () => {
  29. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  30. await ensureProjectExpanded(DEFAULT_WORKSPACE);
  31. await waitForTask(TASK_TITLE_MARQUEE_LONG_ID);
  32. });
  33. afterEach(async () => {
  34. await setReducedMotion(false);
  35. await browser.execute(() => document.documentElement.classList.remove("dark"));
  36. });
  37. after(async () => {
  38. await browser.electron.restoreAllMocks();
  39. await clearAppData();
  40. });
  41. it("TTL01:短标题不挂载 mask、副本、动画或原生 title", async function () {
  42. this.timeout(CASE_TIMEOUT_MS);
  43. const snapshot = await readTitleSnapshot(TASK_TITLE_MARQUEE_SHORT_ID);
  44. expect(snapshot.overflow).toBe(false);
  45. expect(snapshot.copyCount).toBe(1);
  46. expect(snapshot.maskImage).toBe("none");
  47. expect(snapshot.nativeTitle).toBe(null);
  48. await hoverTaskRow(TASK_TITLE_MARQUEE_SHORT_ID);
  49. expect((await readTitleSnapshot(TASK_TITLE_MARQUEE_SHORT_ID)).animationCount).toBe(0);
  50. });
  51. it("TTL02:Project 长标题仅在溢出时显示 24px mask,整行非文字区域 hover 启动动画", async function () {
  52. this.timeout(CASE_TIMEOUT_MS);
  53. const beforeHover = await readTitleSnapshot(TASK_TITLE_MARQUEE_LONG_ID);
  54. expect(beforeHover.overflow).toBe(true);
  55. expect(beforeHover.copyCount).toBe(2);
  56. expect(beforeHover.duplicateAriaHidden).toBe("true");
  57. expect(beforeHover.maskImage).toContain("24px");
  58. expect(beforeHover.nativeTitle).toBe(null);
  59. const hoveredOutsideTitle = await hoverTaskRow(TASK_TITLE_MARQUEE_LONG_ID);
  60. expect(hoveredOutsideTitle).toBe(true);
  61. await waitForAnimationCount(TASK_TITLE_MARQUEE_LONG_ID, 2);
  62. });
  63. it("TTL03:主体尾部到达左边缘时 mask 直接切换,副本头部对齐后停留 2 秒再循环", async function () {
  64. this.timeout(CASE_TIMEOUT_MS);
  65. await hoverTaskRow(TASK_TITLE_MARQUEE_LONG_ID);
  66. // Bug 根因:重复 mouseenter 会先取消上一条 case 留下的动画,再经过 1 秒
  67. // hover delay 创建新动画;立即读取会落在 effect 为空的竞态窗口。
  68. await waitForAnimationCount(TASK_TITLE_MARQUEE_LONG_ID, 2);
  69. const timeline = await readAnimationTimeline(TASK_TITLE_MARQUEE_LONG_ID);
  70. expect(timeline.animations).toHaveLength(2);
  71. expect(timeline.trackOffsets).toHaveLength(3);
  72. expect(timeline.maskOffsets).toHaveLength(5);
  73. expect(timeline.maskOffsets[2]).toBeCloseTo(timeline.maskOffsets[3] ?? -1, 8);
  74. expect(timeline.maskFrames[2]).toContain("transparent 0px");
  75. expect(timeline.maskFrames[3]).toContain("black 0px");
  76. expect(timeline.pauseMs).toBeCloseTo(2_000, -1);
  77. expect(timeline.distance).toBeCloseTo(timeline.originalWidth + 24, 0);
  78. expect(timeline.finalTransform).toBe(timeline.movementTransform);
  79. });
  80. it("TTL04:mouseleave 立即复位,再次 hover 从起点重新滚动", async function () {
  81. this.timeout(CASE_TIMEOUT_MS);
  82. await hoverTaskRow(TASK_TITLE_MARQUEE_LONG_ID);
  83. await waitForAnimationCount(TASK_TITLE_MARQUEE_LONG_ID, 2);
  84. await setAnimationCurrentTime(TASK_TITLE_MARQUEE_LONG_ID, 2_000);
  85. await leaveTaskRow(TASK_TITLE_MARQUEE_LONG_ID);
  86. const reset = await readTitleSnapshot(TASK_TITLE_MARQUEE_LONG_ID);
  87. expect(reset.animationCount).toBe(0);
  88. expect(reset.maskImage).toContain("calc(100% - 24px)");
  89. await hoverTaskRow(TASK_TITLE_MARQUEE_LONG_ID);
  90. await waitForAnimationCount(TASK_TITLE_MARQUEE_LONG_ID, 2);
  91. const restarted = await readAnimationTimeline(TASK_TITLE_MARQUEE_LONG_ID);
  92. expect(restarted.currentTimes.every((time) => time < 500)).toBe(true);
  93. });
  94. it("TTL05:ResizeObserver 驱动 fit → overflow → fit,并同步副本与 mask", async function () {
  95. this.timeout(CASE_TIMEOUT_MS);
  96. await setTitleProbeWidth(TASK_TITLE_MARQUEE_LONG_ID, 2_000);
  97. await waitForOverflow(TASK_TITLE_MARQUEE_LONG_ID, false);
  98. expect((await readTitleSnapshot(TASK_TITLE_MARQUEE_LONG_ID)).copyCount).toBe(1);
  99. await setTitleProbeWidth(TASK_TITLE_MARQUEE_LONG_ID, 100);
  100. await waitForOverflow(TASK_TITLE_MARQUEE_LONG_ID, true);
  101. expect((await readTitleSnapshot(TASK_TITLE_MARQUEE_LONG_ID)).copyCount).toBe(2);
  102. await setTitleProbeWidth(TASK_TITLE_MARQUEE_LONG_ID, 2_000);
  103. await waitForOverflow(TASK_TITLE_MARQUEE_LONG_ID, false);
  104. await setTitleProbeWidth(TASK_TITLE_MARQUEE_LONG_ID, null);
  105. await waitForOverflow(TASK_TITLE_MARQUEE_LONG_ID, true);
  106. });
  107. it("TTL06:reduced-motion 下保留静态溢出提示但不启动动画", async function () {
  108. this.timeout(CASE_TIMEOUT_MS);
  109. await setReducedMotion(true);
  110. await hoverTaskRow(TASK_TITLE_MARQUEE_LONG_ID);
  111. const snapshot = await readTitleSnapshot(TASK_TITLE_MARQUEE_LONG_ID);
  112. expect(snapshot.overflow).toBe(true);
  113. expect(snapshot.copyCount).toBe(2);
  114. expect(snapshot.animationCount).toBe(0);
  115. });
  116. it("TTL07:项目任务行、全局置顶区与深色主题复用相同溢出语义", async function () {
  117. this.timeout(CASE_TIMEOUT_MS);
  118. // Timeline / Group 视图已随虚拟项目重构下线;剩余展示形态是项目树任务行与全局置顶区。
  119. await assertOverflowSurface(TASK_TITLE_MARQUEE_LONG_ID);
  120. await assertOverflowSurface(TASK_TITLE_MARQUEE_PINNED_ID);
  121. await browser.execute(() => document.documentElement.classList.add("dark"));
  122. await assertOverflowSurface(TASK_TITLE_MARQUEE_PINNED_ID);
  123. // 任务标题上下文入口:打开任务后,工作区图标位于名称前,hover/点按均可查看信息。
  124. await ensureProjectExpanded(DEFAULT_WORKSPACE);
  125. const task = await $(`[data-testid="${testId(TID_TASK_ITEM, TASK_TITLE_MARQUEE_SHORT_ID)}"]`);
  126. await task.click();
  127. const icon = await $(
  128. `[data-testid="${TID_WORKSPACE_HEADER}"] [data-testid="${TID_WORKSPACE_PATH}"]`,
  129. );
  130. await icon.waitForDisplayed({ timeout: 15000 });
  131. expect(await icon.getTagName()).toBe("button");
  132. expect(
  133. await browser.execute(
  134. (pathId, titleId) => {
  135. const icon = document.querySelector(`[data-testid="${pathId}"]`)!;
  136. const title = document.querySelector(`[data-testid="${titleId}"]`)!;
  137. return Boolean(icon.compareDocumentPosition(title) & Node.DOCUMENT_POSITION_FOLLOWING);
  138. },
  139. TID_WORKSPACE_PATH,
  140. TID_WORKSPACE_TITLE,
  141. ),
  142. ).toBe(true);
  143. expect(await $("[data-workspace-header-branch]").isExisting()).toBe(false);
  144. await icon.moveTo();
  145. const info = await $("[data-workspace-header-context-info]");
  146. await info.waitForDisplayed({ timeout: 5000 });
  147. expect(await info.getText()).toContain("ZCodeProject");
  148. expect(await info.$("[data-workspace-context-path]").getText()).toContain("ZCodeProject");
  149. const activity = await info.$("[data-workspace-last-activity]");
  150. await activity.waitForDisplayed({ timeout: 10000 });
  151. expect(await activity.getText()).toMatch(/Last active|最近活动/);
  152. expect(await info.$("[data-workspace-chat-counts]").isExisting()).toBe(false);
  153. await $(`[data-testid="${TID_WORKSPACE_TITLE}"]`).moveTo();
  154. await icon.click();
  155. await info.waitForDisplayed({ timeout: 5000 });
  156. });
  157. });
  158. async function waitForTask(taskId: string) {
  159. const selectors = taskRowSelectors(taskId);
  160. await browser.waitUntil(
  161. () =>
  162. browser.execute(
  163. (values) => values.some((selector) => Boolean(document.querySelector(selector))),
  164. selectors,
  165. ),
  166. { timeout: 30_000, timeoutMsg: `侧栏未显示 task: ${taskId}` },
  167. );
  168. }
  169. function taskRowSelectors(taskId: string): string[] {
  170. return [`[data-testid="${testId(TID_TASK_ITEM, taskId)}"]`];
  171. }
  172. async function readTitleSnapshot(taskId: string): Promise<TitleSnapshot> {
  173. return browser.execute(
  174. (selectors, id) => {
  175. const row = selectors
  176. .map((selector) => document.querySelector<HTMLElement>(selector))
  177. .find(Boolean);
  178. const original = row?.querySelector<HTMLElement>('[data-task-title-copy="original"]');
  179. const title = original?.parentElement?.parentElement as HTMLElement | null;
  180. const track = original?.parentElement as HTMLElement | null;
  181. if (!row || !original || !title || !track) throw new Error(`找不到 task title: ${id}`);
  182. const style = getComputedStyle(title);
  183. return {
  184. animationCount: title.getAnimations({ subtree: true }).length,
  185. copyCount: track.querySelectorAll("[data-task-title-copy]").length,
  186. duplicateAriaHidden:
  187. track.querySelector('[data-task-title-copy="duplicate"]')?.getAttribute("aria-hidden") ??
  188. null,
  189. maskImage: style.maskImage || style.webkitMaskImage,
  190. nativeTitle: title.getAttribute("title"),
  191. overflow: title.classList.contains("task-title-marquee"),
  192. rowWidth: row.getBoundingClientRect().width,
  193. titleWidth: title.getBoundingClientRect().width,
  194. trackWidth: track.getBoundingClientRect().width,
  195. };
  196. },
  197. taskRowSelectors(taskId),
  198. taskId,
  199. );
  200. }
  201. async function hoverTaskRow(taskId: string): Promise<boolean> {
  202. return browser.execute(
  203. (selectors, id) => {
  204. const row = selectors
  205. .map((selector) => document.querySelector<HTMLElement>(selector))
  206. .find(Boolean);
  207. const original = row?.querySelector<HTMLElement>('[data-task-title-copy="original"]');
  208. if (!row || !original) throw new Error(`找不到 hover task: ${id}`);
  209. const rowRect = row.getBoundingClientRect();
  210. const titleRect = original.getBoundingClientRect();
  211. // 直接从行容器派发 mouseenter,坐标落在左侧图标区,覆盖“整行而非文字 hover”。
  212. row.dispatchEvent(
  213. new MouseEvent("mouseenter", {
  214. bubbles: false,
  215. clientX: rowRect.left + 4,
  216. clientY: rowRect.top + rowRect.height / 2,
  217. }),
  218. );
  219. return rowRect.left + 4 < titleRect.left;
  220. },
  221. taskRowSelectors(taskId),
  222. taskId,
  223. );
  224. }
  225. async function leaveTaskRow(taskId: string) {
  226. await browser.execute((selectors) => {
  227. selectors
  228. .map((selector) => document.querySelector<HTMLElement>(selector))
  229. .find(Boolean)
  230. ?.dispatchEvent(new MouseEvent("mouseleave"));
  231. }, taskRowSelectors(taskId));
  232. }
  233. async function waitForAnimationCount(taskId: string, count: number) {
  234. await browser.waitUntil(async () => (await readTitleSnapshot(taskId)).animationCount === count, {
  235. timeout: 5_000,
  236. timeoutMsg: `task ${taskId} 没有启动 ${count} 个走马灯动画`,
  237. });
  238. }
  239. async function readAnimationTimeline(taskId: string) {
  240. return browser.execute((selectors) => {
  241. const row = selectors
  242. .map((selector) => document.querySelector<HTMLElement>(selector))
  243. .find(Boolean);
  244. const original = row?.querySelector<HTMLElement>('[data-task-title-copy="original"]');
  245. if (!original) throw new Error("找不到走马灯原始标题");
  246. const track = original?.parentElement as HTMLElement;
  247. const title = track?.parentElement as HTMLElement;
  248. const animations = title.getAnimations({ subtree: true });
  249. const trackEffect = animations.find(
  250. (animation) => (animation.effect as KeyframeEffect).target === track,
  251. )?.effect as KeyframeEffect;
  252. const maskEffect = animations.find(
  253. (animation) => (animation.effect as KeyframeEffect).target === title,
  254. )?.effect as KeyframeEffect;
  255. const trackFrames = trackEffect.getKeyframes();
  256. const maskFrames = maskEffect.getKeyframes();
  257. const duration = Number(trackEffect.getTiming().duration);
  258. const movementOffset = Number(trackFrames[1]?.offset);
  259. const movementTransform = String(trackFrames[1]?.transform);
  260. return {
  261. animations: animations.map((animation) => animation.playState),
  262. currentTimes: animations.map((animation) => Number(animation.currentTime ?? 0)),
  263. distance: Math.abs(Number(movementTransform.match(/-([\d.]+)px/)?.[1] ?? 0)),
  264. finalTransform: String(trackFrames[2]?.transform),
  265. maskFrames: maskFrames.map((frame) => String(frame.maskImage ?? frame.webkitMaskImage)),
  266. maskOffsets: maskFrames.map((frame) => Number(frame.offset)),
  267. movementTransform,
  268. originalWidth: original.scrollWidth,
  269. pauseMs: duration * (1 - movementOffset),
  270. trackOffsets: trackFrames.map((frame) => Number(frame.offset)),
  271. };
  272. }, taskRowSelectors(taskId));
  273. }
  274. async function setAnimationCurrentTime(taskId: string, currentTime: number) {
  275. await browser.execute(
  276. (selectors, time) => {
  277. const row = selectors
  278. .map((selector) => document.querySelector<HTMLElement>(selector))
  279. .find(Boolean);
  280. row?.getAnimations({ subtree: true }).forEach((animation) => {
  281. animation.currentTime = time;
  282. });
  283. },
  284. taskRowSelectors(taskId),
  285. currentTime,
  286. );
  287. }
  288. async function setTitleProbeWidth(taskId: string, width: number | null) {
  289. await browser.execute(
  290. (selectors, nextWidth, id) => {
  291. const row = selectors
  292. .map((selector) => document.querySelector<HTMLElement>(selector))
  293. .find(Boolean);
  294. const original = row?.querySelector<HTMLElement>('[data-task-title-copy="original"]');
  295. const title = original?.parentElement?.parentElement as HTMLElement | undefined;
  296. if (!title) throw new Error(`找不到 resize title: ${id}`);
  297. title.style.flex = nextWidth === null ? "" : "0 0 auto";
  298. title.style.width = nextWidth === null ? "" : `${nextWidth}px`;
  299. },
  300. taskRowSelectors(taskId),
  301. width,
  302. taskId,
  303. );
  304. }
  305. async function waitForOverflow(taskId: string, expected: boolean) {
  306. await browser.waitUntil(async () => (await readTitleSnapshot(taskId)).overflow === expected, {
  307. timeout: 5_000,
  308. timeoutMsg: `task ${taskId} overflow 未切换为 ${expected}`,
  309. });
  310. }
  311. async function setReducedMotion(reduce: boolean) {
  312. await sendRendererEmulationCommand("Emulation.setEmulatedMedia", {
  313. features: [
  314. {
  315. name: "prefers-reduced-motion",
  316. value: reduce ? "reduce" : "no-preference",
  317. },
  318. ],
  319. });
  320. }
  321. async function assertOverflowSurface(taskId: string) {
  322. const selectors = taskRowSelectors(taskId);
  323. await browser.waitUntil(
  324. () =>
  325. browser.execute(
  326. (values) => values.some((selector) => Boolean(document.querySelector(selector))),
  327. selectors,
  328. ),
  329. {
  330. timeout: 15_000,
  331. timeoutMsg: `侧栏未显示 ${taskId}`,
  332. },
  333. );
  334. const snapshot = await readTitleSnapshot(taskId);
  335. expect(snapshot.overflow).toBe(true);
  336. expect(snapshot.copyCount).toBe(2);
  337. expect(snapshot.maskImage).toContain("24px");
  338. expect(snapshot.nativeTitle).toBe(null);
  339. }
  340. async function sendRendererEmulationCommand(method: string, params: Record<string, unknown>) {
  341. const sent = await browser.electron.execute(
  342. async (electron, command, commandParams) => {
  343. const window = electron.BrowserWindow.getAllWindows().find(
  344. (candidate) => !candidate.isDestroyed() && candidate.isVisible(),
  345. );
  346. if (!window) return false;
  347. const devtools = window.webContents.debugger;
  348. if (!devtools.isAttached()) devtools.attach("1.3");
  349. await devtools.sendCommand(command, commandParams);
  350. return true;
  351. },
  352. method,
  353. params,
  354. );
  355. if (!sent) throw new Error(`没有找到可执行 ${method} 的 Electron 窗口`);
  356. }