Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 95 lines · 3.4 KiB · at 6f4e421

// 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 === "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"}`;      }    }  } 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);