File size: 5,928 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 163 | 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
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_candidate_tsv(path: Path, crop: Path) -> None:
fieldnames = [
"priority_rank",
"review_priority",
"review_category",
"suggested_review_action",
"row_id",
"sample_id",
"candidate_line_id",
"candidate_structural_role",
"candidate_confidence",
"max_selected_iou",
"candidate_bbox",
"candidate_text",
"best_selected_text",
"crop_path",
"review_status",
"corrected_text",
"reviewer",
"notes",
]
with path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames, delimiter="\t", lineterminator="\n")
writer.writeheader()
writer.writerow(
{
"priority_rank": "1",
"review_priority": "high",
"review_category": "body_line_candidate",
"suggested_review_action": "transcribe_visible_line_or_reject",
"row_id": "candidate-only-0001-p001",
"sample_id": "p001",
"candidate_line_id": "img-line-0011",
"candidate_structural_role": "text",
"candidate_confidence": "0.47",
"max_selected_iou": "0.31",
"candidate_bbox": "[10,20,100,30]",
"candidate_text": "क्ङ्बा परक्",
"best_selected_text": "nearby",
"crop_path": str(crop),
"review_status": "pending",
"corrected_text": "",
"reviewer": "",
"notes": "",
}
)
def _write_queue(tmp_path: Path) -> Path:
prepare = _load_tool("prepare_limbu_book_missing_line_review_queue")
book = tmp_path / "book"
book.mkdir()
(book / "book-run.json").write_text(
json.dumps(
{
"pages": [
{
"page_number": 1,
"page_id": "p001",
"image_path": str(tmp_path / "page.png"),
"output_dir": str(book / "pages" / "p001"),
"book_document_dir": str(book / "line-assembled-documents" / "p001"),
}
]
},
ensure_ascii=False,
)
+ "\n",
encoding="utf-8",
)
page = tmp_path / "page.png"
Image.new("RGB", (180, 120), "white").save(page)
crop = tmp_path / "crop.png"
Image.new("RGB", (40, 20), "white").save(crop)
candidate_tsv = tmp_path / "candidate.tsv"
_write_candidate_tsv(candidate_tsv, crop)
out = tmp_path / "queue"
prepare.prepare_queue(
Namespace(
book_output_dir=book,
candidate_review_file=candidate_tsv,
out=out,
pending_only=True,
)
)
return out
def test_audit_limbu_book_missing_line_review_queue_passes_clean_artifact(tmp_path: Path) -> None:
audit_tool = _load_tool("audit_limbu_book_missing_line_review_queue")
queue = _write_queue(tmp_path)
audit = audit_tool.audit_queue(queue)
assert audit["audit_passed"] is True
assert audit["operational_review_queue_safe"] is True
assert audit["row_count"] == 1
assert audit["pending_count"] == 1
assert audit["accepted_count"] == 0
assert audit["page_context_count"] == 1
assert audit["category_counts"] == {"body_line_candidate": 1}
assert (queue / "book-missing-line-review-dashboard.html").exists()
assert (queue / "book-missing-line-review-queue-audit.json").exists()
def test_audit_limbu_book_missing_line_review_queue_rejects_tampered_crop_hash(tmp_path: Path) -> None:
audit_tool = _load_tool("audit_limbu_book_missing_line_review_queue")
queue = _write_queue(tmp_path)
tsv = queue / "book-missing-line-review-queue.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_queue(queue, tmp_path / "audit-tampered")
assert audit["audit_passed"] is False
assert any("review TSV SHA-256 mismatch" in issue for issue in audit["issues"])
assert any("crop SHA-256 mismatch" in issue for issue in audit["issues"])
def test_audit_limbu_book_missing_line_review_queue_rejects_tampered_dashboard_hash(tmp_path: Path) -> None:
audit_tool = _load_tool("audit_limbu_book_missing_line_review_queue")
queue = _write_queue(tmp_path)
report_path = queue / "book-missing-line-review-queue.json"
report = json.loads(report_path.read_text(encoding="utf-8"))
report["review_dashboard_sha256"] = "0" * 64
report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
audit = audit_tool.audit_queue(queue, tmp_path / "audit-dashboard-tampered")
assert audit["audit_passed"] is False
assert any("review dashboard SHA-256 mismatch" in issue for issue in audit["issues"])
|