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

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