tabStore.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { describe, expect, it, vi } from "vitest";
  2. import { applyWorkspaceTriggerSelection } from "../src/WorkspaceSidebar.js";
  3. import {
  4. createTabStore,
  5. isWorkspaceReadOnly,
  6. isWorkspaceTab,
  7. SETTINGS_TAB_ID,
  8. } from "../src/store/tabStore.js";
  9. import { WORKSPACE_EXPANSION_STORAGE_KEY } from "../src/lib/workspaceExpansionPreference.js";
  10. function createMemoryStorage(initial: Record<string, string> = {}) {
  11. const values = new Map(Object.entries(initial));
  12. return {
  13. getItem: (key: string) => values.get(key) ?? null,
  14. setItem: (key: string, value: string) => {
  15. values.set(key, value);
  16. },
  17. };
  18. }
  19. describe("tabStore", () => {
  20. it("opens only one settings tab per window", () => {
  21. const store = createTabStore();
  22. store.getState().openSettingsTab();
  23. store.getState().openSettingsTab();
  24. const settingsTabs = store.getState().tabs.filter((tab) => tab.id === SETTINGS_TAB_ID);
  25. expect(settingsTabs).toHaveLength(1);
  26. expect(store.getState().activeTabId).toBe(SETTINGS_TAB_ID);
  27. });
  28. it("restores only workspace tabs from persisted session", () => {
  29. const store = createTabStore();
  30. store.getState().restoreTabs(["/tmp/a", "/tmp/b"], 1);
  31. expect(store.getState().tabs.every(isWorkspaceTab)).toBe(true);
  32. expect(store.getState().tabs).toHaveLength(2);
  33. expect(store.getState().activeTabId).toBe(store.getState().tabs[1]?.id ?? null);
  34. });
  35. it("首帧后补齐 inactive workspace 时保留 active tab identity 和焦点", () => {
  36. const store = createTabStore();
  37. store.getState().restoreTabs(["/tmp/active"], 0);
  38. const activeTabId = store.getState().activeTabId;
  39. store.getState().completeTabRestore(["/tmp/inactive-a", "/tmp/active", "/tmp/inactive-b"]);
  40. expect(
  41. store
  42. .getState()
  43. .tabs.filter(isWorkspaceTab)
  44. .map((tab) => tab.workspacePath),
  45. ).toEqual(["/tmp/inactive-a", "/tmp/active", "/tmp/inactive-b"]);
  46. expect(store.getState().activeTabId).toBe(activeTabId);
  47. expect(store.getState().activeWorkspacePath).toBe("/tmp/active");
  48. expect(store.getState().tabs.find((tab) => tab.id === activeTabId)).toMatchObject({
  49. workspacePath: "/tmp/active",
  50. });
  51. });
  52. it("补齐 inactive workspace 不覆盖启动后用户新开的 tab", () => {
  53. const store = createTabStore();
  54. store.getState().restoreTabs(["/tmp/active"], 0);
  55. const userTabId = store.getState().addTab("/tmp/user-opened");
  56. store.getState().completeTabRestore(["/tmp/inactive", "/tmp/active"]);
  57. expect(
  58. store
  59. .getState()
  60. .tabs.filter(isWorkspaceTab)
  61. .map((tab) => tab.workspacePath),
  62. ).toEqual(["/tmp/user-opened", "/tmp/inactive", "/tmp/active"]);
  63. expect(store.getState().activeTabId).toBe(userTabId);
  64. expect(store.getState().activeWorkspacePath).toBe("/tmp/user-opened");
  65. });
  66. it("preserves conversation purpose when adding and restoring managed workspace tabs", () => {
  67. const store = createTabStore();
  68. store.getState().addTab("/tmp/.zcode/workspace/default", {
  69. workspacePurpose: "conversation",
  70. });
  71. expect(store.getState().tabs[0]).toMatchObject({
  72. workspacePath: "/tmp/.zcode/workspace/default",
  73. workspacePurpose: "conversation",
  74. });
  75. store.getState().restoreTabs(
  76. [
  77. {
  78. workspacePath: "/tmp/.zcode/workspace/default",
  79. workspacePurpose: "conversation",
  80. },
  81. ],
  82. 0,
  83. );
  84. expect(store.getState().tabs[0]).toMatchObject({
  85. workspacePurpose: "conversation",
  86. });
  87. });
  88. it("restores remote workspace metadata together with the tab", () => {
  89. const store = createTabStore();
  90. store.getState().restoreTabs(
  91. [
  92. {
  93. workspacePath: "/workspace/demo",
  94. remoteSessionId: "remote-session-1",
  95. workspaceIdentity: "remote:docker:demo-container:/workspace/demo",
  96. remoteTarget: {
  97. kind: "docker",
  98. container: "demo-container",
  99. },
  100. },
  101. ],
  102. 0,
  103. );
  104. const restoredTab = store.getState().tabs[0];
  105. expect(restoredTab && isWorkspaceTab(restoredTab) ? restoredTab.remoteSessionId : null).toBe(
  106. "remote-session-1",
  107. );
  108. expect(restoredTab && isWorkspaceTab(restoredTab) ? restoredTab.workspaceIdentity : null).toBe(
  109. "remote:docker:demo-container:/workspace/demo",
  110. );
  111. });
  112. it("restores startup availability as renderer-only workspace state", () => {
  113. const store = createTabStore();
  114. store.getState().restoreTabs(
  115. [
  116. {
  117. workspacePath: "/workspace/missing",
  118. availability: "unavailable-local-directory",
  119. },
  120. ],
  121. 0,
  122. );
  123. expect(isWorkspaceReadOnly(store.getState(), "/workspace/missing")).toBe(true);
  124. const restoredTab = store.getState().tabs[0];
  125. expect(restoredTab && isWorkspaceTab(restoredTab) ? restoredTab.availability : null).toBe(
  126. "unavailable-local-directory",
  127. );
  128. });
  129. it("reuses an existing workspace tab instead of duplicating it", () => {
  130. const store = createTabStore();
  131. const firstId = store.getState().addTab("/tmp/demo");
  132. const secondId = store.getState().addTab("/tmp/demo");
  133. expect(secondId).toBe(firstId);
  134. expect(store.getState().tabs.filter(isWorkspaceTab)).toHaveLength(1);
  135. });
  136. it("refreshes remote metadata when reusing an existing remote workspace tab", () => {
  137. const store = createTabStore();
  138. store.getState().addTab("/workspace/demo", {
  139. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  140. remoteTarget: {
  141. kind: "docker",
  142. container: "demo",
  143. },
  144. });
  145. store.getState().addTab("/workspace/demo", {
  146. remoteSessionId: "remote-session-1",
  147. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  148. remoteTarget: {
  149. kind: "docker",
  150. container: "demo",
  151. },
  152. });
  153. const reusedTab = store.getState().tabs.find(
  154. (tab) => isWorkspaceTab(tab) && tab.workspacePath === "/workspace/demo",
  155. );
  156. expect(reusedTab && isWorkspaceTab(reusedTab) ? reusedTab.remoteSessionId : null).toBe(
  157. "remote-session-1",
  158. );
  159. expect(reusedTab && isWorkspaceTab(reusedTab) ? reusedTab.workspaceIdentity : null).toBe(
  160. "remote:docker:demo:/workspace/demo",
  161. );
  162. });
  163. it("keeps separate tabs for same path with different remote workspace identities", () => {
  164. const store = createTabStore();
  165. const firstTabId = store.getState().addTab("/workspace/demo", {
  166. remoteSessionId: "remote-session-a",
  167. workspaceIdentity: "remote:ssh:10.0.0.1:22:dev:/workspace/demo",
  168. remoteTarget: {
  169. kind: "ssh",
  170. host: "10.0.0.1",
  171. username: "dev",
  172. },
  173. });
  174. const secondTabId = store.getState().addTab("/workspace/demo", {
  175. remoteSessionId: "remote-session-b",
  176. workspaceIdentity: "remote:ssh:10.0.0.2:22:dev:/workspace/demo",
  177. remoteTarget: {
  178. kind: "ssh",
  179. host: "10.0.0.2",
  180. username: "dev",
  181. },
  182. });
  183. expect(secondTabId).not.toBe(firstTabId);
  184. expect(store.getState().tabs.filter(isWorkspaceTab)).toHaveLength(2);
  185. });
  186. it("puts newly opened workspaces at the top of the list", () => {
  187. const store = createTabStore();
  188. store.getState().addTab("/tmp/a");
  189. store.getState().addTab("/tmp/b");
  190. store.getState().addTab("/tmp/c");
  191. expect(store.getState().tabs.filter(isWorkspaceTab).map((tab) => tab.workspacePath)).toEqual([
  192. "/tmp/c",
  193. "/tmp/b",
  194. "/tmp/a",
  195. ]);
  196. });
  197. it("keeps the current focus while making an imported workspace visible", () => {
  198. const store = createTabStore();
  199. const workspaceIdentity = "remote:ssh:10.0.0.1:22:dev:/tmp/current";
  200. store.getState().addTab("/tmp/current", {
  201. workspaceIdentity,
  202. remoteTarget: {
  203. kind: "ssh",
  204. host: "10.0.0.1",
  205. username: "dev",
  206. },
  207. });
  208. store.getState().openSettingsTab();
  209. store.getState().ensureWorkspaceTab("/tmp/imported");
  210. expect(store.getState().activeTabId).toBe(SETTINGS_TAB_ID);
  211. expect(store.getState().activeWorkspacePath).toBe("/tmp/current");
  212. expect(store.getState().activeWorkspaceIdentity).toBe(workspaceIdentity);
  213. expect(store.getState().expandedWorkspacePaths.has("/tmp/imported")).toBe(true);
  214. expect(store.getState().tabs.filter(isWorkspaceTab).map((tab) => tab.workspacePath)).toEqual([
  215. "/tmp/imported",
  216. "/tmp/current",
  217. ]);
  218. });
  219. it("preserves manual workspace collapse when switching active tabs", () => {
  220. const store = createTabStore();
  221. const firstId = store.getState().addTab("/tmp/a");
  222. const secondId = store.getState().addTab("/tmp/b");
  223. store.getState().activateTab(firstId);
  224. store.getState().toggleWorkspaceExpanded("/tmp/a");
  225. store.getState().activateTab(secondId);
  226. expect(store.getState().activeWorkspacePath).toBe("/tmp/b");
  227. expect(store.getState().expandedWorkspacePaths.has("/tmp/a")).toBe(false);
  228. expect(store.getState().expandedWorkspacePaths.has("/tmp/b")).toBe(true);
  229. });
  230. it("persists workspace expanded state to local storage", () => {
  231. const storage = createMemoryStorage();
  232. const store = createTabStore(storage);
  233. store.getState().addTab("/tmp/a");
  234. store.getState().toggleWorkspaceExpanded("/tmp/a");
  235. expect(storage.getItem(WORKSPACE_EXPANSION_STORAGE_KEY)).toBe(
  236. JSON.stringify({ "/tmp/a": false }),
  237. );
  238. });
  239. it("restores workspace expanded state from local storage", () => {
  240. const storage = createMemoryStorage({
  241. [WORKSPACE_EXPANSION_STORAGE_KEY]: JSON.stringify({
  242. "/tmp/a": false,
  243. "/tmp/b": true,
  244. }),
  245. });
  246. const store = createTabStore(storage);
  247. store.getState().restoreTabs(["/tmp/a", "/tmp/b"], 1);
  248. expect(store.getState().expandedWorkspacePaths.has("/tmp/a")).toBe(false);
  249. expect(store.getState().expandedWorkspacePaths.has("/tmp/b")).toBe(true);
  250. });
  251. it("reorders only workspace tabs when sorting from the sidebar", () => {
  252. const store = createTabStore();
  253. store.getState().addTab("/tmp/a");
  254. store.getState().addTab("/tmp/b");
  255. store.getState().openSettingsTab();
  256. store.getState().reorderTabs(2, 1);
  257. expect(store.getState().tabs.map((tab) => tab.label)).toEqual(["b", "settings", "a"]);
  258. store.getState().reorderWorkspaceTabs(1, 0);
  259. expect(store.getState().tabs.map((tab) => tab.label)).toEqual(["a", "settings", "b"]);
  260. expect(store.getState().tabs.filter(isWorkspaceTab).map((tab) => tab.workspacePath)).toEqual([
  261. "/tmp/a",
  262. "/tmp/b",
  263. ]);
  264. });
  265. it("reopens a collapsed workspace without toggling it shut again", () => {
  266. const activateTab = vi.fn();
  267. const toggleWorkspaceExpanded = vi.fn();
  268. applyWorkspaceTriggerSelection({
  269. tabId: "tab-a",
  270. workspacePath: "/tmp/a",
  271. isExpanded: false,
  272. activateTab,
  273. toggleWorkspaceExpanded,
  274. });
  275. expect(activateTab).toHaveBeenCalledWith("tab-a");
  276. expect(toggleWorkspaceExpanded).not.toHaveBeenCalled();
  277. });
  278. it("collapses an expanded workspace after activating it", () => {
  279. const activateTab = vi.fn();
  280. const toggleWorkspaceExpanded = vi.fn();
  281. applyWorkspaceTriggerSelection({
  282. tabId: "tab-a",
  283. workspacePath: "/tmp/a",
  284. isExpanded: true,
  285. activateTab,
  286. toggleWorkspaceExpanded,
  287. });
  288. expect(activateTab).toHaveBeenCalledWith("tab-a");
  289. expect(toggleWorkspaceExpanded).toHaveBeenCalledWith("/tmp/a");
  290. });
  291. });