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" / "analyze_page_reading_order_geometry.py" spec = importlib.util.spec_from_file_location("analyze_page_reading_order_geometry", 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 test_analyze_page_reading_order_geometry_splits_reference_order_chunks(tmp_path: Path) -> None: tool = _load_tool() alignment = tmp_path / "line-alignment.csv" alignment.write_text( "\n".join( [ "sample_id,kind,reference_line_id,predicted_line_id,reference_bbox,predicted_bbox,page_index,iou,reference_text,predicted_text,text_codepoint_edits,text_codepoint_ref_len,text_cer_codepoint,text_grapheme_edits,text_grapheme_ref_len,text_cer_grapheme", 's,matched,span-0001,p1,"[100,100,50,10]","[100,100,50,10]",0,1,a,a,0,1,0,0,1,0', 's,matched,span-0002,p2,"[100,120,50,10]","[100,120,50,10]",0,1,b,b,0,1,0,0,1,0', 's,matched,span-0003,p3,"[600,80,50,10]","[600,80,50,10]",0,1,c,c,0,1,0,0,1,0', ] ) + "\n", encoding="utf-8", ) report = tool.analyze( Namespace( line_alignment=alignment, out=tmp_path / "out", min_dx=200.0, min_upward_dy=200.0, min_downward_dy=120.0, ) ) assert report["matched_line_count"] == 3 assert report["chunk_count"] == 2 assert report["jump_count"] == 1 assert report["chunks"][0]["start_reference_line_id"] == "span-0001" assert report["chunks"][0]["end_reference_line_id"] == "span-0002" assert report["chunks"][1]["start_reference_line_id"] == "span-0003" assert (tmp_path / "out" / "reading-order-geometry-chunks.tsv").is_file() persisted = json.loads((tmp_path / "out" / "reading-order-geometry-analysis.json").read_text(encoding="utf-8")) assert persisted["claim_boundary"].startswith("diagnostic geometry analysis")