"""volve_dia.ocr without the service: block parsing, the loop cut, the config and service checks and the page cache.""" import dataclasses import httpx import pytest from volve_dia import ocr CFG = dataclasses.replace(ocr.load_config(), loop_chars=60, loop_max_unit=10) PAGE = ('

Summary report

' '

Operations

' '
' '
' '
Start timeRemark
00:00MÆRSK & co
line 2
' '

kept as paragraph

') def test_html_blocks(): blocks = ocr.html_blocks(PAGE, 3) assert [b.label for b in blocks] == ["page_header", "section_header", "table", "paragraph"] # empty block dropped assert [b.raw_label for b in blocks] == ["Page-Header", "Section-Header", "Table", "Weird"] assert [b.reading_order for b in blocks] == [0, 1, 2, 3] and {b.page for b in blocks} == {3} assert blocks[0].bbox == [10.0, 20.0, 990.0, 60.0] and blocks[3].bbox == [] assert blocks[2].text == "Start time | Remark\n00:00 | MÆRSK & co\nline 2" assert blocks[2].html.startswith("") and blocks[1].html == "

Operations

" def test_bbox(): assert ocr.bbox(["1", "2", "3", "4.06"]) == [1.0, 2.0, 3.0, 4.1] assert ocr.bbox({"xyxy_normalized_1000": [1, 2, 3, 4]}) == [1.0, 2.0, 3.0, 4.0] assert ocr.bbox([1, 2, 3]) == [] and ocr.bbox(None) == [] def test_loop_start_cuts_a_trailing_repetition(): head = "Real text. " assert ocr.loop_start(head + "" * 20, CFG) == len(head) # the cut goes back to where the repetition starts, here the head's own closing tag assert ocr.loop_start("" + "" * 20, CFG) == len("") assert ocr.loop_start("no loop here, just text " * 2, CFG) is None # shorter than loop_chars assert ocr.loop_start("".join(chr(65 + i % 26) + str(i) for i in range(40)), CFG) is None def test_prompt_hash_is_checked(tmp_path): prompt = tmp_path / "prompt.txt" prompt.write_text("a different prompt") cfg = tmp_path / "ocr.yaml" cfg.write_text(ocr.CONFIG.read_text().replace("prompt_file: configs/ocr-prompt.txt", f"prompt_file: {prompt}")) with pytest.raises(ocr.OCRError, match="prompt changed"): ocr.load_config(cfg) def test_cache_is_keyed_by_settings(tmp_path, monkeypatch): monkeypatch.setenv("VOLVE_DIA_OUTPUTS", str(tmp_path)) rec = {"doc_sha256": "ab" * 32, "page": 2, **CFG.key(), "status": "ok", "content": PAGE} ocr._write(rec) assert ocr.page_file(rec["doc_sha256"], 2).is_file() assert ocr.cached(rec["doc_sha256"], 2, CFG)["content"] == PAGE assert ocr.cached(rec["doc_sha256"], 1, CFG) is None assert ocr.cached(rec["doc_sha256"], 2, dataclasses.replace(CFG, dpi=150)) is None blocks, records = ocr.load_blocks(rec["doc_sha256"], 2, CFG) assert [r["status"] for r in records] == ["missing", "ok"] and len(blocks) == 4 and blocks[0].page == 2 def test_check_service_refuses_a_service_that_is_down_or_serves_another_model(): def client(status, body): return httpx.Client(base_url="http://ocr.invalid", transport=httpx.MockTransport( lambda request: httpx.Response(status, **body))) ocr.check_service(client(200, {"json": {"data": [{"id": "chandra"}]}}), CFG) for status, body, message in [(503, {"text": "Pod not running"}, "HTTP 503"), (200, {"json": {"data": [{"id": "other"}]}}, "does not serve 'chandra'"), (200, {"text": "login"}, "did not answer JSON")]: with pytest.raises(ocr.OCRError, match=message) as err: ocr.check_service(client(status, body), CFG) assert "ocr.invalid" not in str(err.value)
real
real