import importlib.util import sys from pathlib import Path import numpy as np import pytest SRC = Path(__file__).resolve().parents[1] / "simulation/semantic-learning" sys.path.insert(0, str(SRC)) spec = importlib.util.spec_from_file_location("crossing_probe", SRC / "crossing_diversity_probe.py") probe = importlib.util.module_from_spec(spec) spec.loader.exec_module(probe) sys.path.remove(str(SRC)) def example(sid, split="train", group="a"): return dict( sample_id=sid, source="rev1", physical_group=group, split=split, inputs=[dict(rgb=dict(sha256=sid))], ) def test_expansion_preserves_targets_and_rejects_control_or_duplicate_additions(): old = {"a": example("a"), "c": example("c", "regression", "b")} new = dict(old, d=example("d")) before = {k: np.array([[0, 1, 255]], dtype="uint8") for k in old} after = dict(before, d=np.array([[0, 1, 255]], dtype="uint8")) probe.check_expansion(old, new, before, after) with pytest.raises(ValueError, match="training physical group"): probe.check_expansion(old, dict(new, d=example("d", "regression", "b")), before, after) with pytest.raises(ValueError, match="training targets changed"): probe.check_expansion(old, new, before, dict(after, a=np.array([[0, 1, 0]]))) with pytest.raises(ValueError, match="Duplicate observation"): probe.check_expansion( old, dict(new, d=dict(example("d"), inputs=[dict(rgb=dict(sha256="a"))])), before, after ) with pytest.raises(ValueError, match="both paint and gap"): probe.check_expansion(old, new, before, dict(after, d=np.array([[1, 255]]))) def test_strict_control_and_gap_gates(): good = dict(baseline=dict(recall=1.0, fpr=0.1), expanded=dict(recall=1.0, fpr=0.1)) audit = dict(baseline=dict(recall=1.0, fpr=1.0), expanded=dict(recall=1.0, fpr=0.5)) assert probe.qualify({"a": good}, audit, True)["accepted"] bad = dict(baseline=dict(recall=None, fpr=0.01), expanded=dict(recall=None, fpr=0.02)) assert not probe.qualify({"a": good, "b": bad}, audit, True)["accepted"] assert not probe.qualify({"a": good}, audit, False)["accepted"] assert not probe.qualify({"a": good}, dict(audit, expanded=dict(recall=0.99, fpr=0.1)), True)[ "accepted" ] assert not probe.qualify({"a": good}, dict(audit, expanded=audit["baseline"]), True)["accepted"]