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