import anonymizer_core_refactored_onnx as core


def test_category_of_each_source():
    assert core._category_of("NOM_FORCE") == "NOM"          # explicite/regex
    assert core._category_of("NIR") == "NIR"                # placeholder-self
    assert core._category_of("NIR_GLOBAL") == "NIR"         # suffixe _GLOBAL
    assert core._category_of("ADHERENT_GLOBAL") == "ADHERENT"
    assert core._category_of("VLM_NOM") == "NOM"            # dérivé VLM
    assert core._category_of("VLM_ETAB") == "ETAB"
    assert core._category_of("EDS_SECU") == "NIR"           # dérivé EDS (SECU→NIR)
    assert core._category_of("EDS_HOPITAL") == "ETAB"
    assert core._category_of("VLM_CP") == "ADRESSE"          # CP suit « Adresses » (Dom 2026-06-26)
    assert core._category_of("EDS_ZIP") == "ADRESSE"


def test_category_of_default_deny():
    # Non toggleables → None (restent TOUJOURS masqués). Sécurité.
    # NB : VILLE reste masquée ; seul CODE_POSTAL (VLM_CP/EDS_ZIP) a été basculé vers ADRESSE.
    for k in ("EMAIL", "IBAN", "IPP", "VILLE", "FAX",
              "VLM_VILLE", "EMAIL_GLOBAL", "INCONNU_XYZ"):
        assert core._category_of(k) is None, k
    # Garde de terminaison de la récursion (_GLOBAL strip) : entrées vides.
    assert core._category_of(None) is None
    assert core._category_of("") is None


def test_no_toggleable_vlm_or_eds_kind_is_uncategorised():
    # ANTI-DÉRIVE : tout kind VLM/EDS dont le placeholder est une des 7 catégories
    # DOIT être catégorisé (sinon toggle faussé sur ce chemin).
    import vlm_manager, eds_pseudo_manager
    seven = {"NOM", "DATE_NAISSANCE", "ETAB", "ADRESSE", "NIR", "TEL", "ADHERENT"}
    for _label, (kind, placeholder) in vlm_manager.VLM_CATEGORY_MAP.items():
        if core._placeholder_to_category(placeholder) in seven:
            assert core._category_of(kind) is not None, f"VLM {kind} non catégorisé"
    for label, placeholder in eds_pseudo_manager.EDS_LABEL_MAP.items():
        if core._placeholder_to_category(placeholder) in seven:
            assert core._category_of(f"EDS_{label}") is not None, f"EDS_{label} non catégorisé"


def test_filter_audit_drops_only_disabled():
    PiiHit = core.PiiHit
    audit = [PiiHit(1, "NOM", "Dupont", "[NOM]"), PiiHit(1, "NIR", "1850574", "[NIR]"),
             PiiHit(1, "EMAIL", "x@y.fr", "[EMAIL]"), PiiHit(1, "NIR_GLOBAL", "1850574", "[NIR]")]
    kinds = {h.kind for h in core._filter_audit_by_disabled(audit, {"NIR"})}
    assert "NIR" not in kinds and "NIR_GLOBAL" not in kinds  # NIR + propagation retirés
    assert "NOM" in kinds and "EMAIL" in kinds               # autres conservés
