"""Unit tests for micro-repair validator covering scenarios REP-001 to REP-016.""" from src.runtime.candidate.models import CandidateObject from src.runtime.hygiene.repairs import validate_and_apply_repairs def test_valid_encoding_repair(): c = CandidateObject( id="blk_01", type="paragraph", text="Você sabia disso?", extractor="trafilatura", position=1, ) cands = {c.id: c} repairs = [ { "target_candidate_id": "blk_01", "original_fragment": "Você", "replacement_fragment": "Você", "category": "encoding", "rationale": "Fix moji-bake encoding artifact.", } ] applied, warnings = validate_and_apply_repairs(cands, repairs) assert len(applied) == 1 assert len(warnings) == 0 assert c.text == "Você sabia disso?" def test_reject_unapproved_category_repair(): c = CandidateObject( id="blk_01", type="paragraph", text="Original text here.", extractor="trafilatura", position=1, ) cands = {c.id: c} repairs = [ { "target_candidate_id": "blk_01", "original_fragment": "Original", "replacement_fragment": "Better", "category": "creative_style", # Unapproved "rationale": "Better wording", } ] applied, warnings = validate_and_apply_repairs(cands, repairs) assert len(applied) == 0 assert len(warnings) == 1 assert "unapproved category" in warnings[0] def test_reject_ungrounded_original_fragment(): c = CandidateObject( id="blk_01", type="paragraph", text="Actual content.", extractor="trafilatura", position=1 ) cands = {c.id: c} repairs = [ { "target_candidate_id": "blk_01", "original_fragment": "NonExistentFragment", "replacement_fragment": "Something", "category": "spacing", "rationale": "Fix space", } ] applied, warnings = validate_and_apply_repairs(cands, repairs) assert len(applied) == 0 assert len(warnings) == 1 assert "not found in candidate" in warnings[0]