| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- import { describe, expect, it } from "vitest";
- import type { SourceServerManagementSnapshot } from "@zcode/shared";
- import { buildRemoteWorkspaceProjection } from "@/remote/remoteWorkspaceProjection.js";
- function createSnapshot(): SourceServerManagementSnapshot {
- return {
- generation: 3,
- servers: [
- {
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- displayName: "Dev Linux",
- osUser: "dev",
- installState: "installed",
- declaredWorkspaces: [],
- },
- ],
- profiles: [
- {
- profileId: "22222222-2222-4222-8222-222222222222",
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- transport: "ssh",
- target: { kind: "ssh", host: "linux-amd64-dev", username: "dev" },
- createdAt: 1,
- lifecycleState: "active",
- },
- ],
- states: [
- {
- profileId: "22222222-2222-4222-8222-222222222222",
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- state: "online",
- updatedAt: 3,
- },
- ],
- workspaceHistory: [
- {
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- profileId: "22222222-2222-4222-8222-222222222222",
- workspacePath: "/workspace/project",
- workspaceIdentity: "remote:server:11111111-1111-4111-8111-111111111111:/workspace/project",
- lastOpenedAt: 2,
- },
- ],
- failedPathCount: 0,
- abandonedPathCount: 0,
- migrationCounts: [],
- };
- }
- describe("RemoteWorkspaceProjection", () => {
- it("joins device status, project history and open attachments without treating online as ready", () => {
- const projection = buildRemoteWorkspaceProjection({
- managementSnapshot: createSnapshot(),
- tabs: [
- {
- id: "conversation",
- kind: "workspace",
- workspacePath: "/workspace/conversation",
- workspaceIdentity:
- "remote:server:11111111-1111-4111-8111-111111111111:/workspace/conversation",
- workspacePurpose: "conversation",
- label: "conversation",
- remoteSessionId: "session-conversation",
- },
- {
- id: "project",
- kind: "workspace",
- workspacePath: "/workspace/project",
- workspaceIdentity:
- "remote:server:11111111-1111-4111-8111-111111111111:/workspace/project",
- workspacePurpose: "project",
- label: "project",
- },
- ],
- attachments: [
- {
- remoteSessionId: "session-conversation",
- workspaceKey:
- "remote:server:11111111-1111-4111-8111-111111111111:/workspace/conversation",
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- state: "ready",
- },
- ],
- });
- expect(projection.devices).toEqual([
- expect.objectContaining({
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- aggregatedStatus: "online",
- }),
- ]);
- expect(projection.openWorkspaces).toEqual([
- expect.objectContaining({
- workspacePath: "/workspace/conversation",
- purpose: "conversation",
- attachmentState: "ready",
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- }),
- expect.objectContaining({
- workspacePath: "/workspace/project",
- purpose: "project",
- attachmentState: "disconnected",
- }),
- ]);
- expect(projection.projectHistory).toEqual([
- expect.objectContaining({ workspacePath: "/workspace/project" }),
- ]);
- });
- it("keeps the same path with different identities isolated", () => {
- const snapshot = createSnapshot();
- snapshot.workspaceHistory = [
- ...snapshot.workspaceHistory,
- {
- sourceServerId: "11111111-1111-4111-8111-111111111111",
- profileId: "22222222-2222-4222-8222-222222222222",
- workspacePath: "/workspace/shared",
- workspaceIdentity: "remote:server:other:/workspace/shared",
- lastOpenedAt: 4,
- },
- ];
- const projection = buildRemoteWorkspaceProjection({
- managementSnapshot: snapshot,
- tabs: [
- {
- id: "remote-a",
- kind: "workspace",
- workspacePath: "/workspace/shared",
- workspaceIdentity: "remote:server:source-a:/workspace/shared",
- workspacePurpose: "project",
- label: "shared-a",
- },
- {
- id: "remote-b",
- kind: "workspace",
- workspacePath: "/workspace/shared",
- workspaceIdentity: "remote:server:source-b:/workspace/shared",
- workspacePurpose: "conversation",
- label: "shared-b",
- },
- ],
- attachments: [],
- });
- expect(projection.openWorkspaces).toHaveLength(2);
- expect(new Set(projection.openWorkspaces.map((item) => item.workspaceKey))).toEqual(
- new Set([
- "remote:server:source-a:/workspace/shared",
- "remote:server:source-b:/workspace/shared",
- ]),
- );
- expect(projection.projectHistory).toHaveLength(2);
- });
- it("keeps local tabs in execution contexts without projecting them as remote workspaces", () => {
- const projection = buildRemoteWorkspaceProjection({
- tabs: [
- {
- id: "local",
- kind: "workspace",
- workspacePath: "/local/project",
- workspacePurpose: "project",
- label: "project",
- },
- {
- id: "remote",
- kind: "workspace",
- workspacePath: "/remote/project",
- workspaceIdentity: "remote:server:11111111-1111-4111-8111-111111111111:/remote/project",
- workspacePurpose: "project",
- label: "project",
- },
- ],
- });
- expect(projection.openWorkspaces.map((item) => item.workspacePath)).toEqual([
- "/remote/project",
- ]);
- expect(projection.executionContexts).toEqual([
- expect.objectContaining({
- kind: "local",
- workspacePath: "/local/project",
- }),
- expect.objectContaining({
- kind: "remote",
- workspacePath: "/remote/project",
- }),
- ]);
- expect(projection.executionContexts[0]).not.toHaveProperty("attachmentState");
- });
- it("没有可信 attachment fact 时不会把在线 remote session 冒充成 ready", () => {
- const projection = buildRemoteWorkspaceProjection({
- managementSnapshot: createSnapshot(),
- tabs: [
- {
- id: "remote-session",
- kind: "workspace",
- workspacePath: "/workspace/project",
- workspaceIdentity:
- "remote:server:11111111-1111-4111-8111-111111111111:/workspace/project",
- remoteSessionId: "session-without-attachment-fact",
- label: "project",
- },
- ],
- });
- expect(projection.openWorkspaces[0]).toEqual(
- expect.objectContaining({ attachmentState: "attaching" }),
- );
- });
- });
|