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

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