"""Tests for Self-Correction Policy (PROMPT 26).""" import sys import os import time sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) # doc_facts has no heavy deps — import directly from crews.agromatrix_crew.doc_facts import extract_fact_claims, build_self_correction from crews.agromatrix_crew import session_context as sc # Aliases for test readability _extract_fact_claims = extract_fact_claims _build_self_correction = build_self_correction def _make_session_with_claim(key: str, value: bool) -> dict: sess = sc._default_session() sess["fact_claims"] = [{"key": key, "value": value, "ts": time.time()}] return sess def test_extract_claims_no_profit(): text = "У документі немає прибутку, лише витрати." claims = _extract_fact_claims(text) assert any(c["key"] == "profit_present" and c["value"] is False for c in claims), \ f"Expected profit_present=False, got {claims}" def test_extract_claims_has_profit(): text = "У звіті є прибуток — 5 972 016 грн." claims = _extract_fact_claims(text) assert any(c["key"] == "profit_present" and c["value"] is True for c in claims), \ f"Expected profit_present=True, got {claims}" def test_self_correction_triggers_when_contradicts(): """ Попереднє твердження: profit_present=False. Нова відповідь: "є прибуток". Очікуємо: _build_self_correction повертає correction prefix. """ prev_session = _make_session_with_claim("profit_present", False) new_response = "У звіті є прибуток — 5 972 016 грн на 497 га." correction = _build_self_correction(new_response, {}, prev_session) assert correction, f"Expected self-correction prefix, got empty string" assert "неточно" in correction.lower() or "написав" in correction.lower(), \ f"Correction phrase unexpected: {correction}" def test_self_correction_no_trigger_when_consistent(): """Той самий твердження — не виправляємо.""" prev_session = _make_session_with_claim("profit_present", True) new_response = "У звіті є прибуток — 5 972 016 грн." correction = _build_self_correction(new_response, {}, prev_session) assert correction == "", f"Should not trigger, got: {correction}" def test_self_correction_no_trigger_without_claims(): """Порожня сесія — нема чого виправляти.""" prev_session = sc._default_session() new_response = "У звіті є прибуток — 5 972 016 грн." correction = _build_self_correction(new_response, {}, prev_session) assert correction == "" def test_self_correction_fail_safe(): """Зламаний session → не кидаємо виняток.""" bad_session = {"fact_claims": "not_a_list"} try: result = _build_self_correction("текст", {}, bad_session) # Може повернути "" або щось — головне не Exception assert isinstance(result, str) except Exception as e: assert False, f"Should not raise: {e}"