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

test-examples.ts

322 lines10.1 KB

1const examples = [
2 "./examples/cloudflare-dev",
3 "./examples/cloudflare-worker",
4 "./examples/cloudflare-pr-package",
5 "./examples/cloudflare-worker-async",
6 "./examples/cloudflare-website-tanstack-start",
7 "./examples/cloudflare-tanstack-start-solid",
8 "./examples/cloudflare-neon-drizzle",
9 "./examples/cloudflare-vue",
10 "./examples/cloudflare-website-solidstart",
11 "./examples/cloudflare-foldkit",
12 "./examples/cloudflare-foldkit-ssr",
13 "./examples/cloudflare-octane",
14 "./examples/cloudflare-website-astro",
15 "./examples/cloudflare-website-foldkit",
16 "./examples/cloudflare-website-nextjs",
17 "./examples/cloudflare-website-nuxt",
18 "./examples/cloudflare-website-react-router",
19 "./examples/cloudflare-website-sveltekit",
20 "./examples/cloudflare-website-vite",
21 "./examples/cloudflare-website-waku",
22 "./examples/aws-dev",
23 // "./examples/aws-ecs",
24 "./examples/aws-lambda",
25 // Cloudfront is too slow
26 // "./examples/aws-website-astro",
27 // "./examples/aws-website-foldkit",
28 // "./examples/aws-website-nextjs",
29 // "./examples/aws-website-nuxt",
30 // "./examples/aws-website-react-router",
31 // "./examples/aws-website-solidstart",
32 // "./examples/aws-website-sveltekit",
33 // "./examples/aws-website-tanstack-start",
34 // "./examples/aws-website-vite",
35 // "./examples/aws-website-waku",
36 "./examples/fly-app",
37 "./examples/fly-service",
38 "./examples/fly-website-vite",
39 "./examples/hetzner-website-vite",
40 // Railway examples are gated out of test:examples for now: repeated
41 // Railway platform outages (deployment queue backlogs, e.g. the
42 // 2026-08-28 "Deployments slow to start" incident) and general
43 // flakiness working with Railway's API make these suites too
44 // unreliable for CI. Run them from the example directory with
45 // `bun test` when Railway is healthy.
46 // "./examples/railway-project",
47 // "./examples/railway-service",
48 // "./examples/railway-website-vite",
49 // Card-app Website composites (same apps as the AWS/Cloudflare
50 // *-website-* examples). Commented so CI does not provision extra
51 // sites; run them from the example directory with `bun test`.
52 // "./examples/fly-website-astro",
53 // "./examples/fly-website-foldkit",
54 // "./examples/fly-website-nextjs",
55 // "./examples/fly-website-nuxt",
56 // "./examples/fly-website-react-router",
57 // "./examples/fly-website-solidstart",
58 // "./examples/fly-website-sveltekit",
59 // "./examples/fly-website-tanstack-start",
60 // "./examples/fly-website-waku",
61 // "./examples/fly-website-vocs",
62 // "./examples/hetzner-website-astro",
63 // "./examples/hetzner-website-foldkit",
64 // "./examples/hetzner-website-nextjs",
65 // "./examples/hetzner-website-nuxt",
66 // "./examples/hetzner-website-react-router",
67 // "./examples/hetzner-website-solidstart",
68 // "./examples/hetzner-website-sveltekit",
69 // "./examples/hetzner-website-tanstack-start",
70 // "./examples/hetzner-website-waku",
71 // "./examples/hetzner-website-vocs",
72 // "./examples/railway-website-astro",
73 // "./examples/railway-website-foldkit",
74 // "./examples/railway-website-nextjs",
75 // "./examples/railway-website-nuxt",
76 // "./examples/railway-website-react-router",
77 // "./examples/railway-website-solidstart",
78 // "./examples/railway-website-sveltekit",
79 // "./examples/railway-website-tanstack-start",
80 // "./examples/railway-website-waku",
81 // "./examples/railway-website-vocs",
82 "./examples/fly-sprite",
83 "./examples/fly-redis",
84 "./examples/fly-bucket",
85 "./examples/fly-postgres",
86] as const;
87​
88type CommandResult = {
89 label: string;
90 command: readonly string[];
91 exitCode: number | null;
92 stdout: string;
93 stderr: string;
94};
95​
96type TaskState = {
97 label: string;
98 command: readonly string[];
99 cwd?: string;
100 status: "pending" | "running" | "ok" | "failed";
101 startedAt?: number;
102 endedAt?: number;
103 exitCode?: number | null;
104};
105​
106const readStream = async (
107 stream: ReadableStream<Uint8Array>,
108): Promise<string> => new Response(stream).text();
109​
110const elapsedSeconds = (state: TaskState): string => {
111 if (state.startedAt === undefined) {
112 return "0s";
113 }
114 const endedAt = state.endedAt ?? performance.now();
115 return `${Math.round((endedAt - state.startedAt) / 1000)}s`;
116};
117​
118const makeStatusRenderer = (states: readonly TaskState[]) => {
119 const interactive = process.stdout.isTTY === true;
120 let renderedRows = 0;
121​
122 const taskLine = (state: TaskState) => {
123 const icon =
124 state.status === "ok"
125 ? "ok"
126 : state.status === "failed"
127 ? "failed"
128 : state.status;
129 const exit =
130 state.exitCode === undefined || state.exitCode === 0
131 ? ""
132 : ` exit ${state.exitCode ?? "signal"}`;
133 return ` ${icon.padEnd(7)} ${state.label} ${elapsedSeconds(state)}${exit}`;
134 };
135​
136 // Physical rows a line occupies once the terminal wraps it.
137 const rowsFor = (line: string) => {
138 const columns = process.stdout.columns || 80;
139 return Math.max(1, Math.ceil(line.length / columns));
140 };
141 const rowsForAll = (output: readonly string[]) =>
142 output.reduce((total, line) => total + rowsFor(line), 0);
143​
144 const fullLines = () => ["Example tests", ...states.map(taskLine)];
145​
146 // The in-place repaint moves the cursor up with `\x1b[NF`, which cannot
147 // climb above the top of the viewport: if a paint is taller than the
148 // terminal (or lines wrap), the write scrolls and every repaint leaks its
149 // topmost rows into scrollback. Keep each paint strictly shorter than the
150 // viewport by collapsing finished tasks into the header once space runs
151 // out (`+ 1` accounts for the cursor parking on the row below the paint).
152 const lines = () => {
153 const rows = process.stdout.rows || 24;
154 const full = fullLines();
155 if (rowsForAll(full) + 1 <= rows) {
156 return full;
157 }
158 const done = states.filter((state) => state.status === "ok").length;
159 const active = states.filter((state) => state.status !== "ok");
160 const header = `Example tests (${done}/${states.length} ok)`;
161 const activeLines = active.map(taskLine);
162 let shown = activeLines.length;
163 const fits = (count: number) => {
164 const overflow = count < activeLines.length ? 1 : 0;
165 return (
166 rowsForAll([header, ...activeLines.slice(0, count)]) + overflow + 1 <=
167 rows
168 );
169 };
170 while (shown > 0 && !fits(shown)) {
171 shown--;
172 }
173 return shown === activeLines.length
174 ? [header, ...activeLines]
175 : [
176 header,
177 ...activeLines.slice(0, shown),
178 ` … ${activeLines.length - shown} more`,
179 ];
180 };
181​
182 return {
183 render() {
184 if (!interactive) {
185 return;
186 }
187 if (renderedRows > 0) {
188 process.stdout.write(`\x1b[${renderedRows}F\x1b[J`);
189 }
190 const output = lines();
191 process.stdout.write(`${output.join("\n")}\n`);
192 renderedRows = rowsForAll(output);
193 },
194 finish() {
195 if (interactive && renderedRows > 0) {
196 process.stdout.write(`\x1b[${renderedRows}F\x1b[J`);
197 renderedRows = 0;
198 }
199 // Always end with the full table (the live view may be compacted).
200 for (const line of fullLines()) {
201 console.log(line);
202 }
203 },
204 };
205};
206​
207const run = async (
208 state: TaskState,
209 render: () => void,
210): Promise<CommandResult> => {
211 state.status = "running";
212 state.startedAt = performance.now();
213 render();
214​
215 const child = Bun.spawn([...state.command], {
216 cwd: state.cwd,
217 // Snapshot env at spawn time — `Bun.spawn` does not pick up later
218 // `process.env` mutations, and we want `ALCHEMY_PROFILE` (and friends)
219 // from the parent invocation.
220 env: { ...process.env },
221 stdout: "pipe",
222 stderr: "pipe",
223 stdin: "inherit",
224 });
225 const [exitCode, stdout, stderr] = await Promise.all([
226 child.exited,
227 readStream(child.stdout),
228 readStream(child.stderr),
229 ]);
230​
231 state.status = exitCode === 0 ? "ok" : "failed";
232 state.exitCode = exitCode;
233 state.endedAt = performance.now();
234 render();
235​
236 return {
237 label: state.label,
238 command: state.command,
239 exitCode,
240 stdout,
241 stderr,
242 };
243};
244​
245const runParallel = async (
246 tasks: readonly {
247 label: string;
248 command: readonly string[];
249 cwd?: string;
250 }[],
251): Promise<readonly CommandResult[]> => {
252 const states = tasks.map((task): TaskState => ({
253 ...task,
254 status: "pending",
255 }));
256 const renderer = makeStatusRenderer(states);
257 renderer.render();
258 const interval = setInterval(() => renderer.render(), 1000);
259​
260 try {
261 return await Promise.all(
262 states.map((state) => run(state, () => renderer.render())),
263 );
264 } finally {
265 clearInterval(interval);
266 renderer.finish();
267 }
268};
269​
270const testResults = await runParallel(
271 examples.map((example) => ({
272 label: example,
273 // Run `bun test` IN the example directory. Concurrent
274 // `bun run --filter <workspace> test` all contend on bun's workspace
275 // graph (each sits in `openat` walking the monorepo) and none of them
276 // ever spawn the actual test process.
277 command: ["bun", "test"] as const,
278 cwd: example,
279 })),
280);
281const failedTests = testResults.filter((result) => result.exitCode !== 0);
282​
283if (failedTests.length > 0) {
284 console.error("\nFailed example tests:");
285 for (const failure of failedTests) {
286 const exit = failure.exitCode === null ? "signal" : failure.exitCode;
287 console.error(
288 `- ${failure.label} (exit ${exit}): ${failure.command.join(" ")}`,
289 );
290 }
291​
292 for (const failure of failedTests) {
293 console.error(`\n--- ${failure.label} stdout ---`);
294 if (failure.stdout.length > 0) {
295 console.error(failure.stdout.trimEnd());
296 } else {
297 console.error("(empty)");
298 }
299​
300 console.error(`\n--- ${failure.label} stderr ---`);
301 if (failure.stderr.length > 0) {
302 console.error(failure.stderr.trimEnd());
303 } else {
304 console.error("(empty)");
305 }
306 }
307 process.exit(1);
309​
310const [formatFailure] = await runParallel([
311 { label: "format", command: ["bun", "run", "format"] },
312]);
313if (formatFailure.exitCode !== 0) {
314 console.error("\nFormat failed:");
315 if (formatFailure.stdout.length > 0) {
316 console.error(formatFailure.stdout.trimEnd());
317 }
318 if (formatFailure.stderr.length > 0) {
319 console.error(formatFailure.stderr.trimEnd());
320 }
321 process.exit(formatFailure.exitCode ?? 1);