100 lines
3.1 KiB
Python
100 lines
3.1 KiB
Python
"""Unit tests for 10-step hygiene harness covering scenarios HYG-001 to HYG-021."""
|
|
|
|
import pytest
|
|
|
|
from src.runtime.hygiene.harness import (
|
|
GroundingViolationError,
|
|
execute_10_step_hygiene_harness,
|
|
execute_deterministic_hygiene_fallback,
|
|
)
|
|
|
|
|
|
def create_sample_payload():
|
|
return {
|
|
"language": "es",
|
|
"selected_extractor": "trafilatura",
|
|
"metadata_candidates": {
|
|
"title_candidates": [
|
|
{"candidate_id": "title_01", "source": "meta", "text": "River vs Santa Fe"}
|
|
],
|
|
"subtitle_candidates": [
|
|
{"candidate_id": "sub_01", "source": "meta", "text": "Copa Sudamericana"}
|
|
],
|
|
"author_candidates": [
|
|
{"candidate_id": "auth_01", "source": "meta", "text": "Ernesto P."}
|
|
],
|
|
},
|
|
"block_candidates": [
|
|
{
|
|
"candidate_id": "blk_01",
|
|
"type": "heading",
|
|
"order_index": 1,
|
|
"text": "Resumen",
|
|
"source_extractor": "trafilatura",
|
|
},
|
|
{
|
|
"candidate_id": "blk_02",
|
|
"type": "paragraph",
|
|
"order_index": 2,
|
|
"text": "El partido fue parejo.",
|
|
"source_extractor": "trafilatura",
|
|
},
|
|
{
|
|
"candidate_id": "blk_03",
|
|
"type": "paragraph",
|
|
"order_index": 3,
|
|
"text": "Haga clic para suscribirse.",
|
|
"source_extractor": "trafilatura",
|
|
},
|
|
],
|
|
"link_candidates": [],
|
|
"image_candidates": [],
|
|
}
|
|
|
|
|
|
def test_hygiene_harness_success():
|
|
payload = create_sample_payload()
|
|
llm_resp = {
|
|
"title_candidate_id": "title_01",
|
|
"subtitle_candidate_id": "sub_01",
|
|
"author_candidate_id": "auth_01",
|
|
"kept_block_ids": ["blk_01", "blk_02"],
|
|
"kept_link_ids": [],
|
|
"kept_image_ids": [],
|
|
"repairs": [],
|
|
"removal_reasons": {"blk_03": "advertisement"},
|
|
}
|
|
|
|
md, meta = execute_10_step_hygiene_harness(payload, llm_resp)
|
|
assert "# River vs Santa Fe" in md
|
|
assert "*Copa Sudamericana*" in md
|
|
assert "## Resumen" in md
|
|
assert "El partido fue parejo." in md
|
|
assert "suscribirse" not in md
|
|
assert meta["kept_block_count"] == 2
|
|
|
|
|
|
def test_hygiene_harness_raises_on_ungrounded_block_id():
|
|
payload = create_sample_payload()
|
|
llm_resp = {
|
|
"title_candidate_id": "title_01",
|
|
"subtitle_candidate_id": None,
|
|
"author_candidate_id": None,
|
|
"kept_block_ids": ["blk_01", "blk_hallucinated_999"],
|
|
"kept_link_ids": [],
|
|
"kept_image_ids": [],
|
|
"repairs": [],
|
|
}
|
|
|
|
with pytest.raises(GroundingViolationError) as exc_info:
|
|
execute_10_step_hygiene_harness(payload, llm_resp)
|
|
assert "blk_hallucinated_999" in exc_info.value.ungrounded_ids
|
|
|
|
|
|
def test_hygiene_deterministic_fallback():
|
|
payload = create_sample_payload()
|
|
md, meta = execute_deterministic_hygiene_fallback(payload)
|
|
assert "# River vs Santa Fe" in md
|
|
assert meta["is_fallback"] is True
|
|
assert meta["kept_block_count"] == 3
|