Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 265 lines · 9.9 KiB · at b0c0c21

// 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 = [];// Scratch copies for examples that rewrite files (fix, migrate, init). Kept// inside the repo so compiled output still resolves @tsus/runtime.const tmpRoot = path.join(repoRoot, ".examples-tmp");const freshCopy = (name, from) => {  const dest = path.join(tmpRoot, name);  fs.rmSync(dest, { recursive: true, force: true });  fs.mkdirSync(dest, { recursive: true });  if (from) {    fs.cpSync(from, dest, {      recursive: true,      filter: (src) => !/(^|[\\/])(dist|node_modules)([\\/]|$)/.test(path.relative(from, src)),    });  }  return dest;};const buildAndRun = (dir, expected) => {  const rb = runNode([cliBin, "build", dir], repoRoot);  if (rb.status !== 0) {    return `build failed:\n${(rb.stderr || rb.stdout || "").trim()}`;  }  const rr = runNode([path.join(dir, "dist/main.js")], dir);  if (rr.status !== 0) {    return `run failed:\n${(rr.stderr || "").trim()}`;  }  return checkLines(rr.stdout, expected);};const sameFile = (actualPath, goldenPath) => {  const actual = fs.readFileSync(actualPath, "utf8");  const golden = fs.readFileSync(goldenPath, "utf8");  return actual === golden ? "" : `${actualPath} differs from ${goldenPath}`;};// Each expectation returns a failure string ("" = pass).const expectFix = (name, dir, manifest) => {  const work = freshCopy(name, dir);  const rf = runNode([cliBin, "fix", work, "--json"], repoRoot);  const parsed = JSON.parse(rf.stdout || "{}");  if (!parsed.ok) {    return `fix left diagnostics: ${(parsed.diagnostics ?? []).map((d) => d.code).join(", ")}`;  }  const applied = (parsed.applied ?? []).map((a) => a.code).sort();  const want = [...(manifest.applied ?? [])].sort();  if (JSON.stringify(applied) !== JSON.stringify(want)) {    return `applied ${JSON.stringify(applied)}, expected ${JSON.stringify(want)}`;  }  for (const [file, golden] of Object.entries(manifest.golden ?? {})) {    const diff = sameFile(path.join(work, file), path.join(dir, golden));    if (diff) {      return diff;    }  }  return buildAndRun(work, manifest.output);};const expectMigrate = (name, dir, manifest) => {  const work = freshCopy(name, dir);  for (const item of manifest.migrate) {    const rm = runNode([cliBin, "migrate", path.join(work, item.input), "--json"], repoRoot);    if (rm.status === 2) {      return `migrate ${item.input} failed:\n${(rm.stderr || "").trim()}`;    }    const report = JSON.parse(rm.stdout);    const todo = report.unconverted.map((u) => (u.kind === "check" ? `check:${u.code}` : u.kind));    if (JSON.stringify(todo) !== JSON.stringify(item.todo)) {      return `${item.input}: todo ${JSON.stringify(todo)}, expected ${JSON.stringify(item.todo)}`;    }    const out = path.join(work, item.input.replace(/\.ts$/, ".tsus"));    const diff = sameFile(out, path.join(dir, item.golden));    if (diff) {      return diff;    }  }  for (const file of manifest.remove ?? []) {    fs.rmSync(path.join(work, file));  }  // Only src/ is built; partial migrations elsewhere are compared, not run.  return buildAndRun(work, manifest.output);};const expectInit = (name, _dir, manifest) => {  const work = freshCopy(name);  const ri = runNode([cliBin, "init", work], repoRoot);  if (ri.status !== 0) {    return `init failed:\n${(ri.stderr || ri.stdout || "").trim()}`;  }  for (const file of manifest.files) {    if (!fs.existsSync(path.join(work, file))) {      return `init did not create ${file}`;    }  }  const primer = sameFile(path.join(work, "AGENTS.md"), path.join(repoRoot, "AGENTS.md"));  if (primer) {    return primer;  }  const again = runNode([cliBin, "init", work, "--json"], repoRoot);  if (JSON.parse(again.stdout).written.length !== 0) {    return "a second init overwrote existing files";  }  return buildAndRun(work, manifest.output);};const SCRATCH_EXPECTATIONS = { fix: expectFix, migrate: expectMigrate, init: expectInit };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 (SCRATCH_EXPECTATIONS[manifest.expect]) {    failure = SCRATCH_EXPECTATIONS[manifest.expect](name, dir, manifest);    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"}`;        }      }      // Codes listed under "fixable" must come with a machine-applicable fix.      for (const code of manifest.fixable ?? []) {        const withFix = (parsed.diagnostics ?? []).filter((d) => d.code === code && d.fix && d.fix.edits.length > 0);        if (withFix.length === 0) {          failure = `${code} has no fix`;        }      }    }  } 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);