remoteWorkspaceHistory.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. import { describe, expect, it } from "vitest";
  2. import type { RemoteTarget, RemoteWorkspaceSessionEntry } from "@zcode/shared";
  3. import {
  4. buildRemoteWorkspaceIdentity,
  5. buildRemoteWorkspaceSessionMutation,
  6. buildPersistedWorkspaceSessionEntries,
  7. buildRemoteWorkspaceSessionEntryMap,
  8. createRemoteTargetFromSnapshot,
  9. createRemoteTargetSnapshot,
  10. findMatchingRemoteWorkspaceSessionEntry,
  11. formatRemoteWorkspaceHeaderHostLabel,
  12. formatRemoteWorkspaceTargetSubtitle,
  13. hasRemoteWorkspaceIdentity,
  14. readPersistedWorkspaceSessionEntries,
  15. removeRemoteWorkspaceSessionEntries,
  16. upsertRemoteWorkspaceSessionEntries,
  17. } from "@/lib/remoteWorkspaceHistory.js";
  18. describe("remoteWorkspaceHistory helpers", () => {
  19. it("reads combined session snapshots directly", () => {
  20. expect(
  21. readPersistedWorkspaceSessionEntries({
  22. lastWorkspaceSession: [
  23. {
  24. kind: "local",
  25. workspacePath: "/tmp/local-a",
  26. },
  27. {
  28. kind: "remote",
  29. workspacePath: "/workspace/demo",
  30. target: {
  31. kind: "docker",
  32. container: "demo",
  33. },
  34. lastOpenedAt: 1,
  35. lastConnectionStatus: "failed",
  36. },
  37. ],
  38. }),
  39. ).toHaveLength(2);
  40. });
  41. it("restores a rollback conversation identity from its bound Source profile", () => {
  42. const workspacePath = "/home/dev/.zcode/workspace/default";
  43. const legacyIdentity = `remote:ssh:localhost:2222:dev:${workspacePath}`;
  44. const stableIdentity = `remote:server:source-1:${workspacePath}`;
  45. expect(
  46. readPersistedWorkspaceSessionEntries({
  47. lastWorkspaceSession: [
  48. {
  49. kind: "remote",
  50. workspacePath,
  51. workspaceIdentity: legacyIdentity,
  52. workspacePurpose: "conversation",
  53. target: {
  54. kind: "ssh",
  55. host: "localhost",
  56. port: 2222,
  57. username: "dev",
  58. },
  59. lastOpenedAt: 1,
  60. lastConnectionStatus: "connected",
  61. },
  62. ],
  63. sourceServerWorkspaceHistory: [],
  64. connectionProfiles: [
  65. {
  66. profileId: "profile-1",
  67. sourceServerId: "source-1",
  68. transport: "ssh",
  69. target: {
  70. kind: "ssh",
  71. host: "localhost",
  72. port: 2222,
  73. username: "dev",
  74. },
  75. createdAt: 1,
  76. },
  77. ],
  78. remoteConversationWorkspaceBySourceServerId: {
  79. "source-1": { workspacePath, lastResolvedAt: 1 },
  80. },
  81. }),
  82. ).toEqual([
  83. expect.objectContaining({
  84. workspaceIdentity: stableIdentity,
  85. workspacePurpose: "conversation",
  86. }),
  87. ]);
  88. });
  89. it("keeps an ambiguous rollback conversation identity fail-closed", () => {
  90. const workspacePath = "/home/dev/.zcode/workspace/default";
  91. const legacyIdentity = `remote:ssh:localhost:2222:dev:${workspacePath}`;
  92. const target = {
  93. kind: "ssh" as const,
  94. host: "localhost",
  95. port: 2222,
  96. username: "dev",
  97. };
  98. expect(
  99. readPersistedWorkspaceSessionEntries({
  100. lastWorkspaceSession: [
  101. {
  102. kind: "remote",
  103. workspacePath,
  104. workspaceIdentity: legacyIdentity,
  105. workspacePurpose: "conversation",
  106. target,
  107. lastOpenedAt: 1,
  108. lastConnectionStatus: "connected",
  109. },
  110. ],
  111. connectionProfiles: [
  112. {
  113. profileId: "profile-1",
  114. sourceServerId: "source-1",
  115. transport: "ssh",
  116. target,
  117. createdAt: 1,
  118. },
  119. {
  120. profileId: "profile-2",
  121. sourceServerId: "source-2",
  122. transport: "ssh",
  123. target,
  124. createdAt: 2,
  125. },
  126. ],
  127. remoteConversationWorkspaceBySourceServerId: {
  128. "source-1": { workspacePath, lastResolvedAt: 1 },
  129. "source-2": { workspacePath, lastResolvedAt: 2 },
  130. },
  131. }),
  132. ).toEqual([expect.objectContaining({ workspaceIdentity: legacyIdentity })]);
  133. });
  134. it("serializes local and remote tabs into a combined session snapshot", () => {
  135. const remoteEntryMap = buildRemoteWorkspaceSessionEntryMap([
  136. {
  137. kind: "remote",
  138. workspacePath: "/workspace/demo",
  139. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  140. target: {
  141. kind: "docker",
  142. container: "demo",
  143. },
  144. lastOpenedAt: 1,
  145. lastConnectionStatus: "failed",
  146. },
  147. ]);
  148. expect(
  149. buildPersistedWorkspaceSessionEntries(
  150. [
  151. {
  152. id: "local-1",
  153. kind: "workspace",
  154. workspacePath: "/tmp/local-a",
  155. label: "local-a",
  156. workspacePurpose: "conversation",
  157. },
  158. {
  159. id: "remote-1",
  160. kind: "workspace",
  161. workspacePath: "/workspace/demo",
  162. label: "demo",
  163. remoteTarget: {
  164. kind: "docker",
  165. container: "demo",
  166. },
  167. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  168. },
  169. ],
  170. remoteEntryMap,
  171. ),
  172. ).toEqual([
  173. {
  174. kind: "local",
  175. workspacePath: "/tmp/local-a",
  176. workspacePurpose: "conversation",
  177. },
  178. {
  179. kind: "remote",
  180. workspacePath: "/workspace/demo",
  181. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  182. target: {
  183. kind: "docker",
  184. container: "demo",
  185. },
  186. lastOpenedAt: 1,
  187. lastConnectionStatus: "failed",
  188. },
  189. ]);
  190. });
  191. it("reuses the existing SSH password credential key when updating a remote session", () => {
  192. const snapshot = createRemoteTargetSnapshot(
  193. "remote:ssh:demo.internal:22:root:/workspace/demo",
  194. {
  195. kind: "ssh",
  196. host: "demo.internal",
  197. username: "root",
  198. password: "secret",
  199. },
  200. {
  201. kind: "ssh",
  202. host: "demo.internal",
  203. username: "root",
  204. passwordCredentialKey:
  205. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  206. },
  207. );
  208. expect(snapshot).toMatchObject({
  209. kind: "ssh",
  210. passwordCredentialKey:
  211. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  212. });
  213. });
  214. it("hydrates an SSH target from a persisted snapshot", () => {
  215. const target = createRemoteTargetFromSnapshot(
  216. {
  217. kind: "ssh",
  218. host: "demo.internal",
  219. port: 2222,
  220. username: "root",
  221. privateKeyPath: "~/.ssh/id_ed25519",
  222. passwordCredentialKey:
  223. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  224. privateKeyPassphraseCredentialKey:
  225. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  226. },
  227. {
  228. password: "secret",
  229. privateKeyPassphrase: "key-secret",
  230. },
  231. );
  232. expect(target).toEqual({
  233. kind: "ssh",
  234. host: "demo.internal",
  235. port: 2222,
  236. username: "root",
  237. privateKeyPath: "~/.ssh/id_ed25519",
  238. password: "secret",
  239. privateKeyPassphrase: "key-secret",
  240. });
  241. });
  242. it("persists SSH asset install mode and drops historical resource packages", () => {
  243. const snapshot = createRemoteTargetSnapshot(
  244. "remote:ssh:demo.internal:22:root:/workspace/demo",
  245. {
  246. kind: "ssh",
  247. host: "demo.internal",
  248. username: "root",
  249. assetInstallMode: "remote-download",
  250. resourcePackages: {
  251. selectedPackageIds: ["server-bundle", "node-runtime", "glm"],
  252. },
  253. },
  254. );
  255. expect(snapshot).toMatchObject({
  256. kind: "ssh",
  257. assetInstallMode: "remote-download",
  258. });
  259. expect(snapshot).not.toHaveProperty("resourcePackages");
  260. expect(
  261. createRemoteTargetFromSnapshot(snapshot, {
  262. password: null,
  263. privateKeyPassphrase: null,
  264. }),
  265. ).toMatchObject({
  266. kind: "ssh",
  267. assetInstallMode: "remote-download",
  268. });
  269. expect(
  270. createRemoteTargetFromSnapshot(snapshot, {
  271. password: null,
  272. privateKeyPassphrase: null,
  273. }),
  274. ).not.toHaveProperty("resourcePackages");
  275. });
  276. it("persists and hydrates SSH config alias metadata", () => {
  277. const snapshot = createRemoteTargetSnapshot("remote:ssh:localhost:2223:root:/root", {
  278. kind: "ssh",
  279. host: "localhost",
  280. port: 2223,
  281. username: "root",
  282. password: "secret",
  283. sshConfigAlias: "linux-arm64",
  284. });
  285. expect(snapshot).toMatchObject({
  286. kind: "ssh",
  287. sshConfigAlias: "linux-arm64",
  288. });
  289. expect(
  290. createRemoteTargetFromSnapshot(snapshot, {
  291. password: "secret",
  292. privateKeyPassphrase: null,
  293. }),
  294. ).toMatchObject({
  295. kind: "ssh",
  296. sshConfigAlias: "linux-arm64",
  297. });
  298. });
  299. it("stores a separate credential key for SSH private key passphrase", () => {
  300. const snapshot = createRemoteTargetSnapshot(
  301. "remote:ssh:demo.internal:22:root:/workspace/demo",
  302. {
  303. kind: "ssh",
  304. host: "demo.internal",
  305. username: "root",
  306. privateKeyPath: "~/.ssh/id_ed25519",
  307. privateKeyPassphrase: "key-secret",
  308. },
  309. {
  310. kind: "ssh",
  311. host: "demo.internal",
  312. username: "root",
  313. privateKeyPath: "~/.ssh/id_ed25519",
  314. privateKeyPassphraseCredentialKey:
  315. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  316. },
  317. );
  318. expect(snapshot).toMatchObject({
  319. kind: "ssh",
  320. privateKeyPassphraseCredentialKey:
  321. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  322. });
  323. });
  324. it("matches an existing remote session by workspace path and target identity", () => {
  325. const target: RemoteTarget = {
  326. kind: "docker",
  327. container: "demo-container",
  328. };
  329. const sessions = [
  330. {
  331. kind: "remote" as const,
  332. workspacePath: "/workspace/demo",
  333. target: {
  334. kind: "docker" as const,
  335. container: "demo-container",
  336. },
  337. lastOpenedAt: 10,
  338. lastConnectionStatus: "connected" as const,
  339. },
  340. ];
  341. expect(
  342. findMatchingRemoteWorkspaceSessionEntry(sessions, "/workspace/demo", target)?.workspacePath,
  343. ).toBe("/workspace/demo");
  344. });
  345. it("keeps the newest remote session at the top when upserting", () => {
  346. const updated = upsertRemoteWorkspaceSessionEntries(
  347. [
  348. {
  349. kind: "remote",
  350. workspacePath: "/workspace/older",
  351. target: {
  352. kind: "wsl",
  353. distro: "Ubuntu",
  354. },
  355. lastOpenedAt: 1,
  356. lastConnectionStatus: "connected",
  357. },
  358. ],
  359. {
  360. kind: "remote",
  361. workspacePath: "/workspace/newer",
  362. target: {
  363. kind: "docker",
  364. container: "demo",
  365. },
  366. lastOpenedAt: 2,
  367. lastConnectionStatus: "failed",
  368. lastConnectionError: "refused",
  369. },
  370. );
  371. expect(updated.map((entry) => entry.workspacePath)).toEqual([
  372. "/workspace/newer",
  373. "/workspace/older",
  374. ]);
  375. });
  376. it("formats a readable remote target subtitle", () => {
  377. expect(
  378. formatRemoteWorkspaceTargetSubtitle({
  379. kind: "wsl",
  380. distro: "Ubuntu-24.04",
  381. }),
  382. ).toBe("WSL · Ubuntu-24.04");
  383. expect(
  384. formatRemoteWorkspaceTargetSubtitle({
  385. kind: "wsl",
  386. distro: "Ubuntu-24.04",
  387. user: "root",
  388. }),
  389. ).toBe("WSL · Ubuntu-24.04 · root");
  390. });
  391. it("uses explicit WSL user in workspace identity and snapshot roundtrip", () => {
  392. const target: RemoteTarget = {
  393. kind: "wsl",
  394. distro: "Ubuntu-24.04",
  395. user: " root ",
  396. };
  397. const snapshot = createRemoteTargetSnapshot(
  398. "remote:wsl:Ubuntu-24.04:root:/workspace/demo",
  399. target,
  400. );
  401. expect(buildRemoteWorkspaceIdentity("/workspace/demo", target)).toBe(
  402. "remote:wsl:Ubuntu-24.04:root:/workspace/demo",
  403. );
  404. expect(snapshot).toEqual({
  405. kind: "wsl",
  406. distro: "Ubuntu-24.04",
  407. user: "root",
  408. });
  409. expect(
  410. createRemoteTargetFromSnapshot(snapshot, {
  411. password: null,
  412. privateKeyPassphrase: null,
  413. }),
  414. ).toEqual({
  415. kind: "wsl",
  416. distro: "Ubuntu-24.04",
  417. user: "root",
  418. });
  419. expect(formatRemoteWorkspaceHeaderHostLabel(snapshot)).toBe("wsl:Ubuntu-24.04:root");
  420. });
  421. it("persists and hydrates server remote target credentials", () => {
  422. const workspaceKey = "remote:server:studio-server-01:/srv/project";
  423. const target: RemoteTarget = {
  424. kind: "server",
  425. url: "https://studio.example.com/zcode/",
  426. name: "Studio Server",
  427. serverId: "Studio Server 01",
  428. workspacePath: "/srv/project",
  429. token: "server-secret",
  430. };
  431. const snapshot = createRemoteTargetSnapshot(workspaceKey, target);
  432. expect(buildRemoteWorkspaceIdentity("\\srv\\project\\", target)).toBe(workspaceKey);
  433. expect(snapshot).toEqual({
  434. kind: "server",
  435. url: "https://studio.example.com/zcode/",
  436. name: "Studio Server",
  437. serverId: "Studio Server 01",
  438. workspacePath: "/srv/project",
  439. tokenCredentialKey:
  440. "remote-workspace:remote:server:studio-server-01:/srv/project:server-token",
  441. });
  442. expect(
  443. createRemoteTargetFromSnapshot(snapshot, {
  444. password: null,
  445. privateKeyPassphrase: null,
  446. token: "server-secret",
  447. }),
  448. ).toEqual(target);
  449. expect(formatRemoteWorkspaceTargetSubtitle(snapshot)).toBe("Server · Studio Server");
  450. expect(formatRemoteWorkspaceHeaderHostLabel(snapshot)).toBe("studio.example.com");
  451. });
  452. it("keeps the legacy WSL identity when no user is specified", () => {
  453. expect(
  454. buildRemoteWorkspaceIdentity("/workspace/demo", {
  455. kind: "wsl",
  456. distro: "Ubuntu-24.04",
  457. }),
  458. ).toBe("remote:wsl:Ubuntu-24.04:/workspace/demo");
  459. });
  460. it("keeps omitted WSL user compatible with legacy history while separating root", () => {
  461. const sessions = [
  462. {
  463. kind: "remote" as const,
  464. workspacePath: "/workspace/demo",
  465. workspaceIdentity: "remote:wsl:Ubuntu-24.04:/workspace/demo",
  466. target: {
  467. kind: "wsl" as const,
  468. distro: "Ubuntu-24.04",
  469. },
  470. lastOpenedAt: 10,
  471. lastConnectionStatus: "connected" as const,
  472. },
  473. ];
  474. expect(
  475. findMatchingRemoteWorkspaceSessionEntry(sessions, "/workspace/demo", {
  476. kind: "wsl",
  477. distro: "Ubuntu-24.04",
  478. user: " ",
  479. })?.workspaceIdentity,
  480. ).toBe("remote:wsl:Ubuntu-24.04:/workspace/demo");
  481. expect(
  482. findMatchingRemoteWorkspaceSessionEntry(sessions, "/workspace/demo", {
  483. kind: "wsl",
  484. distro: "Ubuntu-24.04",
  485. user: "root",
  486. }),
  487. ).toBeNull();
  488. });
  489. it("formats a compact remote host label for workspace header", () => {
  490. expect(
  491. formatRemoteWorkspaceHeaderHostLabel({
  492. kind: "ssh",
  493. host: "10.0.0.8",
  494. port: 22,
  495. username: "dev",
  496. }),
  497. ).toBe("10.0.0.8");
  498. expect(
  499. formatRemoteWorkspaceHeaderHostLabel({
  500. kind: "ssh",
  501. host: "dev.internal",
  502. port: 2222,
  503. username: "dev",
  504. }),
  505. ).toBe("dev.internal:2222");
  506. });
  507. it("treats disconnected remote tabs as remote identities", () => {
  508. expect(
  509. hasRemoteWorkspaceIdentity({
  510. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  511. }),
  512. ).toBe(true);
  513. expect(
  514. hasRemoteWorkspaceIdentity({
  515. workspacePath: "/tmp/demo",
  516. } as never),
  517. ).toBe(false);
  518. });
  519. it("keeps the previous SSH credential key when a reconnect failure is recorded", () => {
  520. const mutation = buildRemoteWorkspaceSessionMutation({
  521. remoteSessions: [
  522. {
  523. kind: "remote",
  524. workspacePath: "/workspace/demo",
  525. target: {
  526. kind: "ssh",
  527. host: "demo.internal",
  528. username: "root",
  529. passwordCredentialKey:
  530. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  531. },
  532. lastOpenedAt: 10,
  533. lastConnectionStatus: "connected",
  534. },
  535. ],
  536. workspacePath: "/workspace/demo",
  537. target: {
  538. kind: "ssh",
  539. host: "demo.internal",
  540. username: "root",
  541. },
  542. lastConnectionStatus: "failed",
  543. lastConnectionError: "connection refused",
  544. touchOpenedAt: false,
  545. });
  546. expect(mutation.entry.target).toMatchObject({
  547. kind: "ssh",
  548. passwordCredentialKey:
  549. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  550. });
  551. expect(mutation.credentialKeysToDelete).toEqual([]);
  552. });
  553. it("does not create workspace-scoped SSH credentials for a Source Server identity", () => {
  554. const mutation = buildRemoteWorkspaceSessionMutation({
  555. remoteSessions: [],
  556. workspacePath: "/srv/project",
  557. workspaceIdentity: "remote:server:3cd4f9da-cd79-426c-9d70-75f36e9f6a87:/srv/project",
  558. target: {
  559. kind: "ssh",
  560. host: "dev.example.com",
  561. username: "alice",
  562. password: "profile-owned-password",
  563. },
  564. lastConnectionStatus: "connected",
  565. touchOpenedAt: true,
  566. });
  567. expect(mutation.entry.target).toEqual({
  568. kind: "ssh",
  569. host: "dev.example.com",
  570. port: undefined,
  571. username: "alice",
  572. assetInstallMode: undefined,
  573. privateKeyPath: undefined,
  574. passwordCredentialKey: undefined,
  575. privateKeyPassphraseCredentialKey: undefined,
  576. });
  577. expect(mutation.credentialsToSave).toEqual([]);
  578. });
  579. it("rekeys a legacy route entry to the Host stable identity without duplicating history or deleting rollback credentials", () => {
  580. const legacyIdentity = "remote:ssh:dev.example.com:22:alice:/srv/project";
  581. const stableIdentity = "remote:server:3cd4f9da-cd79-426c-9d70-75f36e9f6a87:/srv/project";
  582. const mutation = buildRemoteWorkspaceSessionMutation({
  583. remoteSessions: [
  584. {
  585. kind: "remote",
  586. workspacePath: "/srv/project",
  587. localWorkspacePath: "/Users/alice/project",
  588. workspaceIdentity: legacyIdentity,
  589. target: {
  590. kind: "ssh",
  591. host: "dev.example.com",
  592. port: 22,
  593. username: "alice",
  594. passwordCredentialKey: `remote-workspace:${legacyIdentity}:password`,
  595. },
  596. lastOpenedAt: 123,
  597. lastConnectionStatus: "connected",
  598. },
  599. ],
  600. workspacePath: "/srv/project",
  601. workspaceIdentity: stableIdentity,
  602. target: {
  603. kind: "ssh",
  604. host: "dev.example.com",
  605. port: 22,
  606. username: "alice",
  607. },
  608. lastConnectionStatus: "failed",
  609. lastConnectionError: "offline",
  610. touchOpenedAt: false,
  611. });
  612. expect(mutation.nextRemoteSessions).toHaveLength(1);
  613. expect(mutation.entry).toMatchObject({
  614. workspaceIdentity: stableIdentity,
  615. localWorkspacePath: "/Users/alice/project",
  616. lastOpenedAt: 123,
  617. });
  618. expect(mutation.credentialKeysToDelete).toEqual([]);
  619. });
  620. describe("credential ownership during history cleanup", () => {
  621. const profileEntry: RemoteWorkspaceSessionEntry = {
  622. kind: "remote",
  623. workspacePath: "/srv/project",
  624. workspaceIdentity: "remote:ssh:dev.example.com:22:dev:/srv/project",
  625. target: {
  626. kind: "ssh",
  627. host: "dev.example.com",
  628. username: "dev",
  629. passwordCredentialKey: "connection-profile:profile-1:password",
  630. privateKeyPassphraseCredentialKey: "connection-profile:profile-1:private-key-passphrase",
  631. },
  632. lastOpenedAt: 1,
  633. lastConnectionStatus: "connected",
  634. };
  635. it.each(["connected", "failed"] as const)(
  636. "preserves Profile secrets when rekeying a %s workspace",
  637. (lastConnectionStatus) => {
  638. const mutation = buildRemoteWorkspaceSessionMutation({
  639. remoteSessions: [profileEntry],
  640. workspacePath: profileEntry.workspacePath,
  641. workspaceIdentity: "remote:server:source-1:/srv/project",
  642. target: { kind: "ssh", host: "dev.example.com", username: "dev" },
  643. lastConnectionStatus,
  644. touchOpenedAt: false,
  645. });
  646. expect(mutation.nextRemoteSessions).toHaveLength(1);
  647. expect(mutation.entry.workspaceIdentity).toBe("remote:server:source-1:/srv/project");
  648. expect(mutation.credentialKeysToDelete).toEqual([]);
  649. expect(mutation.credentialsToSave).toEqual([]);
  650. if (lastConnectionStatus === "connected") {
  651. expect(mutation.entry.target).not.toHaveProperty(
  652. "passwordCredentialKey",
  653. "connection-profile:profile-1:password",
  654. );
  655. expect(mutation.entry.target).not.toHaveProperty(
  656. "privateKeyPassphraseCredentialKey",
  657. "connection-profile:profile-1:private-key-passphrase",
  658. );
  659. }
  660. },
  661. );
  662. it("does not delete Profile secrets when their history entry is evicted", () => {
  663. const mutation = buildRemoteWorkspaceSessionMutation({
  664. remoteSessions: [
  665. profileEntry,
  666. ...Array.from(
  667. { length: 19 },
  668. (_, index): RemoteWorkspaceSessionEntry => ({
  669. kind: "remote",
  670. workspacePath: `/srv/other-${index}`,
  671. target: { kind: "docker", container: "fixture" },
  672. lastOpenedAt: index + 2,
  673. lastConnectionStatus: "connected",
  674. }),
  675. ),
  676. ],
  677. workspacePath: "/srv/new",
  678. target: { kind: "docker", container: "fixture" },
  679. lastConnectionStatus: "connected",
  680. touchOpenedAt: true,
  681. });
  682. expect(mutation.nextRemoteSessions).toHaveLength(20);
  683. expect(mutation.nextRemoteSessions).not.toContain(profileEntry);
  684. expect(mutation.credentialKeysToDelete).toEqual([]);
  685. });
  686. it("removes history without deleting secrets owned by its connection Profile", () => {
  687. const result = removeRemoteWorkspaceSessionEntries(
  688. [profileEntry],
  689. [profileEntry.workspaceIdentity!],
  690. );
  691. expect(result.nextRemoteSessions).toEqual([]);
  692. expect(result.credentialKeysToDelete).toEqual([]);
  693. });
  694. });
  695. it("stores the local workspace path used for remote MCP path rewriting", () => {
  696. const mutation = buildRemoteWorkspaceSessionMutation({
  697. remoteSessions: [],
  698. workspacePath: "/srv/project",
  699. workspaceIdentity: "remote:ssh:dev.example.com:22:alice:/srv/project",
  700. target: {
  701. kind: "ssh",
  702. host: "dev.example.com",
  703. username: "alice",
  704. },
  705. lastConnectionStatus: "connected",
  706. touchOpenedAt: true,
  707. localWorkspacePath: "/Users/alice/project",
  708. } as Parameters<typeof buildRemoteWorkspaceSessionMutation>[0] & {
  709. localWorkspacePath: string;
  710. });
  711. expect((mutation.entry as { localWorkspacePath?: string }).localWorkspacePath).toBe(
  712. "/Users/alice/project",
  713. );
  714. });
  715. it("removes remote workspace history entries and returns their SSH credential keys", () => {
  716. const result = removeRemoteWorkspaceSessionEntries(
  717. [
  718. {
  719. kind: "remote",
  720. workspacePath: "/workspace/demo",
  721. workspaceIdentity: "remote:ssh:demo.internal:22:root:/workspace/demo",
  722. target: {
  723. kind: "ssh",
  724. host: "demo.internal",
  725. username: "root",
  726. passwordCredentialKey:
  727. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  728. privateKeyPassphraseCredentialKey:
  729. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  730. },
  731. lastOpenedAt: 10,
  732. lastConnectionStatus: "connected",
  733. },
  734. {
  735. kind: "remote",
  736. workspacePath: "/workspace/other",
  737. workspaceIdentity: "remote:docker:demo:/workspace/other",
  738. target: {
  739. kind: "docker",
  740. container: "demo",
  741. },
  742. lastOpenedAt: 9,
  743. lastConnectionStatus: "failed",
  744. },
  745. ],
  746. ["remote:ssh:demo.internal:22:root:/workspace/demo"],
  747. );
  748. expect(result.nextRemoteSessions).toEqual([
  749. {
  750. kind: "remote",
  751. workspacePath: "/workspace/other",
  752. workspaceIdentity: "remote:docker:demo:/workspace/other",
  753. target: {
  754. kind: "docker",
  755. container: "demo",
  756. },
  757. lastOpenedAt: 9,
  758. lastConnectionStatus: "failed",
  759. },
  760. ]);
  761. expect(result.credentialKeysToDelete).toEqual([
  762. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  763. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  764. ]);
  765. });
  766. });