"""Offline worker tests with fake inference; no model or source network access.""" import importlib.util import math import socket import struct import tempfile import unittest from pathlib import Path from types import SimpleNamespace from unittest.mock import patch SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "clinical_image_ocr.py" SPEC = importlib.util.spec_from_file_location("clinical_image_ocr", SCRIPT) ocr = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(ocr) class Image: def close(self): pass def detected(text="SYNTHETIC", confidence=0.9, box=None): return SimpleNamespace(txts=[text], scores=[confidence], boxes=[box or [[0, 0], [1, 0], [1, 1], [0, 1]]]) class WorkerTests(unittest.TestCase): def setUp(self): self.private = tempfile.TemporaryDirectory(prefix="ocr-worker-test-") self.root = Path(self.private.name) self.image = self.root / "image.png" self.image.write_bytes(b"synthetic fake image for injected decoder") self.request = {"allowed_root": str(self.root), "images": [ {"index": 7, "path": str(self.image)}, {"index": 19, "path": str(self.image)}, ], "deadline_ms": 1000, "threads": 1, "detection_limit": 640, "max_text_bytes": 4096} def tearDown(self): self.private.cleanup() def run_worker(self, request=None, factory=None, loader=None, checkpoint=None): return ocr.run(request or self.request, engine_factory=factory or (lambda *_: lambda _image: detected()), image_loader=loader or (lambda _: Image()), checkpoint=checkpoint) def test_complete_global_indices_and_one_engine(self): calls = [] def factory(threads, detection): calls.append((threads, detection)) return lambda _image: detected() result = self.run_worker(factory=factory) self.assertEqual(result["status"], "completed") self.assertEqual([row["index"] for row in result["images"]], [7, 19]) self.assertEqual(calls, [(1, 640)]) def test_invalid_indices_limits_paths_and_no_model_load(self): variants = [ {"images": [{"index": 7, "path": str(self.image)}] * 2}, {"images": [{"index": True, "path": str(self.image)}]}, {"images": [{"index": 7, "path": str(SCRIPT)}]}, {"images": [{"index": 7, "path": "https://synthetic.invalid/image.png"}]}, {"images": [{"index": 7, "path": "\\\\synthetic.invalid\\share\\image.png"}]}, {"allowed_root": "//synthetic.invalid/share"}, {"threads": 3}, {"detection_limit": 10000}, {"max_text_bytes": 9999999}, {"deadline_ms": -1}, {"deadline_ms": 0}, ] for changed in variants: with self.subTest(changed=list(changed)): result = self.run_worker({**self.request, **changed}, factory=lambda *_: self.fail("must not load model")) self.assertEqual(result["status"], "unavailable") def test_too_many_bytes_and_symlink_outside(self): with self.image.open("wb") as handle: handle.truncate(ocr.MAX_BYTES + 1) self.assertEqual(self.run_worker()["status"], "unavailable") self.image.write_bytes(b"synthetic") link = self.root / "outside.png" try: link.symlink_to(SCRIPT) except OSError: return result = self.run_worker({**self.request, "images": [{"index": 7, "path": str(link)}]}) self.assertEqual(result["status"], "unavailable") def test_pixel_header_guard_before_large_decode(self): from PIL import Image as PillowImage image = self.root / "large.bmp" image.write_bytes(b"BM" + struct.pack("