import json from pathlib import Path from ingestion.extract.formulas import ( BARLESS_FORMULA_BOTTOM_PT, FORMULA_BAND_HEIGHT_PT, FORMULA_SIDE_MARGIN_PT, load_formula_regions, ) from ingestion.tables import QUARANTINE_SHAPES, SHAPE_FORMULA_2D VERIFIED = (Path(__file__).resolve().parents[1] / "data" / "verified" / "formula_regions_2d.json") TRANSCRIPTIONS = (Path(__file__).resolve().parents[1] / "data" / "verified" / "outlined_text_transcriptions.json") def test_a_2d_formula_is_always_quarantined(): # linearised, "a / b" reads as "a x b" — a dosing error, not a cosmetic one assert SHAPE_FORMULA_2D in QUARANTINE_SHAPES def test_verified_formula_regions_load_with_the_confirmed_pages(): regions = load_formula_regions() assert {r.physical_page for r in regions} == { 43, 92, 147, 202, 325, 349, 1042, 1043, 1132, 1402, } assert all(r.shape == SHAPE_FORMULA_2D for r in regions) def test_the_region_covers_numerator_and_denominator_not_just_the_bar(): payload = json.loads(VERIFIED.read_text(encoding="utf-8")) bar = next(r for r in payload["regions"] if r["physical_page"] == 1042) region = next(r for r in load_formula_regions() if r.physical_page == 1042) x0, y0, x1, y1 = bar["bar_bbox"] assert region.bbox[1] == y0 - FORMULA_BAND_HEIGHT_PT assert region.bbox[3] == y1 + FORMULA_BAND_HEIGHT_PT assert region.bbox[0] == x0 - FORMULA_SIDE_MARGIN_PT def test_the_barless_adenosin_formula_is_recorded_as_a_recall_limit(): """The source prints no bar, so no geometric detector can find it. Recorded so a later reader does not mistake the fraction-bar scan for complete formula coverage — how many bar-less formulas the book contains has never been measured. """ payload = json.loads(VERIFIED.read_text(encoding="utf-8")) barless = [r for r in payload["regions"] if r.get("source_prints_no_bar")] assert [r["physical_page"] for r in barless] == [147] assert "UNMEASURED" in payload["recall_limit"] def test_barless_adenosin_band_reaches_its_printed_denominator(): payload = json.loads(VERIFIED.read_text(encoding="utf-8")) entry = next(r for r in payload["regions"] if r.get("source_prints_no_bar")) region = next(r for r in load_formula_regions() if r.physical_page == 147) assert region.bbox[3] == entry["bar_bbox"][3] + BARLESS_FORMULA_BOTTOM_PT # "Ví dụ:" begins immediately afterwards with its span centre at ~704.3; # the formula band must stop before that prose and the following table. assert region.bbox[3] < 704 def test_outlined_text_transcriptions_cover_every_detected_run(): payload = json.loads(TRANSCRIPTIONS.read_text(encoding="utf-8")) runs = payload["runs"] assert len(runs) == 51 assert all(r["text"] for r in runs), "a run with no transcription is data loss" pages = {} for run in runs: pages[run["physical_page"]] = pages.get(run["physical_page"], 0) + 1 assert pages == {714: 31, 736: 16, 1373: 1, 1444: 1, 1445: 2} def test_single_glyph_transcriptions_name_the_line_they_were_dropped_from(): """The subtlest form of the defect: one character missing mid-sentence. "Độ ổn định" extracts as "Độ n định" and reads as ordinary text, so nothing downstream can notice. Keeping the owning line in the record is what makes the repair checkable. """ payload = json.loads(TRANSCRIPTIONS.read_text(encoding="utf-8")) singles = [r for r in payload["runs"] if r["single_glyph"]] assert len(singles) == 29 with_context = [r for r in singles if r["extracted_line_it_belongs_to"]] assert with_context, "no dropped glyph could be tied back to its line"