| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- import { describe, expect, it } from "vitest";
- import type { RemoteTarget } from "@zcode/shared";
- import {
- buildRemoteWorkspaceIdentity,
- buildRemoteWorkspaceSessionMutation,
- buildPersistedWorkspaceSessionEntries,
- buildRemoteWorkspaceSessionEntryMap,
- createRemoteTargetFromSnapshot,
- createRemoteTargetSnapshot,
- findMatchingRemoteWorkspaceSessionEntry,
- formatRemoteWorkspaceDisplayLabel,
- formatRemoteWorkspaceHeaderHostLabel,
- formatRemoteWorkspaceTargetSubtitle,
- hasRemoteWorkspaceIdentity,
- readPersistedWorkspaceSessionEntries,
- removeRemoteWorkspaceSessionEntries,
- upsertRemoteWorkspaceSessionEntries,
- } from "@/lib/remoteWorkspaceHistory.js";
- describe("remoteWorkspaceHistory helpers", () => {
- it("reads combined session snapshots directly", () => {
- expect(
- readPersistedWorkspaceSessionEntries({
- lastWorkspaceSession: [
- {
- kind: "local",
- workspacePath: "/tmp/local-a",
- },
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- ],
- }),
- ).toHaveLength(2);
- });
- it("serializes local and remote tabs into a combined session snapshot", () => {
- const remoteEntryMap = buildRemoteWorkspaceSessionEntryMap([
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- workspaceIdentity: "remote:docker:demo:/workspace/demo",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- ]);
- expect(
- buildPersistedWorkspaceSessionEntries(
- [
- {
- id: "local-1",
- kind: "workspace",
- workspacePath: "/tmp/local-a",
- label: "local-a",
- workspacePurpose: "conversation",
- },
- {
- id: "remote-1",
- kind: "workspace",
- workspacePath: "/workspace/demo",
- label: "demo",
- remoteTarget: {
- kind: "docker",
- container: "demo",
- },
- workspaceIdentity: "remote:docker:demo:/workspace/demo",
- },
- ],
- remoteEntryMap,
- ),
- ).toEqual([
- {
- kind: "local",
- workspacePath: "/tmp/local-a",
- workspacePurpose: "conversation",
- },
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- workspaceIdentity: "remote:docker:demo:/workspace/demo",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- ]);
- });
- it("reuses the existing SSH password credential key when updating a remote session", () => {
- const snapshot = createRemoteTargetSnapshot(
- "remote:ssh:demo.internal:22:root:/workspace/demo",
- {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- password: "secret",
- },
- {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- passwordCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
- },
- );
- expect(snapshot).toMatchObject({
- kind: "ssh",
- passwordCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
- });
- });
- it("hydrates an SSH target from a persisted snapshot", () => {
- const target = createRemoteTargetFromSnapshot(
- {
- kind: "ssh",
- host: "demo.internal",
- port: 2222,
- username: "root",
- privateKeyPath: "~/.ssh/id_ed25519",
- passwordCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
- privateKeyPassphraseCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
- },
- {
- password: "secret",
- privateKeyPassphrase: "key-secret",
- },
- );
- expect(target).toEqual({
- kind: "ssh",
- host: "demo.internal",
- port: 2222,
- username: "root",
- privateKeyPath: "~/.ssh/id_ed25519",
- password: "secret",
- privateKeyPassphrase: "key-secret",
- });
- });
- it("persists SSH asset install mode and drops historical resource packages", () => {
- const snapshot = createRemoteTargetSnapshot(
- "remote:ssh:demo.internal:22:root:/workspace/demo",
- {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- assetInstallMode: "remote-download",
- resourcePackages: {
- selectedPackageIds: ["server-bundle", "node-runtime", "glm"],
- },
- },
- );
- expect(snapshot).toMatchObject({
- kind: "ssh",
- assetInstallMode: "remote-download",
- });
- expect(snapshot).not.toHaveProperty("resourcePackages");
- expect(
- createRemoteTargetFromSnapshot(snapshot, {
- password: null,
- privateKeyPassphrase: null,
- }),
- ).toMatchObject({
- kind: "ssh",
- assetInstallMode: "remote-download",
- });
- expect(
- createRemoteTargetFromSnapshot(snapshot, {
- password: null,
- privateKeyPassphrase: null,
- }),
- ).not.toHaveProperty("resourcePackages");
- });
- it("persists and hydrates SSH config alias metadata", () => {
- const snapshot = createRemoteTargetSnapshot(
- "remote:ssh:localhost:2223:root:/root",
- {
- kind: "ssh",
- host: "localhost",
- port: 2223,
- username: "root",
- password: "secret",
- sshConfigAlias: "linux-arm64",
- },
- );
- expect(snapshot).toMatchObject({
- kind: "ssh",
- sshConfigAlias: "linux-arm64",
- });
- expect(
- createRemoteTargetFromSnapshot(snapshot, {
- password: "secret",
- privateKeyPassphrase: null,
- }),
- ).toMatchObject({
- kind: "ssh",
- sshConfigAlias: "linux-arm64",
- });
- });
- it("stores a separate credential key for SSH private key passphrase", () => {
- const snapshot = createRemoteTargetSnapshot(
- "remote:ssh:demo.internal:22:root:/workspace/demo",
- {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- privateKeyPath: "~/.ssh/id_ed25519",
- privateKeyPassphrase: "key-secret",
- },
- {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- privateKeyPath: "~/.ssh/id_ed25519",
- privateKeyPassphraseCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
- },
- );
- expect(snapshot).toMatchObject({
- kind: "ssh",
- privateKeyPassphraseCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
- });
- });
- it("matches an existing remote session by workspace path and target identity", () => {
- const target: RemoteTarget = {
- kind: "docker",
- container: "demo-container",
- };
- const sessions = [
- {
- kind: "remote" as const,
- workspacePath: "/workspace/demo",
- target: {
- kind: "docker" as const,
- container: "demo-container",
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected" as const,
- },
- ];
- expect(
- findMatchingRemoteWorkspaceSessionEntry(
- sessions,
- "/workspace/demo",
- target,
- )?.workspacePath,
- ).toBe("/workspace/demo");
- });
- it("keeps the newest remote session at the top when upserting", () => {
- const updated = upsertRemoteWorkspaceSessionEntries(
- [
- {
- kind: "remote",
- workspacePath: "/workspace/older",
- target: {
- kind: "wsl",
- distro: "Ubuntu",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "connected",
- },
- ],
- {
- kind: "remote",
- workspacePath: "/workspace/newer",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 2,
- lastConnectionStatus: "failed",
- lastConnectionError: "refused",
- },
- );
- expect(updated.map((entry) => entry.workspacePath)).toEqual([
- "/workspace/newer",
- "/workspace/older",
- ]);
- });
- it("formats a readable remote target subtitle", () => {
- expect(
- formatRemoteWorkspaceTargetSubtitle({
- kind: "wsl",
- distro: "Ubuntu-24.04",
- }),
- ).toBe("WSL · Ubuntu-24.04");
- expect(
- formatRemoteWorkspaceTargetSubtitle({
- kind: "wsl",
- distro: "Ubuntu-24.04",
- user: "root",
- }),
- ).toBe("WSL · Ubuntu-24.04 · root");
- });
- it("uses explicit WSL user in workspace identity and snapshot roundtrip", () => {
- const target: RemoteTarget = {
- kind: "wsl",
- distro: "Ubuntu-24.04",
- user: " root ",
- };
- const snapshot = createRemoteTargetSnapshot(
- "remote:wsl:Ubuntu-24.04:root:/workspace/demo",
- target,
- );
- expect(buildRemoteWorkspaceIdentity("/workspace/demo", target)).toBe(
- "remote:wsl:Ubuntu-24.04:root:/workspace/demo",
- );
- expect(snapshot).toEqual({
- kind: "wsl",
- distro: "Ubuntu-24.04",
- user: "root",
- });
- expect(
- createRemoteTargetFromSnapshot(snapshot, {
- password: null,
- privateKeyPassphrase: null,
- }),
- ).toEqual({
- kind: "wsl",
- distro: "Ubuntu-24.04",
- user: "root",
- });
- expect(formatRemoteWorkspaceHeaderHostLabel(snapshot)).toBe(
- "wsl:Ubuntu-24.04:root",
- );
- });
- it("persists and hydrates server remote target credentials", () => {
- const workspaceKey = "remote:server:studio-server-01:/srv/project";
- const target: RemoteTarget = {
- kind: "server",
- url: "https://studio.example.com/zcode/",
- name: "Studio Server",
- serverId: "Studio Server 01",
- workspacePath: "/srv/project",
- token: "server-secret",
- };
- const snapshot = createRemoteTargetSnapshot(workspaceKey, target);
- expect(buildRemoteWorkspaceIdentity("\\srv\\project\\", target)).toBe(workspaceKey);
- expect(snapshot).toEqual({
- kind: "server",
- url: "https://studio.example.com/zcode/",
- name: "Studio Server",
- serverId: "Studio Server 01",
- workspacePath: "/srv/project",
- tokenCredentialKey:
- "remote-workspace:remote:server:studio-server-01:/srv/project:server-token",
- });
- expect(
- createRemoteTargetFromSnapshot(snapshot, {
- password: null,
- privateKeyPassphrase: null,
- token: "server-secret",
- }),
- ).toEqual(target);
- expect(formatRemoteWorkspaceTargetSubtitle(snapshot)).toBe(
- "Server · Studio Server",
- );
- expect(formatRemoteWorkspaceHeaderHostLabel(snapshot)).toBe(
- "studio.example.com",
- );
- });
- it("keeps the legacy WSL identity when no user is specified", () => {
- expect(
- buildRemoteWorkspaceIdentity("/workspace/demo", {
- kind: "wsl",
- distro: "Ubuntu-24.04",
- }),
- ).toBe("remote:wsl:Ubuntu-24.04:/workspace/demo");
- });
- it("keeps omitted WSL user compatible with legacy history while separating root", () => {
- const sessions = [
- {
- kind: "remote" as const,
- workspacePath: "/workspace/demo",
- workspaceIdentity: "remote:wsl:Ubuntu-24.04:/workspace/demo",
- target: {
- kind: "wsl" as const,
- distro: "Ubuntu-24.04",
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected" as const,
- },
- ];
- expect(
- findMatchingRemoteWorkspaceSessionEntry(sessions, "/workspace/demo", {
- kind: "wsl",
- distro: "Ubuntu-24.04",
- user: " ",
- })?.workspaceIdentity,
- ).toBe("remote:wsl:Ubuntu-24.04:/workspace/demo");
- expect(
- findMatchingRemoteWorkspaceSessionEntry(sessions, "/workspace/demo", {
- kind: "wsl",
- distro: "Ubuntu-24.04",
- user: "root",
- }),
- ).toBeNull();
- });
- it("formats a compact remote host label for workspace header", () => {
- expect(
- formatRemoteWorkspaceHeaderHostLabel({
- kind: "ssh",
- host: "10.0.0.8",
- port: 22,
- username: "dev",
- }),
- ).toBe("10.0.0.8");
- expect(
- formatRemoteWorkspaceHeaderHostLabel({
- kind: "ssh",
- host: "dev.internal",
- port: 2222,
- username: "dev",
- }),
- ).toBe("dev.internal:2222");
- });
- it("formats SSH alias as a workspace display suffix", () => {
- expect(
- formatRemoteWorkspaceDisplayLabel("root", {
- kind: "ssh",
- host: "localhost",
- port: 2223,
- username: "root",
- sshConfigAlias: " linux-arm64 ",
- }),
- ).toBe("root [SSH: linux-arm64]");
- expect(
- formatRemoteWorkspaceDisplayLabel("root", {
- kind: "ssh",
- host: "localhost",
- port: 2223,
- username: "root",
- }),
- ).toBe("root");
- });
- it("treats disconnected remote tabs as remote identities", () => {
- expect(
- hasRemoteWorkspaceIdentity({
- workspaceIdentity: "remote:docker:demo:/workspace/demo",
- }),
- ).toBe(true);
- expect(
- hasRemoteWorkspaceIdentity({
- workspacePath: "/tmp/demo",
- } as never),
- ).toBe(false);
- });
- it("keeps the previous SSH credential key when a reconnect failure is recorded", () => {
- const mutation = buildRemoteWorkspaceSessionMutation({
- remoteSessions: [
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- target: {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- passwordCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- ],
- workspacePath: "/workspace/demo",
- target: {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- },
- lastConnectionStatus: "failed",
- lastConnectionError: "connection refused",
- touchOpenedAt: false,
- });
- expect(mutation.entry.target).toMatchObject({
- kind: "ssh",
- passwordCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
- });
- expect(mutation.credentialKeysToDelete).toEqual([]);
- });
- it("stores the local workspace path used for remote MCP path rewriting", () => {
- const mutation = buildRemoteWorkspaceSessionMutation({
- remoteSessions: [],
- workspacePath: "/srv/project",
- workspaceIdentity: "remote:ssh:dev.example.com:22:alice:/srv/project",
- target: {
- kind: "ssh",
- host: "dev.example.com",
- username: "alice",
- },
- lastConnectionStatus: "connected",
- touchOpenedAt: true,
- localWorkspacePath: "/Users/alice/project",
- } as Parameters<typeof buildRemoteWorkspaceSessionMutation>[0] & {
- localWorkspacePath: string;
- });
- expect(
- (mutation.entry as { localWorkspacePath?: string }).localWorkspacePath,
- ).toBe("/Users/alice/project");
- });
- it("removes remote workspace history entries and returns their SSH credential keys", () => {
- const result = removeRemoteWorkspaceSessionEntries(
- [
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- workspaceIdentity: "remote:ssh:demo.internal:22:root:/workspace/demo",
- target: {
- kind: "ssh",
- host: "demo.internal",
- username: "root",
- passwordCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
- privateKeyPassphraseCredentialKey:
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- {
- kind: "remote",
- workspacePath: "/workspace/other",
- workspaceIdentity: "remote:docker:demo:/workspace/other",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 9,
- lastConnectionStatus: "failed",
- },
- ],
- ["remote:ssh:demo.internal:22:root:/workspace/demo"],
- );
- expect(result.nextRemoteSessions).toEqual([
- {
- kind: "remote",
- workspacePath: "/workspace/other",
- workspaceIdentity: "remote:docker:demo:/workspace/other",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 9,
- lastConnectionStatus: "failed",
- },
- ]);
- expect(result.credentialKeysToDelete).toEqual([
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:password",
- "remote-workspace:remote:ssh:demo.internal:22:root:/workspace/demo:private-key-passphrase",
- ]);
- });
- });
|