"""Reporting must never turn validation/shutdown failures into success.""" import importlib.util import json from pathlib import Path from types import SimpleNamespace import pytest ROOT = Path(__file__).resolve().parents[1] SPEC = importlib.util.spec_from_file_location("runtime_common", ROOT / "scripts/runtime_common.py") runtime = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(runtime) def test_atomic_report_rejects_nan_without_overwriting(tmp_path): path = tmp_path / "result.json" runtime.write_json(path, {"passed": False}) with pytest.raises(ValueError): runtime.write_json(path, {"passed": True, "value": float("nan")}) assert json.loads(path.read_text()) == {"passed": False} assert list(tmp_path.iterdir()) == [path] @pytest.mark.parametrize("close_fails", [False, True]) def test_success_only_after_environment_close(tmp_path, close_fails): events = [] def close(): events.append("env") assert not (tmp_path / "result.json").exists() if close_fails: raise RuntimeError("shutdown failed") def close_app(exit_code): events.append("app") report = json.loads((tmp_path / "result.json").read_text()) assert report["passed"] is (not close_fails) assert exit_code == int(close_fails) if close_fails: assert report["milestone_complete"] is False runtime.finish_run( SimpleNamespace(close=close_app), SimpleNamespace(close=close), tmp_path / "result.json", {"passed": True, "milestone_complete": True}, ) assert events == ["env", "app"] def test_critical_log_scan_includes_buffer_overflow(tmp_path): path = tmp_path / "worker.log" path.write_text("[Warning] harmless\n[error] physics\ncontact buffer overflow\n[Fatal] stopped\n") assert runtime.critical_log_lines(path) == ["[error] physics", "contact buffer overflow", "[Fatal] stopped"]