SACB / source /tools /harness-tests /test_runner_ts.py
ilintar's picture
Add corpus source: base repos, task overlays, hidden tests, and the authoring tools
9368cc4 verified
Raw
History Blame Contribute Delete
3.14 kB
#!/usr/bin/env python3
"""Test the TypeScript adapter: tsc diagnostics and node --test TAP scoring."""
import sys, tempfile, shutil
from pathlib import Path
EVAL = Path(__file__).resolve().parents[2] / "examples" / "llama-eval"
sys.path.insert(0, str(EVAL))
from eval_sandbox import Sandbox # noqa: E402
from agentic_eval import LANGS, Runner # noqa: E402
fails = []
def check(name, cond, detail=""):
print(f" {'PASS' if cond else 'FAIL'} {name}" + (f" [{detail}]" if not cond else ""))
if not cond:
fails.append(name)
lang = LANGS["typescript"]
sbx = Sandbox(name="agentic-typescript", packages=[], provision=list(lang.provision), address_space_limit=lang.address_space_limit)
sbx.ensure()
runner = Runner(sbx, lang)
print(f"isolation: {sbx.isolation}")
GOOD = ("export function applyDiscount(price: number, pct: number): number {\n"
" return price * (1 - pct / 100);\n}\n")
BUGGY = ("export function applyDiscount(price: number, pct: number): number {\n"
" return price - pct;\n}\n")
work = Path(tempfile.mkdtemp(prefix="runner-ts-"))
try:
(work / "src").mkdir()
(work / "tests").mkdir()
(work / "src" / "pricing.ts").write_text(BUGGY)
(work / "tests" / "pricing.test.ts").write_text(
"import { test } from 'node:test';\n"
"import assert from 'node:assert';\n"
"import { applyDiscount } from '../src/pricing.ts';\n"
"test('ten percent off 200 is 180', () => {\n"
" assert.strictEqual(applyDiscount(200, 10), 180);\n});\n"
"test('zero percent leaves price', () => {\n"
" assert.strictEqual(applyDiscount(50, 0), 50);\n});\n"
)
out = runner.lint(work, "src")
check("lint: correct types report no problems", out.strip() == "No problems found.", out[:250])
res = runner.test(work)
outcomes = res.get("tests", {})
check("test: TAP parsed, both tests seen", len(outcomes) >= 2, str(res)[:300])
check("test: logic bug fails exactly one",
sum(v == "fail" for v in outcomes.values()) == 1, str(outcomes)[:300])
(work / "src" / "pricing.ts").write_text(GOOD)
res = runner.test(work)
outcomes = res.get("tests", {})
check("test: fix makes all pass",
outcomes and all(v == "pass" for v in outcomes.values()), str(res)[:300])
(work / "src" / "pricing.ts").write_text(
"export function applyDiscount(price: number, pct: number): number {\n"
" return 'free';\n}\n")
out = runner.lint(work, "src")
check("lint: tsc catches type error",
"not assignable" in out or "TS2322" in out, out[:250])
(work / "src" / "pricing.ts").write_text("export function broken(: {\n")
out = runner.lint(work, "src")
check("lint: tsc catches syntax error", "error TS" in out, out[:250])
res = runner.test(work)
check("test: broken source counts as failure, not zero tests",
res.get("tests") or res.get("error"), str(res)[:300])
finally:
shutil.rmtree(work, ignore_errors=True)
print(f"\n{'ALL PASS' if not fails else 'FAILURES: ' + ', '.join(fails)}")
sys.exit(1 if fails else 0)