sshProjectSelection.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { runInNewContext } from "node:vm";
  2. import { afterEach, expect, it, vi } from "vitest";
  3. import { readRemoteTabSnapshot, selectSSHRemoteDirectory } from "#e2e/helpers/ssh-remote-p0";
  4. vi.mock("#e2e/helpers/desktop-app", () => ({ waitForWorkspaceApp: vi.fn() }));
  5. vi.mock("#e2e/helpers/project-sidebar", () => ({ selectRemoteProjectDirectory: vi.fn() }));
  6. vi.mock("#e2e/helpers/custom-openai-provider-store", () => ({}));
  7. afterEach(() => vi.unstubAllGlobals());
  8. it.each([false, true])(
  9. "同路径第二个 Source 按新项目 identity 等待,支持预选目录编辑器=%s",
  10. async (prefilledEditor) => {
  11. const first = "remote:server:11111111-1111-4111-8111-111111111111:/work";
  12. const second = "remote:server:22222222-2222-4222-8222-222222222222:/work";
  13. const projectIds = ["existing"];
  14. let activeIdentity = first;
  15. let switching = false;
  16. let reads = 0;
  17. const document = {
  18. querySelectorAll: () => projectIds.map((id) => ({ getAttribute: () => id })),
  19. };
  20. const window = {
  21. __zcodeTabStoreE2E: {
  22. getState: () => {
  23. if (switching && ++reads > 1) activeIdentity = second;
  24. return {
  25. activeWorkspaceIdentity: activeIdentity,
  26. activeWorkspacePath: "/work",
  27. tabs: [first, second].map((identity) => ({
  28. workspaceIdentity: identity,
  29. workspacePath: "/work",
  30. remoteSessionId: identity === first ? "first-session" : "second-session",
  31. remoteTarget: { kind: "ssh", host: "127.0.0.1" },
  32. })),
  33. };
  34. },
  35. },
  36. };
  37. vi.stubGlobal("$", (selector: string) => {
  38. if (selector === '[data-testid="ssh-success"]') return { isExisting: async () => false };
  39. if (selector === '[data-testid="remote-project-new"]') {
  40. if (prefilledEditor) throw new Error("已在预选目录编辑器,不存在新建项目列表按钮");
  41. return { waitForEnabled: vi.fn(), click: vi.fn() };
  42. }
  43. if (selector === '[data-testid="virtual-project-name"]')
  44. return { setValue: vi.fn(), isDisplayed: async () => prefilledEditor };
  45. if (selector === '[data-testid="virtual-project-root-name"]')
  46. return { getAttribute: async () => "/work", isExisting: async () => true };
  47. if (selector === '[data-testid="virtual-project-directory-browser"]')
  48. return { isDisplayed: async () => false };
  49. if (selector === '[data-testid="virtual-project-save"]') return { click: vi.fn() };
  50. if (selector === '[data-testid="project-folder-trust-confirm"]')
  51. return { waitForDisplayed: vi.fn(), click: () => projectIds.push("new") };
  52. if (selector === '[data-testid="settings-back-button"]')
  53. return { isDisplayed: async () => false };
  54. if (selector === '[data-project-id="new"]')
  55. return {
  56. getAttribute: async () => second,
  57. moveTo: vi.fn(),
  58. $: () => ({
  59. click: () => {
  60. switching = true;
  61. },
  62. }),
  63. };
  64. throw new Error(`Unexpected selector: ${selector}`);
  65. });
  66. vi.stubGlobal("browser", {
  67. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  68. runInNewContext(`(${fn.toString()})(...args)`, { args, document, window }),
  69. waitUntil: async (predicate: () => Promise<boolean>) => {
  70. for (let attempt = 0; attempt < 3; attempt++) if (await predicate()) return;
  71. throw new Error("Second Source did not become active");
  72. },
  73. });
  74. await selectSSHRemoteDirectory("/work", "E2E_SOURCE_SELECTION");
  75. expect(activeIdentity).toBe(second);
  76. },
  77. );
  78. it("无筛选条件时读取当前激活工作区,不能把不存在的路径当作无远程会话", async () => {
  79. const state = {
  80. activeWorkspaceIdentity: "remote:server:source-b:/work",
  81. activeWorkspacePath: "/work",
  82. tabs: [
  83. {
  84. workspaceIdentity: "remote:server:source-a:/work",
  85. workspacePath: "/work",
  86. remoteSessionId: "a",
  87. },
  88. {
  89. workspaceIdentity: "remote:server:source-b:/work",
  90. workspacePath: "/work",
  91. remoteSessionId: "b",
  92. remoteTarget: { kind: "ssh" },
  93. },
  94. ],
  95. };
  96. vi.stubGlobal("browser", {
  97. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  98. runInNewContext(`(${fn.toString()})(...args)`, {
  99. args,
  100. window: { __zcodeTabStoreE2E: { getState: () => state } },
  101. }),
  102. });
  103. expect(await readRemoteTabSnapshot()).toMatchObject({
  104. remoteSessionId: "b",
  105. workspaceIdentity: state.activeWorkspaceIdentity,
  106. });
  107. state.activeWorkspaceIdentity = "";
  108. state.activeWorkspacePath = "/local";
  109. expect((await readRemoteTabSnapshot()).remoteSessionId).toBeNull();
  110. expect((await readRemoteTabSnapshot("remote:server:source-a:/work")).remoteSessionId).toBe("a");
  111. });