Open History to see the last commit
1import { EMPTY_TERMINAL_SESSION_STATE } from "@t3tools/client-runtime/state/terminal";
2import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
3import { expect, it, vi } from "vite-plus/test";
4import { act, fakeTimers, render } from "../../test/render";
5import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
6
7const fixture = vi.hoisted(() => ({
8 statusByThreadId: new Map<string, "running" | "exited">(),
9 pendingSurfaces: [] as Array<() => void>,
10}));
11
12// Every surface method is a no-op; `then` stays undefined so awaiting the surface resolves to it.
13const fakeSurface: object = new Proxy(
14 { input: document.createElement("textarea") },
15 {
16 get: (target, key) =>
17 key === "then" ? undefined : key in target ? Reflect.get(target, key) : () => undefined,
18 },
19);
20vi.mock("~/terminal/ghostty/surface", () => ({
21 GhosttyTerminalSurface: {
22 create: () =>
23 new Promise((resolve) => {
24 fixture.pendingSurfaces.push(() => resolve(fakeSurface));
25 }),
26 },
27}));
28vi.mock("../state/terminalSessions", async () => {
29 const Atom = await import("effect/reactivity/Atom");
30 const sessions = Atom.family((_environmentId: EnvironmentId) =>
31 Atom.family((threadId: ThreadId) =>
32 Atom.make({
33 ...EMPTY_TERMINAL_SESSION_STATE,
34 status: fixture.statusByThreadId.get(threadId) ?? "closed",
35 version: 1,
36 }),
37 ),
38 );
39 return {
40 attachedTerminalSessionAtom: (environmentId: EnvironmentId, terminal: { threadId: ThreadId }) =>
41 sessions(environmentId)(terminal.threadId),
42 };
43});
44vi.mock("../state/session", () => ({
45 useEnvironmentScope: () => true,
46 readEnvironmentScope: () => true,
47}));
48vi.mock("../state/use-atom-command", () => ({
49 useAtomCommand: () => async () => ({ _tag: "Success" }),
50}));
51// The commands are replaced above; importing their runtime would connect a client.
52vi.mock("../state/server", async () => {
53 const Atom = await import("effect/reactivity/Atom");
54 const config = Atom.make(null);
55 return { serverEnvironment: { configValueAtom: () => config } };
56});
57vi.mock("../state/terminal", () => ({ terminalEnvironment: { write: {}, resize: {} } }));
58vi.mock("../state/preview", () => ({ previewEnvironment: { open: {} } }));
59vi.mock("../editorPreferences", () => ({ useOpenInPreferredEditor: () => () => {} }));
60vi.mock("./preview/openTerminalLinkInPreview", () => ({ openTerminalLinkInPreview: () => {} }));
61vi.mock("~/localApi", () => ({ readLocalApi: () => null }));
62vi.mock("../hooks/useSettings", async () => {
63 const { DEFAULT_CLIENT_SETTINGS } = await import("@t3tools/contracts/settings");
64 return {
65 useClientSettings: <T,>(select: (settings: typeof DEFAULT_CLIENT_SETTINGS) => T) =>
66 select(DEFAULT_CLIENT_SETTINGS),
67 };
68});
69
70function Drawer(props: {
71 threadRef: ScopedThreadRef;
72 onCloseTerminal: (terminalId: string, threadRef?: ScopedThreadRef) => void;
73}) {
74 return (
75 <ThreadTerminalDrawer
76 threadRef={props.threadRef}
77 threadId={props.threadRef.threadId}
78 cwd="/repo"
79 height={240}
80 terminalIds={["term-1"]}
81 activeTerminalId="term-1"
82 terminalGroups={[{ id: "group-1", terminalIds: ["term-1"] }]}
83 activeTerminalGroupId="group-1"
84 onSplitTerminal={() => {}}
85 onSplitTerminalVertical={() => {}}
86 onNewTerminal={() => {}}
87 onActiveTerminalChange={() => {}}
88 onCloseTerminal={props.onCloseTerminal}
89 onHeightChange={() => {}}
90 onAddTerminalContext={() => {}}
91 keybindings={[]}
92 />
93 );
94}
95
96function setup() {
97 const original: ScopedThreadRef = {
98 environmentId: EnvironmentId.make("environment-a"),
99 threadId: ThreadId.make("thread-a"),
100 };
101 const other: ScopedThreadRef = {
102 environmentId: EnvironmentId.make("environment-b"),
103 threadId: ThreadId.make("thread-b"),
104 };
105 fixture.statusByThreadId = new Map([
106 [original.threadId, "exited"],
107 [other.threadId, "running"],
108 ]);
109 fixture.pendingSurfaces = [];
110 return { original, other };
111}
112
113it("closes an exited terminal on its own thread after navigating to one reusing its ID", async () => {
114 const { original, other } = setup();
115 const onCloseTerminal = vi.fn();
116 await using screen = await render(
117 <Drawer threadRef={original} onCloseTerminal={onCloseTerminal} />,
118 );
119 using _timers = fakeTimers();
120 // The exit is seen once the surface loads, and its close waits for the next task.
121 await act(async () => {
122 fixture.pendingSurfaces.shift()?.();
123 });
124 await screen.rerender(<Drawer threadRef={other} onCloseTerminal={onCloseTerminal} />);
125 await act(async () => {
126 vi.runOnlyPendingTimers();
127 });
128 expect(onCloseTerminal.mock.calls).toEqual([["term-1", original]]);
129});