"""Faz 5 (#23) birim testleri — VLM captioning (hermetik: gerçek API gerektirmez). Sağlayıcı-bağımsız istemci + zarif degradasyon + saf yardımcılar (target_visual_pages, merge_visual_units). Gerçek VLM çağrısı DEMO'da (key gelince).""" import json import unittest from unittest import mock from src.providers.vlm import VLMCaptioner from src.ingest.canonical import CanonicalUnit from src.ingest.visual_caption import (VISUAL_KIND, target_visual_pages, merge_visual_units, caption_visual_units) def _yanit(payload): """2xx gövdeli `urlopen` taklidi.""" class _R: def read(self_inner): return json.dumps(payload).encode() def __enter__(self_inner): return self_inner def __exit__(self_inner, *a): return False return _R() def _u(page, block_no, kind="paragraph", text="metin"): return CanonicalUnit( span_id=f"doc#{page}.{block_no}", doc_id="doc", sinif="12", ders="biyoloji", kaynak_turu="ders_kitabi", page=page, bbox=(0, 0, 400, 560), block_no=block_no, kind=kind, text=text, page_visual="mixed", retrieval_disi=False) class AvailabilityTests(unittest.TestCase): def test_unavailable_without_config(self): c = VLMCaptioner(model=None, base_url=None, api_key=None) self.assertFalse(c.available()) self.assertEqual(c.caption(b"\x89PNG..."), "") # HTTP yapmaz r = c.caption_with_usage(b"\x89PNG...") self.assertEqual(r.text, "") self.assertEqual(r.usage.total, 0) # boş Usage def test_available_with_full_config(self): c = VLMCaptioner(model="m", base_url="https://x/v1", api_key="k") self.assertTrue(c.available()) def test_partial_config_unavailable(self): self.assertFalse(VLMCaptioner(model="m", base_url="https://x/v1", api_key=None).available()) self.assertFalse(VLMCaptioner(model=None, base_url="https://x/v1", api_key="k").available()) class TargetPagesTests(unittest.TestCase): def test_only_figure_heavy(self): pv = {1: "low_visual", 2: "figure_heavy", 3: "mixed", 4: "figure_heavy"} self.assertEqual(target_visual_pages(pv), [2, 4]) def test_custom_classes(self): pv = {1: "low_visual", 2: "figure_heavy", 3: "mixed"} self.assertEqual(target_visual_pages(pv, ("figure_heavy", "mixed")), [2, 3]) class MergeTests(unittest.TestCase): def test_inserts_after_page_last_text_unit(self): text = [_u(1, 0), _u(1, 1), _u(2, 0)] vis = [_u(1, 8000, VISUAL_KIND, "gorsel1")] merged = merge_visual_units(text, vis) # sayfa 1'in son metin biriminden (block 1) SONRA, sayfa 2'den ÖNCE self.assertEqual([(u.page, u.block_no) for u in merged], [(1, 0), (1, 1), (1, 8000), (2, 0)]) def test_textless_visual_page_appended_end(self): text = [_u(1, 0)] vis = [_u(5, 8000, VISUAL_KIND, "gorsel5")] # sayfa 5'te metin yok merged = merge_visual_units(text, vis) self.assertEqual([(u.page, u.block_no) for u in merged], [(1, 0), (5, 8000)]) def test_empty_visuals_unchanged(self): text = [_u(1, 0), _u(1, 1)] self.assertIs(merge_visual_units(text, []), text) class GracefulCaptionPassTests(unittest.TestCase): def test_unavailable_captioner_no_units(self): # captioner None → boş; kullanılamaz captioner → boş (dosyaya bile dokunmaz) self.assertEqual(caption_visual_units( "yok.pdf", doc_id="d", sinif="12", ders="biyoloji", kaynak_turu="ders_kitabi", page_visual={1: "figure_heavy"}, captioner=None), []) c = VLMCaptioner(model=None, base_url=None, api_key=None) self.assertEqual(caption_visual_units( "yok.pdf", doc_id="d", sinif="12", ders="biyoloji", kaynak_turu="ders_kitabi", page_visual={1: "figure_heavy"}, captioner=c), []) class GatewayBodyErrorTests(unittest.TestCase): """GEÇİT 2xx GÖVDESİNDE HATA DÖNDÜREBİLİR (Kilo/OpenRouter, yük altında HTTP 200 + `{"error":{"code":503,...}}`). Eskiden bu yanıt `data["choices"]` KeyError'ına düşüyor, modülün geniş `except`'i onu YUTUYOR ve görsel SESSİZCE betimsiz kalıyordu: geçici bir yük indekse kalıcı olarak eksik bir birim yazardı. Zarif degradasyon sözleşmesi korunur (kalıcı hata yine boş CaptionResult), ama geçici hata artık deneyerek geçilir. """ _BETIM = {"choices": [{"message": {"content": "sekil: besgen"}}], "usage": {}} def _captioner(self): return VLMCaptioner(model="m", base_url="https://gecit/v1", api_key="k", timeout=1.0) def _yama(self, yanitlar): cagri = {"n": 0} def _say(req, timeout=None): cagri["n"] += 1 yanit = yanitlar[cagri["n"] - 1] if isinstance(yanit, BaseException): raise yanit return yanit return (mock.patch("urllib.request.urlopen", side_effect=_say), mock.patch("src.providers.resilience.backoff_delay", return_value=0.0), cagri) def test_govdede_gecici_hata_yeniden_denenir(self): yanitlar = [_yanit({"error": {"message": "Service temporarily overloaded", "code": 503}}), _yanit(self._BETIM)] p1, p2, cagri = self._yama(yanitlar) with p1, p2: out = self._captioner().caption_with_usage(b"\x89PNG...") self.assertEqual(cagri["n"], 2) self.assertEqual(out.text, "sekil: besgen") def test_govdede_kalici_hata_ilk_denemede_boş_doner(self): """Kalıcı (400) hata: TEK deneme; sözleşme gereği boş CaptionResult.""" yanitlar = [_yanit({"error": {"message": "model bulunamadi", "code": 400}})] p1, p2, cagri = self._yama(yanitlar) with p1, p2: out = self._captioner().caption_with_usage(b"\x89PNG...") self.assertEqual(cagri["n"], 1) self.assertEqual(out.text, "") def test_gercek_http_5xx_yeniden_denenir(self): import urllib.error yanitlar = [urllib.error.HTTPError("u", 503, "m", {}, None), _yanit(self._BETIM)] p1, p2, cagri = self._yama(yanitlar) with p1, p2: out = self._captioner().caption_with_usage(b"\x89PNG...") self.assertEqual(cagri["n"], 2) self.assertEqual(out.text, "sekil: besgen") if __name__ == "__main__": unittest.main()