"""Tests for compute_scenario — fertilizer ×2 impact on profit.""" import sys import os sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from crews.agromatrix_crew.doc_facts import compute_scenario, merge_doc_facts, extract_doc_facts FACTS = { "profit_uah": 5_972_016.0, "fertilizer_uah": 1_521_084.0, "area_ha": 497.0, } def test_fertilizer_double_profit(): question = "якщо збільшити витрати на добрива вдвічі, яким буде прибуток?" ok, text = compute_scenario(question, FACTS) assert ok is True, f"Expected ok=True, got text={text}" # 5 972 016 - 1 521 084 = 4 450 932 assert "4 450 932" in text or "4450932" in text, f"Expected 4450932 in: {text}" def test_fertilizer_double_contains_delta(): question = "якщо добрива збільшити вдвічі?" ok, text = compute_scenario(question, FACTS) assert ok is True # Повинна бути згадка суми добрив як delta assert "1 521 084" in text or "1521084" in text, f"Delta missing: {text}" def test_fertilizer_double_no_profit_fact(): facts_no_profit = {"fertilizer_uah": 1_521_084.0} ok, text = compute_scenario("добрива вдвічі яким буде прибуток?", facts_no_profit) assert ok is False def test_fertilizer_double_no_fertilizer_fact(): facts_no_fert = {"profit_uah": 5_972_016.0} ok, text = compute_scenario("добрива вдвічі?", facts_no_fert) assert ok is False def test_merge_doc_facts_basic(): old = {"profit_uah": 5_972_016.0} new = {"fertilizer_uah": 1_521_084.0, "area_ha": 497.0} merged = merge_doc_facts(old, new) assert merged["profit_uah"] == 5_972_016.0 assert merged["fertilizer_uah"] == 1_521_084.0 assert merged["area_ha"] == 497.0 def test_merge_conflict_detection(): old = {"profit_uah": 5_972_016.0} # Значення відрізняється > 1% new = {"profit_uah": 3_000_000.0} merged = merge_doc_facts(old, new) assert "conflicts" in merged assert "profit_uah" in merged["conflicts"] assert merged.get("needs_recheck") is True # Старе значення збережено assert merged["profit_uah"] == 5_972_016.0 def test_extract_then_compute(): """E2E: extract → compute scenario.""" text = ( "Загальна площа — 497 га. Прибуток — 5 972 016 грн. " "Витрати на добрива — 1 521 084 грн." ) facts = extract_doc_facts(text) assert facts.get("profit_uah") assert facts.get("fertilizer_uah") ok, result = compute_scenario("якщо добрива вдвічі — прибуток?", facts) assert ok is True assert "4 450 932" in result or "4450932" in result