test(ocr): add business-logic tests for polygon extraction, Kraken routing, and confidence markers

Cover Surya polygon/word-level extraction, health endpoint states,
Kraken script-type routing, 503 when models not ready, 400 when
Kraken unavailable for Kurrent, and confidence marker application
during streaming. Production code coverage: 88%.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-13 10:34:23 +02:00
parent 97e5138934
commit 69768a104d
2 changed files with 154 additions and 0 deletions

View File

@@ -45,6 +45,52 @@ def test_surya_extract_page_blocks_returns_blocks_for_single_image():
assert blocks[0]["height"] == 20 / 200
def test_surya_extract_page_blocks_extracts_polygon_when_present():
image = Image.new("RGB", (100, 200))
mock_line = _make_surya_line("Text", [10, 20, 90, 40])
mock_line.polygon = [(10, 20), (90, 20), (90, 40), (10, 40)]
mock_pred = MagicMock()
mock_pred.text_lines = [mock_line]
with patch.object(surya, "_recognition_predictor") as mock_rec, \
patch.object(surya, "_loaded", True):
mock_rec.return_value = [mock_pred]
blocks = surya.extract_page_blocks(image, page_idx=1, language="de")
assert blocks[0]["polygon"] is not None
assert len(blocks[0]["polygon"]) == 4
assert blocks[0]["polygon"][0] == [10 / 100, 20 / 200]
def test_surya_extract_page_blocks_extracts_word_level_confidence():
image = Image.new("RGB", (100, 200))
word1 = MagicMock()
word1.text = "Hallo"
word1.confidence = 0.95
word2 = MagicMock()
word2.text = "Welt"
word2.confidence = 0.3
mock_line = _make_surya_line("Hallo Welt", [10, 20, 90, 40], words=[word1, word2])
mock_pred = MagicMock()
mock_pred.text_lines = [mock_line]
with patch.object(surya, "_recognition_predictor") as mock_rec, \
patch.object(surya, "_loaded", True):
mock_rec.return_value = [mock_pred]
blocks = surya.extract_page_blocks(image, page_idx=1, language="de")
assert len(blocks[0]["words"]) == 2
assert blocks[0]["words"][0]["text"] == "Hallo"
assert blocks[0]["words"][0]["confidence"] == 0.95
def test_surya_extract_blocks_delegates_to_extract_page_blocks():
"""After refactoring, extract_blocks should produce the same output."""
image1 = Image.new("RGB", (100, 200))