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

terminal.test.ts

234 lines5.5 KB

1import { Schema } from "effect";
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";
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>;
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 }
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("rejects invalid bounds", () => {
42 expect(
43 decodes(TerminalOpenInput, {
44 threadId: "thread-1",
45 cwd: "/tmp/project",
46 cols: 10,
47 rows: 2,
48 }),
49 ).toBe(false);
50 });
51​
52 it("defaults terminalId when missing", () => {
53 const parsed = decodeSync(TerminalOpenInput, {
54 threadId: "thread-1",
55 cwd: "/tmp/project",
56 cols: 100,
57 rows: 24,
58 });
59 expect(parsed.terminalId).toBe(DEFAULT_TERMINAL_ID);
60 });
61​
62 it("accepts optional env overrides", () => {
63 const parsed = decodeSync(TerminalOpenInput, {
64 threadId: "thread-1",
65 cwd: "/tmp/project",
66 worktreePath: "/tmp/project/.t3/worktrees/feature-a",
67 cols: 100,
68 rows: 24,
69 env: {
70 T3CODE_PROJECT_ROOT: "/tmp/project",
71 CUSTOM_FLAG: "1",
72 },
73 });
74 expect(parsed.env).toMatchObject({
75 T3CODE_PROJECT_ROOT: "/tmp/project",
76 CUSTOM_FLAG: "1",
77 });
78 expect(parsed.worktreePath).toBe("/tmp/project/.t3/worktrees/feature-a");
79 });
80​
81 it("rejects invalid env keys", () => {
82 expect(
83 decodes(TerminalOpenInput, {
84 threadId: "thread-1",
85 cwd: "/tmp/project",
86 cols: 100,
87 rows: 24,
88 env: {
89 "bad-key": "1",
90 },
91 }),
92 ).toBe(false);
93 });
94});
95​
96describe("TerminalWriteInput", () => {
97 it("accepts non-empty data", () => {
98 expect(
99 decodes(TerminalWriteInput, {
100 threadId: "thread-1",
101 data: "echo hello\n",
102 }),
103 ).toBe(true);
104 });
105​
106 it("rejects empty data", () => {
107 expect(
108 decodes(TerminalWriteInput, {
109 threadId: "thread-1",
110 data: "",
111 }),
112 ).toBe(false);
113 });
114});
115​
116describe("TerminalThreadInput", () => {
117 it("trims thread ids", () => {
118 const parsed = decodeSync(TerminalThreadInput, { threadId: " thread-1 " });
119 expect(parsed.threadId).toBe("thread-1");
120 });
121});
122​
123describe("TerminalResizeInput", () => {
124 it("accepts valid size", () => {
125 expect(
126 decodes(TerminalResizeInput, {
127 threadId: "thread-1",
128 cols: 80,
129 rows: 24,
130 }),
131 ).toBe(true);
132 });
133});
134​
135describe("TerminalClearInput", () => {
136 it("defaults terminal id", () => {
137 const parsed = decodeSync(TerminalClearInput, {
138 threadId: "thread-1",
139 });
140 expect(parsed.terminalId).toBe(DEFAULT_TERMINAL_ID);
141 });
142});
143​
144describe("TerminalCloseInput", () => {
145 it("accepts optional deleteHistory", () => {
146 expect(
147 decodes(TerminalCloseInput, {
148 threadId: "thread-1",
149 deleteHistory: true,
150 }),
151 ).toBe(true);
152 });
153});
154​
155describe("TerminalSessionSnapshot", () => {
156 it("accepts running snapshots", () => {
157 expect(
158 decodes(TerminalSessionSnapshot, {
159 threadId: "thread-1",
160 terminalId: DEFAULT_TERMINAL_ID,
161 cwd: "/tmp/project",
162 worktreePath: null,
163 status: "running",
164 pid: 1234,
165 history: "hello\n",
166 exitCode: null,
167 exitSignal: null,
168 updatedAt: new Date().toISOString(),
169 }),
170 ).toBe(true);
171 });
172});
173​
174describe("TerminalEvent", () => {
175 it("accepts output events", () => {
176 expect(
177 decodes(TerminalEvent, {
178 type: "output",
179 threadId: "thread-1",
180 terminalId: DEFAULT_TERMINAL_ID,
181 createdAt: new Date().toISOString(),
182 data: "line\n",
183 }),
184 ).toBe(true);
185 });
186​
187 it("accepts exited events", () => {
188 expect(
189 decodes(TerminalEvent, {
190 type: "exited",
191 threadId: "thread-1",
192 terminalId: DEFAULT_TERMINAL_ID,
193 createdAt: new Date().toISOString(),
194 exitCode: 0,
195 exitSignal: null,
196 }),
197 ).toBe(true);
198 });
199​
200 it("accepts activity events", () => {
201 expect(
202 decodes(TerminalEvent, {
203 type: "activity",
204 threadId: "thread-1",
205 terminalId: DEFAULT_TERMINAL_ID,
206 createdAt: new Date().toISOString(),
207 hasRunningSubprocess: true,
208 }),
209 ).toBe(true);
210 });
211​
212 it("accepts started events with snapshot worktree metadata", () => {
213 expect(
214 decodes(TerminalEvent, {
215 type: "started",
216 threadId: "thread-1",
217 terminalId: DEFAULT_TERMINAL_ID,
218 createdAt: new Date().toISOString(),
219 snapshot: {
220 threadId: "thread-1",
221 terminalId: DEFAULT_TERMINAL_ID,
222 cwd: "/tmp/project/.t3/worktrees/feature-a",
223 worktreePath: "/tmp/project/.t3/worktrees/feature-a",
224 status: "running",
225 pid: 1234,
226 history: "",
227 exitCode: null,
228 exitSignal: null,
229 updatedAt: new Date().toISOString(),
230 },
231 }),
232 ).toBe(true);
233 });
234});