| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | import json |
| 6 | import unittest |
| 7 | from unittest import mock |
| 8 | |
| 9 | from src.providers.vlm import VLMCaptioner |
| 10 | from src.ingest.canonical import CanonicalUnit |
| 11 | from src.ingest.visual_caption import (VISUAL_KIND, target_visual_pages, |
| 12 | merge_visual_units, caption_visual_units) |
| 13 | |
| 14 | |
| 15 | def _yanit(payload): |
| 16 | |
| 17 | class _R: |
| 18 | def read(self_inner): |
| 19 | return json.dumps(payload).encode() |
| 20 | |
| 21 | def __enter__(self_inner): |
| 22 | return self_inner |
| 23 | |
| 24 | def __exit__(self_inner, *a): |
| 25 | return False |
| 26 | |
| 27 | return _R() |
| 28 | |
| 29 | |
| 30 | def _u(page, block_no, kind="paragraph", text="metin"): |
| 31 | return CanonicalUnit( |
| 32 | span_id=f"doc#{page}.{block_no}", doc_id="doc", sinif="12", ders="biyoloji", |
| 33 | kaynak_turu="ders_kitabi", page=page, bbox=(0, 0, 400, 560), block_no=block_no, |
| 34 | kind=kind, text=text, page_visual="mixed", retrieval_disi=False) |
| 35 | |
| 36 | |
| 37 | class AvailabilityTests(unittest.TestCase): |
| 38 | def test_unavailable_without_config(self): |
| 39 | c = VLMCaptioner(model=None, base_url=None, api_key=None) |
| 40 | self.assertFalse(c.available()) |
| 41 | self.assertEqual(c.caption(b"\x89PNG..."), "") |
| 42 | r = c.caption_with_usage(b"\x89PNG...") |
| 43 | self.assertEqual(r.text, "") |
| 44 | self.assertEqual(r.usage.total, 0) |
| 45 | |
| 46 | def test_available_with_full_config(self): |
| 47 | c = VLMCaptioner(model="m", base_url="https://x/v1", api_key="k") |
| 48 | self.assertTrue(c.available()) |
| 49 | |
| 50 | def test_partial_config_unavailable(self): |
| 51 | self.assertFalse(VLMCaptioner(model="m", base_url="https://x/v1", api_key=None).available()) |
| 52 | self.assertFalse(VLMCaptioner(model=None, base_url="https://x/v1", api_key="k").available()) |
| 53 | |
| 54 | |
| 55 | class TargetPagesTests(unittest.TestCase): |
| 56 | def test_only_figure_heavy(self): |
| 57 | pv = {1: "low_visual", 2: "figure_heavy", 3: "mixed", 4: "figure_heavy"} |
| 58 | self.assertEqual(target_visual_pages(pv), [2, 4]) |
| 59 | |
| 60 | def test_custom_classes(self): |
| 61 | pv = {1: "low_visual", 2: "figure_heavy", 3: "mixed"} |
| 62 | self.assertEqual(target_visual_pages(pv, ("figure_heavy", "mixed")), [2, 3]) |
| 63 | |
| 64 | |
| 65 | class MergeTests(unittest.TestCase): |
| 66 | def test_inserts_after_page_last_text_unit(self): |
| 67 | text = [_u(1, 0), _u(1, 1), _u(2, 0)] |
| 68 | vis = [_u(1, 8000, VISUAL_KIND, "gorsel1")] |
| 69 | merged = merge_visual_units(text, vis) |
| 70 | |
| 71 | self.assertEqual([(u.page, u.block_no) for u in merged], |
| 72 | [(1, 0), (1, 1), (1, 8000), (2, 0)]) |
| 73 | |
| 74 | def test_textless_visual_page_appended_end(self): |
| 75 | text = [_u(1, 0)] |
| 76 | vis = [_u(5, 8000, VISUAL_KIND, "gorsel5")] |
| 77 | merged = merge_visual_units(text, vis) |
| 78 | self.assertEqual([(u.page, u.block_no) for u in merged], [(1, 0), (5, 8000)]) |
| 79 | |
| 80 | def test_empty_visuals_unchanged(self): |
| 81 | text = [_u(1, 0), _u(1, 1)] |
| 82 | self.assertIs(merge_visual_units(text, []), text) |
| 83 | |
| 84 | |
| 85 | class GracefulCaptionPassTests(unittest.TestCase): |
| 86 | def test_unavailable_captioner_no_units(self): |
| 87 | |
| 88 | self.assertEqual(caption_visual_units( |
| 89 | "yok.pdf", doc_id="d", sinif="12", ders="biyoloji", kaynak_turu="ders_kitabi", |
| 90 | page_visual={1: "figure_heavy"}, captioner=None), []) |
| 91 | c = VLMCaptioner(model=None, base_url=None, api_key=None) |
| 92 | self.assertEqual(caption_visual_units( |
| 93 | "yok.pdf", doc_id="d", sinif="12", ders="biyoloji", kaynak_turu="ders_kitabi", |
| 94 | page_visual={1: "figure_heavy"}, captioner=c), []) |
| 95 | |
| 96 | |
| 97 | class GatewayBodyErrorTests(unittest.TestCase): |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | _BETIM = {"choices": [{"message": {"content": "sekil: besgen"}}], "usage": {}} |
| 109 | |
| 110 | def _captioner(self): |
| 111 | return VLMCaptioner(model="m", base_url="https://gecit/v1", api_key="k", |
| 112 | timeout=1.0) |
| 113 | |
| 114 | def _yama(self, yanitlar): |
| 115 | cagri = {"n": 0} |
| 116 | |
| 117 | def _say(req, timeout=None): |
| 118 | cagri["n"] += 1 |
| 119 | yanit = yanitlar[cagri["n"] - 1] |
| 120 | if isinstance(yanit, BaseException): |
| 121 | raise yanit |
| 122 | return yanit |
| 123 | |
| 124 | return (mock.patch("urllib.request.urlopen", side_effect=_say), |
| 125 | mock.patch("src.providers.resilience.backoff_delay", |
| 126 | return_value=0.0), cagri) |
| 127 | |
| 128 | def test_govdede_gecici_hata_yeniden_denenir(self): |
| 129 | yanitlar = [_yanit({"error": {"message": "Service temporarily overloaded", |
| 130 | "code": 503}}), |
| 131 | _yanit(self._BETIM)] |
| 132 | p1, p2, cagri = self._yama(yanitlar) |
| 133 | with p1, p2: |
| 134 | out = self._captioner().caption_with_usage(b"\x89PNG...") |
| 135 | self.assertEqual(cagri["n"], 2) |
| 136 | self.assertEqual(out.text, "sekil: besgen") |
| 137 | |
| 138 | def test_govdede_kalici_hata_ilk_denemede_boş_doner(self): |
| 139 | |
| 140 | yanitlar = [_yanit({"error": {"message": "model bulunamadi", "code": 400}})] |
| 141 | p1, p2, cagri = self._yama(yanitlar) |
| 142 | with p1, p2: |
| 143 | out = self._captioner().caption_with_usage(b"\x89PNG...") |
| 144 | self.assertEqual(cagri["n"], 1) |
| 145 | self.assertEqual(out.text, "") |
| 146 | |
| 147 | def test_gercek_http_5xx_yeniden_denenir(self): |
| 148 | import urllib.error |
| 149 | yanitlar = [urllib.error.HTTPError("u", 503, "m", {}, None), |
| 150 | _yanit(self._BETIM)] |
| 151 | p1, p2, cagri = self._yama(yanitlar) |
| 152 | with p1, p2: |
| 153 | out = self._captioner().caption_with_usage(b"\x89PNG...") |
| 154 | self.assertEqual(cagri["n"], 2) |
| 155 | self.assertEqual(out.text, "sekil: besgen") |
| 156 | |
| 157 | |
| 158 | if __name__ == "__main__": |
| 159 | unittest.main() |