import importlib.util from pathlib import Path import pytest SRC = Path(__file__).resolve().parents[1] / "simulation/semantic-learning/fit_capacity.py" if not SRC.exists(): SRC = Path(__file__).with_name("fit_capacity.py") spec = importlib.util.spec_from_file_location("fit_capacity", SRC) m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) def before(): return dict(bce=0.2, weak_fn=10, background_fp=20, clear_fn=2) def test_exact_half_boundary_passes_without_promotion(): a = dict(bce=0.1, weak_fn=5, background_fp=10, clear_fn=2) r = m.capacity_screen(before(), a) assert r["passed"] and not r["production_eligible"] @pytest.mark.parametrize( "metric,value", [("bce", 0.100001), ("weak_fn", 6), ("background_fp", 11), ("clear_fn", 3)] ) def test_each_guard_fails_even_when_other_metrics_improve(metric, value): a = dict(bce=0.01, weak_fn=0, background_fp=0, clear_fn=0) a[metric] = value r = m.capacity_screen(before(), a) assert not r["passed"] and r["failures"] == [metric] def test_zero_baseline_cannot_hide_new_errors_or_divide_by_zero(): z = dict(bce=0.0, weak_fn=0, background_fp=0, clear_fn=0) assert m.capacity_screen(z, z)["passed"] a = dict(z, background_fp=1) assert m.capacity_screen(z, a)["failures"] == ["background_fp"] @pytest.mark.parametrize( "metric,value", [ ("bce", float("nan")), ("bce", float("inf")), ("bce", -1), ("bce", True), ("weak_fn", 1.5), ("background_fp", -1), ("clear_fn", True), ], ) def test_invalid_metrics_are_rejected(metric, value): a = before() a[metric] = value for left, right in [(a, before()), (before(), a)]: with pytest.raises(ValueError): m.capacity_screen(left, right) def test_extra_or_missing_cohorts_are_rejected(): for a in [ dict(before(), street_threshold=1), {k: v for k, v in before().items() if k != "weak_fn"}, ]: with pytest.raises(ValueError): m.capacity_screen(a, before())