"""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 time | Remark |
|---|---|
| 00:00 | MÆRSK & co line 2 |
kept as paragraph
| real | " + "
| real | ") 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)