conversation-session-remote-project-policy.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { DatabaseSync } from "node:sqlite";
  2. import { basename, join } from "node:path";
  3. import { isE2EHomeDirName } from "#e2e/helpers/e2e-runtime-paths";
  4. import { DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  5. import { loginWithApiKey, prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  6. /** 仅验证离线入口与真实侧栏交互;SSH 执行和手机 shared-host 使用各自链路用例。 */
  7. describe("远程单目录项目展示", () => {
  8. before(async function () {
  9. this.timeout(120_000);
  10. await prepareV4ConversationE2E({ skipProvider: true });
  11. const home = process.env.ZCODE_E2E_HOME_DIR;
  12. // 根因:旧守卫只接受手工运行的目录名,拒绝队列按批次/case 分配的独立 HOME。
  13. // 本例只验离线缓存,可由普通 runner 执行;仍要求统一隔离 HOME 前缀。
  14. // 修复原因:旧守卫只认 `.e2e-home-` 前缀,Windows CI 未设 ZCODE_E2E_HOME_DIR 时默认 HOME 是
  15. // `.e2e-home`,before 直接抛错。统一用 isE2EHomeDirName,只接受 `.e2e-home` 与 `.e2e-home-*`。
  16. const isolatedRunnerHome = home && isE2EHomeDirName(basename(home));
  17. if (
  18. !home ||
  19. (!basename(home).startsWith("zcode-remote-project-policy-e2e-") && !isolatedRunnerHome)
  20. )
  21. throw new Error("isolated HOME required");
  22. const db = new DatabaseSync(join(home, ".zcode/v2/tasks-index.sqlite"));
  23. try {
  24. db.exec("BEGIN IMMEDIATE");
  25. db.prepare(
  26. "INSERT INTO projects(project_id,host_id,name,position,created_at,updated_at) VALUES('e2e-vpr-local','local','E2E_VPR_LOCAL',0,1,1)",
  27. ).run();
  28. db.prepare(
  29. "INSERT INTO project_roots(project_id,root_path,position,is_primary) VALUES('e2e-vpr-local',?,0,1),('e2e-vpr-local',?,1,0)",
  30. ).run(DEFAULT_WORKSPACE, join(DEFAULT_WORKSPACE, "extra"));
  31. for (const device of ["a", "b"]) {
  32. const host = `remote:server:e2e-vpr-${device}`;
  33. // 无线路的离线入口只渲染缓存;用例不得借造假的连接声称 SSH 已验证。
  34. db.prepare(
  35. "INSERT INTO hosts(host_id,kind,membership_id,position,target_json) VALUES(?,'server',?,?,?)",
  36. ).run(
  37. host,
  38. `member-${device}`,
  39. device === "a" ? 1 : 2,
  40. JSON.stringify({
  41. kind: "server",
  42. name: `Device ${device}`,
  43. url: `http://127.0.0.1:1/${device}`,
  44. }),
  45. );
  46. db.prepare(
  47. "INSERT INTO remote_project_entries(entry_id,host_id,remote_project_id,cached_name,cached_roots_json,last_synced_at) VALUES(?,?,?,?,?,1)",
  48. ).run(
  49. `entry-${device}`,
  50. host,
  51. `e2e-vpr-${device}`,
  52. `E2E_VPR_${device.toUpperCase()}`,
  53. JSON.stringify([{ rootPath: "/same", isPrimary: true, position: 0 }]),
  54. );
  55. }
  56. db.exec("COMMIT");
  57. } finally {
  58. db.close();
  59. }
  60. await refreshSidebar();
  61. try {
  62. await $('[data-project-id="e2e-vpr-b"]').waitForDisplayed({ timeout: 15_000 });
  63. } catch (error) {
  64. console.error("VPR DOM", await browser.execute(() => document.body.innerText));
  65. await browser.saveScreenshot(join(home, "session-vpr-failure.png"));
  66. throw error;
  67. }
  68. });
  69. it("VPR01:跨设备离线排序、刷新及移除入口,侧栏不出现 Host 分组", async () => {
  70. expect(await $('[data-testid="remote-project-host"]').isExisting()).toBe(false);
  71. expect(await $$('[data-testid="project-device"]')).toHaveLength(2);
  72. const order = () =>
  73. browser.execute(() =>
  74. [...document.querySelectorAll('[data-testid="project-section"] [data-project-id]')].map(
  75. (row) => row.getAttribute("data-project-id"),
  76. ),
  77. );
  78. const before = await order();
  79. const coords = await browser.execute(() => {
  80. const rect = (id: string) =>
  81. document
  82. .querySelector(`[data-project-id="${id}"] [data-testid="virtual-project-toggle"]`)!
  83. .getBoundingClientRect();
  84. const a = rect("e2e-vpr-a"),
  85. b = rect("e2e-vpr-b");
  86. return {
  87. x: Math.round(b.x + 40),
  88. from: Math.round(b.y + b.height / 2),
  89. to: Math.round(a.y + a.height / 2),
  90. };
  91. });
  92. await browser.performActions([
  93. {
  94. type: "pointer",
  95. id: "project-mouse",
  96. parameters: { pointerType: "mouse" },
  97. actions: [
  98. { type: "pointerMove", duration: 0, x: coords.x, y: coords.from },
  99. { type: "pointerDown", button: 0 },
  100. { type: "pointerMove", duration: 100, x: coords.x, y: coords.from - 8 },
  101. { type: "pointerMove", duration: 200, x: coords.x, y: coords.to },
  102. { type: "pointerUp", button: 0 },
  103. ],
  104. },
  105. ]);
  106. await browser.releaseActions();
  107. await browser.waitUntil(async () => JSON.stringify(await order()) !== JSON.stringify(before));
  108. const sorted = await order();
  109. expect(sorted.indexOf("e2e-vpr-b")).toBeLessThan(sorted.indexOf("e2e-vpr-a"));
  110. await refreshSidebar();
  111. await $('[data-project-id="e2e-vpr-b"]').waitForDisplayed({ timeout: 45_000 });
  112. expect(await order()).toEqual(sorted);
  113. const row = $('[data-project-id="e2e-vpr-b"]');
  114. // 项目缓存先到、设备状态后到;按真实离线投影断言,不能把行存在当成状态就绪。
  115. await browser.waitUntil(
  116. async () => (await row.getAttribute("data-connection-state")) === "offline",
  117. );
  118. expect(await row.$('[data-testid="virtual-project-create-task"]').isExisting()).toBe(false);
  119. const readOrderPreference = () =>
  120. browser.execute(
  121. () =>
  122. JSON.parse(localStorage.getItem("zcode-project-sidebar-preferences") ?? "{}")
  123. .order as string[],
  124. );
  125. const preferenceBefore = await readOrderPreference();
  126. await row.moveTo();
  127. await row.$('[data-testid="virtual-project-menu"]').click();
  128. await $('[data-testid="virtual-project-remove-entry"]').click();
  129. await row.waitForExist({ reverse: true });
  130. // 2026-10-15 已裁决保留暂不可见项目排序键;提前删键会让旧目录行跳到列表末尾。
  131. expect(await readOrderPreference()).toEqual(preferenceBefore);
  132. expect(await order()).toEqual(sorted.filter((id) => id !== "e2e-vpr-b"));
  133. await refreshSidebar();
  134. await $('[data-project-id="e2e-vpr-a"]').waitForDisplayed();
  135. expect(await row.isExisting()).toBe(false);
  136. expect(await order()).toEqual(sorted.filter((id) => id !== "e2e-vpr-b"));
  137. });
  138. });
  139. async function refreshSidebar() {
  140. await browser.refresh();
  141. const login = $('[data-testid="login-use-api-key-button"]');
  142. // 最后一轮已经移除 B,刷新屏障必须等稳定的项目分区,不能等被测删除对象复活。
  143. const project = $('[data-testid="project-section"]');
  144. await browser.waitUntil(
  145. async () => (await login.isDisplayed()) || (await project.isDisplayed()),
  146. { timeout: 45_000 },
  147. );
  148. if (await login.isDisplayed()) await loginWithApiKey();
  149. await project.waitForDisplayed({ timeout: 45_000 });
  150. }