Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { type KnownTerminalSession } from "@t3tools/client-runtime/state/terminal";
4import { DEFAULT_TERMINAL_ID, EnvironmentId, ThreadId } from "@t3tools/contracts";
5
6import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
7
8import {
9 buildTerminalMenuSessions,
10 nextOpenTerminalId,
11 nextTerminalId,
12 previousLiveTerminalId,
13 resolveProjectScriptTerminalId,
14 type TerminalMenuSession,
15} from "./terminalMenu";
16
17function makeMenuSession(input: {
18 readonly terminalId: string;
19 readonly status: TerminalMenuSession["status"];
20}): TerminalMenuSession {
21 return {
22 terminalId: input.terminalId,
23 cwd: null,
24 status: input.status,
25 hasRunningSubprocess: false,
26 displayLabel: getTerminalLabel(input.terminalId),
27 updatedAt: null,
28 };
29}
30
31function makeKnownSession(input: {
32 readonly terminalId: string;
33 readonly status: KnownTerminalSession["state"]["status"];
34 readonly cwd?: string | null;
35 readonly updatedAt?: string | null;
36}): KnownTerminalSession {
37 return {
38 target: {
39 environmentId: EnvironmentId.make("env-1"),
40 threadId: ThreadId.make("thread-1"),
41 terminalId: input.terminalId,
42 },
43 state: {
44 summary: input.cwd
45 ? {
46 threadId: "thread-1",
47 terminalId: input.terminalId,
48 cwd: input.cwd,
49 worktreePath: input.cwd,
50 status: input.status === "closed" ? "error" : input.status,
51 pid: input.status === "running" ? 123 : null,
52 exitCode: null,
53 exitSignal: null,
54 hasRunningSubprocess: false,
55 label: getTerminalLabel(input.terminalId),
56 updatedAt: input.updatedAt ?? "2026-04-15T20:00:00.000Z",
57 }
58 : null,
59 buffer: "",
60 status: input.status,
61 error: null,
62 hasRunningSubprocess: false,
63 updatedAt: input.updatedAt ?? "2026-04-15T20:00:00.000Z",
64 version: 1,
65 },
66 };
67}
68
69describe("buildTerminalMenuSessions", () => {
70 it("only lists server-known sessions that are running or starting (plus current)", () => {
71 expect(
72 buildTerminalMenuSessions({
73 knownSessions: [
74 makeKnownSession({
75 terminalId: "term-3",
76 status: "running",
77 cwd: "/workspace/feature",
78 updatedAt: "2026-04-15T20:05:00.000Z",
79 }),
80 makeKnownSession({
81 terminalId: "term-2",
82 status: "exited",
83 cwd: "/workspace/exited",
84 updatedAt: "2026-04-15T20:06:00.000Z",
85 }),
86 ],
87 workspaceRoot: "/workspace/root",
88 }),
89 ).toEqual([
90 {
91 terminalId: "term-3",
92 cwd: "/workspace/feature",
93 status: "running",
94 hasRunningSubprocess: false,
95 displayLabel: "Terminal 3",
96 updatedAt: "2026-04-15T20:05:00.000Z",
97 },
98 ]);
99 });
100
101 it("keeps the current terminal visible even if it is no longer running", () => {
102 expect(
103 buildTerminalMenuSessions({
104 knownSessions: [],
105 workspaceRoot: "/workspace/root",
106 currentSession: {
107 terminalId: "term-4",
108 cwd: "/workspace/exited",
109 status: "exited",
110 hasRunningSubprocess: false,
111 displayLabel: "Terminal 4",
112 updatedAt: "2026-04-15T20:07:00.000Z",
113 },
114 }),
115 ).toEqual([
116 {
117 terminalId: "term-4",
118 cwd: "/workspace/exited",
119 status: "exited",
120 hasRunningSubprocess: false,
121 displayLabel: "Terminal 4",
122 updatedAt: "2026-04-15T20:07:00.000Z",
123 },
124 ]);
125 });
126});
127
128describe("nextTerminalId", () => {
129 it("uses the primary id when no terminals are listed yet", () => {
130 expect(nextTerminalId([])).toBe(DEFAULT_TERMINAL_ID);
131 });
132
133 it("allocates term-2 when only the primary shell exists", () => {
134 expect(nextTerminalId([DEFAULT_TERMINAL_ID])).toBe("term-2");
135 });
136});
137
138describe("nextOpenTerminalId", () => {
139 it("matches nextTerminalId when not on a terminal route", () => {
140 expect(nextOpenTerminalId({ listedTerminalIds: [] })).toBe(DEFAULT_TERMINAL_ID);
141 expect(nextOpenTerminalId({ listedTerminalIds: [DEFAULT_TERMINAL_ID] })).toBe("term-2");
142 });
143
144 it("avoids the mounted primary tab when the session list is still empty", () => {
145 expect(
146 nextOpenTerminalId({
147 listedTerminalIds: [],
148 activeRouteTerminalId: DEFAULT_TERMINAL_ID,
149 }),
150 ).toBe("term-2");
151 });
152
153 it("does not double-count when the route id is already listed", () => {
154 expect(
155 nextOpenTerminalId({
156 listedTerminalIds: [DEFAULT_TERMINAL_ID],
157 activeRouteTerminalId: DEFAULT_TERMINAL_ID,
158 }),
159 ).toBe("term-2");
160 });
161});
162
163describe("previousLiveTerminalId", () => {
164 it("returns null when no other live session remains", () => {
165 expect(
166 previousLiveTerminalId({
167 sessions: [
168 makeMenuSession({ terminalId: "term-2", status: "exited" }),
169 makeMenuSession({ terminalId: "term-3", status: "closed" }),
170 ],
171 exitedTerminalId: "term-2",
172 }),
173 ).toBe(null);
174 });
175
176 it("prefers the nearest live session below the exited id", () => {
177 expect(
178 previousLiveTerminalId({
179 sessions: [
180 makeMenuSession({ terminalId: DEFAULT_TERMINAL_ID, status: "running" }),
181 makeMenuSession({ terminalId: "term-2", status: "running" }),
182 makeMenuSession({ terminalId: "term-3", status: "exited" }),
183 makeMenuSession({ terminalId: "term-4", status: "running" }),
184 ],
185 exitedTerminalId: "term-3",
186 }),
187 ).toBe("term-2");
188 });
189
190 it("falls back to the nearest live session above when the exited id was lowest", () => {
191 expect(
192 previousLiveTerminalId({
193 sessions: [
194 makeMenuSession({ terminalId: DEFAULT_TERMINAL_ID, status: "exited" }),
195 makeMenuSession({ terminalId: "term-2", status: "starting" }),
196 makeMenuSession({ terminalId: "term-4", status: "running" }),
197 ],
198 exitedTerminalId: DEFAULT_TERMINAL_ID,
199 }),
200 ).toBe("term-2");
201 });
202
203 it("ignores dead sessions when picking the fallback", () => {
204 expect(
205 previousLiveTerminalId({
206 sessions: [
207 makeMenuSession({ terminalId: DEFAULT_TERMINAL_ID, status: "running" }),
208 makeMenuSession({ terminalId: "term-2", status: "exited" }),
209 makeMenuSession({ terminalId: "term-3", status: "exited" }),
210 ],
211 exitedTerminalId: "term-3",
212 }),
213 ).toBe(DEFAULT_TERMINAL_ID);
214 });
215});
216
217describe("resolveProjectScriptTerminalId", () => {
218 it("reuses the default shell when no terminal is running", () => {
219 expect(
220 resolveProjectScriptTerminalId({
221 existingTerminalIds: [DEFAULT_TERMINAL_ID],
222 hasRunningTerminal: false,
223 }),
224 ).toBe(DEFAULT_TERMINAL_ID);
225 });
226
227 it("opens a new terminal when a shell is already running", () => {
228 expect(
229 resolveProjectScriptTerminalId({
230 existingTerminalIds: [DEFAULT_TERMINAL_ID, "term-2", "term-4"],
231 hasRunningTerminal: true,
232 }),
233 ).toBe("term-3");
234 });
235});