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