Files
t3code/apps/web/src/rightPanelStore.test.ts
T
2026-06-13 22:18:37 -07:00

112 lines
4.6 KiB
TypeScript

import { scopeThreadRef } from "@t3tools/client-runtime";
import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
import { beforeEach, describe, expect, it } from "vite-plus/test";
import {
selectActiveRightPanel,
selectActiveRightPanelSurface,
selectActiveRightPanelKindWithUrl,
selectThreadRightPanelState,
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("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, "plan");
useRightPanelStore.getState().open(refA, "preview");
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
expect(
selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
).toHaveLength(2);
});
it("close clears the active panel", () => {
useRightPanelStore.getState().open(refA, "plan");
useRightPanelStore.getState().close(refA);
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
});
it("toggle opens then closes the same kind", () => {
useRightPanelStore.getState().toggle(refA, "preview");
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
useRightPanelStore.getState().toggle(refA, "preview");
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
});
it("toggle to a different kind switches active", () => {
useRightPanelStore.getState().toggle(refA, "preview");
useRightPanelStore.getState().toggle(refA, "plan");
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("plan");
});
it("?diff=1 always wins over persisted state", () => {
useRightPanelStore.getState().open(refA, "preview");
expect(
selectActiveRightPanelKindWithUrl(useRightPanelStore.getState().byThreadKey, refA, true),
).toBe("diff");
expect(
selectActiveRightPanelKindWithUrl(useRightPanelStore.getState().byThreadKey, refA, false),
).toBe("preview");
});
it("removeThread clears persisted state", () => {
useRightPanelStore.getState().open(refA, "plan");
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("closing the active surface activates a neighboring surface", () => {
useRightPanelStore.getState().openBrowser(refA, "tab-a");
useRightPanelStore.getState().open(refA, "terminal");
useRightPanelStore.getState().closeSurface(refA, "terminal");
expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
"browser:tab-a",
);
});
it("reconciles browser surfaces without deleting other surface kinds", () => {
useRightPanelStore.getState().open(refA, "terminal");
useRightPanelStore.getState().openBrowser(refA, "tab-a");
useRightPanelStore.getState().openBrowser(refA, "tab-b");
useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
expect(
selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
(surface) => surface.id,
),
).toEqual(["terminal", "browser:tab-b", "browser:tab-c"]);
});
});