tabStore.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createTabStore,
  4. isWorkspaceReadOnly,
  5. isWorkspaceTab,
  6. SETTINGS_TAB_ID,
  7. } from "../src/store/tabStore.js";
  8. import { WORKSPACE_EXPANSION_STORAGE_KEY } from "../src/lib/workspaceExpansionPreference.js";
  9. function createMemoryStorage(initial: Record<string, string> = {}) {
  10. const values = new Map(Object.entries(initial));
  11. return {
  12. getItem: (key: string) => values.get(key) ?? null,
  13. setItem: (key: string, value: string) => {
  14. values.set(key, value);
  15. },
  16. };
  17. }
  18. describe("tabStore", () => {
  19. it("reuses unchanged tabs when opening a draft but publishes actual reconnection metadata", () => {
  20. const store = createTabStore();
  21. const identity = "remote:server:source-a:/srv/app";
  22. store.getState().addTab("/srv/app", {
  23. workspaceIdentity: identity,
  24. workspacePurpose: "project",
  25. remoteSessionId: "connection-1",
  26. });
  27. const original = store.getState().tabs;
  28. store.getState().addTab("/srv/app", {
  29. workspaceIdentity: identity,
  30. workspacePurpose: "project",
  31. });
  32. expect(store.getState().tabs).toBe(original);
  33. store.getState().addTab("/srv/app", {
  34. workspaceIdentity: identity,
  35. remoteSessionId: "connection-2",
  36. });
  37. expect(store.getState().tabs).not.toBe(original);
  38. expect(store.getState().tabs).toHaveLength(1);
  39. expect(store.getState().tabs[0]).toMatchObject({
  40. workspaceIdentity: identity,
  41. workspacePurpose: "project",
  42. remoteSessionId: "connection-2",
  43. });
  44. });
  45. it("RPN-02 keeps expansion and order isolated for same-path remote projects", () => {
  46. const storage = createMemoryStorage();
  47. const store = createTabStore(storage);
  48. const a = "remote:server:source-a:/srv/app";
  49. const b = "remote:server:source-b:/srv/app";
  50. store.getState().addTab("/srv/app", { workspaceIdentity: a });
  51. store.getState().addTab("/srv/app", { workspaceIdentity: b });
  52. store.getState().toggleWorkspaceExpanded(a);
  53. expect(store.getState().expandedWorkspacePaths.has(a)).toBe(false);
  54. expect(store.getState().expandedWorkspacePaths.has(b)).toBe(true);
  55. store.getState().reorderWorkspaceTabs(0, 1);
  56. expect(
  57. store
  58. .getState()
  59. .tabs.filter(isWorkspaceTab)
  60. .map((tab) => tab.workspaceIdentity),
  61. ).toEqual([a, b]);
  62. const restored = createTabStore(storage);
  63. restored.getState().restoreTabs(
  64. [
  65. { workspacePath: "/srv/app", workspaceIdentity: a },
  66. { workspacePath: "/srv/app", workspaceIdentity: b },
  67. ],
  68. 0,
  69. );
  70. expect(restored.getState().expandedWorkspacePaths.has(a)).toBe(false);
  71. expect(restored.getState().expandedWorkspacePaths.has(b)).toBe(true);
  72. });
  73. it("opens only one settings tab per window", () => {
  74. const store = createTabStore();
  75. store.getState().openSettingsTab();
  76. store.getState().openSettingsTab();
  77. const settingsTabs = store.getState().tabs.filter((tab) => tab.id === SETTINGS_TAB_ID);
  78. expect(settingsTabs).toHaveLength(1);
  79. expect(store.getState().activeTabId).toBe(SETTINGS_TAB_ID);
  80. });
  81. it("closeTab 只为带远端身份的 tab 发布 lastClosedWorkspaceEvent", () => {
  82. // 契约:本地 tab 关闭不进入远程关闭链路,其持久化移除只依赖“按当前 tabs 序列化”;
  83. // 带 stable identity 的远程 tab 关闭则必须发布 user-close 事件供远程流程清理。
  84. const store = createTabStore();
  85. const stableIdentity = "remote:server:11111111-1111-4111-8111-111111111111:/workspace/demo";
  86. const localTabId = store.getState().addTab("/tmp/local");
  87. store.getState().closeTab(localTabId);
  88. expect(store.getState().lastClosedWorkspaceEvent).toBeNull();
  89. const remoteTabId = store.getState().addTab("/workspace/demo", {
  90. workspaceIdentity: stableIdentity,
  91. remoteTarget: { kind: "ssh", host: "dev.example.com", port: 22, username: "alice" },
  92. });
  93. store.getState().closeTab(remoteTabId);
  94. expect(store.getState().lastClosedWorkspaceEvent).toEqual({
  95. sequence: 1,
  96. workspaceKey: stableIdentity,
  97. origin: "user-close",
  98. });
  99. });
  100. it("restores only workspace tabs from persisted session", () => {
  101. const store = createTabStore();
  102. store.getState().restoreTabs(["/tmp/a", "/tmp/b"], 1);
  103. expect(store.getState().tabs.every(isWorkspaceTab)).toBe(true);
  104. expect(store.getState().tabs).toHaveLength(2);
  105. expect(store.getState().activeTabId).toBe(store.getState().tabs[1]?.id ?? null);
  106. });
  107. it("首帧后补齐 inactive workspace 时保留 active tab identity 和焦点", () => {
  108. const store = createTabStore();
  109. store.getState().restoreTabs(["/tmp/active"], 0);
  110. const activeTabId = store.getState().activeTabId;
  111. store.getState().completeTabRestore(["/tmp/inactive-a", "/tmp/active", "/tmp/inactive-b"]);
  112. expect(
  113. store
  114. .getState()
  115. .tabs.filter(isWorkspaceTab)
  116. .map((tab) => tab.workspacePath),
  117. ).toEqual(["/tmp/inactive-a", "/tmp/active", "/tmp/inactive-b"]);
  118. expect(store.getState().activeTabId).toBe(activeTabId);
  119. expect(store.getState().activeWorkspacePath).toBe("/tmp/active");
  120. expect(store.getState().tabs.find((tab) => tab.id === activeTabId)).toMatchObject({
  121. workspacePath: "/tmp/active",
  122. });
  123. });
  124. it("补齐 inactive workspace 不覆盖启动后用户新开的 tab", () => {
  125. const store = createTabStore();
  126. store.getState().restoreTabs(["/tmp/active"], 0);
  127. const userTabId = store.getState().addTab("/tmp/user-opened");
  128. store.getState().completeTabRestore(["/tmp/inactive", "/tmp/active"]);
  129. expect(
  130. store
  131. .getState()
  132. .tabs.filter(isWorkspaceTab)
  133. .map((tab) => tab.workspacePath),
  134. ).toEqual(["/tmp/user-opened", "/tmp/inactive", "/tmp/active"]);
  135. expect(store.getState().activeTabId).toBe(userTabId);
  136. expect(store.getState().activeWorkspacePath).toBe("/tmp/user-opened");
  137. });
  138. it("preserves conversation purpose when adding and restoring managed workspace tabs", () => {
  139. const store = createTabStore();
  140. store.getState().addTab("/tmp/.zcode/workspace/default", {
  141. workspacePurpose: "conversation",
  142. });
  143. expect(store.getState().tabs[0]).toMatchObject({
  144. workspacePath: "/tmp/.zcode/workspace/default",
  145. workspacePurpose: "conversation",
  146. });
  147. store.getState().restoreTabs(
  148. [
  149. {
  150. workspacePath: "/tmp/.zcode/workspace/default",
  151. workspacePurpose: "conversation",
  152. },
  153. ],
  154. 0,
  155. );
  156. expect(store.getState().tabs[0]).toMatchObject({
  157. workspacePurpose: "conversation",
  158. });
  159. });
  160. it("restores remote workspace metadata together with the tab", () => {
  161. const store = createTabStore();
  162. store.getState().restoreTabs(
  163. [
  164. {
  165. workspacePath: "/workspace/demo",
  166. remoteSessionId: "remote-session-1",
  167. workspaceIdentity: "remote:docker:demo-container:/workspace/demo",
  168. remoteTarget: {
  169. kind: "docker",
  170. container: "demo-container",
  171. },
  172. },
  173. ],
  174. 0,
  175. );
  176. const restoredTab = store.getState().tabs[0];
  177. expect(restoredTab && isWorkspaceTab(restoredTab) ? restoredTab.remoteSessionId : null).toBe(
  178. "remote-session-1",
  179. );
  180. expect(restoredTab && isWorkspaceTab(restoredTab) ? restoredTab.workspaceIdentity : null).toBe(
  181. "remote:docker:demo-container:/workspace/demo",
  182. );
  183. });
  184. it("restores startup availability as renderer-only workspace state", () => {
  185. const store = createTabStore();
  186. store.getState().restoreTabs(
  187. [
  188. {
  189. workspacePath: "/workspace/missing",
  190. availability: "unavailable-local-directory",
  191. },
  192. ],
  193. 0,
  194. );
  195. expect(isWorkspaceReadOnly(store.getState(), "/workspace/missing")).toBe(true);
  196. const restoredTab = store.getState().tabs[0];
  197. expect(restoredTab && isWorkspaceTab(restoredTab) ? restoredTab.availability : null).toBe(
  198. "unavailable-local-directory",
  199. );
  200. });
  201. it("reuses an existing workspace tab instead of duplicating it", () => {
  202. const store = createTabStore();
  203. const firstId = store.getState().addTab("/tmp/demo");
  204. const secondId = store.getState().addTab("/tmp/demo");
  205. expect(secondId).toBe(firstId);
  206. expect(store.getState().tabs.filter(isWorkspaceTab)).toHaveLength(1);
  207. });
  208. it("refreshes remote metadata when reusing an existing remote workspace tab", () => {
  209. const store = createTabStore();
  210. store.getState().addTab("/workspace/demo", {
  211. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  212. remoteTarget: {
  213. kind: "docker",
  214. container: "demo",
  215. },
  216. });
  217. store.getState().addTab("/workspace/demo", {
  218. remoteSessionId: "remote-session-1",
  219. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  220. remoteTarget: {
  221. kind: "docker",
  222. container: "demo",
  223. },
  224. });
  225. const reusedTab = store
  226. .getState()
  227. .tabs.find((tab) => isWorkspaceTab(tab) && tab.workspacePath === "/workspace/demo");
  228. expect(reusedTab && isWorkspaceTab(reusedTab) ? reusedTab.remoteSessionId : null).toBe(
  229. "remote-session-1",
  230. );
  231. expect(reusedTab && isWorkspaceTab(reusedTab) ? reusedTab.workspaceIdentity : null).toBe(
  232. "remote:docker:demo:/workspace/demo",
  233. );
  234. });
  235. it("rekeys a restored legacy remote tab when Host returns the stable Source Server identity", () => {
  236. const store = createTabStore();
  237. const legacyIdentity = "remote:ssh:dev.example.com:22:alice:/workspace/demo";
  238. const stableIdentity = "remote:server:server-1:/workspace/demo";
  239. const firstId = store.getState().addTab("/workspace/demo", {
  240. workspaceIdentity: legacyIdentity,
  241. });
  242. const rekeyedId = store.getState().ensureWorkspaceTab("/workspace/demo", {
  243. previousWorkspaceIdentity: legacyIdentity,
  244. workspaceIdentity: stableIdentity,
  245. remoteSessionId: "remote-session-1",
  246. });
  247. expect(rekeyedId).toBe(firstId);
  248. expect(store.getState().tabs.filter(isWorkspaceTab)).toEqual([
  249. expect.objectContaining({
  250. id: firstId,
  251. workspaceIdentity: stableIdentity,
  252. remoteSessionId: "remote-session-1",
  253. }),
  254. ]);
  255. expect(store.getState().activeTabId).toBe(firstId);
  256. expect(store.getState().activeWorkspaceIdentity).toBe(stableIdentity);
  257. });
  258. it("rekeys the recently active remote identity without leaving settings", () => {
  259. const store = createTabStore();
  260. const legacyIdentity = "remote:ssh:dev.example.com:22:alice:/workspace/demo";
  261. const stableIdentity = "remote:server:server-1:/workspace/demo";
  262. store.getState().addTab("/workspace/demo", {
  263. workspaceIdentity: legacyIdentity,
  264. });
  265. store.getState().openSettingsTab();
  266. store.getState().ensureWorkspaceTab("/workspace/demo", {
  267. previousWorkspaceIdentity: legacyIdentity,
  268. workspaceIdentity: stableIdentity,
  269. });
  270. expect(store.getState().activeTabId).toBe(SETTINGS_TAB_ID);
  271. expect(store.getState().activeWorkspacePath).toBe("/workspace/demo");
  272. expect(store.getState().activeWorkspaceIdentity).toBe(stableIdentity);
  273. });
  274. it("keeps separate tabs for same path with different remote workspace identities", () => {
  275. const store = createTabStore();
  276. const firstTabId = store.getState().addTab("/workspace/demo", {
  277. remoteSessionId: "remote-session-a",
  278. workspaceIdentity: "remote:ssh:10.0.0.1:22:dev:/workspace/demo",
  279. remoteTarget: {
  280. kind: "ssh",
  281. host: "10.0.0.1",
  282. username: "dev",
  283. },
  284. });
  285. const secondTabId = store.getState().addTab("/workspace/demo", {
  286. remoteSessionId: "remote-session-b",
  287. workspaceIdentity: "remote:ssh:10.0.0.2:22:dev:/workspace/demo",
  288. remoteTarget: {
  289. kind: "ssh",
  290. host: "10.0.0.2",
  291. username: "dev",
  292. },
  293. });
  294. expect(secondTabId).not.toBe(firstTabId);
  295. expect(store.getState().tabs.filter(isWorkspaceTab)).toHaveLength(2);
  296. });
  297. it("puts newly opened workspaces at the top of the list", () => {
  298. const store = createTabStore();
  299. store.getState().addTab("/tmp/a");
  300. store.getState().addTab("/tmp/b");
  301. store.getState().addTab("/tmp/c");
  302. expect(
  303. store
  304. .getState()
  305. .tabs.filter(isWorkspaceTab)
  306. .map((tab) => tab.workspacePath),
  307. ).toEqual(["/tmp/c", "/tmp/b", "/tmp/a"]);
  308. });
  309. it("keeps the current focus while making an imported workspace visible", () => {
  310. const store = createTabStore();
  311. const workspaceIdentity = "remote:ssh:10.0.0.1:22:dev:/tmp/current";
  312. store.getState().addTab("/tmp/current", {
  313. workspaceIdentity,
  314. remoteTarget: {
  315. kind: "ssh",
  316. host: "10.0.0.1",
  317. username: "dev",
  318. },
  319. });
  320. store.getState().openSettingsTab();
  321. store.getState().ensureWorkspaceTab("/tmp/imported");
  322. expect(store.getState().activeTabId).toBe(SETTINGS_TAB_ID);
  323. expect(store.getState().activeWorkspacePath).toBe("/tmp/current");
  324. expect(store.getState().activeWorkspaceIdentity).toBe(workspaceIdentity);
  325. expect(store.getState().expandedWorkspacePaths.has("/tmp/imported")).toBe(true);
  326. expect(
  327. store
  328. .getState()
  329. .tabs.filter(isWorkspaceTab)
  330. .map((tab) => tab.workspacePath),
  331. ).toEqual(["/tmp/imported", "/tmp/current"]);
  332. });
  333. it("preserves manual workspace collapse when switching active tabs", () => {
  334. const store = createTabStore();
  335. const firstId = store.getState().addTab("/tmp/a");
  336. const secondId = store.getState().addTab("/tmp/b");
  337. store.getState().activateTab(firstId);
  338. store.getState().toggleWorkspaceExpanded("/tmp/a");
  339. store.getState().activateTab(secondId);
  340. expect(store.getState().activeWorkspacePath).toBe("/tmp/b");
  341. expect(store.getState().expandedWorkspacePaths.has("/tmp/a")).toBe(false);
  342. expect(store.getState().expandedWorkspacePaths.has("/tmp/b")).toBe(true);
  343. });
  344. it("persists workspace expanded state to local storage", () => {
  345. const storage = createMemoryStorage();
  346. const store = createTabStore(storage);
  347. store.getState().addTab("/tmp/a");
  348. store.getState().toggleWorkspaceExpanded("/tmp/a");
  349. expect(storage.getItem(WORKSPACE_EXPANSION_STORAGE_KEY)).toBe(
  350. JSON.stringify({ "/tmp/a": false }),
  351. );
  352. });
  353. it("restores workspace expanded state from local storage", () => {
  354. const storage = createMemoryStorage({
  355. [WORKSPACE_EXPANSION_STORAGE_KEY]: JSON.stringify({
  356. "/tmp/a": false,
  357. "/tmp/b": true,
  358. }),
  359. });
  360. const store = createTabStore(storage);
  361. store.getState().restoreTabs(["/tmp/a", "/tmp/b"], 1);
  362. expect(store.getState().expandedWorkspacePaths.has("/tmp/a")).toBe(false);
  363. expect(store.getState().expandedWorkspacePaths.has("/tmp/b")).toBe(true);
  364. });
  365. it("reorders only workspace tabs when sorting from the sidebar", () => {
  366. const store = createTabStore();
  367. store.getState().addTab("/tmp/a");
  368. store.getState().addTab("/tmp/b");
  369. store.getState().openSettingsTab();
  370. store.getState().reorderTabs(2, 1);
  371. expect(store.getState().tabs.map((tab) => tab.label)).toEqual(["b", "settings", "a"]);
  372. store.getState().reorderWorkspaceTabs(1, 0);
  373. expect(store.getState().tabs.map((tab) => tab.label)).toEqual(["a", "settings", "b"]);
  374. expect(
  375. store
  376. .getState()
  377. .tabs.filter(isWorkspaceTab)
  378. .map((tab) => tab.workspacePath),
  379. ).toEqual(["/tmp/a", "/tmp/b"]);
  380. });
  381. });
  382. describe("文件面板跨工作区请求", () => {
  383. it.each([undefined, "remote:target"])(
  384. "目标身份 %s 与激活 workspace 一致,旧消费不能清除新请求",
  385. (identity) => {
  386. const store = createTabStore();
  387. const original = store.getState().addTab("/original");
  388. const target = {
  389. workspacePath: "/target",
  390. workspaceIdentity: identity,
  391. workspaceRemoteSessionId: identity ? "remote-session" : undefined,
  392. };
  393. store.getState().requestFileManager(target);
  394. expect(store.getState().activeWorkspacePath).toBe("/target");
  395. expect(store.getState().activeWorkspaceIdentity).toBe(identity ?? null);
  396. expect(store.getState().pendingFileManagerTarget).toBe(target);
  397. const next = { ...target, revealPath: "/target/file.ts" };
  398. store.getState().requestFileManager(next);
  399. store.getState().consumeFileManager(target);
  400. expect(store.getState().pendingFileManagerTarget).toBe(next);
  401. store.getState().consumeFileManager(next);
  402. expect(store.getState().pendingFileManagerTarget).toBeNull();
  403. store.getState().activateTab(original);
  404. expect(store.getState().activeWorkspacePath).toBe("/original");
  405. },
  406. );
  407. });