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 resolveProjectScriptTerminalId,
13} from "./terminalMenu";
14
15function makeKnownSession(input: {
16 readonly terminalId: string;
17 readonly status: KnownTerminalSession["state"]["status"];
18 readonly cwd?: string | null;
19 readonly updatedAt?: string | null;
20}): KnownTerminalSession {
21 return {
22 target: {
23 environmentId: EnvironmentId.make("env-1"),
24 threadId: ThreadId.make("thread-1"),
25 terminalId: input.terminalId,
26 },
27 state: {
28 summary: input.cwd
29 ? {
30 threadId: "thread-1",
31 terminalId: input.terminalId,
32 cwd: input.cwd,
33 worktreePath: input.cwd,
34 status: input.status === "closed" ? "error" : input.status,
35 pid: input.status === "running" ? 123 : null,
36 exitCode: null,
37 exitSignal: null,
38 hasRunningSubprocess: false,
39 label: getTerminalLabel(input.terminalId),
40 updatedAt: input.updatedAt ?? "2026-04-15T20:00:00.000Z",
41 }
42 : null,
43 buffer: "",
44 status: input.status,
45 error: null,
46 hasRunningSubprocess: false,
47 updatedAt: input.updatedAt ?? "2026-04-15T20:00:00.000Z",
48 version: 1,
49 },
50 };
51}
52
53describe("buildTerminalMenuSessions", () => {
54 it("only lists server-known sessions that are running or starting (plus current)", () => {
55 expect(
56 buildTerminalMenuSessions({
57 knownSessions: [
58 makeKnownSession({
59 terminalId: "term-3",
60 status: "running",
61 cwd: "/workspace/feature",
62 updatedAt: "2026-04-15T20:05:00.000Z",
63 }),
64 makeKnownSession({
65 terminalId: "term-2",
66 status: "exited",
67 cwd: "/workspace/exited",
68 updatedAt: "2026-04-15T20:06:00.000Z",
69 }),
70 ],
71 workspaceRoot: "/workspace/root",
72 }),
73 ).toEqual([
74 {
75 terminalId: "term-3",
76 cwd: "/workspace/feature",
77 status: "running",
78 hasRunningSubprocess: false,
79 displayLabel: "Terminal 3",
80 updatedAt: "2026-04-15T20:05:00.000Z",
81 },
82 ]);
83 });
84
85 it("keeps the current terminal visible even if it is no longer running", () => {
86 expect(
87 buildTerminalMenuSessions({
88 knownSessions: [],
89 workspaceRoot: "/workspace/root",
90 currentSession: {
91 terminalId: "term-4",
92 cwd: "/workspace/exited",
93 status: "exited",
94 hasRunningSubprocess: false,
95 displayLabel: "Terminal 4",
96 updatedAt: "2026-04-15T20:07:00.000Z",
97 },
98 }),
99 ).toEqual([
100 {
101 terminalId: "term-4",
102 cwd: "/workspace/exited",
103 status: "exited",
104 hasRunningSubprocess: false,
105 displayLabel: "Terminal 4",
106 updatedAt: "2026-04-15T20:07:00.000Z",
107 },
108 ]);
109 });
110});
111
112describe("nextTerminalId", () => {
113 it("uses the primary id when no terminals are listed yet", () => {
114 expect(nextTerminalId([])).toBe(DEFAULT_TERMINAL_ID);
115 });
116
117 it("allocates term-2 when only the primary shell exists", () => {
118 expect(nextTerminalId([DEFAULT_TERMINAL_ID])).toBe("term-2");
119 });
120});
121
122describe("nextOpenTerminalId", () => {
123 it("matches nextTerminalId when not on a terminal route", () => {
124 expect(nextOpenTerminalId({ listedTerminalIds: [] })).toBe(DEFAULT_TERMINAL_ID);
125 expect(nextOpenTerminalId({ listedTerminalIds: [DEFAULT_TERMINAL_ID] })).toBe("term-2");
126 });
127
128 it("avoids the mounted primary tab when the session list is still empty", () => {
129 expect(
130 nextOpenTerminalId({
131 listedTerminalIds: [],
132 activeRouteTerminalId: DEFAULT_TERMINAL_ID,
133 }),
134 ).toBe("term-2");
135 });
136
137 it("does not double-count when the route id is already listed", () => {
138 expect(
139 nextOpenTerminalId({
140 listedTerminalIds: [DEFAULT_TERMINAL_ID],
141 activeRouteTerminalId: DEFAULT_TERMINAL_ID,
142 }),
143 ).toBe("term-2");
144 });
145});
146
147describe("resolveProjectScriptTerminalId", () => {
148 it("reuses the default shell when no terminal is running", () => {
149 expect(
150 resolveProjectScriptTerminalId({
151 existingTerminalIds: [DEFAULT_TERMINAL_ID],
152 hasRunningTerminal: false,
153 }),
154 ).toBe(DEFAULT_TERMINAL_ID);
155 });
156
157 it("opens a new terminal when a shell is already running", () => {
158 expect(
159 resolveProjectScriptTerminalId({
160 existingTerminalIds: [DEFAULT_TERMINAL_ID, "term-2", "term-4"],
161 hasRunningTerminal: true,
162 }),
163 ).toBe("term-3");
164 });
165});