698 lines · 24.3 KB · TypeScript
RawDownload
import { scopeThreadRef } from "@t3tools/client-runtime/environment";import { type EnvironmentId, ThreadId } from "@t3tools/contracts";import { beforeEach, describe, expect, it } from "vite-plus/test"; import {  migratePersistedRightPanelState,  pullRequestSurfaceId,  selectActiveRightPanel,  selectActiveRightPanelSurface,  selectSelectedRightPanelSurface,  selectThreadRightPanelState,  updatePullRequestTabStatus,  useRightPanelStore,} from "./rightPanelStore"; const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B")); beforeEach(() => {  useRightPanelStore.setState({ byThreadKey: {} });}); describe("rightPanelStore", () => {  it("drops the legacy singleton terminal surface during migration", () => {    expect(      migratePersistedRightPanelState({        byThreadKey: {          "env-1:thread-A": {            activeSurfaceId: "terminal",            surfaces: [              { id: "browser:tab-a", kind: "preview", resourceId: "tab-a" },              { id: "terminal", kind: "terminal" },            ],          },        },      }),    ).toEqual({      byThreadKey: {        "env-1:thread-A": {          isOpen: false,          activeSurfaceId: null,          surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],        },      },    });  });   it("upgrades saved single-session terminal surfaces to split-capable surfaces", () => {    expect(      migratePersistedRightPanelState({        byThreadKey: {          "env-1:thread-A": {            isOpen: true,            activeSurfaceId: "terminal:term-1",            surfaces: [{ id: "terminal:term-1", kind: "terminal", resourceId: "term-1" }],          },        },      }),    ).toEqual({      byThreadKey: {        "env-1:thread-A": {          isOpen: true,          activeSurfaceId: "terminal:term-1",          surfaces: [            {              id: "terminal:term-1",              kind: "terminal",              resourceId: "term-1",              terminalIds: ["term-1"],              activeTerminalId: "term-1",            },          ],        },      },    });  });   it("upgrades saved file surfaces with neutral reveal state", () => {    expect(      migratePersistedRightPanelState({        byThreadKey: {          "env-1:thread-A": {            isOpen: true,            activeSurfaceId: "file:src/index.ts",            surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],          },        },      }),    ).toEqual({      byThreadKey: {        "env-1:thread-A": {          isOpen: true,          activeSurfaceId: "file:src/index.ts",          surfaces: [            {              id: "file:src/index.ts",              kind: "file",              relativePath: "src/index.ts",              revealLine: null,              revealRequestId: 0,            },          ],        },      },    });  });   it("upgrades the legacy singleton pull request surface to a reference-keyed tab", () => {    const id = pullRequestSurfaceId({      projectId: "project-a",      repository: "pingdotgg/t3code",      number: 4909,    });    expect(      migratePersistedRightPanelState({        byThreadKey: {          "env-1:thread-A": {            isOpen: true,            activeSurfaceId: "pull-request",            surfaces: [              {                id: "pull-request",                kind: "pull-request",                projectId: "project-a",                repository: "pingdotgg/t3code",                number: 4909,              },            ],          },        },      }),    ).toEqual({      byThreadKey: {        "env-1:thread-A": {          isOpen: true,          activeSurfaceId: id,          surfaces: [            {              id,              kind: "pull-request",              projectId: "project-a",              repository: "pingdotgg/t3code",              number: 4909,            },          ],        },      },    });  });   it("drops the pull-request list's shared panel so a restart opens the page fresh", () => {    const id = pullRequestSurfaceId({      projectId: "project-a",      repository: "pingdotgg/t3code",      number: 4909,    });    const panelState = {      isOpen: true,      activeSurfaceId: id,      surfaces: [        {          id,          kind: "pull-request" as const,          projectId: "project-a",          repository: "pingdotgg/t3code",          number: 4909,        },      ],    };    expect(      migratePersistedRightPanelState({        byThreadKey: {          "env-1:pull-requests-panel": panelState,          "env-1:thread-A": panelState,        },      }),    ).toEqual({ byThreadKey: { "env-1:thread-A": panelState } });  });   it("drops persisted plan surfaces and does not reopen an empty panel", () => {    expect(      migratePersistedRightPanelState({        byThreadKey: {          "env-1:thread-A": {            isOpen: true,            activeSurfaceId: "plan",            surfaces: [{ id: "plan", kind: "plan" }],          },          "env-1:thread-B": {            isOpen: true,            activeSurfaceId: "plan",            surfaces: [              { id: "plan", kind: "plan" },              { id: "diff", kind: "diff" },            ],          },        },      }),    ).toEqual({      byThreadKey: {        "env-1:thread-A": {          isOpen: false,          activeSurfaceId: null,          surfaces: [],        },        "env-1:thread-B": {          isOpen: true,          activeSurfaceId: "diff",          surfaces: [{ id: "diff", kind: "diff" }],        },      },    });  });   it("open sets the active panel for a thread", () => {    useRightPanelStore.getState().open(refA, "preview");    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();  });   it("opening a different kind keeps both surfaces and activates the new one", () => {    useRightPanelStore.getState().open(refA, "agents");    useRightPanelStore.getState().open(refA, "preview");    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");    expect(      selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,    ).toHaveLength(2);  });   it("reopening an inactive singleton activates its existing surface", () => {    useRightPanelStore.getState().open(refA, "diff");    useRightPanelStore.getState().open(refA, "agents");    useRightPanelStore.getState().open(refA, "diff");     expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: true,      activeSurfaceId: "diff",      surfaces: [        { id: "diff", kind: "diff" },        { id: "agents", kind: "agents" },      ],    });  });   it("keeps files as a singleton surface", () => {    useRightPanelStore.getState().open(refA, "files");    useRightPanelStore.getState().open(refA, "files");    expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: true,      activeSurfaceId: "files",      surfaces: [{ id: "files", kind: "files" }],    });  });   it("replaces the standalone explorer with peer file surfaces", () => {    useRightPanelStore.getState().open(refA, "files");    useRightPanelStore.getState().openFile(refA, "src/index.ts");    useRightPanelStore.getState().openFile(refA, "src/index.ts");    useRightPanelStore.getState().openFile(refA, "README.md");     expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: true,      activeSurfaceId: "file:README.md",      surfaces: [        {          id: "file:src/index.ts",          kind: "file",          relativePath: "src/index.ts",          revealLine: null,          revealRequestId: 2,        },        {          id: "file:README.md",          kind: "file",          relativePath: "README.md",          revealLine: null,          revealRequestId: 1,        },      ],    });  });   it("updates line reveal requests when reopening a file surface", () => {    useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);    useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);     expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: true,      activeSurfaceId: "file:src/index.ts",      surfaces: [        {          id: "file:src/index.ts",          kind: "file",          relativePath: "src/index.ts",          revealLine: 87,          revealRequestId: 2,        },      ],    });     useRightPanelStore.getState().openFile(refA, "src/index.ts");     expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: true,      activeSurfaceId: "file:src/index.ts",      surfaces: [        {          id: "file:src/index.ts",          kind: "file",          relativePath: "src/index.ts",          revealLine: null,          revealRequestId: 3,        },      ],    });  });   it("removes persisted file surfaces when their workspace no longer exists", () => {    useRightPanelStore.getState().openFile(refA, "src/index.ts");    useRightPanelStore.getState().open(refA, "agents");    useRightPanelStore.getState().openFile(refA, "README.md");     useRightPanelStore.getState().reconcileFileSurfaces(refA, false);     expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: true,      activeSurfaceId: "agents",      surfaces: [{ id: "agents", kind: "agents" }],    });     useRightPanelStore.getState().openFile(refB, "conductor.json");    useRightPanelStore.getState().reconcileFileSurfaces(refB, false);    expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({      isOpen: false,      activeSurfaceId: null,      surfaces: [],    });  });   it("close hides the panel without clearing its selected surface", () => {    useRightPanelStore.getState().open(refA, "agents");    useRightPanelStore.getState().close(refA);    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();    expect(      selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),    ).toEqual({ id: "agents", kind: "agents" });    expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: false,      activeSurfaceId: "agents",      surfaces: [{ id: "agents", kind: "agents" }],    });  });   it("toggles empty panel visibility without creating a surface", () => {    useRightPanelStore.getState().toggleVisibility(refA);    expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: true,      activeSurfaceId: null,      surfaces: [],    });     useRightPanelStore.getState().toggleVisibility(refA);    expect(useRightPanelStore.getState().byThreadKey).toEqual({});  });   it("toggle hides the panel without discarding the active surface", () => {    useRightPanelStore.getState().toggle(refA, "diff");    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");    useRightPanelStore.getState().toggle(refA, "diff");    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();    expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      isOpen: false,      activeSurfaceId: "diff",      surfaces: [{ id: "diff", kind: "diff" }],    });  });   it("toggle to a different kind switches active", () => {    useRightPanelStore.getState().toggle(refA, "preview");    useRightPanelStore.getState().toggle(refA, "agents");    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("agents");  });   it("removeThread clears persisted state", () => {    useRightPanelStore.getState().open(refA, "agents");    useRightPanelStore.getState().removeThread(refA);    expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();  });   it("close on never-opened thread is a no-op", () => {    useRightPanelStore.getState().close(refA);    expect(useRightPanelStore.getState().byThreadKey).toEqual({});  });   it("tracks one surface per browser session", () => {    useRightPanelStore.getState().openBrowser(refA, "tab-a");    useRightPanelStore.getState().openBrowser(refA, "tab-b");     const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);    expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);    expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({      id: "browser:tab-b",      kind: "preview",      resourceId: "tab-b",    });  });   it("tracks one surface per pull request", () => {    const first = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };    const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };    useRightPanelStore.getState().openPullRequest(refA, first);    useRightPanelStore.getState().openPullRequest(refA, second);    useRightPanelStore.getState().openPullRequest(refA, first);     const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);    expect(state.surfaces.map((surface) => surface.id)).toEqual([      pullRequestSurfaceId(first),      pullRequestSurfaceId(second),    ]);    expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));  });   it("keeps one pull request read from two servers as two tabs", () => {    const local = {      environmentId: "local",      projectId: "project-a",      repository: "pingdotgg/t3code",      number: 4909,    };    const remote = { ...local, environmentId: "remote" };     useRightPanelStore.getState().openPullRequest(refA, local);    useRightPanelStore.getState().openPullRequest(refA, remote);     const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);    expect(state.surfaces.map((surface) => surface.id)).toEqual([      pullRequestSurfaceId(local),      pullRequestSurfaceId(remote),    ]);  });   it("keeps the page's panel tabs reachable when the set of connected servers changes", () => {    // The pull-requests page keys its one shared panel by a fixed sentinel environment, not by    // whichever capable server happens to sort first (see PULL_REQUESTS_PANEL_ENVIRONMENT_ID in    // _chat.pull-requests.tsx) — a server disconnecting must not move every open tab to a store    // key nobody wrote them under.    const panelId = ThreadId.make("pull-requests-panel");    const stableRef = scopeThreadRef("pull-requests-panel" as EnvironmentId, panelId);    const fromServerA = {      environmentId: "server-a",      projectId: "project-a",      repository: "pingdotgg/t3code",      number: 1,    };    const fromServerB = {      environmentId: "server-b",      projectId: "project-b",      repository: "pingdotgg/t3code",      number: 2,    };     // Both servers connected: tabs from each open under the one stable ref.    useRightPanelStore.getState().openPullRequest(stableRef, fromServerA);    useRightPanelStore.getState().openPullRequest(stableRef, fromServerB);     // Server A disconnects. The stable ref does not depend on which servers remain connected, so    // the same lookup still finds both tabs.    const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, stableRef);    expect(state.surfaces.map((surface) => surface.id)).toEqual([      pullRequestSurfaceId(fromServerA),      pullRequestSurfaceId(fromServerB),    ]);     // The bug this guards against: a ref keyed by the first capable environment instead of a    // fixed sentinel changes identity when that environment drops out, and a lookup under the new    // key finds nothing even though the tabs are still sitting under the old one.    const refWhileBothConnected = scopeThreadRef("server-a" as EnvironmentId, panelId);    const refAfterServerADisconnects = scopeThreadRef("server-b" as EnvironmentId, panelId);    expect(refWhileBothConnected).not.toEqual(refAfterServerADisconnects);    expect(      selectThreadRightPanelState(        useRightPanelStore.getState().byThreadKey,        refAfterServerADisconnects,      ).surfaces,    ).toEqual([]);  });   describe("updatePullRequestTabStatus", () => {    const status = (isDraft: boolean) => ({      projectId: "project-a",      repository: "pingdotgg/t3code",      number: 4909,      state: "open" as const,      isDraft,    });     // Regression for the tab wearing no state: this failed when the status was written under a    // key rebuilt from the pull request while the tab strip reads it under the surface's own id.    it("keys a status under the same id a surface opened from an environment carries", () => {      const target = {

Lines 1–500 of 698.