conversation-session-sidebar-width-stability.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { restartIntoWorkspacePreservingProfile } from "#e2e/helpers/model-provider-restart-runtime";
  2. import { mkdir, writeFile } from "node:fs/promises";
  3. import { basename, join } from "node:path";
  4. import { DatabaseSync } from "node:sqlite";
  5. import { DEFAULT_WORKSPACE, waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  6. const projectId = "E2E_SIDEBAR_WIDTH";
  7. const projectSelector = `[data-project-id="${projectId}"]`;
  8. const moreSelector = './/button[normalize-space(.)="显示更多" or normalize-space(.)="Show more"]';
  9. // 合成索引仅准备分页/长标题布局,不恢复这些任务,也不请求模型。
  10. async function seedTaskRows() {
  11. const home = process.env.ZCODE_E2E_HOME_DIR;
  12. // 原因:批量 runner 使用 runId 隔离 HOME,不能要求旧手工执行的固定目录名。
  13. if (!home || !/^(?:zcode-sidebar-width-|\.e2e-home-)/u.test(basename(home)))
  14. throw new Error("隔离 HOME required");
  15. const root = join(home, "E2E_SIDEBAR_WIDTH_ROOT");
  16. await mkdir(root, { recursive: true });
  17. const db = new DatabaseSync(join(home, ".zcode/v2/tasks-index.sqlite"));
  18. try {
  19. const now = Date.now();
  20. db.exec("BEGIN IMMEDIATE");
  21. db.prepare(
  22. "INSERT INTO projects(project_id,host_id,name,position,created_at,updated_at) VALUES(?,'local',?,-1,?,?)",
  23. ).run(projectId, projectId, now, now);
  24. db.prepare(
  25. "INSERT INTO project_roots(project_id,root_path,position,is_primary) VALUES(?,?,0,1)",
  26. ).run(projectId, root);
  27. const insert =
  28. db.prepare(`INSERT INTO tasks(workspace_key,workspace_path,task_id,title,created_at,updated_at,provider,host_id,root_path,project_id,cli_title,cli_title_source,cli_created_at,cli_updated_at,cli_last_outcome,cli_deleted,handoff_pending)
  29. VALUES(?,?,?,?,?,?,'glm','local',?,?,?,'custom',?,?,'completed',0,8)`);
  30. for (const group of [projectId, null]) {
  31. for (let index = 0; index < 25; index++) {
  32. const id = `E2E_WIDTH_${group ?? "TASKS"}_${index}`;
  33. const title =
  34. index % 3 === 0 ? `E2E_LONG_${"W".repeat(55)}_${index}` : `E2E_SHORT_${index}`;
  35. insert.run(root, root, id, title, now, now, root, group, title, now, now);
  36. }
  37. }
  38. db.exec("COMMIT");
  39. } finally {
  40. db.close();
  41. }
  42. }
  43. async function sampleWidths() {
  44. return browser.executeAsync(
  45. (
  46. done: (value: Array<{ grid: number; section: number; width: number; left: number }>) => void,
  47. ) => {
  48. const samples: Array<{ grid: number; section: number; width: number; left: number }> = [];
  49. const start = performance.now();
  50. const tick = () => {
  51. const grid = document.querySelector('[data-purpose-section-list="true"]')!;
  52. for (const button of Array.from(grid.querySelectorAll("button")).filter((node) =>
  53. /^(显示更多|Show more)$/.test(node.textContent?.trim() ?? ""),
  54. )) {
  55. const rect = button.getBoundingClientRect();
  56. samples.push({
  57. grid: grid.getBoundingClientRect().width,
  58. section: button
  59. .closest('[data-testid="project-section"], [data-testid="conversation-section"]')!
  60. .getBoundingClientRect().width,
  61. width: rect.width,
  62. left: rect.left,
  63. });
  64. }
  65. if (performance.now() - start >= 1200) done(samples);
  66. else requestAnimationFrame(tick);
  67. };
  68. requestAnimationFrame(tick);
  69. },
  70. );
  71. }
  72. describe("项目侧栏长标题宽度", () => {
  73. it("VP-WIDTH-01:静止时分区/显示更多不跳动,缩进稳定且分页正常", async function () {
  74. this.timeout(90_000);
  75. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  76. // 旧夹具缺少 provider 而被列表过滤;停机播种,避免 Host 快照读到旧数据。
  77. await restartIntoWorkspacePreservingProfile(DEFAULT_WORKSPACE, {
  78. afterElectronProcessExit: seedTaskRows,
  79. });
  80. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  81. const project = $(projectSelector);
  82. await project.waitForDisplayed();
  83. const tasks = $('[data-testid="conversation-section"]');
  84. for (const section of [project, tasks]) {
  85. await section.$(moreSelector).waitForExist();
  86. expect(await section.$$("[data-task-item-key]").length).toBe(5);
  87. }
  88. // 停在会话区,排除标题 hover 动画;连续观察真实 ResizeObserver 循环。
  89. await $('[data-workspace-conversation-frame="true"]').moveTo();
  90. const evidence = [];
  91. for (const theme of ["zai-light", "zai-dark"]) {
  92. await browser.execute((value) => {
  93. const actions = (
  94. window as typeof window & { __testActions?: { setTheme(theme: string): void } }
  95. ).__testActions;
  96. if (!actions) throw new Error("缺少主题入口");
  97. actions.setTheme(value);
  98. }, theme);
  99. await browser.waitUntil(() =>
  100. browser.execute(
  101. (value) => document.documentElement.classList.contains(`theme-${value}`),
  102. theme,
  103. ),
  104. );
  105. for (const maxWidth of ["240px", "none"]) {
  106. // 只改变共享组件的容器约束,不绕过布局或修改标题状态;窄容器代表手机侧栏。
  107. await browser.execute((value) => {
  108. (
  109. document.querySelector('[data-purpose-section-list="true"]') as HTMLElement
  110. ).style.maxWidth = value;
  111. }, maxWidth);
  112. const samples = await sampleWidths();
  113. evidence.push({ theme, maxWidth, samples });
  114. expect(samples.length).toBeGreaterThan(10);
  115. expect(
  116. Math.max(...samples.map((sample) => sample.section - sample.grid)),
  117. ).toBeLessThanOrEqual(1);
  118. // 后续分页文字规格保留缩进;分别验证两区的位置稳定,不再强制旧版居中。
  119. for (const parity of [0, 1]) {
  120. const positions = samples
  121. .filter((_, index) => index % 2 === parity)
  122. .map((sample) => sample.left);
  123. expect(Math.max(...positions) - Math.min(...positions)).toBeLessThanOrEqual(1);
  124. }
  125. const projectWidths = samples
  126. .filter((_, index) => index % 2 === 0)
  127. .map((sample) => sample.width);
  128. expect(Math.max(...projectWidths) - Math.min(...projectWidths)).toBeLessThanOrEqual(1);
  129. }
  130. }
  131. await writeFile(
  132. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "sidebar-width-samples.json"),
  133. JSON.stringify(evidence),
  134. );
  135. for (const section of [project, tasks]) {
  136. const more = section.$(moreSelector);
  137. for (const count of [10, 15, 20, 25]) {
  138. await more.scrollIntoView();
  139. await more.click();
  140. await browser.waitUntil(
  141. async () => (await section.$$("[data-task-item-key]").length) === count,
  142. );
  143. }
  144. expect(await section.$(moreSelector).isExisting()).toBe(false);
  145. }
  146. await project.scrollIntoView();
  147. await browser.saveScreenshot(
  148. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "sidebar-width-stable.png"),
  149. );
  150. });
  151. });