from __future__ import annotations import importlib.util import json import subprocess import sys from pathlib import Path SCRIPT = Path(__file__).parents[2] / "scripts" / "run_belgium_building_training_loop.py" SPEC = importlib.util.spec_from_file_location("training_loop", SCRIPT) assert SPEC and SPEC.loader MODULE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(MODULE) def test_training_command_is_cuda_deterministic_and_bound_to_frozen_inputs(tmp_path: Path) -> None: command = MODULE.training_command( "yolo", model=tmp_path / "base.pt", data=tmp_path / "dataset.yaml", project=tmp_path / "runs", name="iteration-001", epochs=160, seed=42, batch=2, workers=4, ) assert command[:2] == ["yolo", "train"] assert "device=0" in command assert "deterministic=True" in command assert "seed=42" in command assert "epochs=160" in command assert "max_det=1000" in command assert "imgsz=640" in command assert f"data={tmp_path / 'dataset.yaml'}" in command def test_loop_refuses_failed_dataset_audit(tmp_path: Path) -> None: audit = tmp_path / "audit.json" audit.write_text(json.dumps({"status": "needs_attention", "low_variance_positive_tile_count": 4})) result = subprocess.run( [ sys.executable, str(SCRIPT), "--initial-model", str(tmp_path / "base.pt"), "--train-yaml", str(tmp_path / "dataset.yaml"), "--dataset-audit", str(audit), "--calibration-summary", str(tmp_path / "cal.json"), "--test-summary", str(tmp_path / "test.json"), "--background-summary", str(tmp_path / "background.json"), "--corpus-manifest", str(tmp_path / "manifest.json"), "--output-dir", str(tmp_path / "output"), ], capture_output=True, text=True, check=False, ) assert result.returncode != 0 assert "Dataset audit is not ok" in result.stderr