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

terminal.test.ts

208 lines4.6 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 cols: 100,
67 rows: 24,
68 env: {
69 T3CODE_PROJECT_ROOT: "/tmp/project",
70 CUSTOM_FLAG: "1",
71 },
72 });
73 expect(parsed.env).toMatchObject({
74 T3CODE_PROJECT_ROOT: "/tmp/project",
75 CUSTOM_FLAG: "1",
76 });
77 });
78​
79 it("rejects invalid env keys", () => {
80 expect(
81 decodes(TerminalOpenInput, {
82 threadId: "thread-1",
83 cwd: "/tmp/project",
84 cols: 100,
85 rows: 24,
86 env: {
87 "bad-key": "1",
88 },
89 }),
90 ).toBe(false);
91 });
92});
93​
94describe("TerminalWriteInput", () => {
95 it("accepts non-empty data", () => {
96 expect(
97 decodes(TerminalWriteInput, {
98 threadId: "thread-1",
99 data: "echo hello\n",
100 }),
101 ).toBe(true);
102 });
103​
104 it("rejects empty data", () => {
105 expect(
106 decodes(TerminalWriteInput, {
107 threadId: "thread-1",
108 data: "",
109 }),
110 ).toBe(false);
111 });
112});
113​
114describe("TerminalThreadInput", () => {
115 it("trims thread ids", () => {
116 const parsed = decodeSync(TerminalThreadInput, { threadId: " thread-1 " });
117 expect(parsed.threadId).toBe("thread-1");
118 });
119});
120​
121describe("TerminalResizeInput", () => {
122 it("accepts valid size", () => {
123 expect(
124 decodes(TerminalResizeInput, {
125 threadId: "thread-1",
126 cols: 80,
127 rows: 24,
128 }),
129 ).toBe(true);
130 });
131});
132​
133describe("TerminalClearInput", () => {
134 it("defaults terminal id", () => {
135 const parsed = decodeSync(TerminalClearInput, {
136 threadId: "thread-1",
137 });
138 expect(parsed.terminalId).toBe(DEFAULT_TERMINAL_ID);
139 });
140});
141​
142describe("TerminalCloseInput", () => {
143 it("accepts optional deleteHistory", () => {
144 expect(
145 decodes(TerminalCloseInput, {
146 threadId: "thread-1",
147 deleteHistory: true,
148 }),
149 ).toBe(true);
150 });
151});
152​
153describe("TerminalSessionSnapshot", () => {
154 it("accepts running snapshots", () => {
155 expect(
156 decodes(TerminalSessionSnapshot, {
157 threadId: "thread-1",
158 terminalId: DEFAULT_TERMINAL_ID,
159 cwd: "/tmp/project",
160 status: "running",
161 pid: 1234,
162 history: "hello\n",
163 exitCode: null,
164 exitSignal: null,
165 updatedAt: new Date().toISOString(),
166 }),
167 ).toBe(true);
168 });
169});
170​
171describe("TerminalEvent", () => {
172 it("accepts output events", () => {
173 expect(
174 decodes(TerminalEvent, {
175 type: "output",
176 threadId: "thread-1",
177 terminalId: DEFAULT_TERMINAL_ID,
178 createdAt: new Date().toISOString(),
179 data: "line\n",
180 }),
181 ).toBe(true);
182 });
183​
184 it("accepts exited events", () => {
185 expect(
186 decodes(TerminalEvent, {
187 type: "exited",
188 threadId: "thread-1",
189 terminalId: DEFAULT_TERMINAL_ID,
190 createdAt: new Date().toISOString(),
191 exitCode: 0,
192 exitSignal: null,
193 }),
194 ).toBe(true);
195 });
196​
197 it("accepts activity events", () => {
198 expect(
199 decodes(TerminalEvent, {
200 type: "activity",
201 threadId: "thread-1",
202 terminalId: DEFAULT_TERMINAL_ID,
203 createdAt: new Date().toISOString(),
204 hasRunningSubprocess: true,
205 }),
206 ).toBe(true);
207 });
208});