| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627 |
- import { describe, expect, it, vi } from "vitest";
- import {
- buildRemoteWorkspacePersistPatch,
- restorePersistedRemoteWorkspaceSessions,
- } from "@/root/remoteWorkspaceSessionPersistence.js";
- import {
- buildRemoteWorkspaceIdentity,
- getRemoteWorkspaceSessionEntries,
- removeRemoteWorkspaceSessionEntries,
- } from "@/lib/remoteWorkspaceHistory.js";
- import { createTabStore, isWorkspaceTab } from "@/store/tabStore.js";
- describe("restorePersistedRemoteWorkspaceSessions", () => {
- it("active-first 模式先恢复 active workspace,首帧后再补齐完整顺序", () => {
- const tabStoreApi = createTabStore();
- const result = restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- { kind: "local", workspacePath: "/workspace/a" },
- { kind: "local", workspacePath: "/workspace/active" },
- { kind: "local", workspacePath: "/workspace/b" },
- ],
- lastActiveTabIndex: 1,
- },
- restoreMode: "active-first",
- tabStoreApi,
- });
- expect(
- tabStoreApi
- .getState()
- .tabs.filter(isWorkspaceTab)
- .map((tab) => tab.workspacePath),
- ).toEqual(["/workspace/active"]);
- result?.deferredRestore?.();
- expect(
- tabStoreApi
- .getState()
- .tabs.filter(isWorkspaceTab)
- .map((tab) => tab.workspacePath),
- ).toEqual(["/workspace/a", "/workspace/active", "/workspace/b"]);
- });
- it("active-first 补齐不会复活用户在 idle callback 前关闭的 active workspace", () => {
- const tabStoreApi = createTabStore();
- const result = restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- { kind: "local", workspacePath: "/workspace/a" },
- { kind: "local", workspacePath: "/workspace/active" },
- { kind: "local", workspacePath: "/workspace/b" },
- ],
- lastActiveTabIndex: 1,
- },
- restoreMode: "active-first",
- tabStoreApi,
- });
- const activeTabId = tabStoreApi.getState().activeTabId;
- expect(activeTabId).not.toBeNull();
- tabStoreApi.getState().closeTab(activeTabId!);
- result?.deferredRestore?.();
- expect(
- tabStoreApi
- .getState()
- .tabs.filter(isWorkspaceTab)
- .map((tab) => tab.workspacePath),
- ).toEqual(["/workspace/a", "/workspace/b"]);
- });
- it("恢复本地会话工作区时保留用途", () => {
- const restoreTabs = vi.fn();
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- {
- kind: "local",
- workspacePath: "/Users/demo/.zcode/workspace/default",
- workspacePurpose: "conversation",
- },
- ],
- lastActiveTabIndex: 0,
- },
- tabStoreApi: {
- getState: () => ({ restoreTabs }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- {
- workspacePath: "/Users/demo/.zcode/workspace/default",
- workspacePurpose: "conversation",
- },
- ],
- 0,
- );
- });
- it("canonical conversation workspace 的旧 project 用途会在恢复时自愈", () => {
- const restoreTabs = vi.fn();
- const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- { kind: "local", workspacePath: "/Users/demo/project" },
- {
- kind: "local",
- workspacePath: conversationWorkspacePath,
- workspacePurpose: "project",
- },
- ],
- lastActiveTabIndex: 0,
- },
- conversationWorkspacePath,
- tabStoreApi: {
- getState: () => ({ restoreTabs }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- "/Users/demo/project",
- {
- workspacePath: conversationWorkspacePath,
- workspacePurpose: "conversation",
- },
- ],
- 0,
- );
- });
- it("canonical conversation workspace 会替换持久化遗留的其他 conversation 路径", () => {
- const restoreTabs = vi.fn();
- const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- { kind: "local", workspacePath: "/Users/demo/project" },
- {
- kind: "local",
- workspacePath: conversationWorkspacePath,
- workspacePurpose: "conversation",
- },
- {
- kind: "local",
- workspacePath: "/private/tmp/test/.zcode/workspace/default",
- workspacePurpose: "conversation",
- },
- ],
- lastActiveTabIndex: 2,
- },
- conversationWorkspacePath,
- tabStoreApi: {
- getState: () => ({ restoreTabs }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- "/Users/demo/project",
- {
- workspacePath: conversationWorkspacePath,
- workspacePurpose: "conversation",
- },
- ],
- 1,
- );
- });
- it("持久化列表缺少 conversation workspace 时非激活补建", () => {
- const restoreTabs = vi.fn();
- const conversationWorkspacePath = "/Users/demo/.zcode/workspace/default";
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- { kind: "local", workspacePath: "/Users/demo/project" },
- ],
- lastActiveTabIndex: 0,
- },
- conversationWorkspacePath,
- tabStoreApi: {
- getState: () => ({ restoreTabs }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- "/Users/demo/project",
- {
- workspacePath: conversationWorkspacePath,
- workspacePurpose: "conversation",
- },
- ],
- 0,
- );
- });
- it("仅将启动期命中的本地 workspace 恢复为临时不可用状态", () => {
- const restoreTabs = vi.fn();
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- { kind: "local", workspacePath: "/workspace/available" },
- { kind: "local", workspacePath: "/workspace/missing" },
- ],
- lastActiveTabIndex: 1,
- },
- unavailableWorkspacePath: "/workspace/missing",
- tabStoreApi: {
- getState: () => ({ restoreTabs }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- "/workspace/available",
- {
- workspacePath: "/workspace/missing",
- availability: "unavailable-local-directory",
- },
- ],
- 1,
- );
- });
- it("远程 workspace 即使路径同名也不应用本地不可用状态", () => {
- const restoreTabs = vi.fn();
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/workspace/missing",
- target: { kind: "docker", container: "demo" },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- ],
- lastActiveTabIndex: 0,
- },
- unavailableWorkspacePath: "/workspace/missing",
- tabStoreApi: {
- getState: () => ({ restoreTabs }),
- } as never,
- });
- expect(restoreTabs.mock.calls[0]?.[0]?.[0]).not.toHaveProperty("availability");
- });
- it("启动时恢复远程断开态 tab,但不再自动重连", () => {
- const restoreTabs = vi.fn();
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- ],
- lastActiveTabIndex: 0,
- },
- tabStoreApi: {
- getState: () => ({
- restoreTabs,
- }),
- } as never,
- });
- const expectedWorkspaceIdentity = buildRemoteWorkspaceIdentity("/workspace/demo", {
- kind: "docker",
- container: "demo",
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- {
- workspacePath: "/workspace/demo",
- remoteTarget: {
- kind: "docker",
- container: "demo",
- },
- workspaceIdentity: expectedWorkspaceIdentity,
- },
- ],
- 0,
- );
- });
- it("启动时 lastActiveTabIndex 指向远程 workspace 时跳到后面的本地 workspace", () => {
- const restoreTabs = vi.fn();
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/root",
- workspaceIdentity: "remote:ssh:localhost:2222:root:/root",
- target: {
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "root",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "connected",
- },
- {
- kind: "local",
- workspacePath: "/workspace/local",
- },
- ],
- lastActiveTabIndex: 0,
- },
- tabStoreApi: {
- getState: () => ({
- restoreTabs,
- }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- {
- workspacePath: "/root",
- remoteTarget: {
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "root",
- },
- workspaceIdentity: "remote:ssh:localhost:2222:root:/root",
- },
- "/workspace/local",
- ],
- 1,
- );
- });
- it("启动时 lastActiveTabIndex 指向末尾远程 workspace 时回到已有本地 workspace", () => {
- const restoreTabs = vi.fn();
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/root-a",
- workspaceIdentity: "remote:ssh:localhost:2222:root:/root-a",
- target: {
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "root",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "connected",
- },
- {
- kind: "local",
- workspacePath: "/workspace/local",
- },
- {
- kind: "remote",
- workspacePath: "/root-b",
- workspaceIdentity: "remote:ssh:localhost:2222:root:/root-b",
- target: {
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "root",
- },
- lastOpenedAt: 2,
- lastConnectionStatus: "connected",
- },
- ],
- lastActiveTabIndex: 2,
- },
- tabStoreApi: {
- getState: () => ({
- restoreTabs,
- }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- {
- workspacePath: "/root-a",
- remoteTarget: {
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "root",
- },
- workspaceIdentity: "remote:ssh:localhost:2222:root:/root-a",
- },
- "/workspace/local",
- {
- workspacePath: "/root-b",
- remoteTarget: {
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "root",
- },
- workspaceIdentity: "remote:ssh:localhost:2222:root:/root-b",
- },
- ],
- 1,
- );
- });
- it("同一远程身份只恢复一次", () => {
- const restoreTabs = vi.fn();
- const sharedWorkspaceIdentity = buildRemoteWorkspaceIdentity("/workspace/demo", {
- kind: "docker",
- container: "demo",
- });
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- workspaceIdentity: sharedWorkspaceIdentity,
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- {
- kind: "remote",
- workspacePath: "/workspace/alias",
- workspaceIdentity: sharedWorkspaceIdentity,
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- ],
- lastActiveTabIndex: 0,
- },
- tabStoreApi: {
- getState: () => ({
- restoreTabs,
- }),
- } as never,
- });
- expect(restoreTabs).toHaveBeenCalledWith(
- [
- {
- workspacePath: "/workspace/demo",
- remoteTarget: {
- kind: "docker",
- container: "demo",
- },
- workspaceIdentity: sharedWorkspaceIdentity,
- },
- ],
- 0,
- );
- });
- it("远程入口不可见时只恢复本地 workspace", () => {
- const restoreTabs = vi.fn();
- restorePersistedRemoteWorkspaceSessions({
- settings: {
- lastWorkspaceSession: [
- { kind: "local", workspacePath: "/workspace/local" },
- {
- kind: "remote",
- workspacePath: "/workspace/demo",
- target: {
- kind: "docker",
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed",
- },
- ],
- lastActiveTabIndex: 1,
- },
- tabStoreApi: {
- getState: () => ({
- restoreTabs,
- }),
- } as never,
- allowRemoteWorkspaceRestore: false,
- });
- expect(restoreTabs).toHaveBeenCalledWith(["/workspace/local"], 0);
- });
- it("远程入口隐藏后,后续持久化仍保留远程会话快照", () => {
- const restoreTabs = vi.fn();
- const settings = {
- lastWorkspaceSession: [
- { kind: "local" as const, workspacePath: "/workspace/local" },
- {
- kind: "remote" as const,
- workspacePath: "/workspace/demo",
- target: {
- kind: "docker" as const,
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed" as const,
- },
- ],
- lastActiveTabIndex: 1,
- };
- restorePersistedRemoteWorkspaceSessions({
- settings,
- tabStoreApi: {
- getState: () => ({
- restoreTabs,
- }),
- } as never,
- allowRemoteWorkspaceRestore: false,
- });
- expect(restoreTabs).toHaveBeenCalledWith(["/workspace/local"], 0);
- const remoteSessions = getRemoteWorkspaceSessionEntries(settings);
- const patch = buildRemoteWorkspacePersistPatch(
- {
- tabs: [
- {
- id: "local-1",
- kind: "workspace",
- workspacePath: "/workspace/local",
- label: "local",
- },
- ],
- activeWorkspacePath: "/workspace/local",
- } as never,
- remoteSessions,
- );
- expect(patch.lastWorkspaceSession).toEqual([
- { kind: "local", workspacePath: "/workspace/local" },
- settings.lastWorkspaceSession[1],
- ]);
- });
- it("持久化本地会话工作区时保留用途", () => {
- const patch = buildRemoteWorkspacePersistPatch(
- {
- tabs: [
- {
- id: "conversation-1",
- kind: "workspace",
- workspacePath: "/Users/demo/.zcode/workspace/default",
- workspacePurpose: "conversation",
- availability: "unavailable-local-directory",
- label: "default",
- },
- ],
- activeWorkspacePath: "/Users/demo/.zcode/workspace/default",
- } as never,
- [],
- );
- expect(patch.lastWorkspaceSession).toEqual([
- {
- kind: "local",
- workspacePath: "/Users/demo/.zcode/workspace/default",
- workspacePurpose: "conversation",
- },
- ]);
- });
- it("用户移除远程 tab 后,持久化补丁不再保留对应远程快照", () => {
- const remoteSession = {
- kind: "remote" as const,
- workspacePath: "/workspace/demo",
- workspaceIdentity: "remote:docker:demo:/workspace/demo",
- target: {
- kind: "docker" as const,
- container: "demo",
- },
- lastOpenedAt: 1,
- lastConnectionStatus: "failed" as const,
- };
- const removal = removeRemoteWorkspaceSessionEntries(
- [remoteSession],
- ["remote:docker:demo:/workspace/demo"],
- );
- const patch = buildRemoteWorkspacePersistPatch(
- {
- tabs: [],
- activeWorkspacePath: null,
- } as never,
- removal.nextRemoteSessions,
- );
- expect(patch.lastWorkspaceSession).toEqual([]);
- });
- });
|