conversation-session-local-conversation-identity.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { mkdir, stat, writeFile } from "node:fs/promises";
  2. import { dirname, join, resolve } from "node:path";
  3. import { buildServerRemoteWorkspaceIdentity, type RemoteTarget } from "@zcode/shared";
  4. import {
  5. TID_COMPOSER_WORK_OUTSIDE_PROJECT,
  6. TID_COMPOSER_WORKSPACE_TRIGGER,
  7. } from "@zcode/shared/test-ids";
  8. import {
  9. DEFAULT_WORKSPACE,
  10. clearAppData,
  11. getE2EAppDataPaths,
  12. waitForWorkspaceApp,
  13. } from "#e2e/helpers/desktop-app";
  14. const CASE_MARKER = "E2E_LOCAL_CONVERSATION_IDENTITY";
  15. const REMOTE_IDENTITY = buildServerRemoteWorkspaceIdentity({
  16. serverId: "25db8db2-d27b-40de-a23e-697b3c127234",
  17. workspacePath: DEFAULT_WORKSPACE,
  18. });
  19. const artifactDir = resolve(
  20. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/local-conversation-identity",
  21. );
  22. interface NavigationTab {
  23. id: string;
  24. kind: string;
  25. workspacePath?: string;
  26. workspaceIdentity?: string;
  27. workspacePurpose?: string;
  28. remoteSessionId?: string;
  29. }
  30. interface NavigationWindow extends Window {
  31. __zcodeTabStoreE2E?: {
  32. getState(): {
  33. tabs: NavigationTab[];
  34. activeTabId: string | null;
  35. activeWorkspacePath: string | null;
  36. activeWorkspaceIdentity: string | null;
  37. addTab(
  38. path: string,
  39. options: { workspaceIdentity: string; remoteTarget: RemoteTarget },
  40. ): string;
  41. activateTab(id: string): void;
  42. };
  43. };
  44. }
  45. async function readNavigation() {
  46. return browser.execute(() => {
  47. const state = (window as NavigationWindow).__zcodeTabStoreE2E?.getState();
  48. if (!state) throw new Error("导航 store 未就绪");
  49. return {
  50. activeTabId: state.activeTabId,
  51. activeWorkspacePath: state.activeWorkspacePath,
  52. activeWorkspaceIdentity: state.activeWorkspaceIdentity,
  53. tabs: state.tabs.map((tab) => ({
  54. id: tab.id,
  55. workspacePath: tab.workspacePath ?? null,
  56. workspaceIdentity: tab.workspaceIdentity ?? null,
  57. workspacePurpose: tab.workspacePurpose ?? null,
  58. remoteSessionId: tab.remoteSessionId ?? null,
  59. })),
  60. };
  61. });
  62. }
  63. describe(`${CASE_MARKER}: 本地项目解除绑定按完整工作区身份匹配`, () => {
  64. const facts: Record<string, unknown> = {};
  65. after(async () => {
  66. await mkdir(artifactDir, { recursive: true });
  67. facts.final = await readNavigation();
  68. await writeFile(
  69. resolve(artifactDir, "local-conversation-identity.json"),
  70. JSON.stringify(facts, null, 2),
  71. );
  72. await browser.saveScreenshot(resolve(artifactDir, "local-conversation-identity.png"));
  73. await browser.electron.restoreAllMocks();
  74. await clearAppData();
  75. });
  76. it("RPN-12 本地项目与离线远端同路径且远端在前,仍能在项目外工作", async function () {
  77. this.timeout(60_000);
  78. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30_000);
  79. // 只构造离线 open-set,不连接 SSH;调用真实 store 的 add/activate,复现新 tab 置顶的顺序。
  80. const localId = await browser.execute(
  81. (path, identity) => {
  82. const store = (window as NavigationWindow).__zcodeTabStoreE2E;
  83. const localTab = store
  84. ?.getState()
  85. .tabs.find(
  86. (tab) =>
  87. tab.kind === "workspace" && tab.workspacePath === path && !tab.workspaceIdentity,
  88. );
  89. if (!store || !localTab) throw new Error("本地项目 tab 未就绪");
  90. store.getState().addTab(path, {
  91. workspaceIdentity: identity,
  92. remoteTarget: { kind: "ssh", host: "offline.example.com", username: "dev", port: 22 },
  93. });
  94. store.getState().activateTab(localTab.id);
  95. return localTab.id;
  96. },
  97. DEFAULT_WORKSPACE,
  98. REMOTE_IDENTITY,
  99. );
  100. const before = await readNavigation();
  101. facts.before = before;
  102. expect(before.activeTabId).toBe(localId);
  103. expect(before.activeWorkspaceIdentity).toBeNull();
  104. expect(before.tabs[0]?.workspaceIdentity).toBe(REMOTE_IDENTITY);
  105. expect(before.tabs[0]?.remoteSessionId).toBeNull();
  106. expect(before.tabs[1]?.id).toBe(localId);
  107. await $(`[data-testid="${TID_COMPOSER_WORKSPACE_TRIGGER}"]`).click();
  108. const workOutsideProject = $(`[data-testid="${TID_COMPOSER_WORK_OUTSIDE_PROJECT}"]`);
  109. await workOutsideProject.waitForDisplayed({ timeout: 10_000 });
  110. await workOutsideProject.click();
  111. await browser.waitUntil(
  112. async () => {
  113. const state = await readNavigation();
  114. return (
  115. state.activeWorkspacePath !== null &&
  116. state.activeWorkspacePath !== DEFAULT_WORKSPACE &&
  117. state.activeWorkspaceIdentity === null &&
  118. state.tabs.find((tab) => tab.id === state.activeTabId)?.workspacePurpose ===
  119. "conversation"
  120. );
  121. },
  122. { timeout: 10_000, timeoutMsg: `${CASE_MARKER}: 点击后没有进入本地 conversation workspace` },
  123. );
  124. const after = await readNavigation();
  125. // 未归类任务现在准备独立目录;身份隔离不能再绑定退役的 workspace/default 常量。
  126. expect(dirname(after.activeWorkspacePath!)).toBe(
  127. join(getE2EAppDataPaths().storageRoot, "workspace"),
  128. );
  129. expect((await stat(after.activeWorkspacePath!)).isDirectory()).toBe(true);
  130. await waitForWorkspaceApp(after.activeWorkspacePath!, 10_000);
  131. facts.after = after;
  132. expect(after.tabs.find((tab) => tab.id === after.activeTabId)?.workspacePurpose).toBe(
  133. "conversation",
  134. );
  135. expect(after.tabs.find((tab) => tab.id === localId)).toEqual(before.tabs[1]);
  136. expect(after.tabs.find((tab) => tab.workspaceIdentity === REMOTE_IDENTITY)).toEqual(
  137. before.tabs[0],
  138. );
  139. });
  140. });