okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-020-ci' into f-020-int76e51103c

main
3.3 KiB76 linesraw
1#!/usr/bin/env python3
2"""Exercise fake-engine serving from a clean runtime-only Python environment.
3
4Create a fresh venv, install requirements.txt, then run this script with that
5venv's Python. The runtime integration module exercises foreground and POSIX
6owned serving; no GPU, model weights or optional terminal framework is needed.
7"""
8
9from __future__ import annotations
10
11import argparse
12import hashlib
13import importlib.util
14import json
15import os
16import platform
17import re
18import subprocess
19import sys
20import time
21from pathlib import Path
22
23
24ROOT = Path(__file__).resolve().parents[1]
25TEST_MODULE = "tests.terminal.test_runtime"
26MIN_TESTS = 32
27
28
29def smoke() -> dict:
30    if sys.prefix == sys.base_prefix:
31        raise RuntimeError("run with a dedicated virtual environment")
32    if os.name != "posix":
33        raise RuntimeError("this owned-runtime check currently requires POSIX")
34    visible = {name: importlib.util.find_spec(name) is not None
35               for name in ("rich", "textual", "numpy", "jinja2", "requests")}
36    if visible != {"rich": False, "textual": False, "numpy": True,
37                   "jinja2": True, "requests": True}:
38        raise RuntimeError(f"runtime-only dependency set was not isolated: {visible}")
39    started = time.monotonic()
40    result = subprocess.run([sys.executable, "-m", "unittest", TEST_MODULE], cwd=ROOT,
41                            capture_output=True, text=True, timeout=240, check=False)
42    summary = result.stdout + result.stderr
43    # Windows-only tests skip on POSIX, so unittest ends "OK (skipped=N)", not a bare "OK".
44    count = re.search(r"Ran (\d+) tests in [0-9.]+s\s+OK(?: \(skipped=(\d+)\))?\s*$", summary)
45    executed = int(count[1]) - int(count[2] or 0) if count else 0
46    passed = result.returncode == 0 and count is not None and executed >= MIN_TESTS
47    return {"schema_version": 1, "scope": "source_checkout_fake_engine_runtime_only",
48            "platform": platform.platform(), "python": platform.python_version(),
49            "requirements_sha256": hashlib.sha256((ROOT / "requirements.txt").read_bytes()).hexdigest(),
50            "optional_packages_visible": {name: visible[name] for name in ("rich", "textual")},
51            "server_packages_visible": {name: visible[name] for name in ("numpy", "jinja2", "requests")},
52            "test_module": TEST_MODULE, "tests_run": executed if count else None,
53            "duration_s": round(time.monotonic() - started, 2),
54            "test_exit_code": result.returncode, "actual_gpu_engine": False,
55            "passed": bool(passed), "test_summary": summary[-1000:]}
56
57
58def main() -> int:
59    parser = argparse.ArgumentParser(description=__doc__)
60    parser.add_argument("--report", type=Path, required=True, help="new absolute JSON report path")
61    args = parser.parse_args()
62    report = args.report.expanduser()
63    if not report.is_absolute() or report.exists():
64        parser.error("--report must be an absolute new file")
65    result = smoke()
66    report.parent.mkdir(parents=True, exist_ok=True)
67    descriptor = os.open(report, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
68    with os.fdopen(descriptor, "w", encoding="utf-8") as stream:
69        json.dump(result, stream, indent=2)
70        stream.write("\n")
71    print(json.dumps({"report": str(report), "passed": result["passed"]}), flush=True)
72    return 0 if result["passed"] else 6
73
74
75if __name__ == "__main__":
76    raise SystemExit(main())