forked from browserless/browserless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.integration.setup.ts
More file actions
460 lines (409 loc) · 18 KB
/
vitest.integration.setup.ts
File metadata and controls
460 lines (409 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* vitest globalSetup: auto-build and start browserless for integration tests.
*
* Local mode (default): kills ALL stale node build/index processes, builds via
* tsc, spawns a fresh server with env vars loaded via Vite loadEnv, and verifies
* OUR spawned PID owns port 3000 before tests run.
*
* Remote mode (TEST_ENV=prod): skips build/spawn, health-checks the remote
* endpoint from BROWSERLESS_ENDPOINT env var.
*
* IMPORTANT: env-cmd is NOT used — it silently fails on .env.dev files (treats
* .dev extension as a JSON RC file). All env vars come from Vite loadEnv.
*
* IMPORTANT: `node --watch` dev servers race with our spawned server. After tsc
* rebuilds `build/index.js`, any running `--watch` process auto-restarts and
* grabs port 3000 before our spawn. killByName() kills ALL matching processes
* regardless of port binding state. See: 2026-03-04 zombie process incident.
*/
import { execFileSync, spawn, type ChildProcess } from "node:child_process";
import { createWriteStream, existsSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { loadEnv } from "vite";
// Load .env.{TEST_ENV} into process.env — globalSetup doesn't receive vitest's test.env
// MUST use import.meta.dirname (not process.cwd()) — cwd may differ from project root
const testEnvMode = process.env.TEST_ENV || "dev";
const envVars = loadEnv(testEnvMode, import.meta.dirname, "");
Object.assign(process.env, envVars);
console.log(
`[globalSetup] Loaded ${Object.keys(envVars).length} env vars from .env.${testEnvMode}`,
);
async function killPort(port: number): Promise<void> {
try {
const stdout = execFileSync("lsof", ["-ti", `:${port}`], { encoding: "utf-8" });
const pids = stdout.trim().split("\n").filter(Boolean);
if (pids.length === 0) return;
console.log(`[globalSetup] Killing stale process(es) on port ${port}: ${pids.join(", ")}`);
for (const pid of pids) {
process.kill(Number(pid), "SIGTERM");
}
// Wait for graceful shutdown, then SIGKILL survivors
await new Promise((r) => setTimeout(r, 1000));
for (const pid of pids) {
try {
process.kill(Number(pid), "SIGKILL");
} catch {
/* already dead */
}
}
} catch {
// No process on port — nothing to kill
}
}
/**
* Kill ALL node processes matching `build/index.js` — regardless of port state.
*
* killPort() only finds processes already LISTENING on :3000. A `node --watch`
* process that just restarted (after tsc rebuilt build/index.js) may not have
* bound the port yet — it races with our spawn. This function catches those
* in-flight zombies by matching the process command line.
*
* Skips our own PID to avoid self-termination.
*/
async function killByName(): Promise<void> {
try {
const stdout = execFileSync("pgrep", ["-f", "node.*build/index\\.js"], { encoding: "utf-8" });
const pids = stdout.trim().split("\n").filter(Boolean).map(Number);
const ownPid = process.pid;
const toKill = pids.filter((pid) => pid !== ownPid);
if (toKill.length === 0) return;
console.log(`[globalSetup] Killing stale node build/index processes: ${toKill.join(", ")}`);
for (const pid of toKill) {
try {
process.kill(pid, "SIGTERM");
} catch {
/* already dead */
}
}
// Wait for graceful shutdown, then SIGKILL survivors
await new Promise((r) => setTimeout(r, 1000));
for (const pid of toKill) {
try {
process.kill(pid, "SIGKILL");
} catch {
/* already dead */
}
}
} catch {
// pgrep returns exit code 1 when no processes match — expected
}
}
const PORT = 3000;
const HEALTH_URL = `http://127.0.0.1:${PORT}/json/version`;
const BROWSERLESS_HTTP = `http://127.0.0.1:${PORT}`;
const REPLAY_HTTP = process.env.REPLAY_INGEST_URL;
if (!REPLAY_HTTP) {
throw new Error("REPLAY_INGEST_URL env var required — set in .env.dev or .env.prod");
}
// Worktree-aware: if real npm dependencies don't exist here (git worktree),
// resolve to the main repo root. Check for a real package (effect) instead of
// just node_modules/ — vitest creates node_modules/.vite cache in the CWD,
// which would fool a bare existsSync('node_modules') check.
function resolveBrowserlessDir(): string {
const dir = path.resolve(import.meta.dirname);
if (existsSync(path.join(dir, "node_modules", "effect"))) return dir;
try {
const output = execFileSync("git", ["worktree", "list", "--porcelain"], {
cwd: dir,
encoding: "utf8",
});
const match = output.match(/^worktree (.+)$/m);
if (match && existsSync(path.join(match[1], "node_modules", "effect"))) {
console.log(`[globalSetup] Worktree detected — using main repo: ${match[1]}`);
return match[1];
}
} catch {
/* not a git repo */
}
return dir;
}
const BROWSERLESS_DIR = resolveBrowserlessDir();
const MAX_WAIT_MS = 30_000;
const POLL_INTERVAL_MS = 500;
const RESULTS_FILE = path.join(tmpdir(), "cf-integration-results.jsonl");
async function isRunning(): Promise<boolean> {
try {
const res = await fetch(HEALTH_URL);
return res.ok;
} catch {
return false;
}
}
async function waitForReady(timeoutMs: number): Promise<void> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (await isRunning()) return;
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
}
// ── Diagnostic dump on startup failure ─────────────────────────────
// Collect everything needed to diagnose WHY the server didn't start.
// Without this, vitest shows "No test files found" — zero signal.
const diagnostics: string[] = [`Browserless did not start within ${timeoutMs}ms`];
// Server process state
if (serverProcess) {
diagnostics.push(` PID: ${serverProcess.pid ?? "unknown"}`);
diagnostics.push(` exitCode: ${serverProcess.exitCode}`);
diagnostics.push(` killed: ${serverProcess.killed}`);
diagnostics.push(` signalCode: ${serverProcess.signalCode}`);
}
// Port ownership
try {
const lsofOut = execFileSync("lsof", ["-ti", `:${PORT}`], { encoding: "utf-8" });
diagnostics.push(` Port ${PORT} owners: ${lsofOut.trim().replace(/\n/g, ", ")}`);
} catch {
diagnostics.push(` Port ${PORT}: no process listening`);
}
// Server log tail — last 20 lines
const logPath = path.join(BROWSERLESS_DIR, "test-server.log");
if (existsSync(logPath)) {
const log = readFileSync(logPath, "utf-8");
const lines = log.trim().split("\n");
const tail = lines.slice(-20).join("\n");
diagnostics.push(` Server log (last ${Math.min(20, lines.length)} lines):\n${tail}`);
} else {
diagnostics.push(" Server log: file not found");
}
const message = diagnostics.join("\n");
// Print to stderr so it's visible even if vitest swallows the error
console.error(`\n[globalSetup] STARTUP FAILURE DIAGNOSTICS:\n${message}\n`);
throw new Error(message);
}
let serverProcess: ChildProcess | null = null;
export async function setup() {
// Clear results file from previous run
writeFileSync(RESULTS_FILE, "");
// Check proxy — fail fast before any build/spawn work
if (!process.env.LOCAL_MOBILE_PROXY) {
throw new Error(
"LOCAL_MOBILE_PROXY env var required. Run with:\n" +
" npx vitest run --config vitest.integration.config.ts",
);
}
// Validate proxy connectivity — env var existing != proxy working.
// Without this, tests run with a dead proxy and produce confusing
// "0 CF markers" failures instead of a clear "proxy down" error.
// Route a real request THROUGH the proxy via Node 24 native env proxy (NODE_USE_ENV_PROXY).
const proxyUrl = process.env.LOCAL_MOBILE_PROXY;
const proxyHost = proxyUrl.replace(/^https?:\/\/(?:[^@]+@)?/, "").replace(/\/$/, "");
console.log(`[globalSetup] Validating proxy connectivity: ${proxyHost}...`);
process.env.NODE_USE_ENV_PROXY = "1";
process.env.HTTP_PROXY = proxyUrl;
try {
const proxyCheck = await fetch("http://httpbin.org/ip", {
signal: AbortSignal.timeout(10_000),
});
if (!proxyCheck.ok) throw new Error(`HTTP ${proxyCheck.status}`);
const body = (await proxyCheck.json()) as { origin?: string };
console.log(`[globalSetup] Proxy working (exit IP: ${body.origin ?? "unknown"})`);
} catch (err) {
throw new Error(
`Proxy not working at ${proxyHost}. Integration tests require a working proxy.\n` +
` Verify the proxy server is running before running integration tests.`,
);
} finally {
// Clean up — don't leave env proxy on for the rest of the test run
delete process.env.NODE_USE_ENV_PROXY;
delete process.env.HTTP_PROXY;
}
// Remote mode — skip build + spawn, just verify reachability
const isRemote = !!process.env.BROWSERLESS_ENDPOINT;
if (isRemote) {
const endpoint = process.env.BROWSERLESS_ENDPOINT!;
console.log(`[globalSetup] Using remote browserless: ${endpoint}`);
const healthUrl = `${endpoint}/json/version`;
const res = await fetch(healthUrl).catch(() => null);
if (!res?.ok) throw new Error(`Remote browserless not reachable at ${healthUrl}`);
console.log("[globalSetup] Remote browserless is healthy");
return;
}
// ── Kill ALL stale browserless processes ────────────────────────────
// Two strategies: by port (catches anything listening on :3000) AND by
// process name (catches `node --watch` zombies that haven't bound yet).
// Both run before AND after tsc — tsc rebuilds trigger --watch restarts.
await killPort(PORT);
await killByName();
// Always build — ensures build/ reflects latest source edits
console.log("[globalSetup] Building browserless (tsc)...");
const buildStart = Date.now();
execFileSync("npx", ["tsc"], { cwd: BROWSERLESS_DIR, stdio: "inherit" });
console.log(`[globalSetup] Build done (${Date.now() - buildStart}ms)`);
// Always rebuild rrweb Chrome extension — the recorder.ts source may have
// changed but tsc doesn't trigger the esbuild bundle step. Stale bundles
// cause zero-event replays (extension double-init guard regression).
console.log("[globalSetup] Building rrweb extension...");
execFileSync("bun", ["extensions/replay/build.js"], { cwd: BROWSERLESS_DIR, stdio: "inherit" });
// Kill AGAIN after build — tsc file changes trigger `node --watch` restarts
await killPort(PORT);
await killByName();
console.log("[globalSetup] Starting browserless...");
// MUST use node (not bun — bun breaks WS proxying).
// Env vars come from Vite loadEnv (line 19), NOT env-cmd.
serverProcess = spawn("node", ["build/index.js"], {
cwd: BROWSERLESS_DIR,
env: { ...process.env, PORT: String(PORT), HOST: "0.0.0.0", TEST_TRACE_COLLECT: "1" },
stdio: ["ignore", "pipe", "pipe"],
});
const spawnedPid = serverProcess.pid;
console.log(`[globalSetup] Spawned server PID: ${spawnedPid}`);
// Redirect server output to log file — keeps test output clean
const logPath = path.join(BROWSERLESS_DIR, "test-server.log");
const logStream = createWriteStream(logPath);
serverProcess.stdout?.pipe(logStream);
serverProcess.stderr?.pipe(logStream);
console.log(`[globalSetup] Server logs: ${logPath}`);
serverProcess.on("error", (err) => {
throw new Error(`Failed to spawn browserless: ${err.message}`);
});
serverProcess.on("exit", (code, signal) => {
if (code && code !== 0) {
// Dump server log on crash — visible to any agent reading stdout
const logPath = path.join(BROWSERLESS_DIR, "test-server.log");
let logTail = "";
if (existsSync(logPath)) {
const lines = readFileSync(logPath, "utf-8").trim().split("\n");
logTail = lines.slice(-10).join("\n");
}
console.error(
`[globalSetup] Browserless crashed!\n` +
` exit code: ${code}, signal: ${signal}\n` +
` Server log (last 10 lines):\n${logTail}`,
);
}
});
await waitForReady(MAX_WAIT_MS);
// ── PID ownership verification ─────────────────────────────────────
// After the port is ready, verify OUR spawned PID owns it — not a zombie.
// This catches the exact scenario from 2026-03-04: a stale `node --watch`
// process grabs port 3000, our server fails to bind (but doesn't crash),
// and all connections go to the wrong process.
if (spawnedPid) {
try {
const lsofOut = execFileSync("lsof", ["-ti", `:${PORT}`], { encoding: "utf-8" });
const portPids = lsofOut.trim().split("\n").filter(Boolean).map(Number);
if (!portPids.includes(spawnedPid)) {
// Our PID is NOT on the port — a zombie stole it
const portOwners = portPids.join(", ");
throw new Error(
`Port ${PORT} owned by PID(s) ${portOwners}, but we spawned PID ${spawnedPid}. ` +
`A stale process is intercepting connections. ` +
`Kill all: pkill -f "node.*build/index"`,
);
}
} catch (err) {
if (err instanceof Error && err.message.includes("owned by PID")) throw err;
// lsof failed — non-fatal, server is responding to health check
}
}
console.log("[globalSetup] Browserless ready");
}
export async function teardown() {
// ── Print summary table ──────────────────────────────────────────
// Runs in the main vitest process → process.stdout.write goes
// directly to the terminal, no worker interception, no escape stripping.
printSummaryTable();
if (!serverProcess) return; // didn't spawn (remote mode)
serverProcess.kill("SIGTERM");
// Wait up to 5s for graceful shutdown, then SIGKILL
await new Promise<void>((resolve) => {
const timeout = setTimeout(() => {
serverProcess?.kill("SIGKILL");
resolve();
}, 5000);
serverProcess!.on("exit", () => {
clearTimeout(timeout);
resolve();
});
});
// Belt-and-suspenders: kill anything still on the port (child processes, etc.)
await killPort(PORT);
}
// ── Summary table (printed from main process) ──────────────────────
interface SiteResult {
name: string;
summary: {
label: string;
type: string;
method: string;
signal?: string;
durationMs?: number;
} | null;
replayId: string | null;
durationMs: number;
status: "PASS" | "FAIL" | "SKIP";
error?: string;
}
function printSummaryTable() {
if (!existsSync(RESULTS_FILE)) return;
const raw = readFileSync(RESULTS_FILE, "utf-8").trim();
if (!raw) return;
const results: SiteResult[] = raw.split("\n").map((line) => JSON.parse(line));
if (results.length === 0) return;
const out = process.stdout;
// OSC 8 terminal hyperlink: \e]8;;URL\e\\TEXT\e]8;;\e\\
const link = (url: string, text: string) => `\x1b]8;;${url}\x1b\\${text}\x1b]8;;\x1b\\`;
// Colors
const green = (s: string) => `\x1b[32m${s}\x1b[0m`;
const red = (s: string) => `\x1b[31m${s}\x1b[0m`;
const dim = (s: string) => `\x1b[2m${s}\x1b[0m`;
out.write("\n");
out.write(
"╔════════════════╤═════════╤══════════════╤═════════════════╤════════════════╤══════════╤════════╗\n",
);
out.write(
"║ Site │ Summary │ Type │ Method │ Signal │ Duration │ Replay ║\n",
);
out.write(
"╠════════════════╪═════════╪══════════════╪═════════════════╪════════════════╪══════════╪════════╣\n",
);
for (const r of results) {
const s = r.summary;
const rawLabel = s?.label ?? (r.status === "SKIP" ? "SKIP" : "FAIL");
const type = s?.type ?? "";
const method = s?.method ?? "";
const signal = s?.signal ?? "";
const dur = s?.durationMs != null ? `${(s.durationMs / 1000).toFixed(1)}s` : "";
// Color the summary label — red for failures even if solver produced a label
let label: string;
if (r.status === "FAIL") {
label = red(rawLabel.padEnd(7));
} else if (rawLabel.includes("✓") || rawLabel.includes("→")) {
label = green(rawLabel.padEnd(7));
} else if (rawLabel === "SKIP") {
label = dim(rawLabel.padEnd(7));
} else {
label = red(rawLabel.padEnd(7));
}
// Replay column — clickable OSC 8 link
let replayCell: string;
if (r.replayId) {
const replayUrl = `${REPLAY_HTTP}/replay/${r.replayId}`;
replayCell = link(replayUrl, "replay");
} else {
replayCell = dim(" -- ");
}
out.write(
`║ ${r.name.padEnd(14)} │ ${label} │ ${type.padEnd(12)} │ ${method.padEnd(15)} │ ${signal.padEnd(14)} │ ${dur.padEnd(8)} │ ${replayCell} ║\n`,
);
}
out.write(
"╚════════════════╧═════════╧══════════════╧═════════════════╧════════════════╧══════════╧════════╝\n",
);
// Print failure reasons
const failures = results.filter((r) => r.status === "FAIL" && r.error);
if (failures.length > 0) {
out.write("\n");
for (const f of failures) {
out.write(` ${red("✗")} ${f.name}: ${f.error}\n`);
}
}
// Pass/fail counts
const passed = results.filter((r) => r.status === "PASS").length;
const failed = results.filter((r) => r.status === "FAIL").length;
const skipped = results.filter((r) => r.status === "SKIP").length;
const parts: string[] = [];
if (passed > 0) parts.push(green(`${passed} passed`));
if (failed > 0) parts.push(red(`${failed} failed`));
if (skipped > 0) parts.push(dim(`${skipped} skipped`));
out.write(`\n${parts.join(", ")} out of ${results.length} sites\n\n`);
}