from __future__ import annotations import importlib.util import json import sys from argparse import Namespace from pathlib import Path from ocrtech.manifest import ManifestEntry, sha256_file, write_manifest from ocrtech.schemas import BBox, Block, Document, Page, TextLine def _load_tool(): path = Path(__file__).resolve().parents[1] / "tools" / "audit_page_ocr_documents.py" spec = importlib.util.spec_from_file_location("audit_page_ocr_documents", path) assert spec is not None assert spec.loader is not None module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) return module def _write_fixture(tmp_path: Path, *, copy_eval_image: bool = False) -> tuple[Path, Path]: source_image = tmp_path / "prepared.png" source_image.write_bytes(b"same prepared image bytes") eval_image = tmp_path / "prepared-copy.png" if copy_eval_image else source_image if copy_eval_image: eval_image.write_bytes(source_image.read_bytes()) crop = tmp_path / "crop.png" crop.write_bytes(b"line crop bytes") manifest = tmp_path / "manifest.jsonl" write_manifest( [ ManifestEntry( sample_id="page-1", dataset="unit-pages", split="eval", image_path=str(eval_image), text="ᤁᤂᤃ", sha256=sha256_file(eval_image), ) ], manifest, ) document = Document( source_path=str(source_image), pages=[ Page( page_index=0, width=100, height=120, text_lines=[ TextLine( text="ᤁᤂᤃ", bbox=BBox(10, 12, 50, 14), confidence=0.91, page_index=0, line_id="line-1", metadata={ "detector_source_pass": "yolo-model", "structural_role": "text", "crop_path": str(crop), "crop_sha256": sha256_file(crop), }, ) ], blocks=[ Block( block_id="block-1", block_type="text", page_index=0, bbox=BBox(10, 12, 50, 14), order=0, text="ᤁᤂᤃ", line_ids=["line-1"], ) ], ) ], metadata={"ocr_output_kind": "live_detector", "line_count": 1}, ) docs = tmp_path / "documents" sample_dir = docs / "page-1" sample_dir.mkdir(parents=True) (sample_dir / "document.json").write_text( json.dumps(document.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) return manifest, docs def _args(manifest: Path, docs: Path, **overrides) -> Namespace: values = { "eval_manifest": manifest, "documents_dir": docs, "out": None, "expected_ocr_output_kind": "live_detector", "expected_detector_source_pass": "yolo-model", "require_source_image": True, "require_document_source_match": True, "require_line_crops": True, "require_line_confidence": True, "allow_empty_text": False, "bbox_tolerance_px": 2.0, "min_line_count": 1, "max_line_count": None, } values.update(overrides) return Namespace(**values) def test_audit_page_ocr_documents_writes_per_page_output_audits(tmp_path: Path) -> None: tool = _load_tool() manifest, docs = _write_fixture(tmp_path, copy_eval_image=True) result = tool.audit_page_ocr_documents(_args(manifest, docs)) assert result["passed"] is True assert result["passed_count"] == 1 sample_audit = docs / "page-1" / "limbu-output-audit.json" assert sample_audit.is_file() payload = json.loads(sample_audit.read_text(encoding="utf-8")) assert payload["passed"] is True assert payload["source_binding"]["match_kind"] == "same_sha256" assert payload["line_count"] == 1 assert payload["crop_hash_checked_count"] == 1 assert (docs / "page-ocr-documents-audit.json").is_file() def test_audit_page_ocr_documents_rejects_source_image_mismatch(tmp_path: Path) -> None: tool = _load_tool() manifest, docs = _write_fixture(tmp_path) document_path = docs / "page-1" / "document.json" payload = json.loads(document_path.read_text(encoding="utf-8")) wrong = tmp_path / "wrong.png" wrong.write_bytes(b"different image") payload["source_path"] = str(wrong) document_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") result = tool.audit_page_ocr_documents(_args(manifest, docs)) assert result["passed"] is False assert result["failed_count"] == 1 sample = result["samples"][0] assert any("does not match eval input by path or SHA-256" in issue for issue in sample["issues"]) assert json.loads((docs / "page-1" / "limbu-output-audit.json").read_text(encoding="utf-8"))["passed"] is False def test_audit_page_ocr_documents_rejects_tampered_crop_hash(tmp_path: Path) -> None: tool = _load_tool() manifest, docs = _write_fixture(tmp_path) document_path = docs / "page-1" / "document.json" payload = json.loads(document_path.read_text(encoding="utf-8")) payload["pages"][0]["text_lines"][0]["metadata"]["crop_sha256"] = "0" * 64 document_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") result = tool.audit_page_ocr_documents(_args(manifest, docs)) assert result["passed"] is False assert any("crop_sha256 mismatch" in issue for issue in result["samples"][0]["issues"])