Clean up nested duplicate: v2/v2/tools/test_tools.py
Browse files- v2/v2/tools/test_tools.py +0 -98
v2/v2/tools/test_tools.py
DELETED
|
@@ -1,98 +0,0 @@
|
|
| 1 |
-
"""Verification runners — targeted tests and typecheck for the campus repo.
|
| 2 |
-
|
| 3 |
-
Used by the test_runner chip to verify generated code instead of
|
| 4 |
-
asserting it works. Both runners degrade honestly: when the repo (or
|
| 5 |
-
tsc/npm) isn't reachable from where Rivet runs, they return
|
| 6 |
-
`available=False` and the discipline gate caps confidence accordingly.
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
import re
|
| 10 |
-
import tempfile
|
| 11 |
-
from dataclasses import dataclass
|
| 12 |
-
from pathlib import Path
|
| 13 |
-
|
| 14 |
-
from tools.guard import run_checked
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
@dataclass
|
| 18 |
-
class VerifyResult:
|
| 19 |
-
available: bool
|
| 20 |
-
passed: bool = False
|
| 21 |
-
command: str = ""
|
| 22 |
-
output: str = ""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
class TestTools:
|
| 26 |
-
def __init__(self, repo_root: str = ""):
|
| 27 |
-
self.repo_root = repo_root
|
| 28 |
-
|
| 29 |
-
def repo_available(self) -> bool:
|
| 30 |
-
return bool(self.repo_root) and Path(self.repo_root).is_dir()
|
| 31 |
-
|
| 32 |
-
def typecheck(self, workspace: str = "server") -> VerifyResult:
|
| 33 |
-
"""tsc --noEmit over a workspace of the monorepo."""
|
| 34 |
-
if not self.repo_available():
|
| 35 |
-
return VerifyResult(available=False,
|
| 36 |
-
output="campus repo not on this machine")
|
| 37 |
-
cwd = str(Path(self.repo_root) / workspace)
|
| 38 |
-
result = run_checked(["npx", "tsc", "--noEmit"], cwd=cwd, timeout=300)
|
| 39 |
-
return VerifyResult(
|
| 40 |
-
available=True, passed=result.ok,
|
| 41 |
-
command=f"npx tsc --noEmit (in {workspace}/)",
|
| 42 |
-
output=(result.stdout + result.stderr)[-5000:],
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
def run_tests(self, test_file: str = "", workspace: str = "server") -> VerifyResult:
|
| 46 |
-
"""npm test, optionally targeted at one file."""
|
| 47 |
-
if not self.repo_available():
|
| 48 |
-
return VerifyResult(available=False,
|
| 49 |
-
output="campus repo not on this machine")
|
| 50 |
-
cwd = str(Path(self.repo_root) / workspace)
|
| 51 |
-
argv = ["npm", "test"]
|
| 52 |
-
if test_file:
|
| 53 |
-
argv += ["--", test_file]
|
| 54 |
-
result = run_checked(argv, cwd=cwd, timeout=600)
|
| 55 |
-
return VerifyResult(
|
| 56 |
-
available=True, passed=result.ok,
|
| 57 |
-
command=" ".join(argv) + f" (in {workspace}/)",
|
| 58 |
-
output=(result.stdout + result.stderr)[-5000:],
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
def syntax_check_snippet(self, code: str, lang: str = "ts") -> VerifyResult:
|
| 62 |
-
"""Standalone syntax check of a generated snippet via tsc.
|
| 63 |
-
|
| 64 |
-
Weaker than a repo typecheck (no project types) but catches
|
| 65 |
-
outright syntax errors even when the repo isn't present.
|
| 66 |
-
"""
|
| 67 |
-
tsc_probe = run_checked(["npx", "tsc", "--version"], timeout=30)
|
| 68 |
-
if not tsc_probe.ok:
|
| 69 |
-
return VerifyResult(available=False,
|
| 70 |
-
output="tsc not available on this machine")
|
| 71 |
-
suffix = ".ts" if lang in ("ts", "typescript") else ".js"
|
| 72 |
-
with tempfile.NamedTemporaryFile(
|
| 73 |
-
"w", suffix=suffix, delete=False) as tmp:
|
| 74 |
-
tmp.write(code)
|
| 75 |
-
tmp_path = tmp.name
|
| 76 |
-
try:
|
| 77 |
-
result = run_checked(
|
| 78 |
-
["npx", "tsc", "--noEmit", "--skipLibCheck", "--target",
|
| 79 |
-
"es2022", "--moduleResolution", "bundler", "--module",
|
| 80 |
-
"esnext", tmp_path],
|
| 81 |
-
timeout=60,
|
| 82 |
-
)
|
| 83 |
-
return VerifyResult(
|
| 84 |
-
available=True, passed=result.ok,
|
| 85 |
-
command="npx tsc --noEmit <snippet>",
|
| 86 |
-
output=(result.stdout + result.stderr)[-3000:],
|
| 87 |
-
)
|
| 88 |
-
finally:
|
| 89 |
-
Path(tmp_path).unlink(missing_ok=True)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
def extract_code_blocks(markdown: str) -> list:
|
| 93 |
-
"""Pull fenced code blocks out of a draft answer for verification."""
|
| 94 |
-
blocks = []
|
| 95 |
-
for m in re.finditer(r"```(\w*)\n(.*?)```", markdown, re.DOTALL):
|
| 96 |
-
lang = (m.group(1) or "").lower()
|
| 97 |
-
blocks.append({"lang": lang, "code": m.group(2)})
|
| 98 |
-
return blocks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|