Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalMenu.test.ts

224 lines6.7 KB

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