Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 DEFAULT_TERMINAL_ID,
6 TerminalAttachInput,
7 TerminalClearInput,
8 TerminalCloseInput,
9 TerminalEvent,
10 TerminalOpenInput,
11 TerminalResizeInput,
12 TerminalSessionSnapshot,
13 TerminalThreadInput,
14 TerminalWriteInput,
15} from "./terminal.ts";
16
17function decodeSync<S extends Schema.Top>(schema: S, input: unknown): Schema.Schema.Type<S> {
18 return Schema.decodeUnknownSync(schema as never)(input) as Schema.Schema.Type<S>;
19}
20
21function decodes<S extends Schema.Top>(schema: S, input: unknown): boolean {
22 try {
23 Schema.decodeUnknownSync(schema as never)(input);
24 return true;
25 } catch {
26 return false;
27 }
28}
29
30describe("TerminalOpenInput", () => {
31 it("accepts valid open input", () => {
32 expect(
33 decodes(TerminalOpenInput, {
34 threadId: "thread-1",
35 terminalId: DEFAULT_TERMINAL_ID,
36 cwd: "/tmp/project",
37 cols: 120,
38 rows: 40,
39 }),
40 ).toBe(true);
41 });
42
43 it("accepts ultrawide terminal dimensions from xterm fit", () => {
44 expect(
45 decodes(TerminalOpenInput, {
46 threadId: "thread-1",
47 terminalId: DEFAULT_TERMINAL_ID,
48 cwd: "/tmp/project",
49 cols: 423,
50 rows: 40,
51 }),
52 ).toBe(true);
53 });
54
55 it("rejects invalid bounds", () => {
56 expect(
57 decodes(TerminalOpenInput, {
58 threadId: "thread-1",
59 terminalId: DEFAULT_TERMINAL_ID,
60 cwd: "/tmp/project",
61 cols: 10,
62 rows: 0,
63 }),
64 ).toBe(false);
65 });
66
67 it("requires terminalId — the client must always pick an id", () => {
68 expect(
69 decodes(TerminalOpenInput, {
70 threadId: "thread-1",
71 cwd: "/tmp/project",
72 cols: 100,
73 rows: 24,
74 }),
75 ).toBe(false);
76 });
77
78 it("accepts optional env overrides", () => {
79 const parsed = decodeSync(TerminalOpenInput, {
80 threadId: "thread-1",
81 terminalId: DEFAULT_TERMINAL_ID,
82 cwd: "/tmp/project",
83 worktreePath: "/tmp/project/.t3/worktrees/feature-a",
84 cols: 100,
85 rows: 24,
86 env: {
87 T3CODE_PROJECT_ROOT: "/tmp/project",
88 CUSTOM_FLAG: "1",
89 },
90 });
91 expect(parsed.env).toMatchObject({
92 T3CODE_PROJECT_ROOT: "/tmp/project",
93 CUSTOM_FLAG: "1",
94 });
95 expect(parsed.worktreePath).toBe("/tmp/project/.t3/worktrees/feature-a");
96 });
97
98 it("rejects invalid env keys", () => {
99 expect(
100 decodes(TerminalOpenInput, {
101 threadId: "thread-1",
102 cwd: "/tmp/project",
103 cols: 100,
104 rows: 24,
105 env: {
106 "bad-key": "1",
107 },
108 }),
109 ).toBe(false);
110 });
111});
112
113describe("TerminalAttachInput", () => {
114 it("accepts explicit inactive-session restart intent", () => {
115 const parsed = decodeSync(TerminalAttachInput, {
116 threadId: "thread-1",
117 terminalId: DEFAULT_TERMINAL_ID,
118 cwd: "/tmp/project",
119 restartIfNotRunning: true,
120 });
121
122 expect(parsed.restartIfNotRunning).toBe(true);
123 });
124});
125
126describe("TerminalWriteInput", () => {
127 it("accepts non-empty data", () => {
128 expect(
129 decodes(TerminalWriteInput, {
130 threadId: "thread-1",
131 terminalId: DEFAULT_TERMINAL_ID,
132 data: "echo hello\n",
133 }),
134 ).toBe(true);
135 });
136
137 it("rejects empty data", () => {
138 expect(
139 decodes(TerminalWriteInput, {
140 threadId: "thread-1",
141 terminalId: DEFAULT_TERMINAL_ID,
142 data: "",
143 }),
144 ).toBe(false);
145 });
146
147 it("rejects missing terminalId", () => {
148 expect(
149 decodes(TerminalWriteInput, {
150 threadId: "thread-1",
151 data: "echo hello\n",
152 }),
153 ).toBe(false);
154 });
155});
156
157describe("TerminalThreadInput", () => {
158 it("trims thread ids", () => {
159 const parsed = decodeSync(TerminalThreadInput, { threadId: " thread-1 " });
160 expect(parsed.threadId).toBe("thread-1");
161 });
162});
163
164describe("TerminalResizeInput", () => {
165 it("accepts valid size", () => {
166 expect(
167 decodes(TerminalResizeInput, {
168 threadId: "thread-1",
169 terminalId: DEFAULT_TERMINAL_ID,
170 cols: 80,
171 rows: 24,
172 }),
173 ).toBe(true);
174 });
175
176 it("rejects missing terminalId", () => {
177 expect(
178 decodes(TerminalResizeInput, {
179 threadId: "thread-1",
180 cols: 80,
181 rows: 24,
182 }),
183 ).toBe(false);
184 });
185});
186
187describe("TerminalClearInput", () => {
188 it("requires terminalId", () => {
189 expect(decodes(TerminalClearInput, { threadId: "thread-1" })).toBe(false);
190 });
191
192 it("accepts an explicit terminalId", () => {
193 const parsed = decodeSync(TerminalClearInput, {
194 threadId: "thread-1",
195 terminalId: DEFAULT_TERMINAL_ID,
196 });
197 expect(parsed.terminalId).toBe(DEFAULT_TERMINAL_ID);
198 });
199});
200
201describe("TerminalCloseInput", () => {
202 it("accepts optional deleteHistory", () => {
203 expect(
204 decodes(TerminalCloseInput, {
205 threadId: "thread-1",
206 deleteHistory: true,
207 }),
208 ).toBe(true);
209 });
210});
211
212describe("TerminalSessionSnapshot", () => {
213 const isoTimestamp = "2026-01-01T00:00:00.000Z";
214
215 it("accepts running snapshots", () => {
216 expect(
217 decodes(TerminalSessionSnapshot, {
218 threadId: "thread-1",
219 terminalId: DEFAULT_TERMINAL_ID,
220 cwd: "/tmp/project",
221 worktreePath: null,
222 status: "running",
223 pid: 1234,
224 history: "hello\n",
225 exitCode: null,
226 exitSignal: null,
227 label: "Primary",
228 updatedAt: isoTimestamp,
229 }),
230 ).toBe(true);
231 });
232});
233
234describe("TerminalEvent", () => {
235 const isoTimestamp = "2026-01-01T00:00:00.000Z";
236
237 it("accepts output events", () => {
238 expect(
239 decodes(TerminalEvent, {
240 type: "output",
241 threadId: "thread-1",
242 terminalId: DEFAULT_TERMINAL_ID,
243 data: "line\n",
244 }),
245 ).toBe(true);
246 });
247
248 it("accepts exited events", () => {
249 expect(
250 decodes(TerminalEvent, {
251 type: "exited",
252 threadId: "thread-1",
253 terminalId: DEFAULT_TERMINAL_ID,
254 exitCode: 0,
255 exitSignal: null,
256 }),
257 ).toBe(true);
258 });
259
260 it("accepts closed events", () => {
261 expect(
262 decodes(TerminalEvent, {
263 type: "closed",
264 threadId: "thread-1",
265 terminalId: DEFAULT_TERMINAL_ID,
266 }),
267 ).toBe(true);
268 });
269
270 it("accepts activity events", () => {
271 expect(
272 decodes(TerminalEvent, {
273 type: "activity",
274 threadId: "thread-1",
275 terminalId: DEFAULT_TERMINAL_ID,
276 hasRunningSubprocess: true,
277 label: "vim",
278 }),
279 ).toBe(true);
280 });
281
282 it("accepts started events with snapshot worktree metadata", () => {
283 expect(
284 decodes(TerminalEvent, {
285 type: "started",
286 threadId: "thread-1",
287 terminalId: DEFAULT_TERMINAL_ID,
288 snapshot: {
289 threadId: "thread-1",
290 terminalId: DEFAULT_TERMINAL_ID,
291 cwd: "/tmp/project/.t3/worktrees/feature-a",
292 worktreePath: "/tmp/project/.t3/worktrees/feature-a",
293 status: "running",
294 pid: 1234,
295 history: "",
296 exitCode: null,
297 exitSignal: null,
298 label: "Primary",
299 updatedAt: isoTimestamp,
300 },
301 }),
302 ).toBe(true);
303 });
304});