// Runs every example against the tsus toolchain and verifies the outcome// declared in each example's example.json.import fs from "node:fs";import path from "node:path";import { fileURLToPath, pathToFileURL } from "node:url";import { spawnSync } from "node:child_process";const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");const cliBin = path.join(repoRoot, "packages/cli/bin/tsus.js");const examplesDir = path.join(repoRoot, "examples");const results = [];const runNode = (args, cwd) => spawnSync(process.execPath, args, { cwd, encoding: "utf8", timeout: 30_000 });const checkLines = (stdout, expected) => { const lines = stdout.trim().split("\n").map((l) => l.trim()); if (lines.length !== expected.length) { return `expected ${expected.length} lines, got ${lines.length}: ${JSON.stringify(lines)}`; } for (let i = 0; i < expected.length; i += 1) { if (lines[i] !== expected[i]) { return `line ${i + 1}: expected ${JSON.stringify(expected[i])}, got ${JSON.stringify(lines[i])}`; } } return "";};for (const name of fs.readdirSync(examplesDir).sort()) { const dir = path.join(examplesDir, name); const manifestPath = path.join(dir, "example.json"); if (!fs.existsSync(manifestPath)) { continue; } const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); let failure = ""; if (manifest.expect === "command") { // Framework examples: install their own deps, run their own build/run // command, then assert on stdout and/or produced files. if (manifest.install && !fs.existsSync(path.join(dir, "node_modules"))) { const ri = spawnSync("npm", ["install", "--no-audit", "--no-fund"], { cwd: dir, encoding: "utf8", timeout: 600_000, }); if (ri.status !== 0) { failure = `npm install failed:\n${(ri.stderr || ri.stdout || "").trim().slice(-2000)}`; } } if (failure === "") { const rc = spawnSync(manifest.command[0], manifest.command.slice(1), { cwd: dir, encoding: "utf8", timeout: 600_000, }); if (rc.status !== 0) { failure = `command failed:\n${(rc.stderr || rc.stdout || "").trim().slice(-2000)}`; } else { if (manifest.output) { failure = checkLines(rc.stdout, manifest.output); } for (const fc of manifest.fileContains ?? []) { const produced = path.join(dir, fc.file); if (!fs.existsSync(produced)) { failure = `expected output file ${fc.file} was not produced`; } else if (!fs.readFileSync(produced, "utf8").includes(fc.contains)) { failure = `${fc.file} does not contain ${JSON.stringify(fc.contains)}`; } } } } results.push({ name, ok: failure === "", failure }); continue; } if (manifest.expect === "check-fail") { const r = runNode([cliBin, "check", dir, "--json"], repoRoot); if (r.status === 0) { failure = "check unexpectedly passed"; } else { const parsed = JSON.parse(r.stdout || "{}"); const seen = new Set((parsed.diagnostics ?? []).map((d) => d.code)); const missing = (manifest.codes ?? []).filter((c) => !seen.has(c)); if (missing.length > 0) { failure = `missing expected codes ${missing.join(", ")}; saw ${[...seen].join(", ") || "none"}`; } // Optional exact expectations: each listed diagnostic must be reported, // matching every field it names (code, file, line, col, tsCode). This // pins positions mapped back to the .tsus source. for (const want of manifest.diagnostics ?? []) { const hit = (parsed.diagnostics ?? []).some((d) => Object.entries(want).every(([key, value]) => d[key] === value), ); if (!hit && failure === "") { const seenAt = (parsed.diagnostics ?? []).map((d) => `${d.code}@${d.line}:${d.col}`).join(", "); failure = `expected diagnostic ${JSON.stringify(want)}; saw ${seenAt || "none"}`; } } } } else { const rb = runNode([cliBin, "build", dir], repoRoot); if (rb.status !== 0) { failure = `build failed:\n${(rb.stderr || rb.stdout || "").trim()}`; } else if (manifest.expect === "run") { const rr = runNode([path.join(dir, "dist/main.js")], dir); if (rr.status !== 0) { failure = `run failed:\n${(rr.stderr || "").trim()}`; } else { failure = checkLines(rr.stdout, manifest.output); } } else if (manifest.expect === "worker") { const entry = pathToFileURL(path.join(dir, "dist", manifest.entry)).href; try { const mod = await import(entry); for (const req of manifest.requests) { const res = await mod.default.fetch(new Request(`http://worker.local${req.path}`)); const body = await res.text(); if (res.status !== req.status || body !== req.body) { failure = `${req.path}: expected ${req.status} ${JSON.stringify(req.body)}, got ${res.status} ${JSON.stringify(body)}`; break; } } } catch (error) { failure = `worker import failed: ${error.message}`; } } else { failure = `unknown expectation ${manifest.expect}`; } } results.push({ name, ok: failure === "", failure });}let passed = 0;for (const r of results) { if (r.ok) { passed += 1; console.log(`PASS ${r.name}`); } else { console.log(`FAIL ${r.name}\n ${r.failure.replaceAll("\n", "\n ")}`); }}console.log(`\n${passed}/${results.length} examples passed`);process.exit(passed === results.length ? 0 : 1);