remoteWorkspaceSessionPersistence.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. buildRemoteWorkspacePersistPatch,
  4. projectRemoteConversationWorkspacePurpose,
  5. restorePersistedRemoteWorkspaceSessions,
  6. } from "@/root/remoteWorkspaceSessionPersistence.js";
  7. import {
  8. buildRemoteWorkspaceIdentity,
  9. getRemoteWorkspaceSessionEntries,
  10. projectRemoteWorkspaceSessionIdentityAliases,
  11. } from "@/lib/remoteWorkspaceHistory.js";
  12. import { createTabStore, isWorkspaceTab } from "@/store/tabStore.js";
  13. describe("restorePersistedRemoteWorkspaceSessions", () => {
  14. it.each(["A", "B"])(
  15. "新版远程活动目录 %s 恢复,旧投影中的 sibling 不创建 attachment 候选",
  16. (active) => {
  17. const makeEntry = (name: string) => ({
  18. kind: "remote" as const,
  19. workspacePath: `/srv/${name}`,
  20. workspaceIdentity: `remote:server:source-1:/srv/${name}`,
  21. target: { kind: "ssh" as const, host: "dev.example.com", username: "dev" },
  22. lastOpenedAt: 1,
  23. lastConnectionStatus: "connected" as const,
  24. });
  25. // 真实冷启动轨迹同时有旧 A/B 投影和新版单一活动目录,不能把旧投影当作 open set。
  26. const settings = {
  27. lastActiveWorkspace: makeEntry(active),
  28. lastWorkspaceSession: [makeEntry("A"), makeEntry("B")],
  29. lastActiveTabIndex: 0,
  30. };
  31. const tabStoreApi = createTabStore();
  32. const restored = restorePersistedRemoteWorkspaceSessions({
  33. settings,
  34. tabStoreApi,
  35. restoreMode: "active-first",
  36. });
  37. expect(restored?.deferredRestore).toBeUndefined();
  38. expect(getRemoteWorkspaceSessionEntries(settings).map((e) => e.workspacePath)).toEqual([
  39. `/srv/${active}`,
  40. ]);
  41. expect(
  42. tabStoreApi
  43. .getState()
  44. .tabs.filter(isWorkspaceTab)
  45. .map((t) => t.workspaceIdentity),
  46. ).toEqual([`remote:server:source-1:/srv/${active}`]);
  47. expect(tabStoreApi.getState().activeWorkspaceIdentity).toBe(
  48. `remote:server:source-1:/srv/${active}`,
  49. );
  50. },
  51. );
  52. it.each(["all", "active-first"] as const)(
  53. "新版活动项目失效时直接进入无项目工作区,补齐不抢占用户选择(%s)",
  54. (restoreMode) => {
  55. const tabStoreApi = createTabStore();
  56. const result = restorePersistedRemoteWorkspaceSessions({
  57. settings: {
  58. lastActiveWorkspace: { kind: "local", workspacePath: "/workspace/missing" },
  59. },
  60. unavailableWorkspacePath: "/workspace/missing",
  61. conversationWorkspacePath: "/workspace/default",
  62. restoreMode,
  63. tabStoreApi,
  64. });
  65. expect(tabStoreApi.getState().activeWorkspacePath).toBe("/workspace/default");
  66. expect(tabStoreApi.getState().tabs.filter(isWorkspaceTab)).toContainEqual(
  67. expect.objectContaining({
  68. workspacePath: "/workspace/default",
  69. workspacePurpose: "conversation",
  70. }),
  71. );
  72. tabStoreApi.getState().addTab("/workspace/user-selected");
  73. result?.deferredRestore?.();
  74. expect(tabStoreApi.getState().activeWorkspacePath).toBe("/workspace/user-selected");
  75. expect(tabStoreApi.getState().tabs.filter(isWorkspaceTab)).toContainEqual(
  76. expect.objectContaining({
  77. workspacePath: "/workspace/missing",
  78. availability: "unavailable-local-directory",
  79. }),
  80. );
  81. },
  82. );
  83. it.each(["before", "after", "absent"] as const)(
  84. "旧版首个项目失效时无项目目录的位置不影响默认草稿(%s)",
  85. (position) => {
  86. const tabStoreApi = createTabStore();
  87. const missing = { kind: "local" as const, workspacePath: "/workspace/missing" };
  88. const conversation = {
  89. kind: "local" as const,
  90. workspacePath: "/workspace/default",
  91. workspacePurpose: "conversation" as const,
  92. };
  93. restorePersistedRemoteWorkspaceSessions({
  94. settings: {
  95. lastWorkspaceSession:
  96. position === "before"
  97. ? [conversation, missing]
  98. : position === "after"
  99. ? [missing, conversation]
  100. : [missing],
  101. lastActiveTabIndex: position === "before" ? 1 : 0,
  102. },
  103. unavailableWorkspacePath: missing.workspacePath,
  104. conversationWorkspacePath: conversation.workspacePath,
  105. tabStoreApi,
  106. });
  107. expect(tabStoreApi.getState().activeWorkspacePath).toBe(conversation.workspacePath);
  108. expect(
  109. tabStoreApi
  110. .getState()
  111. .tabs.filter(isWorkspaceTab)
  112. .filter((tab) => tab.workspacePath === conversation.workspacePath),
  113. ).toHaveLength(1);
  114. },
  115. );
  116. it("失效的非活动项目不改变当前有效项目的选择", () => {
  117. const tabStoreApi = createTabStore();
  118. restorePersistedRemoteWorkspaceSessions({
  119. settings: {
  120. lastWorkspaceSession: [
  121. { kind: "local", workspacePath: "/workspace/missing" },
  122. { kind: "local", workspacePath: "/workspace/available" },
  123. ],
  124. lastActiveTabIndex: 1,
  125. },
  126. unavailableWorkspacePath: "/workspace/missing",
  127. conversationWorkspacePath: "/workspace/default",
  128. tabStoreApi,
  129. });
  130. expect(tabStoreApi.getState().activeWorkspacePath).toBe("/workspace/available");
  131. });
  132. it("新建任务的活动 cwd 保持独立,不被旧 canonical conversation 目录替换", () => {
  133. const tabStoreApi = createTabStore();
  134. restorePersistedRemoteWorkspaceSessions({
  135. settings: {
  136. lastActiveWorkspace: {
  137. kind: "local",
  138. workspacePath: "/workspace/new-task",
  139. workspacePurpose: "conversation",
  140. },
  141. },
  142. conversationWorkspacePath: "/workspace/default",
  143. tabStoreApi,
  144. });
  145. expect(
  146. tabStoreApi
  147. .getState()
  148. .tabs.filter(isWorkspaceTab)
  149. .map((tab) => tab.workspacePath),
  150. ).toEqual(["/workspace/new-task"]);
  151. });
  152. it("新版只恢复 lastActiveWorkspace,兼容字段中的旧项目不参与恢复", () => {
  153. const tabStoreApi = createTabStore();
  154. restorePersistedRemoteWorkspaceSessions({
  155. settings: {
  156. lastActiveWorkspace: { kind: "local", workspacePath: "/selected" },
  157. lastWorkspaceSession: [{ kind: "local", workspacePath: "/deleted" }],
  158. lastActiveTabIndex: 4,
  159. },
  160. tabStoreApi,
  161. });
  162. expect(
  163. tabStoreApi
  164. .getState()
  165. .tabs.filter(isWorkspaceTab)
  166. .map((tab) => tab.workspacePath),
  167. ).toEqual(["/selected"]);
  168. });
  169. it("新版 null 活动选择不会回读旧兼容集合", () => {
  170. const tabStoreApi = createTabStore();
  171. restorePersistedRemoteWorkspaceSessions({
  172. settings: {
  173. lastActiveWorkspace: null,
  174. lastWorkspaceSession: [{ kind: "local", workspacePath: "/deleted" }],
  175. },
  176. tabStoreApi,
  177. });
  178. expect(tabStoreApi.getState().tabs.filter(isWorkspaceTab)).toEqual([]);
  179. });
  180. it("窗口只写活动目录,并按 identity 区分同路径的远端", () => {
  181. const tabStoreApi = createTabStore();
  182. const target = { kind: "ssh" as const, host: "example.test", username: "user" };
  183. const sessions = ["a", "b"].map((id) => ({
  184. kind: "remote" as const,
  185. workspacePath: "/same",
  186. workspaceIdentity: `remote:server:${id}:/same`,
  187. target,
  188. lastOpenedAt: 1,
  189. lastConnectionStatus: "connected" as const,
  190. }));
  191. tabStoreApi.getState().restoreTabs(
  192. sessions.map((entry) => ({
  193. workspacePath: entry.workspacePath,
  194. workspaceIdentity: entry.workspaceIdentity,
  195. remoteTarget: target,
  196. })),
  197. 1,
  198. );
  199. expect(buildRemoteWorkspacePersistPatch(tabStoreApi.getState(), sessions)).toEqual({
  200. lastActiveWorkspace: sessions[1],
  201. });
  202. });
  203. it("projects a legacy route session to the Source Server stable identity before restoring UI state", () => {
  204. const legacyIdentity = "remote:ssh:dev.example.com:22:alice:/srv/project";
  205. const stableIdentity = "remote:server:server-1:/srv/project";
  206. const settings = {
  207. lastWorkspaceSession: [
  208. {
  209. kind: "remote" as const,
  210. workspacePath: "/srv/project",
  211. workspaceIdentity: legacyIdentity,
  212. target: {
  213. kind: "ssh" as const,
  214. host: "dev.example.com",
  215. port: 22,
  216. username: "alice",
  217. },
  218. lastOpenedAt: 1,
  219. lastConnectionStatus: "failed" as const,
  220. },
  221. ],
  222. sourceServerWorkspaceHistory: [
  223. {
  224. workspacePath: "/srv/project",
  225. workspaceIdentity: stableIdentity,
  226. legacyWorkspaceIdentity: legacyIdentity,
  227. profileId: "profile-1",
  228. sourceServerId: "server-1",
  229. lastOpenedAt: 1,
  230. },
  231. ],
  232. };
  233. expect(getRemoteWorkspaceSessionEntries(settings)).toEqual([
  234. expect.objectContaining({ workspaceIdentity: stableIdentity }),
  235. ]);
  236. const tabStoreApi = createTabStore();
  237. restorePersistedRemoteWorkspaceSessions({ settings, tabStoreApi });
  238. expect(tabStoreApi.getState().tabs.filter(isWorkspaceTab)).toEqual([
  239. expect.objectContaining({ workspaceIdentity: stableIdentity }),
  240. ]);
  241. });
  242. it("active-first 模式先恢复 active workspace,首帧后再补齐完整顺序", () => {
  243. const tabStoreApi = createTabStore();
  244. const result = restorePersistedRemoteWorkspaceSessions({
  245. settings: {
  246. lastWorkspaceSession: [
  247. { kind: "local", workspacePath: "/workspace/a" },
  248. { kind: "local", workspacePath: "/workspace/active" },
  249. { kind: "local", workspacePath: "/workspace/b" },
  250. ],
  251. lastActiveTabIndex: 1,
  252. },
  253. restoreMode: "active-first",
  254. tabStoreApi,
  255. });
  256. expect(
  257. tabStoreApi
  258. .getState()
  259. .tabs.filter(isWorkspaceTab)
  260. .map((tab) => tab.workspacePath),
  261. ).toEqual(["/workspace/active"]);
  262. result?.deferredRestore?.();
  263. expect(
  264. tabStoreApi
  265. .getState()
  266. .tabs.filter(isWorkspaceTab)
  267. .map((tab) => tab.workspacePath),
  268. ).toEqual(["/workspace/a", "/workspace/active", "/workspace/b"]);
  269. });
  270. it("active-first 补齐不会复活用户在 idle callback 前关闭的 active workspace", () => {
  271. const tabStoreApi = createTabStore();
  272. const result = restorePersistedRemoteWorkspaceSessions({
  273. settings: {
  274. lastWorkspaceSession: [
  275. { kind: "local", workspacePath: "/workspace/a" },
  276. { kind: "local", workspacePath: "/workspace/active" },
  277. { kind: "local", workspacePath: "/workspace/b" },
  278. ],
  279. lastActiveTabIndex: 1,
  280. },
  281. restoreMode: "active-first",
  282. tabStoreApi,
  283. });
  284. const activeTabId = tabStoreApi.getState().activeTabId;
  285. expect(activeTabId).not.toBeNull();
  286. tabStoreApi.getState().closeTab(activeTabId!);
  287. result?.deferredRestore?.();
  288. expect(
  289. tabStoreApi
  290. .getState()
  291. .tabs.filter(isWorkspaceTab)
  292. .map((tab) => tab.workspacePath),
  293. ).toEqual(["/workspace/a", "/workspace/b"]);
  294. });
  295. it("恢复本地会话工作区时保留用途", () => {
  296. const restoreTabs = vi.fn();
  297. restorePersistedRemoteWorkspaceSessions({
  298. settings: {
  299. lastWorkspaceSession: [
  300. {
  301. kind: "local",
  302. workspacePath: "/Users/demo/.zcode/workspace/default",
  303. workspacePurpose: "conversation",
  304. },
  305. ],
  306. lastActiveTabIndex: 0,
  307. },
  308. tabStoreApi: {
  309. getState: () => ({ restoreTabs }),
  310. } as never,
  311. });
  312. expect(restoreTabs).toHaveBeenCalledWith(
  313. [
  314. {
  315. workspacePath: "/Users/demo/.zcode/workspace/default",
  316. workspacePurpose: "conversation",
  317. },
  318. ],
  319. 0,
  320. );
  321. });
  322. it("canonical conversation workspace 的旧 project 用途会在恢复时自愈", () => {
  323. const restoreTabs = vi.fn();
  324. const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
  325. restorePersistedRemoteWorkspaceSessions({
  326. settings: {
  327. lastWorkspaceSession: [
  328. { kind: "local", workspacePath: "/Users/demo/project" },
  329. {
  330. kind: "local",
  331. workspacePath: conversationWorkspacePath,
  332. workspacePurpose: "project",
  333. },
  334. ],
  335. lastActiveTabIndex: 0,
  336. },
  337. conversationWorkspacePath,
  338. tabStoreApi: {
  339. getState: () => ({ restoreTabs }),
  340. } as never,
  341. });
  342. expect(restoreTabs).toHaveBeenCalledWith(
  343. [
  344. "/Users/demo/project",
  345. {
  346. workspacePath: conversationWorkspacePath,
  347. workspacePurpose: "conversation",
  348. },
  349. ],
  350. 0,
  351. );
  352. });
  353. it("canonical conversation workspace 会替换持久化遗留的其他 conversation 路径", () => {
  354. const restoreTabs = vi.fn();
  355. const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
  356. restorePersistedRemoteWorkspaceSessions({
  357. settings: {
  358. lastWorkspaceSession: [
  359. { kind: "local", workspacePath: "/Users/demo/project" },
  360. {
  361. kind: "local",
  362. workspacePath: conversationWorkspacePath,
  363. workspacePurpose: "conversation",
  364. },
  365. {
  366. kind: "local",
  367. workspacePath: "/private/tmp/test/.zcode/workspace/default",
  368. workspacePurpose: "conversation",
  369. },
  370. ],
  371. lastActiveTabIndex: 2,
  372. },
  373. conversationWorkspacePath,
  374. tabStoreApi: {
  375. getState: () => ({ restoreTabs }),
  376. } as never,
  377. });
  378. expect(restoreTabs).toHaveBeenCalledWith(
  379. [
  380. "/Users/demo/project",
  381. {
  382. workspacePath: conversationWorkspacePath,
  383. workspacePurpose: "conversation",
  384. },
  385. ],
  386. 1,
  387. );
  388. });
  389. it("RPN-09 excludes an old project entry identified by its exact canonical conversation record", () => {
  390. const restoreTabs = vi.fn();
  391. const sourceServerId = "11111111-1111-4111-8111-111111111111";
  392. const workspacePath = "/home/dev/.zcode/workspace/default";
  393. restorePersistedRemoteWorkspaceSessions({
  394. settings: {
  395. lastWorkspaceSession: [
  396. {
  397. kind: "remote",
  398. workspacePath,
  399. workspaceIdentity: `remote:server:${sourceServerId}:${workspacePath}`,
  400. target: { kind: "ssh", host: "linux-amd64-dev", username: "dev" },
  401. lastOpenedAt: 1,
  402. lastConnectionStatus: "connected",
  403. },
  404. ],
  405. remoteConversationWorkspaceBySourceServerId: {
  406. [sourceServerId]: { workspacePath, lastResolvedAt: 2 },
  407. },
  408. },
  409. tabStoreApi: { getState: () => ({ restoreTabs }) } as never,
  410. });
  411. expect(restoreTabs).not.toHaveBeenCalled();
  412. });
  413. it("持久化列表缺少 conversation workspace 时非激活补建", () => {
  414. const restoreTabs = vi.fn();
  415. const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
  416. restorePersistedRemoteWorkspaceSessions({
  417. settings: {
  418. lastWorkspaceSession: [{ kind: "local", workspacePath: "/Users/demo/project" }],
  419. lastActiveTabIndex: 0,
  420. },
  421. conversationWorkspacePath,
  422. tabStoreApi: {
  423. getState: () => ({ restoreTabs }),
  424. } as never,
  425. });
  426. expect(restoreTabs).toHaveBeenCalledWith(
  427. [
  428. "/Users/demo/project",
  429. {
  430. workspacePath: conversationWorkspacePath,
  431. workspacePurpose: "conversation",
  432. },
  433. ],
  434. 0,
  435. );
  436. });
  437. it("仅将启动期命中的本地 workspace 恢复为临时不可用状态", () => {
  438. const restoreTabs = vi.fn();
  439. restorePersistedRemoteWorkspaceSessions({
  440. settings: {
  441. lastWorkspaceSession: [
  442. { kind: "local", workspacePath: "/workspace/available" },
  443. { kind: "local", workspacePath: "/workspace/missing" },
  444. ],
  445. lastActiveTabIndex: 1,
  446. },
  447. unavailableWorkspacePath: "/workspace/missing",
  448. tabStoreApi: {
  449. getState: () => ({ restoreTabs }),
  450. } as never,
  451. });
  452. expect(restoreTabs).toHaveBeenCalledWith(
  453. [
  454. "/workspace/available",
  455. {
  456. workspacePath: "/workspace/missing",
  457. availability: "unavailable-local-directory",
  458. },
  459. ],
  460. 1,
  461. );
  462. });
  463. it("远程 workspace 即使路径同名也不应用本地不可用状态", () => {
  464. const restoreTabs = vi.fn();
  465. restorePersistedRemoteWorkspaceSessions({
  466. settings: {
  467. lastWorkspaceSession: [
  468. {
  469. kind: "remote",
  470. workspacePath: "/workspace/missing",
  471. target: { kind: "docker", container: "demo" },
  472. lastOpenedAt: 1,
  473. lastConnectionStatus: "failed",
  474. },
  475. ],
  476. lastActiveTabIndex: 0,
  477. },
  478. unavailableWorkspacePath: "/workspace/missing",
  479. tabStoreApi: {
  480. getState: () => ({ restoreTabs }),
  481. } as never,
  482. });
  483. expect(restoreTabs.mock.calls[0]?.[0]?.[0]).not.toHaveProperty("availability");
  484. });
  485. it("启动时恢复远程断开态 tab,但不再自动重连", () => {
  486. const restoreTabs = vi.fn();
  487. restorePersistedRemoteWorkspaceSessions({
  488. settings: {
  489. lastWorkspaceSession: [
  490. {
  491. kind: "remote",
  492. workspacePath: "/workspace/demo",
  493. target: {
  494. kind: "docker",
  495. container: "demo",
  496. },
  497. lastOpenedAt: 1,
  498. lastConnectionStatus: "failed",
  499. },
  500. ],
  501. lastActiveTabIndex: 0,
  502. },
  503. tabStoreApi: {
  504. getState: () => ({
  505. restoreTabs,
  506. }),
  507. } as never,
  508. });
  509. const expectedWorkspaceIdentity = buildRemoteWorkspaceIdentity("/workspace/demo", {
  510. kind: "docker",
  511. container: "demo",
  512. });
  513. expect(restoreTabs).toHaveBeenCalledWith(
  514. [
  515. {
  516. workspacePath: "/workspace/demo",
  517. remoteTarget: {
  518. kind: "docker",
  519. container: "demo",
  520. },
  521. workspaceIdentity: expectedWorkspaceIdentity,
  522. },
  523. ],
  524. 0,
  525. );
  526. });
  527. it("启动时恢复激活的远程 workspace,不跳到后面的本地项目", () => {
  528. const restoreTabs = vi.fn();
  529. restorePersistedRemoteWorkspaceSessions({
  530. settings: {
  531. lastWorkspaceSession: [
  532. {
  533. kind: "remote",
  534. workspacePath: "/root",
  535. workspaceIdentity: "remote:ssh:localhost:2222:root:/root",
  536. target: {
  537. kind: "ssh",
  538. host: "localhost",
  539. port: 2222,
  540. username: "root",
  541. },
  542. lastOpenedAt: 1,
  543. lastConnectionStatus: "connected",
  544. },
  545. {
  546. kind: "local",
  547. workspacePath: "/workspace/local",
  548. },
  549. ],
  550. lastActiveTabIndex: 0,
  551. },
  552. tabStoreApi: {
  553. getState: () => ({
  554. restoreTabs,
  555. }),
  556. } as never,
  557. });
  558. expect(restoreTabs).toHaveBeenCalledWith(
  559. [
  560. {
  561. workspacePath: "/root",
  562. remoteTarget: {
  563. kind: "ssh",
  564. host: "localhost",
  565. port: 2222,
  566. username: "root",
  567. },
  568. workspaceIdentity: "remote:ssh:localhost:2222:root:/root",
  569. },
  570. "/workspace/local",
  571. ],
  572. 0,
  573. );
  574. });
  575. it("启动时恢复末尾远程 workspace 的激活位置", () => {
  576. const restoreTabs = vi.fn();
  577. restorePersistedRemoteWorkspaceSessions({
  578. settings: {
  579. lastWorkspaceSession: [
  580. {
  581. kind: "remote",
  582. workspacePath: "/root-a",
  583. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-a",
  584. target: {
  585. kind: "ssh",
  586. host: "localhost",
  587. port: 2222,
  588. username: "root",
  589. },
  590. lastOpenedAt: 1,
  591. lastConnectionStatus: "connected",
  592. },
  593. {
  594. kind: "local",
  595. workspacePath: "/workspace/local",
  596. },
  597. {
  598. kind: "remote",
  599. workspacePath: "/root-b",
  600. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-b",
  601. target: {
  602. kind: "ssh",
  603. host: "localhost",
  604. port: 2222,
  605. username: "root",
  606. },
  607. lastOpenedAt: 2,
  608. lastConnectionStatus: "connected",
  609. },
  610. ],
  611. lastActiveTabIndex: 2,
  612. },
  613. tabStoreApi: {
  614. getState: () => ({
  615. restoreTabs,
  616. }),
  617. } as never,
  618. });
  619. expect(restoreTabs).toHaveBeenCalledWith(
  620. [
  621. {
  622. workspacePath: "/root-a",
  623. remoteTarget: {
  624. kind: "ssh",
  625. host: "localhost",
  626. port: 2222,
  627. username: "root",
  628. },
  629. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-a",
  630. },
  631. "/workspace/local",
  632. {
  633. workspacePath: "/root-b",
  634. remoteTarget: {
  635. kind: "ssh",
  636. host: "localhost",
  637. port: 2222,
  638. username: "root",
  639. },
  640. workspaceIdentity: "remote:ssh:localhost:2222:root:/root-b",
  641. },
  642. ],
  643. 2,
  644. );
  645. });
  646. it("同一远程身份只恢复一次", () => {
  647. const restoreTabs = vi.fn();
  648. const sharedWorkspaceIdentity = buildRemoteWorkspaceIdentity("/workspace/demo", {
  649. kind: "docker",
  650. container: "demo",
  651. });
  652. restorePersistedRemoteWorkspaceSessions({
  653. settings: {
  654. lastWorkspaceSession: [
  655. {
  656. kind: "remote",
  657. workspacePath: "/workspace/demo",
  658. workspaceIdentity: sharedWorkspaceIdentity,
  659. target: {
  660. kind: "docker",
  661. container: "demo",
  662. },
  663. lastOpenedAt: 1,
  664. lastConnectionStatus: "failed",
  665. },
  666. {
  667. kind: "remote",
  668. workspacePath: "/workspace/alias",
  669. workspaceIdentity: sharedWorkspaceIdentity,
  670. target: {
  671. kind: "docker",
  672. container: "demo",
  673. },
  674. lastOpenedAt: 1,
  675. lastConnectionStatus: "failed",
  676. },
  677. ],
  678. lastActiveTabIndex: 0,
  679. },
  680. tabStoreApi: {
  681. getState: () => ({
  682. restoreTabs,
  683. }),
  684. } as never,
  685. });
  686. expect(restoreTabs).toHaveBeenCalledWith(
  687. [
  688. {
  689. workspacePath: "/workspace/demo",
  690. remoteTarget: {
  691. kind: "docker",
  692. container: "demo",
  693. },
  694. workspaceIdentity: sharedWorkspaceIdentity,
  695. },
  696. ],
  697. 0,
  698. );
  699. });
  700. it("远程入口不可见时只恢复本地 workspace", () => {
  701. const restoreTabs = vi.fn();
  702. restorePersistedRemoteWorkspaceSessions({
  703. settings: {
  704. lastWorkspaceSession: [
  705. { kind: "local", workspacePath: "/workspace/local" },
  706. {
  707. kind: "remote",
  708. workspacePath: "/workspace/demo",
  709. target: {
  710. kind: "docker",
  711. container: "demo",
  712. },
  713. lastOpenedAt: 1,
  714. lastConnectionStatus: "failed",
  715. },
  716. ],
  717. lastActiveTabIndex: 1,
  718. },
  719. tabStoreApi: {
  720. getState: () => ({
  721. restoreTabs,
  722. }),
  723. } as never,
  724. allowRemoteWorkspaceRestore: false,
  725. });
  726. expect(restoreTabs).toHaveBeenCalledWith(["/workspace/local"], 0);
  727. });
  728. });
  729. describe("projectRemoteConversationWorkspacePurpose", () => {
  730. it("self-heals only the exact open entry recorded as the Source conversation path", () => {
  731. const sourceServerId = "11111111-1111-4111-8111-111111111111";
  732. const conversationPath = "/home/dev/.zcode/workspace/default";
  733. const conversation = {
  734. kind: "remote" as const,
  735. workspacePath: conversationPath,
  736. workspaceIdentity: `remote:server:${sourceServerId}:${conversationPath}`,
  737. workspacePurpose: "project" as const,
  738. target: { kind: "ssh" as const, host: "localhost", username: "dev" },
  739. lastOpenedAt: 1,
  740. lastConnectionStatus: "failed" as const,
  741. };
  742. const project = {
  743. ...conversation,
  744. workspacePath: "/home/dev",
  745. workspaceIdentity: `remote:server:${sourceServerId}:/home/dev`,
  746. };
  747. expect(
  748. projectRemoteConversationWorkspacePurpose([conversation, project], {
  749. [sourceServerId]: {
  750. workspacePath: conversationPath,
  751. lastResolvedAt: 2,
  752. },
  753. }),
  754. ).toEqual({
  755. remoteSessions: [{ ...conversation, workspacePurpose: "conversation" }, project],
  756. updatedEntries: [
  757. {
  758. workspacePath: conversationPath,
  759. workspaceIdentity: conversation.workspaceIdentity,
  760. },
  761. ],
  762. });
  763. });
  764. });
  765. describe("projectRemoteWorkspaceSessionIdentityAliases", () => {
  766. it("uses the Source conversation alias when project history has already been cleaned", () => {
  767. const sourceServerId = "11111111-1111-4111-8111-111111111111";
  768. const workspacePath = "/home/dev/.zcode/workspace/default";
  769. const target = {
  770. kind: "ssh" as const,
  771. host: "127.0.0.1",
  772. port: 2222,
  773. username: "dev",
  774. };
  775. const legacyIdentity = buildRemoteWorkspaceIdentity(workspacePath, target);
  776. const stableIdentity = `remote:server:${sourceServerId}:${workspacePath}`;
  777. const result = projectRemoteWorkspaceSessionIdentityAliases(
  778. [
  779. {
  780. kind: "remote",
  781. workspacePath,
  782. workspaceIdentity: legacyIdentity,
  783. target,
  784. workspacePurpose: "conversation",
  785. lastOpenedAt: 1,
  786. lastConnectionStatus: "connected",
  787. },
  788. ],
  789. {
  790. sourceServerWorkspaceHistory: [],
  791. connectionProfiles: [
  792. {
  793. profileId: "profile-127",
  794. sourceServerId,
  795. displayName: "127.0.0.1",
  796. transport: "ssh",
  797. target,
  798. createdAt: 1,
  799. lifecycleState: "active",
  800. },
  801. ],
  802. remoteConversationWorkspaceBySourceServerId: {
  803. [sourceServerId]: { workspacePath, lastResolvedAt: 2 },
  804. },
  805. },
  806. );
  807. expect(result.identityRekeys).toEqual([
  808. {
  809. workspacePath,
  810. previousWorkspaceIdentity: legacyIdentity,
  811. workspaceIdentity: stableIdentity,
  812. },
  813. ]);
  814. expect(result.remoteSessions[0]?.workspaceIdentity).toBe(stableIdentity);
  815. });
  816. it("uses every bound Profile route for a canonical project history entry", () => {
  817. const sourceServerId = "22222222-2222-4222-8222-222222222222";
  818. const workspacePath = "/home/dev";
  819. const target = {
  820. kind: "ssh" as const,
  821. host: "127.0.0.1",
  822. port: 2222,
  823. username: "dev",
  824. };
  825. const legacyIdentity = buildRemoteWorkspaceIdentity(workspacePath, target);
  826. const stableIdentity = `remote:server:${sourceServerId}:${workspacePath}`;
  827. const result = projectRemoteWorkspaceSessionIdentityAliases(
  828. [
  829. {
  830. kind: "remote",
  831. workspacePath,
  832. workspaceIdentity: legacyIdentity,
  833. target,
  834. lastOpenedAt: 1,
  835. lastConnectionStatus: "failed",
  836. },
  837. ],
  838. {
  839. sourceServerWorkspaceHistory: [
  840. {
  841. workspacePath,
  842. workspaceIdentity: stableIdentity,
  843. legacyWorkspaceIdentity: "remote:ssh:localhost:2222:dev:/home/dev",
  844. sourceServerId,
  845. profileId: "profile-localhost",
  846. lastOpenedAt: 2,
  847. },
  848. ],
  849. connectionProfiles: [
  850. {
  851. profileId: "profile-127",
  852. sourceServerId,
  853. displayName: "127.0.0.1",
  854. transport: "ssh",
  855. target,
  856. createdAt: 1,
  857. lifecycleState: "active",
  858. },
  859. ],
  860. },
  861. );
  862. expect(result.identityRekeys).toEqual([
  863. {
  864. workspacePath,
  865. previousWorkspaceIdentity: legacyIdentity,
  866. workspaceIdentity: stableIdentity,
  867. },
  868. ]);
  869. expect(result.remoteSessions[0]?.workspaceIdentity).toBe(stableIdentity);
  870. });
  871. });