#!/usr/bin/env python3
"""
Tests de non-regression pour le contrat des regles d'administration.
"""
from pathlib import Path

from admin_rules import (
    generate_rule_variants,
    load_effective_admin_rules_dict,
    validate_rules_config,
)


def test_default_admin_rules_template_is_valid():
    path = Path("config/admin_rules.default.yml")

    data = load_effective_admin_rules_dict(path)
    errors = validate_rules_config(data)

    assert errors == []


def test_normalized_identifier_variants_cover_requested_forms():
    rule = {
        "type": "normalized_identifier",
        "match": {
            "canonical_value": "1234567",
        },
        "normalization": {
            "allow_bare_value": True,
            "multiline": True,
            "accepted_prefixes": ["N°", "No"],
            "prefix_value_separators": ["", " "],
        },
    }

    variants = generate_rule_variants(rule, limit=20)

    assert "1234567" in variants
    assert "N°1234567" in variants
    assert "N° 1234567" in variants
    assert "No1234567" in variants
    assert "No 1234567" in variants


def test_preserve_phrase_must_use_preserve_action():
    data = {
        "version": 1,
        "rules": [
            {
                "id": "rule_bad_preserve",
                "label": "Bad preserve",
                "type": "preserve_phrase",
                "action": "mask",
                "status": "draft",
                "match": {
                    "exact_value": "classification internationale",
                },
                "scope": {
                    "document_families": ["all"],
                    "environments": ["test"],
                    "sections": ["narrative"],
                },
                "governance": {
                    "owner": "qualite",
                    "justification": "test de contrat",
                    "created_at": "2026-04-21",
                    "review_required_for_activation": True,
                    "approved_by": None,
                    "tests": {
                        "required_case_ids": ["006_whitelist_phrases_preserved"],
                    },
                },
            }
        ],
    }

    errors = validate_rules_config(data)

    assert any("preserve_phrase" in error for error in errors)
