@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
tsx 389 lines 11.5 KB
Raw
1 import { Fragment, useEffect, useRef, useState, type ReactNode } from "react";
2 import { Line, sleep, TermChrome, useSpinner } from "./_terminal";
3
4 /* ============================================================
5 Combined Dev + Test terminal. One TermChrome, two narratives.
6 Loops:
7 DEV — alchemy dev (boot → ready → edit → reload → request)
8 TEST — bun test (deploy → tests → destroy)
9 The badge + title swap when the mode flips, reinforcing
10 "same primitive, two modes" without doubling vertical space.
11 ============================================================ */
12
13 type Mode = "dev" | "test";
14
15 interface DevStep {
16 kind: "boot" | "ready" | "edit" | "reload" | "diff" | "wire" | "request";
17 label?: string;
18 detail?: string;
19 ms: number;
20 }
21
22 const DEV_STEPS: DevStep[] = [
23 { kind: "boot", label: "Photos", detail: "Cloudflare.R2.Bucket", ms: 600 },
24 {
25 kind: "boot",
26 label: "Sessions",
27 detail: "Cloudflare.KV.Namespace",
28 ms: 500,
29 },
30 {
31 kind: "boot",
32 label: "Api",
33 detail: "Cloudflare.Worker · local → workerd",
34 ms: 700,
35 },
36 { kind: "ready", ms: 700 },
37 { kind: "edit", label: "src/Api.ts", ms: 900 },
38 { kind: "reload", detail: "38ms", ms: 600 },
39 { kind: "request", label: "GET /object/hello.txt", detail: "200", ms: 900 },
40 { kind: "edit", label: "alchemy.run.ts", ms: 900 },
41 { kind: "diff", label: "Queue", detail: "Cloudflare.Queues.Queue", ms: 700 },
42 { kind: "wire", label: "Api → Queue.bind", ms: 700 },
43 { kind: "ready", ms: 1000 },
44 ];
45
46 const TEST_STAGE = "pr-1729";
47
48 interface BaseTestStep {
49 id: string;
50 label: string;
51 runMs: number;
52 }
53 interface PhaseTestStep extends BaseTestStep {
54 kind: "phase";
55 detail: string;
56 durSec: string;
57 url?: string;
58 }
59 interface UnitTestStep extends BaseTestStep {
60 kind: "test";
61 durMs: string;
62 }
63 type TestStep = PhaseTestStep | UnitTestStep;
64 type RunningTest = TestStep & { status: "running" | "done" };
65
66 const TEST_STEPS: TestStep[] = [
67 {
68 id: "deploy",
69 kind: "phase",
70 label: "deploy",
71 detail: "3 resources",
72 runMs: 1000,
73 durSec: "4.2",
74 url: `https://api.${TEST_STAGE}.workers.dev`,
75 },
76 {
77 id: "t1",
78 kind: "test",
79 label: "PUT + GET round-trips through R2",
80 runMs: 800,
81 durMs: "312",
82 },
83 {
84 id: "t2",
85 kind: "test",
86 label: "Room DO preserves state across requests",
87 runMs: 700,
88 durMs: "184",
89 },
90 {
91 id: "destroy",
92 kind: "phase",
93 label: "destroy",
94 detail: "3 resources",
95 runMs: 800,
96 durSec: "1.8",
97 },
98 ];
99
100 interface LogLine {
101 text: ReactNode;
102 key: string;
103 }
104
105 let _id = 0;
106
107 const DEV_ACCENT = "var(--alc-accent-bright)";
108 const TEST_ACCENT = "var(--alc-success)";
109
110 export default function DevTestTerminal() {
111 const [mode, setMode] = useState<Mode>("dev");
112
113 // Dev state
114 const [devLines, setDevLines] = useState<LogLine[]>([]);
115 const [devBusy, setDevBusy] = useState(false);
116
117 // Test state
118 const [cmd, setCmd] = useState("");
119 const [caret, setCaret] = useState(false);
120 const [testSteps, setTestSteps] = useState<RunningTest[]>([]);
121 const [summary, setSummary] = useState<{
122 tests: number;
123 secs: string;
124 } | null>(null);
125
126 const cancelRef = useRef(false);
127
128 useEffect(() => {
129 cancelRef.current = false;
130 const aborted = () => cancelRef.current;
131
132 const pushDev = (text: ReactNode) =>
133 setDevLines((ls) => [...ls, { text, key: `${++_id}` }].slice(-13));
134 const resetDev = () => setDevLines([]);
135
136 const typeCmd = async (text: string) => {
137 setCmd("");
138 setCaret(true);
139 for (let i = 1; i <= text.length; i++) {
140 if (aborted()) return;
141 setCmd(text.slice(0, i));
142 await sleep(34 + Math.random() * 22);
143 }
144 await sleep(160);
145 setCaret(false);
146 };
147
148 const runDev = async () => {
149 resetDev();
150 pushDev(
151 <>
152 <span style={{ color: "var(--alc-code-comment)" }}>$ </span>
153 <span>alchemy dev</span>
154 </>,
155 );
156 await sleep(280);
157 for (const step of DEV_STEPS) {
158 if (aborted()) return;
159 if (step.kind === "boot") {
160 setDevBusy(true);
161 await sleep(step.ms);
162 setDevBusy(false);
163 pushDev(
164 <>
165 <span style={{ color: "var(--alc-success)" }}>✓ </span>
166 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{step.label}</span>
167 <span style={{ color: "var(--alc-code-comment)" }}>{` (${step.detail})`}</span>
168 <span style={{ color: "var(--alc-success)" }}> created</span>
169 </>,
170 );
171 } else if (step.kind === "ready") {
172 await sleep(step.ms);
173 pushDev(
174 <>
175 <span style={{ color: "var(--alc-code-comment)" }}> → </span>
176 <span style={{ color: "var(--alc-accent-bright)" }}>http://localhost:1337</span>
177 </>,
178 );
179 pushDev(<span style={{ color: "var(--alc-code-comment)" }}>Watching for changes…</span>);
180 } else if (step.kind === "edit") {
181 await sleep(step.ms);
182 pushDev(
183 <>
184 <span style={{ color: "var(--alc-warn)" }}>↻ </span>
185 <span style={{ color: "var(--alc-code-comment)" }}>{step.label} changed</span>
186 </>,
187 );
188 } else if (step.kind === "reload") {
189 setDevBusy(true);
190 await sleep(step.ms);
191 setDevBusy(false);
192 pushDev(
193 <>
194 <span style={{ color: "var(--alc-success)" }}>✓ </span>
195 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>Api</span>
196 <span style={{ color: "var(--alc-code-comment)" }}>{` reloaded in `}</span>
197 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{step.detail}</span>
198 </>,
199 );
200 } else if (step.kind === "diff") {
201 await sleep(step.ms);
202 pushDev(
203 <>
204 <span style={{ color: "var(--alc-success)" }}>+ </span>
205 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{step.label}</span>
206 <span style={{ color: "var(--alc-code-comment)" }}>{` (${step.detail})`}</span>
207 <span style={{ color: "var(--alc-success)" }}> created</span>
208 </>,
209 );
210 } else if (step.kind === "wire") {
211 await sleep(step.ms);
212 pushDev(
213 <>
214 <span style={{ color: "var(--alc-warn)" }}>~ </span>
215 <span style={{ color: "var(--alc-code-comment)" }}>wired </span>
216 <span style={{ color: "var(--alc-code-type)" }}>{step.label}</span>
217 </>,
218 );
219 } else if (step.kind === "request") {
220 await sleep(step.ms);
221 pushDev(
222 <>
223 <span
224 style={{ color: "var(--alc-code-comment)" }}
225 >{`[${new Date().toLocaleTimeString().slice(0, 8)}] `}</span>
226 <span style={{ color: "var(--alc-fg-invert)" }}>{step.label}</span>
227 <span style={{ color: "var(--alc-success)" }}>{` ${step.detail}`}</span>
228 </>,
229 );
230 }
231 }
232 };
233
234 const runTest = async () => {
235 setTestSteps([]);
236 setSummary(null);
237 setCmd("");
238 await sleep(200);
239 await typeCmd("bun test");
240 if (aborted()) return;
241 await sleep(260);
242
243 const t0 = Date.now();
244 for (const s of TEST_STEPS) {
245 if (aborted()) return;
246 setTestSteps((arr) => [...arr, { ...s, status: "running" }]);
247 await sleep(s.runMs);
248 if (aborted()) return;
249 setTestSteps((arr) => arr.map((r) => (r.id === s.id ? { ...r, status: "done" } : r)));
250 await sleep(140);
251 }
252 if (aborted()) return;
253 const elapsed = ((Date.now() - t0) / 1000).toFixed(1);
254 setSummary({ tests: 2, secs: elapsed });
255 };
256
257 const loop = async () => {
258 while (!aborted()) {
259 setMode("dev");
260 await runDev();
261 if (aborted()) return;
262 await sleep(1600);
263
264 setMode("test");
265 await runTest();
266 if (aborted()) return;
267 await sleep(2400);
268 }
269 };
270
271 void loop();
272 return () => {
273 cancelRef.current = true;
274 };
275 }, []);
276
277 const devSpinner = useSpinner(devBusy);
278 const anyTestRunning = testSteps.some((s) => s.status === "running");
279 const testSpinner = useSpinner(anyTestRunning);
280
281 const accent = mode === "dev" ? DEV_ACCENT : TEST_ACCENT;
282 const title = mode === "dev" ? "~/my-app" : `ci · ${TEST_STAGE}`;
283 const badge = mode === "dev" ? "DEV" : "TEST";
284
285 return (
286 <TermChrome title={title} badge={badge} badgeColor={accent} maxLines={13}>
287 {mode === "dev" ? (
288 <>
289 {devLines.map((l) => (
290 <Line key={l.key}>{l.text}</Line>
291 ))}
292 {devBusy && (
293 <Line>
294 <span style={{ color: "var(--alc-code-comment)" }}>{devSpinner} </span>
295 </Line>
296 )}
297 </>
298 ) : (
299 <>
300 <Line>
301 <span style={{ color: accent }}>$ </span>
302 {cmd}
303 {caret && <span style={{ color: "var(--alc-fg-invert)" }}>▍</span>}
304 </Line>
305 {testSteps.length > 0 && (
306 <>
307 <Line> </Line>
308 {testSteps.map((s, i, arr) => renderTestStep(s, i, arr, accent, testSpinner))}
309 </>
310 )}
311 {summary && (
312 <>
313 <Line> </Line>
314 <Line>
315 <span
316 style={{
317 background: accent,
318 color: "var(--alc-bg-code)",
319 fontWeight: 700,
320 padding: "0 8px",
321 marginRight: 8,
322 }}
323 >
324 {" PASS "}
325 </span>
326 <span>{summary.tests} tests · </span>
327 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>
328 {summary.secs}s
329 </span>
330 </Line>
331 </>
332 )}
333 </>
334 )}
335 </TermChrome>
336 );
337 }
338
339 function renderTestStep(
340 s: RunningTest,
341 idx: number,
342 arr: RunningTest[],
343 accent: string,
344 spinner: string,
345 ) {
346 const isRunning = s.status === "running";
347 const icon = isRunning ? spinner : "✓";
348 const iconColor = isRunning ? "var(--alc-code-type)" : accent;
349
350 if (s.kind === "phase") {
351 return (
352 <Fragment key={s.id}>
353 <Line>
354 <span
355 style={{
356 color: iconColor,
357 width: "1.2em",
358 display: "inline-block",
359 }}
360 >
361 {icon}
362 </span>
363 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{s.label}</span>
364 {!isRunning ? (
365 <span style={{ color: "var(--alc-code-comment)" }}>
366 {` (${s.detail} · ${s.durSec}s)`}
367 </span>
368 ) : (
369 <span style={{ color: "var(--alc-code-comment)" }}>{` (${s.detail})`}</span>
370 )}
371 </Line>
372 {s.url && !isRunning && (
373 <Line>
374 <span style={{ color: "var(--alc-code-comment)" }}>{" → "}</span>
375 <span style={{ color: "var(--alc-code-comment)" }}>{s.url}</span>
376 </Line>
377 )}
378 {!isRunning && idx < arr.length - 1 && <Line> </Line>}
379 </Fragment>
380 );
381 }
382 return (
383 <Line key={s.id}>
384 <span style={{ color: iconColor, width: "1.2em", display: "inline-block" }}>{icon}</span>
385 <span style={{ color: "var(--alc-fg-invert)" }}>{s.label}</span>
386 {!isRunning && <span style={{ color: "var(--alc-code-comment)" }}>{` (${s.durMs}ms)`}</span>}
387 </Line>
388 );
389 }