command-center-selection.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { writeFile } from "node:fs/promises";
  2. import { basename, join } from "node:path";
  3. import { DatabaseSync } from "node:sqlite";
  4. import { DEFAULT_WORKSPACE, waitForWorkspaceApp } from "../../../helpers/desktop-app.js";
  5. const itemSelector = '[role="dialog"] [cmdk-item]';
  6. const inputSelector = '[role="dialog"] [cmdk-input]';
  7. const duplicateTitle = "E2E_COMMAND_CENTER_DUPLICATE";
  8. describe("Command Center 重名任务选中", () => {
  9. afterEach(async () => {
  10. const state = await browser.execute(() => ({
  11. dialog: document.querySelector('[role="dialog"]')?.textContent,
  12. tasks: Array.from(document.querySelectorAll("[data-task-item-key]")).map(
  13. (row) => row.textContent,
  14. ),
  15. tabs: (
  16. window as typeof window & { __zcodeTabStoreE2E?: { getState(): unknown } }
  17. ).__zcodeTabStoreE2E?.getState(),
  18. }));
  19. await writeFile(
  20. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "command-center-state.json"),
  21. JSON.stringify(state, null, 2),
  22. );
  23. });
  24. before(async () => {
  25. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  26. const home = process.env.ZCODE_E2E_HOME_DIR;
  27. if (!home || !basename(home).startsWith("zcode-command-center-")) {
  28. throw new Error("需要 zcode-command-center- 前缀的隔离 E2E HOME");
  29. }
  30. // 纯列表 fixture:在隔离索引准备重名任务,不恢复合成会话或发送模型请求。
  31. // node:sqlite 仅提供同步接口,短事务结束后立即关闭连接。
  32. const db = new DatabaseSync(join(home, ".zcode/v2/tasks-index.sqlite"));
  33. try {
  34. const now = Date.now();
  35. const insert = db.prepare(`
  36. INSERT INTO tasks (
  37. workspace_key, workspace_path, task_id, title, created_at, updated_at, provider,
  38. host_id, root_path, cli_title, cli_title_source, cli_created_at,
  39. cli_updated_at, cli_last_outcome, cli_deleted, handoff_pending, meta_json
  40. ) VALUES (?, ?, ?, ?, ?, ?, 'glm', 'local', ?, ?, 'custom', ?, ?, 'completed', 0, 8, ?)
  41. `);
  42. db.exec("BEGIN IMMEDIATE");
  43. for (let index = 0; index < 4; index++) {
  44. const title = index === 2 ? "E2E_COMMAND_CENTER_OTHER" : duplicateTitle;
  45. const timestamp = now - index * 1000;
  46. const taskId = `e2e-command-center-${index}`;
  47. insert.run(
  48. DEFAULT_WORKSPACE,
  49. DEFAULT_WORKSPACE,
  50. taskId,
  51. title,
  52. timestamp,
  53. timestamp,
  54. DEFAULT_WORKSPACE,
  55. title,
  56. timestamp,
  57. timestamp,
  58. JSON.stringify({
  59. taskId,
  60. traceId: `trace-${taskId}`,
  61. title,
  62. workspacePath: DEFAULT_WORKSPACE,
  63. createdAt: timestamp,
  64. updatedAt: timestamp,
  65. mode: "build",
  66. provider: "glm",
  67. status: "completed",
  68. }),
  69. );
  70. }
  71. db.exec("COMMIT");
  72. } finally {
  73. db.close();
  74. }
  75. await browser.refresh();
  76. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  77. });
  78. it("CC-SELECT-01:三个重名最近任务仅高亮鼠标/键盘指向的一行", async () => {
  79. const evidence = [];
  80. for (const theme of ["zai-light", "zai-dark"]) {
  81. await browser.execute((value) => {
  82. const actions = (
  83. window as typeof window & {
  84. __testActions?: { setTheme(theme: string): void };
  85. }
  86. ).__testActions;
  87. if (!actions) throw new Error("缺少主题测试入口");
  88. actions.setTheme(value);
  89. }, theme);
  90. await $('[data-testid="sidebar-search-open"]').click();
  91. await $(inputSelector).waitForDisplayed();
  92. await $('[role="dialog"]')
  93. .$('.//button[@role="tab" and (normalize-space(.)="任务" or normalize-space(.)="Tasks")]')
  94. .click();
  95. await browser.waitUntil(async () => (await $$(itemSelector).length) === 4, {
  96. timeout: 5000,
  97. timeoutMsg: "最近任务应显示四条隔离索引 fixture",
  98. });
  99. const rows = await $$(itemSelector);
  100. expect(await rows[0]!.getText()).toContain(duplicateTitle);
  101. expect(await rows[1]!.getText()).toContain(duplicateTitle);
  102. expect(await rows[3]!.getText()).toContain(duplicateTitle);
  103. for (const width of ["", "360px"]) {
  104. // 窄容器验证共享面板;不宣称覆盖手机 shared-host 连接。
  105. await browser.execute((value) => {
  106. (document.querySelector('[role="dialog"]') as HTMLElement).style.maxWidth = value;
  107. }, width);
  108. for (const index of [0, 1, 3, 2]) {
  109. await rows[index]!.moveTo();
  110. await expectOnlySelected(index);
  111. }
  112. await $(inputSelector).click();
  113. await browser.keys("Home");
  114. await expectOnlySelected(0);
  115. await browser.keys("ArrowDown");
  116. await expectOnlySelected(1);
  117. await browser.keys("ArrowUp");
  118. await expectOnlySelected(0);
  119. evidence.push({ theme, width: width || "default", selected: await readSelected() });
  120. }
  121. await browser.keys("Escape");
  122. }
  123. await writeFile(
  124. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "command-center-selection.json"),
  125. JSON.stringify(evidence, null, 2),
  126. );
  127. });
  128. });
  129. async function readSelected() {
  130. return browser.execute(
  131. (selector) =>
  132. Array.from(document.querySelectorAll(selector))
  133. .map((row, index) => ({ index, selected: row.getAttribute("aria-selected") }))
  134. .filter((row) => row.selected === "true")
  135. .map((row) => row.index),
  136. itemSelector,
  137. );
  138. }
  139. async function expectOnlySelected(index: number) {
  140. await browser.waitUntil(
  141. async () => JSON.stringify(await readSelected()) === JSON.stringify([index]),
  142. {
  143. timeout: 3000,
  144. timeoutMsg: `仅第 ${index} 行应处于选中态`,
  145. },
  146. );
  147. }