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

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