from __future__ import annotations import csv import importlib.util import json import sys from argparse import Namespace from pathlib import Path from PIL import Image, ImageDraw from ocrtech.manifest import sha256_file def _load_tool(name: str): path = Path(__file__).resolve().parents[1] / "tools" / f"{name}.py" spec = importlib.util.spec_from_file_location(name, 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_packet(tmp_path: Path) -> Path: page = Image.new("RGB", (220, 80), "white") draw = ImageDraw.Draw(page) draw.rectangle([20, 24, 70, 42], fill="black") draw.rectangle([105, 24, 160, 42], fill="black") source = tmp_path / "page.png" page.save(source) document_path = tmp_path / "document.json" document_path.write_text( json.dumps( { "schema_version": "1.0", "source_path": str(source), "pages": [{"page_index": 0, "text_lines": [], "blocks": []}], "tables": [], "figures": [], "metadata": {}, } ) + "\n", encoding="utf-8", ) crop = tmp_path / "crop.png" page.crop((10, 18, 180, 50)).save(crop) packet = tmp_path / "packet.tsv" with packet.open("w", encoding="utf-8", newline="") as handle: fieldnames = [ "packet_row_id", "review_item_type", "book_page_id", "book_line_id", "book_document", "bbox", "text", "crop_path", "crop_sha256", "review_status", ] writer = csv.DictWriter(handle, fieldnames=fieldnames, delimiter="\t", lineterminator="\n") writer.writeheader() writer.writerow( { "packet_row_id": "existing-1", "review_item_type": "existing_book_line", "book_page_id": "p001", "book_line_id": "line-1", "book_document": str(document_path), "bbox": "[10,18,170,32]", "text": "wide noisy line", "crop_path": str(crop), "crop_sha256": sha256_file(crop), "review_status": "pending", } ) return packet def _write_suggestions(tmp_path: Path) -> Path: prepare = _load_tool("prepare_limbu_book_line_split_suggestions") report = prepare.prepare_suggestions( Namespace( review_packet=_write_packet(tmp_path), out=tmp_path / "suggestions", crop_padding=8, col_min_ink=2, max_col_gap=8, min_segment_width=20, min_segment_ink=12, split_padding=1, min_bbox_width=100.0, min_aspect_ratio=2.0, ) ) assert report["suggestion_count"] == 1 return Path(report["out"]) def test_audit_limbu_book_line_split_suggestions_passes_clean_artifact(tmp_path: Path) -> None: audit_tool = _load_tool("audit_limbu_book_line_split_suggestions") suggestions = _write_suggestions(tmp_path) audit = audit_tool.audit_suggestions(suggestions) assert audit["audit_passed"] is True assert audit["operational_review_aid_safe"] is True assert audit["suggestion_count"] == 1 assert audit["overlay_count"] == 1 assert audit["reason_counts"] == {"visual_column_projection_segments": 1} assert (suggestions / "book-line-split-suggestions-audit.json").exists() def test_audit_limbu_book_line_split_suggestions_rejects_tampered_crop_hash(tmp_path: Path) -> None: audit_tool = _load_tool("audit_limbu_book_line_split_suggestions") suggestions = _write_suggestions(tmp_path) tsv = suggestions / "book-line-split-suggestions.tsv" with tsv.open("r", encoding="utf-8", newline="") as handle: reader = csv.DictReader(handle, delimiter="\t") fieldnames = reader.fieldnames rows = list(reader) assert fieldnames is not None rows[0]["crop_sha256"] = "0" * 64 with tsv.open("w", encoding="utf-8", newline="") as handle: writer = csv.DictWriter(handle, fieldnames=fieldnames, delimiter="\t", lineterminator="\n") writer.writeheader() writer.writerows(rows) audit = audit_tool.audit_suggestions(suggestions, tmp_path / "audit") assert audit["audit_passed"] is False assert any("suggestions TSV SHA-256 mismatch" in issue for issue in audit["issues"]) assert any("crop SHA-256 mismatch" in issue for issue in audit["issues"])