From 96fdbd7db7c69686e8195919fcfc2753d33d645e Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Wed, 29 Apr 2026 21:29:05 +0200 Subject: [PATCH] test(perception): add strict TDD suite for autonomous ad marker learning and verification --- tests/unit/test_autonomous_ad_learning.py | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/unit/test_autonomous_ad_learning.py diff --git a/tests/unit/test_autonomous_ad_learning.py b/tests/unit/test_autonomous_ad_learning.py new file mode 100644 index 0000000..c92860e --- /dev/null +++ b/tests/unit/test_autonomous_ad_learning.py @@ -0,0 +1,49 @@ +import pytest +import os +import json +from GramAddict.core.utils import is_ad, learn_ad_marker, get_learned_ad_markers + +@pytest.fixture(autouse=True) +def clean_ad_markers(): + # Clean up any existing learned markers file before and after tests + file_path = os.path.join(os.getcwd(), "learned_ad_markers.json") + if os.path.exists(file_path): + os.remove(file_path) + import GramAddict.core.utils + GramAddict.core.utils._LEARNED_AD_MARKERS_CACHE = None + yield + if os.path.exists(file_path): + os.remove(file_path) + GramAddict.core.utils._LEARNED_AD_MARKERS_CACHE = None + +def test_learn_ad_marker_validates_against_xml(): + xml_dump = """ + + + + """ + + # Attempt to learn a hallucinated marker + learn_ad_marker("Hallucination", xml_dump) + assert "hallucination" not in get_learned_ad_markers() + + # Attempt to learn an actual marker present in XML + learn_ad_marker("Sponsorisé", xml_dump) + assert "sponsorisé" in get_learned_ad_markers() + +def test_is_ad_uses_learned_markers(): + xml_dump = """ + + + + + """ + + # Initially, it shouldn't recognize "Sponsorisé" because it's not in the hardcoded list + assert is_ad(xml_dump) is False + + # Learn the new marker + learn_ad_marker("Sponsorisé", xml_dump) + + # Now, is_ad should return True immediately without VLM + assert is_ad(xml_dump) is True