remoteWorkspaceHistory.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. import { describe, expect, it } from "vitest";
  2. import type { RemoteTarget } from "@zcode/shared";
  3. import {
  4. buildRemoteWorkspaceIdentity,
  5. buildRemoteWorkspaceSessionMutation,
  6. buildPersistedWorkspaceSessionEntries,
  7. buildRemoteWorkspaceSessionEntryMap,
  8. createRemoteTargetFromSnapshot,
  9. createRemoteTargetSnapshot,
  10. findMatchingRemoteWorkspaceSessionEntry,
  11. formatRemoteWorkspaceDisplayLabel,
  12. formatRemoteWorkspaceHeaderHostLabel,
  13. formatRemoteWorkspaceTargetSubtitle,
  14. hasRemoteWorkspaceIdentity,
  15. readPersistedWorkspaceSessionEntries,
  16. removeRemoteWorkspaceSessionEntries,
  17. upsertRemoteWorkspaceSessionEntries,
  18. } from "@/lib/remoteWorkspaceHistory.js";
  19. describe("remoteWorkspaceHistory helpers", () => {
  20. it("reads combined session snapshots directly", () => {
  21. expect(
  22. readPersistedWorkspaceSessionEntries({
  23. lastWorkspaceSession: [
  24. {
  25. kind: "local",
  26. workspacePath: "/tmp/local-a",
  27. },
  28. {
  29. kind: "remote",
  30. workspacePath: "/workspace/demo",
  31. target: {
  32. kind: "docker",
  33. container: "demo",
  34. },
  35. lastOpenedAt: 1,
  36. lastConnectionStatus: "failed",
  37. },
  38. ],
  39. }),
  40. ).toHaveLength(2);
  41. });
  42. it("serializes local and remote tabs into a combined session snapshot", () => {
  43. const remoteEntryMap = buildRemoteWorkspaceSessionEntryMap([
  44. {
  45. kind: "remote",
  46. workspacePath: "/workspace/demo",
  47. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  48. target: {
  49. kind: "docker",
  50. container: "demo",
  51. },
  52. lastOpenedAt: 1,
  53. lastConnectionStatus: "failed",
  54. },
  55. ]);
  56. expect(
  57. buildPersistedWorkspaceSessionEntries(
  58. [
  59. {
  60. id: "local-1",
  61. kind: "workspace",
  62. workspacePath: "/tmp/local-a",
  63. label: "local-a",
  64. workspacePurpose: "conversation",
  65. },
  66. {
  67. id: "remote-1",
  68. kind: "workspace",
  69. workspacePath: "/workspace/demo",
  70. label: "demo",
  71. remoteTarget: {
  72. kind: "docker",
  73. container: "demo",
  74. },
  75. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  76. },
  77. ],
  78. remoteEntryMap,
  79. ),
  80. ).toEqual([
  81. {
  82. kind: "local",
  83. workspacePath: "/tmp/local-a",
  84. workspacePurpose: "conversation",
  85. },
  86. {
  87. kind: "remote",
  88. workspacePath: "/workspace/demo",
  89. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  90. target: {
  91. kind: "docker",
  92. container: "demo",
  93. },
  94. lastOpenedAt: 1,
  95. lastConnectionStatus: "failed",
  96. },
  97. ]);
  98. });
  99. it("reuses the existing SSH password credential key when updating a remote session", () => {
  100. const snapshot = createRemoteTargetSnapshot(
  101. "remote:ssh:demo.internal:22:root:/workspace/demo",
  102. {
  103. kind: "ssh",
  104. host: "demo.internal",
  105. username: "root",
  106. password: "secret",
  107. },
  108. {
  109. kind: "ssh",
  110. host: "demo.internal",
  111. username: "root",
  112. passwordCredentialKey:
  113. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  114. },
  115. );
  116. expect(snapshot).toMatchObject({
  117. kind: "ssh",
  118. passwordCredentialKey:
  119. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  120. });
  121. });
  122. it("hydrates an SSH target from a persisted snapshot", () => {
  123. const target = createRemoteTargetFromSnapshot(
  124. {
  125. kind: "ssh",
  126. host: "demo.internal",
  127. port: 2222,
  128. username: "root",
  129. privateKeyPath: "~/.ssh/id_ed25519",
  130. passwordCredentialKey:
  131. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  132. privateKeyPassphraseCredentialKey:
  133. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  134. },
  135. {
  136. password: "secret",
  137. privateKeyPassphrase: "key-secret",
  138. },
  139. );
  140. expect(target).toEqual({
  141. kind: "ssh",
  142. host: "demo.internal",
  143. port: 2222,
  144. username: "root",
  145. privateKeyPath: "~/.ssh/id_ed25519",
  146. password: "secret",
  147. privateKeyPassphrase: "key-secret",
  148. });
  149. });
  150. it("persists SSH asset install mode and drops historical resource packages", () => {
  151. const snapshot = createRemoteTargetSnapshot(
  152. "remote:ssh:demo.internal:22:root:/workspace/demo",
  153. {
  154. kind: "ssh",
  155. host: "demo.internal",
  156. username: "root",
  157. assetInstallMode: "remote-download",
  158. resourcePackages: {
  159. selectedPackageIds: ["server-bundle", "node-runtime", "glm"],
  160. },
  161. },
  162. );
  163. expect(snapshot).toMatchObject({
  164. kind: "ssh",
  165. assetInstallMode: "remote-download",
  166. });
  167. expect(snapshot).not.toHaveProperty("resourcePackages");
  168. expect(
  169. createRemoteTargetFromSnapshot(snapshot, {
  170. password: null,
  171. privateKeyPassphrase: null,
  172. }),
  173. ).toMatchObject({
  174. kind: "ssh",
  175. assetInstallMode: "remote-download",
  176. });
  177. expect(
  178. createRemoteTargetFromSnapshot(snapshot, {
  179. password: null,
  180. privateKeyPassphrase: null,
  181. }),
  182. ).not.toHaveProperty("resourcePackages");
  183. });
  184. it("persists and hydrates SSH config alias metadata", () => {
  185. const snapshot = createRemoteTargetSnapshot(
  186. "remote:ssh:localhost:2223:root:/root",
  187. {
  188. kind: "ssh",
  189. host: "localhost",
  190. port: 2223,
  191. username: "root",
  192. password: "secret",
  193. sshConfigAlias: "linux-arm64",
  194. },
  195. );
  196. expect(snapshot).toMatchObject({
  197. kind: "ssh",
  198. sshConfigAlias: "linux-arm64",
  199. });
  200. expect(
  201. createRemoteTargetFromSnapshot(snapshot, {
  202. password: "secret",
  203. privateKeyPassphrase: null,
  204. }),
  205. ).toMatchObject({
  206. kind: "ssh",
  207. sshConfigAlias: "linux-arm64",
  208. });
  209. });
  210. it("stores a separate credential key for SSH private key passphrase", () => {
  211. const snapshot = createRemoteTargetSnapshot(
  212. "remote:ssh:demo.internal:22:root:/workspace/demo",
  213. {
  214. kind: "ssh",
  215. host: "demo.internal",
  216. username: "root",
  217. privateKeyPath: "~/.ssh/id_ed25519",
  218. privateKeyPassphrase: "key-secret",
  219. },
  220. {
  221. kind: "ssh",
  222. host: "demo.internal",
  223. username: "root",
  224. privateKeyPath: "~/.ssh/id_ed25519",
  225. privateKeyPassphraseCredentialKey:
  226. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  227. },
  228. );
  229. expect(snapshot).toMatchObject({
  230. kind: "ssh",
  231. privateKeyPassphraseCredentialKey:
  232. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  233. });
  234. });
  235. it("matches an existing remote session by workspace path and target identity", () => {
  236. const target: RemoteTarget = {
  237. kind: "docker",
  238. container: "demo-container",
  239. };
  240. const sessions = [
  241. {
  242. kind: "remote" as const,
  243. workspacePath: "/workspace/demo",
  244. target: {
  245. kind: "docker" as const,
  246. container: "demo-container",
  247. },
  248. lastOpenedAt: 10,
  249. lastConnectionStatus: "connected" as const,
  250. },
  251. ];
  252. expect(
  253. findMatchingRemoteWorkspaceSessionEntry(
  254. sessions,
  255. "/workspace/demo",
  256. target,
  257. )?.workspacePath,
  258. ).toBe("/workspace/demo");
  259. });
  260. it("keeps the newest remote session at the top when upserting", () => {
  261. const updated = upsertRemoteWorkspaceSessionEntries(
  262. [
  263. {
  264. kind: "remote",
  265. workspacePath: "/workspace/older",
  266. target: {
  267. kind: "wsl",
  268. distro: "Ubuntu",
  269. },
  270. lastOpenedAt: 1,
  271. lastConnectionStatus: "connected",
  272. },
  273. ],
  274. {
  275. kind: "remote",
  276. workspacePath: "/workspace/newer",
  277. target: {
  278. kind: "docker",
  279. container: "demo",
  280. },
  281. lastOpenedAt: 2,
  282. lastConnectionStatus: "failed",
  283. lastConnectionError: "refused",
  284. },
  285. );
  286. expect(updated.map((entry) => entry.workspacePath)).toEqual([
  287. "/workspace/newer",
  288. "/workspace/older",
  289. ]);
  290. });
  291. it("formats a readable remote target subtitle", () => {
  292. expect(
  293. formatRemoteWorkspaceTargetSubtitle({
  294. kind: "wsl",
  295. distro: "Ubuntu-24.04",
  296. }),
  297. ).toBe("WSL · Ubuntu-24.04");
  298. expect(
  299. formatRemoteWorkspaceTargetSubtitle({
  300. kind: "wsl",
  301. distro: "Ubuntu-24.04",
  302. user: "root",
  303. }),
  304. ).toBe("WSL · Ubuntu-24.04 · root");
  305. });
  306. it("uses explicit WSL user in workspace identity and snapshot roundtrip", () => {
  307. const target: RemoteTarget = {
  308. kind: "wsl",
  309. distro: "Ubuntu-24.04",
  310. user: " root ",
  311. };
  312. const snapshot = createRemoteTargetSnapshot(
  313. "remote:wsl:Ubuntu-24.04:root:/workspace/demo",
  314. target,
  315. );
  316. expect(buildRemoteWorkspaceIdentity("/workspace/demo", target)).toBe(
  317. "remote:wsl:Ubuntu-24.04:root:/workspace/demo",
  318. );
  319. expect(snapshot).toEqual({
  320. kind: "wsl",
  321. distro: "Ubuntu-24.04",
  322. user: "root",
  323. });
  324. expect(
  325. createRemoteTargetFromSnapshot(snapshot, {
  326. password: null,
  327. privateKeyPassphrase: null,
  328. }),
  329. ).toEqual({
  330. kind: "wsl",
  331. distro: "Ubuntu-24.04",
  332. user: "root",
  333. });
  334. expect(formatRemoteWorkspaceHeaderHostLabel(snapshot)).toBe(
  335. "wsl:Ubuntu-24.04:root",
  336. );
  337. });
  338. it("persists and hydrates server remote target credentials", () => {
  339. const workspaceKey = "remote:server:studio-server-01:/srv/project";
  340. const target: RemoteTarget = {
  341. kind: "server",
  342. url: "https://studio.example.com/zcode/",
  343. name: "Studio Server",
  344. serverId: "Studio Server 01",
  345. workspacePath: "/srv/project",
  346. token: "server-secret",
  347. };
  348. const snapshot = createRemoteTargetSnapshot(workspaceKey, target);
  349. expect(buildRemoteWorkspaceIdentity("\\srv\\project\\", target)).toBe(workspaceKey);
  350. expect(snapshot).toEqual({
  351. kind: "server",
  352. url: "https://studio.example.com/zcode/",
  353. name: "Studio Server",
  354. serverId: "Studio Server 01",
  355. workspacePath: "/srv/project",
  356. tokenCredentialKey:
  357. "remote-workspace:remote:server:studio-server-01:/srv/project:server-token",
  358. });
  359. expect(
  360. createRemoteTargetFromSnapshot(snapshot, {
  361. password: null,
  362. privateKeyPassphrase: null,
  363. token: "server-secret",
  364. }),
  365. ).toEqual(target);
  366. expect(formatRemoteWorkspaceTargetSubtitle(snapshot)).toBe(
  367. "Server · Studio Server",
  368. );
  369. expect(formatRemoteWorkspaceHeaderHostLabel(snapshot)).toBe(
  370. "studio.example.com",
  371. );
  372. });
  373. it("keeps the legacy WSL identity when no user is specified", () => {
  374. expect(
  375. buildRemoteWorkspaceIdentity("/workspace/demo", {
  376. kind: "wsl",
  377. distro: "Ubuntu-24.04",
  378. }),
  379. ).toBe("remote:wsl:Ubuntu-24.04:/workspace/demo");
  380. });
  381. it("keeps omitted WSL user compatible with legacy history while separating root", () => {
  382. const sessions = [
  383. {
  384. kind: "remote" as const,
  385. workspacePath: "/workspace/demo",
  386. workspaceIdentity: "remote:wsl:Ubuntu-24.04:/workspace/demo",
  387. target: {
  388. kind: "wsl" as const,
  389. distro: "Ubuntu-24.04",
  390. },
  391. lastOpenedAt: 10,
  392. lastConnectionStatus: "connected" as const,
  393. },
  394. ];
  395. expect(
  396. findMatchingRemoteWorkspaceSessionEntry(sessions, "/workspace/demo", {
  397. kind: "wsl",
  398. distro: "Ubuntu-24.04",
  399. user: " ",
  400. })?.workspaceIdentity,
  401. ).toBe("remote:wsl:Ubuntu-24.04:/workspace/demo");
  402. expect(
  403. findMatchingRemoteWorkspaceSessionEntry(sessions, "/workspace/demo", {
  404. kind: "wsl",
  405. distro: "Ubuntu-24.04",
  406. user: "root",
  407. }),
  408. ).toBeNull();
  409. });
  410. it("formats a compact remote host label for workspace header", () => {
  411. expect(
  412. formatRemoteWorkspaceHeaderHostLabel({
  413. kind: "ssh",
  414. host: "10.0.0.8",
  415. port: 22,
  416. username: "dev",
  417. }),
  418. ).toBe("10.0.0.8");
  419. expect(
  420. formatRemoteWorkspaceHeaderHostLabel({
  421. kind: "ssh",
  422. host: "dev.internal",
  423. port: 2222,
  424. username: "dev",
  425. }),
  426. ).toBe("dev.internal:2222");
  427. });
  428. it("formats SSH alias as a workspace display suffix", () => {
  429. expect(
  430. formatRemoteWorkspaceDisplayLabel("root", {
  431. kind: "ssh",
  432. host: "localhost",
  433. port: 2223,
  434. username: "root",
  435. sshConfigAlias: " linux-arm64 ",
  436. }),
  437. ).toBe("root [SSH: linux-arm64]");
  438. expect(
  439. formatRemoteWorkspaceDisplayLabel("root", {
  440. kind: "ssh",
  441. host: "localhost",
  442. port: 2223,
  443. username: "root",
  444. }),
  445. ).toBe("root");
  446. });
  447. it("treats disconnected remote tabs as remote identities", () => {
  448. expect(
  449. hasRemoteWorkspaceIdentity({
  450. workspaceIdentity: "remote:docker:demo:/workspace/demo",
  451. }),
  452. ).toBe(true);
  453. expect(
  454. hasRemoteWorkspaceIdentity({
  455. workspacePath: "/tmp/demo",
  456. } as never),
  457. ).toBe(false);
  458. });
  459. it("keeps the previous SSH credential key when a reconnect failure is recorded", () => {
  460. const mutation = buildRemoteWorkspaceSessionMutation({
  461. remoteSessions: [
  462. {
  463. kind: "remote",
  464. workspacePath: "/workspace/demo",
  465. target: {
  466. kind: "ssh",
  467. host: "demo.internal",
  468. username: "root",
  469. passwordCredentialKey:
  470. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  471. },
  472. lastOpenedAt: 10,
  473. lastConnectionStatus: "connected",
  474. },
  475. ],
  476. workspacePath: "/workspace/demo",
  477. target: {
  478. kind: "ssh",
  479. host: "demo.internal",
  480. username: "root",
  481. },
  482. lastConnectionStatus: "failed",
  483. lastConnectionError: "connection refused",
  484. touchOpenedAt: false,
  485. });
  486. expect(mutation.entry.target).toMatchObject({
  487. kind: "ssh",
  488. passwordCredentialKey:
  489. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  490. });
  491. expect(mutation.credentialKeysToDelete).toEqual([]);
  492. });
  493. it("stores the local workspace path used for remote MCP path rewriting", () => {
  494. const mutation = buildRemoteWorkspaceSessionMutation({
  495. remoteSessions: [],
  496. workspacePath: "/srv/project",
  497. workspaceIdentity: "remote:ssh:dev.example.com:22:alice:/srv/project",
  498. target: {
  499. kind: "ssh",
  500. host: "dev.example.com",
  501. username: "alice",
  502. },
  503. lastConnectionStatus: "connected",
  504. touchOpenedAt: true,
  505. localWorkspacePath: "/Users/alice/project",
  506. } as Parameters<typeof buildRemoteWorkspaceSessionMutation>[0] & {
  507. localWorkspacePath: string;
  508. });
  509. expect(
  510. (mutation.entry as { localWorkspacePath?: string }).localWorkspacePath,
  511. ).toBe("/Users/alice/project");
  512. });
  513. it("removes remote workspace history entries and returns their SSH credential keys", () => {
  514. const result = removeRemoteWorkspaceSessionEntries(
  515. [
  516. {
  517. kind: "remote",
  518. workspacePath: "/workspace/demo",
  519. workspaceIdentity: "remote:ssh:demo.internal:22:root:/workspace/demo",
  520. target: {
  521. kind: "ssh",
  522. host: "demo.internal",
  523. username: "root",
  524. passwordCredentialKey:
  525. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  526. privateKeyPassphraseCredentialKey:
  527. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  528. },
  529. lastOpenedAt: 10,
  530. lastConnectionStatus: "connected",
  531. },
  532. {
  533. kind: "remote",
  534. workspacePath: "/workspace/other",
  535. workspaceIdentity: "remote:docker:demo:/workspace/other",
  536. target: {
  537. kind: "docker",
  538. container: "demo",
  539. },
  540. lastOpenedAt: 9,
  541. lastConnectionStatus: "failed",
  542. },
  543. ],
  544. ["remote:ssh:demo.internal:22:root:/workspace/demo"],
  545. );
  546. expect(result.nextRemoteSessions).toEqual([
  547. {
  548. kind: "remote",
  549. workspacePath: "/workspace/other",
  550. workspaceIdentity: "remote:docker:demo:/workspace/other",
  551. target: {
  552. kind: "docker",
  553. container: "demo",
  554. },
  555. lastOpenedAt: 9,
  556. lastConnectionStatus: "failed",
  557. },
  558. ]);
  559. expect(result.credentialKeysToDelete).toEqual([
  560. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
  561. "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
  562. ]);
  563. });
  564. });