"""Tests for harness/attribution.py with an in-memory fake collection (no live Mongo).""" import pytest from harness import attribution class FakeCollection: def __init__(self): self.docs = {} def update_one(self, flt, update, upsert=False): self.docs[flt["video_id"]] = dict(update["$set"]) def find(self, query, projection): return [dict(d) for d in self.docs.values()] def create_index(self, *a, **k): pass @pytest.fixture def fake_col(monkeypatch): col = FakeCollection() monkeypatch.setattr(attribution, "_collection", lambda: col) return col def test_record_and_map_roundtrip(fake_col): attribution.record_attribution( video_id="vid1", variant_id="variant_2", genome_hash="deadbeef0000", parent_genome="variant_1", upload_date="2026-06-14", ) m = attribution.attribution_map() assert "vid1" in m assert m["vid1"].variant_id == "variant_2" assert m["vid1"].parent_genome == "variant_1" def test_record_is_idempotent(fake_col): for _ in range(3): attribution.record_attribution( video_id="v", variant_id="A", genome_hash="h", parent_genome="seed", upload_date="2026-06-10", ) assert len(attribution.attribution_map()) == 1 def test_blank_video_id_is_noop(fake_col): attribution.record_attribution(video_id=" ", variant_id="A", genome_hash="h", parent_genome="s", upload_date="d") assert attribution.attribution_map() == {}