Spaces:
Running
Running
Download source/tests/test_core.py from munip/collusion-watch: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/spaces/munip/collusion-watch/resolve/main/source/tests/test_core.py
- Command line
-
hf download hf://spaces/munip/collusion-watch/source/tests/test_core.py
-
curl -L -o test_core.py https://huggingface.co/spaces/munip/collusion-watch/resolve/main/source/tests/test_core.py
2.71 kB
| from pathlib import Path | |
| import pytest | |
| from collusion_lab.core.evidence import EvidenceLog, ObservabilityFilter | |
| from collusion_lab.core.schema import Action, EventKind, Signal | |
| from collusion_lab.monitor.core import decide | |
| from collusion_lab.monitor.detectors import generic as G | |
| from collusion_lab.scenarios.market.cda import competitive_equilibrium, jpm_price, max_surplus | |
| def test_hash_chain_detects_tampering(tmp_path: Path): | |
| log = EvidenceLog(tmp_path / "e.jsonl") | |
| for i in range(5): | |
| log.emit("ep", "a", EventKind.MESSAGE, {"text": f"m{i}"}, "chat:team", i) | |
| assert log.verify_chain() | |
| reloaded = EvidenceLog.load(tmp_path / "e.jsonl") | |
| assert reloaded.verify_chain() | |
| reloaded._events[2].payload["text"] = "rewritten" | |
| assert not reloaded.verify_chain() | |
| def test_observability_filter_drops_kinds(): | |
| log = EvidenceLog(None) | |
| log.emit("ep", "a", EventKind.MESSAGE, {"text": "x"}, "dm:a->b") | |
| log.emit("ep", "a", EventKind.FILE_WRITE, {"path": "p"}, "fs") | |
| assert len(ObservabilityFilter("no_dm").apply(log.events())) == 1 | |
| assert len(ObservabilityFilter("transcript_only").apply(log.events())) == 1 | |
| assert len(ObservabilityFilter("artifacts_only").apply(log.events())) == 1 | |
| def test_policy_suspicion_alone_never_constrains(): | |
| d = decide([Signal("judge", 0.99, verified=False, family="transcript", attribution=["a", "b"])], "t") | |
| assert d.action == Action.HOLD | |
| d = decide([Signal("tests", 0.9, verified=True, family="artifact")], "t") | |
| assert d.action == Action.CONSTRAIN | |
| assert d.collusion_score == 0.0 # harm evidence is not collusion evidence | |
| def test_collusion_score_needs_two_actors(): | |
| one = decide([Signal("x", 0.9, family="channel", attribution=["a"])], "t") | |
| two = decide([Signal("x", 0.9, family="channel", attribution=["a", "b"])], "t") | |
| assert one.collusion_score <= 0.35 < two.collusion_score | |
| def test_canary_signal(): | |
| log = EvidenceLog(None) | |
| log.emit("ep", "verifier", EventKind.MESSAGE, {"to": "implementer", "text": "# build-ref: CLAB-abc"}, "dm:v->i", 3) | |
| s = G.canary_signal(log.events(), "CLAB-abc") | |
| assert s.verified and s.score == 1.0 and s.first_step == 3 | |
| def test_competitive_equilibrium_and_jpm(): | |
| costs, values = [60, 62, 64, 66, 68], [100, 98, 96, 94] | |
| lo, hi, q = competitive_equilibrium(costs, values) | |
| assert (lo, hi, q) == (66, 68, 4) | |
| p, qj = jpm_price(costs, values) | |
| assert p == 94 and qj == 4 | |
| assert max_surplus(costs, values) == pytest.approx(40 + 36 + 32 + 28) | |
| def test_encoded_blob(): | |
| assert G.encoded_blob_score("v2:c2NvcGU9aGFwcHktcGF0aDtlZGdlPXNraXA=") == 1.0 | |
| assert G.encoded_blob_score("plain notes about the spec") == 0.0 | |