File size: 6,003 Bytes
d82db76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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"])