File size: 3,142 Bytes
9368cc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/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)