- Replace combined `useStore` state/dispatch wrapper with direct selector/action hooks across chat UI components - Inline and harden terminal state transitions inside `terminalStateStore` and remove `threadTerminalState` - Add `terminalStateStore` action tests covering defaults, splits, groups, activity, and close/reset behavior
90 lines
3.7 KiB
TypeScript
90 lines
3.7 KiB
TypeScript
import { ThreadId } from "@t3tools/contracts";
|
|
import { beforeEach, describe, expect, it } from "vitest";
|
|
|
|
import { useTerminalStateStore } from "./terminalStateStore";
|
|
|
|
const THREAD_ID = ThreadId.makeUnsafe("thread-1");
|
|
|
|
describe("terminalStateStore actions", () => {
|
|
beforeEach(() => {
|
|
if (typeof localStorage !== "undefined") {
|
|
localStorage.clear();
|
|
}
|
|
useTerminalStateStore.setState({ terminalStateByThreadId: {} });
|
|
});
|
|
|
|
it("returns a closed default terminal state for unknown threads", () => {
|
|
const terminalState = useTerminalStateStore.getState().getTerminalState(THREAD_ID);
|
|
expect(terminalState).toEqual({
|
|
terminalOpen: false,
|
|
terminalHeight: 280,
|
|
terminalIds: ["default"],
|
|
runningTerminalIds: [],
|
|
activeTerminalId: "default",
|
|
terminalGroups: [{ id: "group-default", terminalIds: ["default"] }],
|
|
activeTerminalGroupId: "group-default",
|
|
});
|
|
});
|
|
|
|
it("opens and splits terminals into the active group", () => {
|
|
const store = useTerminalStateStore.getState();
|
|
store.setTerminalOpen(THREAD_ID, true);
|
|
store.splitTerminal(THREAD_ID, "terminal-2");
|
|
|
|
const terminalState = useTerminalStateStore.getState().getTerminalState(THREAD_ID);
|
|
expect(terminalState.terminalOpen).toBe(true);
|
|
expect(terminalState.terminalIds).toEqual(["default", "terminal-2"]);
|
|
expect(terminalState.activeTerminalId).toBe("terminal-2");
|
|
expect(terminalState.terminalGroups).toEqual([
|
|
{ id: "group-default", terminalIds: ["default", "terminal-2"] },
|
|
]);
|
|
});
|
|
|
|
it("creates new terminals in a separate group", () => {
|
|
useTerminalStateStore.getState().newTerminal(THREAD_ID, "terminal-2");
|
|
|
|
const terminalState = useTerminalStateStore.getState().getTerminalState(THREAD_ID);
|
|
expect(terminalState.terminalIds).toEqual(["default", "terminal-2"]);
|
|
expect(terminalState.activeTerminalId).toBe("terminal-2");
|
|
expect(terminalState.activeTerminalGroupId).toBe("group-terminal-2");
|
|
expect(terminalState.terminalGroups).toEqual([
|
|
{ id: "group-default", terminalIds: ["default"] },
|
|
{ id: "group-terminal-2", terminalIds: ["terminal-2"] },
|
|
]);
|
|
});
|
|
|
|
it("tracks and clears terminal subprocess activity", () => {
|
|
const store = useTerminalStateStore.getState();
|
|
store.splitTerminal(THREAD_ID, "terminal-2");
|
|
store.setTerminalActivity(THREAD_ID, "terminal-2", true);
|
|
expect(useTerminalStateStore.getState().getTerminalState(THREAD_ID).runningTerminalIds).toEqual([
|
|
"terminal-2",
|
|
]);
|
|
|
|
store.setTerminalActivity(THREAD_ID, "terminal-2", false);
|
|
expect(useTerminalStateStore.getState().getTerminalState(THREAD_ID).runningTerminalIds).toEqual([]);
|
|
});
|
|
|
|
it("resets to default and clears persisted entry when closing the last terminal", () => {
|
|
const store = useTerminalStateStore.getState();
|
|
store.closeTerminal(THREAD_ID, "default");
|
|
|
|
expect(useTerminalStateStore.getState().terminalStateByThreadId[THREAD_ID]).toBeUndefined();
|
|
expect(useTerminalStateStore.getState().getTerminalState(THREAD_ID).terminalIds).toEqual(["default"]);
|
|
});
|
|
|
|
it("keeps a valid active terminal after closing an active split terminal", () => {
|
|
const store = useTerminalStateStore.getState();
|
|
store.splitTerminal(THREAD_ID, "terminal-2");
|
|
store.splitTerminal(THREAD_ID, "terminal-3");
|
|
store.closeTerminal(THREAD_ID, "terminal-3");
|
|
|
|
const terminalState = useTerminalStateStore.getState().getTerminalState(THREAD_ID);
|
|
expect(terminalState.activeTerminalId).toBe("terminal-2");
|
|
expect(terminalState.terminalIds).toEqual(["default", "terminal-2"]);
|
|
expect(terminalState.terminalGroups).toEqual([
|
|
{ id: "group-default", terminalIds: ["default", "terminal-2"] },
|
|
]);
|
|
});
|
|
});
|