mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 19:47:25 +01:00
Addresses the Test/Quality panel finding that only ~12 of ~32 test:* scripts ran automatically and a newly-added test:* was born dead (never in CI): - scripts/test-unit.ts globs scripts/test/*.ts and runs every pure test in parallel. New pure tests are picked up for free. Infra-dependent tests are skipped with an explicit reason (server/DB/Groq/embeddings). Classification was verified under true CI conditions (.env.local hidden + dead DATABASE_URL, since dotenvx otherwise injects a live DB and masks the dependency): 29/29 pure tests green, 6 correctly skipped. - ci.yml and .husky/pre-push both now run `npm run test:unit` (+ test:home), so a green push and a green CI run the same suite and can't diverge. - Runner avoids top-level await (tsx→CJS rejects it), uses the local tsx bin at concurrency 2 (esbuild-friendly on a 2-core box), and handles spawn errors. NOT done (needs your decision — flagged): required-status-checks branch protection would REJECT direct pushes to main, breaking the push-to-main-deploy flow, unless the deploy also moves into CI (needs the box SSH key as a GitHub secret — your call). `next build` in CI still needs a Postgres service to pre-render; deferred (deploy-time build + the new rollback already gate it). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
87 lines
4.0 KiB
TypeScript
87 lines
4.0 KiB
TypeScript
// Discover-and-run every pure unit test under scripts/test/*.ts in parallel.
|
|
//
|
|
// Why this exists: the ~30 test:* scripts were wired one-by-one into ci.yml /
|
|
// pre-push, so only ~12 ran automatically and a newly-added test:* was born
|
|
// dead (never run by CI). This runner globs the directory, so any new pure
|
|
// test is picked up for free. Infra-dependent tests are explicitly skipped
|
|
// (with a reason) — they run where their infra exists, not in this suite.
|
|
//
|
|
// Environment-independent by construction: every test listed here passes with
|
|
// NO DATABASE_URL and no running server (verified), so it behaves identically
|
|
// on a laptop and in CI. Run: npx tsx scripts/test-unit.ts
|
|
import { readdirSync, existsSync } from "fs";
|
|
import { join, dirname } from "path";
|
|
import { fileURLToPath } from "url";
|
|
import { spawn } from "child_process";
|
|
|
|
const SCRIPTS_DIR = dirname(fileURLToPath(import.meta.url));
|
|
const TEST_DIR = join(SCRIPTS_DIR, "test");
|
|
// Invoke the local tsx binary directly (not `npx tsx`): npx adds resolution
|
|
// overhead and a second esbuild service per child, which thrashes esbuild on a
|
|
// 2-core box. .bin/tsx is one process per test.
|
|
const TSX_BIN = join(SCRIPTS_DIR, "..", "node_modules", ".bin", "tsx");
|
|
|
|
// Files under scripts/test/ that are NOT part of the pure suite. Keep the
|
|
// reason — this list is the honest boundary between unit tests and the ones
|
|
// that need real infra (they stay wired where that infra is available).
|
|
const SKIP: Record<string, string> = {
|
|
"print-session-token.ts": "helper — prints a token, not a test",
|
|
"authenticated-smoke.ts": "needs a running server + FLEETCROWN_SESSION_TOKEN (pre-push/prod dogfood)",
|
|
"rag-retrieval.ts": "needs EMBEDDINGS_BASE_URL (fastembed service)",
|
|
"push-notifications.ts": "needs push/web-push env — run manually",
|
|
"inject-prompt.ts": "needs a live DB (only passes locally via .env.local)",
|
|
"verify-project-brief.ts": "needs a live DB + Groq API (network + GROQ_API_KEY)",
|
|
};
|
|
|
|
const MAX_PARALLEL = Number(process.env.TEST_UNIT_PARALLEL ?? 2);
|
|
|
|
const files = readdirSync(TEST_DIR)
|
|
.filter((f) => f.endsWith(".ts") && !(f in SKIP))
|
|
.sort();
|
|
|
|
const skipped = Object.entries(SKIP);
|
|
if (skipped.length) {
|
|
console.log(`↷ skipping ${skipped.length} infra-dependent test(s):`);
|
|
for (const [f, why] of skipped) console.log(` ${f} — ${why}`);
|
|
}
|
|
console.log(`→ running ${files.length} unit tests (parallel ${MAX_PARALLEL})\n`);
|
|
|
|
const runner = existsSync(TSX_BIN) ? TSX_BIN : "npx";
|
|
const baseArgs = existsSync(TSX_BIN) ? [] : ["tsx"];
|
|
|
|
function run(file: string): Promise<{ file: string; ok: boolean; tail: string }> {
|
|
return new Promise((resolve) => {
|
|
const child = spawn(runner, [...baseArgs, join(TEST_DIR, file)], { stdio: ["ignore", "pipe", "pipe"] });
|
|
let out = "";
|
|
child.stdout.on("data", (d) => (out += d));
|
|
child.stderr.on("data", (d) => (out += d));
|
|
child.on("error", (err) => resolve({ file, ok: false, tail: `spawn error: ${err.message}` }));
|
|
child.on("close", (code) => {
|
|
const tail = out.trim().split("\n").slice(-1)[0] ?? "";
|
|
resolve({ file, ok: code === 0, tail });
|
|
});
|
|
});
|
|
}
|
|
|
|
// No top-level await: tsx transforms this file to CJS (no "type":"module"),
|
|
// which rejects top-level await. Orchestrate inside an async main() instead.
|
|
async function main(): Promise<number> {
|
|
const results: { file: string; ok: boolean; tail: string }[] = [];
|
|
const queue = [...files];
|
|
async function worker() {
|
|
let f: string | undefined;
|
|
while ((f = queue.shift())) {
|
|
const r = await run(f);
|
|
results.push(r);
|
|
console.log(`${r.ok ? "✓" : "✗"} ${r.file.replace(/\.ts$/, "")}${r.ok ? "" : `\n ${r.tail}`}`);
|
|
}
|
|
}
|
|
await Promise.all(Array.from({ length: MAX_PARALLEL }, worker));
|
|
const failed = results.filter((r) => !r.ok);
|
|
console.log(`\n${results.length - failed.length}/${results.length} unit test files passed`);
|
|
if (failed.length) console.error(`✗ failed: ${failed.map((r) => r.file).join(", ")}`);
|
|
return failed.length ? 1 : 0;
|
|
}
|
|
|
|
main().then((code) => process.exit(code));
|