freshyo/apps/backend/tests/run-all.ts
2026-10-20 00:31:05 +05:30

149 lines
5.3 KiB
TypeScript

#!/usr/bin/env bun
/**
* TEST-ONLY resilient runner for the live API suite.
*
* Why this exists
* ---------------
* The whole suite in one jest process keeps one `wrangler dev` (workerd)
* instance alive for ~13-15 minutes. On a large local D1 that instance can hit
* V8's heap limit ("V8 fatal error ... Reached heap limit"), after which every
* remaining suite fails with "fetch failed".
*
* This runner:
* - splits the test files into chunks (default 10, round-robin by file size),
* - runs each chunk with `jest --runInBand`,
* - restarts the backend between chunks (resetting the workerd heap),
* - starts the backend if it is down, and fails fast if it cannot start.
*
* Seeds/D1 state persist across restarts. `tests/global-setup.ts` still runs per
* chunk and clears the KV order tracker.
*
* Usage (from apps/backend):
* bun run test:api:all
* CHUNK_SIZE=8 bun run test:api:all
*/
import { spawnSync } from 'node:child_process'
import { readdirSync, statSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'
const BACKEND_DIR = path.resolve(process.cwd())
const API_BASE_URL = (process.env.API_BASE_URL ?? 'http://localhost:8787').replace(/\/+$/, '')
const CHUNK_SIZE = Math.max(1, Number(process.env.CHUNK_SIZE ?? 10))
const SERVER_LOG = path.join(os.tmpdir(), 'freshyo-wrangler-run-all.log')
function listTests(): string[] {
const api = readdirSync(path.join(BACKEND_DIR, 'tests/api'))
.filter((file) => file.endsWith('.test.ts'))
.map((file) => `tests/api/${file}`)
const unit = readdirSync(path.join(BACKEND_DIR, 'tests/unit'))
.filter((file) => file.endsWith('.test.ts'))
.map((file) => `tests/unit/${file}`)
// Round-robin by file size so the slow suites are spread across chunks.
const bySize = [...api, ...unit].sort(
(a, b) => statSync(path.join(BACKEND_DIR, b)).size - statSync(path.join(BACKEND_DIR, a)).size
)
const chunks: string[][] = Array.from({ length: Math.ceil(bySize.length / CHUNK_SIZE) }, () => [])
bySize.forEach((file, index) => chunks[index % chunks.length].push(file))
return chunks.flat()
}
async function healthy(): Promise<boolean> {
try {
const res = await fetch(`${API_BASE_URL}/api/health`, { signal: AbortSignal.timeout(2_000) })
return res.ok
} catch {
return false
}
}
function killBackend(): void {
// Whatever is listening on 8787 (the backend workerd/wrangler).
const lsof = spawnSync('lsof', ['-ti', 'tcp:8787'], { encoding: 'utf8' })
for (const pid of (lsof.stdout ?? '').split(/\s+/).filter(Boolean)) {
try {
process.kill(Number(pid), 'SIGTERM')
} catch {
/* already gone */
}
}
// Backend wrangler supervisor (never touch other apps' wrangler processes).
const ps = spawnSync(
'bash',
[
'-lc',
"ps aux | grep 'wrangler-dist/cli.js dev' | grep 'apps/backend' | grep -v grep | awk '{print $2}'",
],
{ encoding: 'utf8' }
)
for (const pid of (ps.stdout ?? '').split(/\s+/).filter(Boolean)) {
try {
process.kill(Number(pid), 'SIGTERM')
} catch {
/* already gone */
}
}
}
async function waitForHealth(timeoutMs = 60_000): Promise<boolean> {
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
if (await healthy()) return true
await new Promise((resolve) => setTimeout(resolve, 1_000))
}
return false
}
async function startBackend(): Promise<void> {
console.log(`[run-all] starting backend (log: ${SERVER_LOG})`)
spawnSync('bash', ['-lc', `cd ${JSON.stringify(BACKEND_DIR)} && nohup bun run dev > ${JSON.stringify(SERVER_LOG)} 2>&1 &`], {
stdio: 'ignore',
})
if (!(await waitForHealth())) {
throw new Error(`[run-all] backend did not become healthy at ${API_BASE_URL}`)
}
}
async function restartBackend(): Promise<void> {
console.log('[run-all] restarting backend to reset the workerd heap…')
killBackend()
await new Promise((resolve) => setTimeout(resolve, 2_000))
await startBackend()
}
async function main(): Promise<void> {
const files = listTests()
const chunks: string[][] = []
for (let i = 0; i < files.length; i += CHUNK_SIZE) chunks.push(files.slice(i, i + CHUNK_SIZE))
console.log(`[run-all] ${files.length} files in ${chunks.length} chunks of <=${CHUNK_SIZE}`)
if (!(await healthy())) await startBackend()
const results: Array<{ chunk: number; files: number; code: number | null }> = []
for (let i = 0; i < chunks.length; i += 1) {
console.log(`\n[run-all] ===== chunk ${i + 1}/${chunks.length} (${chunks[i].length} files) =====`)
const run = spawnSync(
'bunx',
['jest', '--config', 'tests/jest.config.js', '--runInBand', ...chunks[i]],
{ cwd: BACKEND_DIR, stdio: 'inherit' }
)
results.push({ chunk: i + 1, files: chunks[i].length, code: run.status })
if (i < chunks.length - 1) await restartBackend()
}
console.log('\n[run-all] summary')
for (const result of results) {
console.log(` chunk ${result.chunk}: ${result.code === 0 ? 'PASS' : `FAIL (exit ${result.code})`} (${result.files} files)`)
}
const failed = results.filter((result) => result.code !== 0)
console.log(` ${results.length - failed.length}/${results.length} chunks passed`)
process.exit(failed.length === 0 ? 0 : 1)
}
main().catch((error) => {
console.error('[run-all] failed:', error)
process.exit(1)
})