from __future__ import annotations import importlib.util import json import sys from argparse import Namespace from pathlib import Path def _load_tool(): path = Path(__file__).resolve().parents[1] / "tools" / "apply_page_reading_order_geometry_profile.py" spec = importlib.util.spec_from_file_location("apply_page_reading_order_geometry_profile", 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_document(root: Path) -> None: doc_dir = root / "sample-1" doc_dir.mkdir(parents=True) lines = [ ("right-low", "right low", [500, 100, 40, 10]), ("left-top", "left top", [100, 10, 40, 10]), ("right-top", "right top", [500, 10, 40, 10]), ] document_lines = [ {"line_id": line_id, "page_index": 0, "text": text, "bbox": bbox, "confidence": 0.9, "metadata": {}} for line_id, text, bbox in lines ] blocks = [ { "block_id": f"block-{index}", "block_type": "text", "page_index": 0, "bbox": bbox, "order": index, "text": text, "confidence": 0.9, "line_ids": [line_id], "table_id": None, "figure_id": None, "metadata": {}, } for index, (line_id, text, bbox) in enumerate(lines, start=1) ] payload = { "schema_version": "1.0", "source_path": "page.png", "tables": [], "figures": [], "metadata": {}, "pages": [{"page_index": 0, "width": 800, "height": 200, "metadata": {}, "text_lines": document_lines, "blocks": blocks}], } (doc_dir / "document.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") def _write_document_with_sample(root: Path, sample_id: str) -> None: doc_dir = root / sample_id doc_dir.mkdir(parents=True) lines = [ ("a", f"{sample_id} a", [100, 10, 40, 10]), ("b", f"{sample_id} b", [500, 10, 40, 10]), ] payload = { "schema_version": "1.0", "source_path": f"{sample_id}.png", "tables": [], "figures": [], "metadata": {}, "pages": [ { "page_index": 0, "width": 800, "height": 200, "metadata": {}, "text_lines": [ {"line_id": line_id, "page_index": 0, "text": text, "bbox": bbox, "confidence": 0.9, "metadata": {}} for line_id, text, bbox in lines ], "blocks": [ { "block_id": f"block-{index}", "block_type": "text", "page_index": 0, "bbox": bbox, "order": index, "text": text, "confidence": 0.9, "line_ids": [line_id], "table_id": None, "figure_id": None, "metadata": {}, } for index, (line_id, text, bbox) in enumerate(lines, start=1) ], } ], } (doc_dir / "document.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") def test_apply_page_reading_order_geometry_profile_orders_by_chunks(tmp_path: Path) -> None: tool = _load_tool() source = tmp_path / "docs" _write_document(source) profile = tmp_path / "profile.json" profile.write_text( json.dumps( { "chunks": [ {"chunk_index": 1, "cx_median": 520, "y_min": 0, "y_max": 120, "width_median": 40}, {"chunk_index": 2, "cx_median": 120, "y_min": 0, "y_max": 120, "width_median": 40}, ] }, ensure_ascii=False, ) + "\n", encoding="utf-8", ) report = tool.apply_profile( Namespace( source_documents_dir=source, profile=profile, out=tmp_path / "out", x_scale=1.0, y_scale=80.0, outside_weight=2.0, ) ) assert report["assignment_count"] == 3 document = json.loads((tmp_path / "out" / "sample-1" / "document.json").read_text(encoding="utf-8")) page = document["pages"][0] assert [line["text"] for line in page["text_lines"]] == ["right top", "right low", "left top"] assert page["text_lines"][0]["metadata"]["reading_order_geometry_profile_assignment"]["chunk_index"] == 1 assert (tmp_path / "out" / "reading-order-geometry-profile-assignments.jsonl").is_file() def test_apply_page_reading_order_geometry_profile_requires_claim_safe_when_requested(tmp_path: Path) -> None: tool = _load_tool() source = tmp_path / "docs" _write_document(source) profile = tmp_path / "profile.json" profile.write_text( json.dumps( { "profile_claim_safe": False, "chunks": [ {"chunk_index": 1, "cx_median": 520, "y_min": 0, "y_max": 120, "width_median": 40}, ], }, ensure_ascii=False, ) + "\n", encoding="utf-8", ) try: tool.apply_profile( Namespace( source_documents_dir=source, profile=profile, out=tmp_path / "out", x_scale=1.0, y_scale=80.0, outside_weight=2.0, require_profile_claim_safe=True, ) ) except ValueError as exc: assert "profile_claim_safe is not true" in str(exc) else: raise AssertionError("unsafe profile should be rejected when claim-safe profile is required") def test_apply_page_reading_order_geometry_profile_uses_profile_map_per_sample(tmp_path: Path) -> None: tool = _load_tool() source = tmp_path / "docs" _write_document_with_sample(source, "left-first") _write_document_with_sample(source, "right-first") left_profile = tmp_path / "left-profile.json" right_profile = tmp_path / "right-profile.json" left_profile.write_text( json.dumps( { "profile_claim_safe": True, "chunks": [ {"chunk_index": 1, "cx_median": 120, "y_min": 0, "y_max": 100, "width_median": 40}, {"chunk_index": 2, "cx_median": 520, "y_min": 0, "y_max": 100, "width_median": 40}, ], }, ensure_ascii=False, ) + "\n", encoding="utf-8", ) right_profile.write_text( json.dumps( { "profile_claim_safe": True, "chunks": [ {"chunk_index": 1, "cx_median": 520, "y_min": 0, "y_max": 100, "width_median": 40}, {"chunk_index": 2, "cx_median": 120, "y_min": 0, "y_max": 100, "width_median": 40}, ], }, ensure_ascii=False, ) + "\n", encoding="utf-8", ) profile_map = tmp_path / "profile-map.json" profile_map.write_text( json.dumps( { "profiles": [ {"sample_id": "left-first", "profile": str(left_profile)}, {"sample_id": "right-first", "profile": str(right_profile)}, ] }, ensure_ascii=False, indent=2, ) + "\n", encoding="utf-8", ) report = tool.apply_profile( Namespace( source_documents_dir=source, profile=None, profile_map=profile_map, out=tmp_path / "out", x_scale=1.0, y_scale=80.0, outside_weight=2.0, require_profile_claim_safe=True, ) ) assert report["profile_claim_safe"] is True assert len(report["profile_usage"]) == 2 left_doc = json.loads((tmp_path / "out" / "left-first" / "document.json").read_text(encoding="utf-8")) right_doc = json.loads((tmp_path / "out" / "right-first" / "document.json").read_text(encoding="utf-8")) assert [line["text"] for line in left_doc["pages"][0]["text_lines"]] == ["left-first a", "left-first b"] assert [line["text"] for line in right_doc["pages"][0]["text_lines"]] == ["right-first b", "right-first a"]