remoteWorkspaceSessionPersistence.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. buildRemoteWorkspacePersistPatch,
  4. restorePersistedRemoteWorkspaceSessions,
  5. } from "@/root/remoteWorkspaceSessionPersistence.js";
  6. import {
  7. buildRemoteWorkspaceIdentity,
  8. getRemoteWorkspaceSessionEntries,
  9. removeRemoteWorkspaceSessionEntries,
  10. } from "@/lib/remoteWorkspaceHistory.js";
  11. import { createTabStore, isWorkspaceTab } from "@/store/tabStore.js";
  12. describe("restorePersistedRemoteWorkspaceSessions", () => {
  13. it("active-first 模式先恢复 active workspace,首帧后再补齐完整顺序", () => {
  14. const tabStoreApi = createTabStore();
  15. const result = restorePersistedRemoteWorkspaceSessions({
  16. settings: {
  17. lastWorkspaceSession: [
  18. { kind: "local", workspacePath: "/workspace/a" },
  19. { kind: "local", workspacePath: "/workspace/active" },
  20. { kind: "local", workspacePath: "/workspace/b" },
  21. ],
  22. lastActiveTabIndex: 1,
  23. },
  24. restoreMode: "active-first",
  25. tabStoreApi,
  26. });
  27. expect(
  28. tabStoreApi
  29. .getState()
  30. .tabs.filter(isWorkspaceTab)
  31. .map((tab) => tab.workspacePath),
  32. ).toEqual(["/workspace/active"]);
  33. result?.deferredRestore?.();
  34. expect(
  35. tabStoreApi
  36. .getState()
  37. .tabs.filter(isWorkspaceTab)
  38. .map((tab) => tab.workspacePath),
  39. ).toEqual(["/workspace/a", "/workspace/active", "/workspace/b"]);
  40. });
  41. it("active-first 补齐不会复活用户在 idle callback 前关闭的 active workspace", () => {
  42. const tabStoreApi = createTabStore();
  43. const result = restorePersistedRemoteWorkspaceSessions({
  44. settings: {
  45. lastWorkspaceSession: [
  46. { kind: "local", workspacePath: "/workspace/a" },
  47. { kind: "local", workspacePath: "/workspace/active" },
  48. { kind: "local", workspacePath: "/workspace/b" },
  49. ],
  50. lastActiveTabIndex: 1,
  51. },
  52. restoreMode: "active-first",
  53. tabStoreApi,
  54. });
  55. const activeTabId = tabStoreApi.getState().activeTabId;
  56. expect(activeTabId).not.toBeNull();
  57. tabStoreApi.getState().closeTab(activeTabId!);
  58. result?.deferredRestore?.();
  59. expect(
  60. tabStoreApi
  61. .getState()
  62. .tabs.filter(isWorkspaceTab)
  63. .map((tab) => tab.workspacePath),
  64. ).toEqual(["/workspace/a", "/workspace/b"]);
  65. });
  66. it("恢复本地会话工作区时保留用途", () => {
  67. const restoreTabs = vi.fn();
  68. restorePersistedRemoteWorkspaceSessions({
  69. settings: {
  70. lastWorkspaceSession: [
  71. {
  72. kind: "local",
  73. workspacePath: "/Users/demo/.zcode/workspace/default",
  74. workspacePurpose: "conversation",
  75. },
  76. ],
  77. lastActiveTabIndex: 0,
  78. },
  79. tabStoreApi: {
  80. getState: () => ({ restoreTabs }),
  81. } as never,
  82. });
  83. expect(restoreTabs).toHaveBeenCalledWith(
  84. [
  85. {
  86. workspacePath: "/Users/demo/.zcode/workspace/default",
  87. workspacePurpose: "conversation",
  88. },
  89. ],
  90. 0,
  91. );
  92. });
  93. it("canonical conversation workspace 的旧 project 用途会在恢复时自愈", () => {
  94. const restoreTabs = vi.fn();
  95. const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
  96. restorePersistedRemoteWorkspaceSessions({
  97. settings: {
  98. lastWorkspaceSession: [
  99. { kind: "local", workspacePath: "/Users/demo/project" },
  100. {
  101. kind: "local",
  102. workspacePath: conversationWorkspacePath,
  103. workspacePurpose: "project",
  104. },
  105. ],
  106. lastActiveTabIndex: 0,
  107. },
  108. conversationWorkspacePath,
  109. tabStoreApi: {
  110. getState: () => ({ restoreTabs }),
  111. } as never,
  112. });
  113. expect(restoreTabs).toHaveBeenCalledWith(
  114. [
  115. "/Users/demo/project",
  116. {
  117. workspacePath: conversationWorkspacePath,
  118. workspacePurpose: "conversation",
  119. },
  120. ],
  121. 0,
  122. );
  123. });
  124. it("canonical conversation workspace 会替换持久化遗留的其他 conversation 路径", () => {
  125. const restoreTabs = vi.fn();
  126. const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
  127. restorePersistedRemoteWorkspaceSessions({
  128. settings: {
  129. lastWorkspaceSession: [
  130. { kind: "local", workspacePath: "/Users/demo/project" },
  131. {
  132. kind: "local",
  133. workspacePath: conversationWorkspacePath,
  134. workspacePurpose: "conversation",
  135. },
  136. {
  137. kind: "local",
  138. workspacePath: "/private/tmp/test/.zcode/workspace/default",
  139. workspacePurpose: "conversation",
  140. },
  141. ],
  142. lastActiveTabIndex: 2,
  143. },
  144. conversationWorkspacePath,
  145. tabStoreApi: {
  146. getState: () => ({ restoreTabs }),
  147. } as never,
  148. });
  149. expect(restoreTabs).toHaveBeenCalledWith(
  150. [
  151. "/Users/demo/project",
  152. {
  153. workspacePath: conversationWorkspacePath,
  154. workspacePurpose: "conversation",
  155. },
  156. ],
  157. 1,
  158. );
  159. });
  160. it("持久化列表缺少 conversation workspace 时非激活补建", () => {
  161. const restoreTabs = vi.fn();
  162. const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
  163. restorePersistedRemoteWorkspaceSessions({
  164. settings: {
  165. lastWorkspaceSession: [
  166. { kind: "local", workspacePath: "/Users/demo/project" },
  167. ],
  168. lastActiveTabIndex: 0,
  169. },
  170. conversationWorkspacePath,
  171. tabStoreApi: {
  172. getState: () => ({ restoreTabs }),
  173. } as never,
  174. });
  175. expect(restoreTabs).toHaveBeenCalledWith(
  176. [
  177. "/Users/demo/project",
  178. {
  179. workspacePath: conversationWorkspacePath,
  180. workspacePurpose: "conversation",
  181. },
  182. ],
  183. 0,
  184. );
  185. });
  186. it("仅将启动期命中的本地 workspace 恢复为临时不可用状态", () => {
  187. const restoreTabs = vi.fn();
  188. restorePersistedRemoteWorkspaceSessions({
  189. settings: {
  190. lastWorkspaceSession: [
  191. { kind: "local", workspacePath: "/workspace/available" },
  192. { kind: "local", workspacePath: "/workspace/missing" },
  193. ],
  194. lastActiveTabIndex: 1,
  195. },
  196. unavailableWorkspacePath: "/workspace/missing",
  197. tabStoreApi: {
  198. getState: () => ({ restoreTabs }),
  199. } as never,
  200. });
  201. expect(restoreTabs).toHaveBeenCalledWith(
  202. [
  203. "/workspace/available",
  204. {
  205. workspacePath: "/workspace/missing",
  206. availability: "unavailable-local-directory",
  207. },
  208. ],
  209. 1,
  210. );
  211. });
  212. it("远程 workspace 即使路径同名也不应用本地不可用状态", () => {
  213. const restoreTabs = vi.fn();
  214. restorePersistedRemoteWorkspaceSessions({
  215. settings: {
  216. lastWorkspaceSession: [
  217. {
  218. kind: "remote",
  219. workspacePath: "/workspace/missing",
  220. target: { kind: "docker", container: "demo" },
  221. lastOpenedAt: 1,
  222. lastConnectionStatus: "failed",
  223. },
  224. ],
  225. lastActiveTabIndex: 0,
  226. },
  227. unavailableWorkspacePath: "/workspace/missing",
  228. tabStoreApi: {
  229. getState: () => ({ restoreTabs }),
  230. } as never,
  231. });
  232. expect(restoreTabs.mock.calls[0]?.[0]?.[0]).not.toHaveProperty("availability");
  233. });
  234. it("启动时恢复远程断开态 tab,但不再自动重连", () => {
  235. const restoreTabs = vi.fn();
  236. restorePersistedRemoteWorkspaceSessions({
  237. settings: {
  238. lastWorkspaceSession: [
  239. {
  240. kind: "remote",
  241. workspacePath: "/workspace/demo",
  242. target: {
  243. kind: "docker",
  244. container: "demo",
  245. },
  246. lastOpenedAt: 1,
  247. lastConnectionStatus: "failed",
  248. },
  249. ],
  250. lastActiveTabIndex: 0,
  251. },
  252. tabStoreApi: {
  253. getState: () => ({
  254. restoreTabs,
  255. }),
  256. } as never,
  257. });
  258. const expectedWorkspaceIdentity = buildRemoteWorkspaceIdentity("/workspace/demo", {
  259. kind: "docker",
  260. container: "demo",
  261. });
  262. expect(restoreTabs).toHaveBeenCalledWith(
  263. [
  264. {
  265. workspacePath: "/workspace/demo",
  266. remoteTarget: {
  267. kind: "docker",
  268. container: "demo",
  269. },
  270. workspaceIdentity: expectedWorkspaceIdentity,
  271. },
  272. ],
  273. 0,
  274. );
  275. });
  276. it("启动时 lastActiveTabIndex 指向远程 workspace 时跳到后面的本地 workspace", () => {
  277. const restoreTabs = vi.fn();
  278. restorePersistedRemoteWorkspaceSessions({
  279. settings: {
  280. lastWorkspaceSession: [
  281. {
  282. kind: "remote",
  283. workspacePath: "/root",
  284. workspaceIdentity: "remote:ssh:localhost:2222:root:/root",
  285. target: {
  286. kind: "ssh",
  287. host: "localhost",
  288. port: 2222,
  289. username: "root",
  290. },
  291. lastOpenedAt: 1,
  292. lastConnectionStatus: "connected",
  293. },
  294. {
  295. kind: "local",
  296. workspacePath: "/workspace/local",
  297. },
  298. ],
  299. lastActiveTabIndex: 0,
  300. },
  301. tabStoreApi: {
  302. getState: () => ({
  303. restoreTabs,
  304. }),
  305. } as never,
  306. });
  307. expect(restoreTabs).toHaveBeenCalledWith(
  308. [
  309. {
  310. workspacePath: "/root",
  311. remoteTarget: {
  312. kind: "ssh",
  313. host: "localhost",
  314. port: 2222,
  315. username: "root",
  316. },
  317. workspaceIdentity: "remote:ssh:localhost:2222:root:/root",
  318. },
  319. "/workspace/local",
  320. ],
  321. 1,
  322. );
  323. });
  324. it("启动时 lastActiveTabIndex 指向末尾远程 workspace 时回到已有本地 workspace", () => {
  325. const restoreTabs = vi.fn();
  326. restorePersistedRemoteWorkspaceSessions({
  327. settings: {
  328. lastWorkspaceSession: [
  329. {
  330. kind: "remote",
  331. workspacePath: "/root-a",
  332. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-a",
  333. target: {
  334. kind: "ssh",
  335. host: "localhost",
  336. port: 2222,
  337. username: "root",
  338. },
  339. lastOpenedAt: 1,
  340. lastConnectionStatus: "connected",
  341. },
  342. {
  343. kind: "local",
  344. workspacePath: "/workspace/local",
  345. },
  346. {
  347. kind: "remote",
  348. workspacePath: "/root-b",
  349. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-b",
  350. target: {
  351. kind: "ssh",
  352. host: "localhost",
  353. port: 2222,
  354. username: "root",
  355. },
  356. lastOpenedAt: 2,
  357. lastConnectionStatus: "connected",
  358. },
  359. ],
  360. lastActiveTabIndex: 2,
  361. },
  362. tabStoreApi: {
  363. getState: () => ({
  364. restoreTabs,
  365. }),
  366. } as never,
  367. });
  368. expect(restoreTabs).toHaveBeenCalledWith(
  369. [
  370. {
  371. workspacePath: "/root-a",
  372. remoteTarget: {
  373. kind: "ssh",
  374. host: "localhost",
  375. port: 2222,
  376. username: "root",
  377. },
  378. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-a",
  379. },
  380. "/workspace/local",
  381. {
  382. workspacePath: "/root-b",
  383. remoteTarget: {
  384. kind: "ssh",
  385. host: "localhost",
  386. port: 2222,
  387. username: "root",
  388. },
  389. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-b",
  390. },
  391. ],
  392. 1,
  393. );
  394. });
  395. it("同一远程身份只恢复一次", () => {
  396. const restoreTabs = vi.fn();
  397. const sharedWorkspaceIdentity = buildRemoteWorkspaceIdentity("/workspace/demo", {
  398. kind: "docker",
  399. container: "demo",
  400. });
  401. restorePersistedRemoteWorkspaceSessions({
  402. settings: {
  403. lastWorkspaceSession: [
  404. {
  405. kind: "remote",
  406. workspacePath: "/workspace/demo",
  407. workspaceIdentity: sharedWorkspaceIdentity,
  408. target: {
  409. kind: "docker",
  410. container: "demo",
  411. },
  412. lastOpenedAt: 1,
  413. lastConnectionStatus: "failed",
  414. },
  415. {
  416. kind: "remote",
  417. workspacePath: "/workspace/alias",
  418. workspaceIdentity: sharedWorkspaceIdentity,
  419. target: {
  420. kind: "docker",
  421. container: "demo",
  422. },
  423. lastOpenedAt: 1,
  424. lastConnectionStatus: "failed",
  425. },
  426. ],
  427. lastActiveTabIndex: 0,
  428. },
  429. tabStoreApi: {
  430. getState: () => ({
  431. restoreTabs,
  432. }),
  433. } as never,
  434. });
  435. expect(restoreTabs).toHaveBeenCalledWith(
  436. [
  437. {
  438. workspacePath: "/workspace/demo",
  439. remoteTarget: {
  440. kind: "docker",
  441. container: "demo",
  442. },
  443. workspaceIdentity: sharedWorkspaceIdentity,
  444. },
  445. ],
  446. 0,
  447. );
  448. });
  449. it("远程入口不可见时只恢复本地 workspace", () => {
  450. const restoreTabs = vi.fn();
  451. restorePersistedRemoteWorkspaceSessions({
  452. settings: {
  453. lastWorkspaceSession: [
  454. { kind: "local", workspacePath: "/workspace/local" },
  455. {
  456. kind: "remote",
  457. workspacePath: "/workspace/demo",
  458. target: {
  459. kind: "docker",
  460. container: "demo",
  461. },
  462. lastOpenedAt: 1,
  463. lastConnectionStatus: "failed",
  464. },
  465. ],
  466. lastActiveTabIndex: 1,
  467. },
  468. tabStoreApi: {
  469. getState: () => ({
  470. restoreTabs,
  471. }),
  472. } as never,
  473. allowRemoteWorkspaceRestore: false,
  474. });
  475. expect(restoreTabs).toHaveBeenCalledWith(["/workspace/local"], 0);
  476. });
  477. it("远程入口隐藏后,后续持久化仍保留远程会话快照", () => {
  478. const restoreTabs = vi.fn();
  479. const settings = {
  480. lastWorkspaceSession: [
  481. { kind: "local" as const, workspacePath: "/workspace/local" },
  482. {
  483. kind: "remote" as const,
  484. workspacePath: "/workspace/demo",
  485. target: {
  486. kind: "docker" as const,
  487. container: "demo",
  488. },
  489. lastOpenedAt: 1,
  490. lastConnectionStatus: "failed" as const,
  491. },
  492. ],
  493. lastActiveTabIndex: 1,
  494. };
  495. restorePersistedRemoteWorkspaceSessions({
  496. settings,
  497. tabStoreApi: {
  498. getState: () => ({
  499. restoreTabs,
  500. }),
  501. } as never,
  502. allowRemoteWorkspaceRestore: false,
  503. });
  504. expect(restoreTabs).toHaveBeenCalledWith(["/workspace/local"], 0);
  505. const remoteSessions = getRemoteWorkspaceSessionEntries(settings);
  506. const patch = buildRemoteWorkspacePersistPatch(
  507. {
  508. tabs: [
  509. {
  510. id: "local-1",
  511. kind: "workspace",
  512. workspacePath: "/workspace/local",
  513. label: "local",
  514. },
  515. ],
  516. activeWorkspacePath: "/workspace/local",
  517. } as never,
  518. remoteSessions,
  519. );
  520. expect(patch.lastWorkspaceSession).toEqual([
  521. { kind: "local", workspacePath: "/workspace/local" },
  522. settings.lastWorkspaceSession[1],
  523. ]);
  524. });
  525. it("持久化本地会话工作区时保留用途", () => {
  526. const patch = buildRemoteWorkspacePersistPatch(
  527. {
  528. tabs: [
  529. {
  530. id: "conversation-1",
  531. kind: "workspace",
  532. workspacePath: "/Users/demo/.zcode/workspace/default",
  533. workspacePurpose: "conversation",
  534. availability: "unavailable-local-directory",
  535. label: "default",
  536. },
  537. ],
  538. activeWorkspacePath: "/Users/demo/.zcode/workspace/default",
  539. } as never,
  540. [],
  541. );
  542. expect(patch.lastWorkspaceSession).toEqual([
  543. {
  544. kind: "local",
  545. workspacePath: "/Users/demo/.zcode/workspace/default",
  546. workspacePurpose: "conversation",
  547. },
  548. ]);
  549. });
  550. it("用户移除远程 tab 后,持久化补丁不再保留对应远程快照", () => {
  551. const remoteSession = {
  552. kind: "remote" as const,
  553. workspacePath: "/workspace/demo",
  554. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  555. target: {
  556. kind: "docker" as const,
  557. container: "demo",
  558. },
  559. lastOpenedAt: 1,
  560. lastConnectionStatus: "failed" as const,
  561. };
  562. const removal = removeRemoteWorkspaceSessionEntries(
  563. [remoteSession],
  564. ["remote:docker:demo:/workspace/demo"],
  565. );
  566. const patch = buildRemoteWorkspacePersistPatch(
  567. {
  568. tabs: [],
  569. activeWorkspacePath: null,
  570. } as never,
  571. removal.nextRemoteSessions,
  572. );
  573. expect(patch.lastWorkspaceSession).toEqual([]);
  574. });
  575. });