from __future__ import annotations import importlib.util import json import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] TOOL_PATH = ROOT / "tools/audit_newari_bhujimol_reference_chart.py" REGISTRY = ROOT / "languages/nepal-v1.json" PIPELINE = ROOT / "configs/nepal-language-pack-pipeline-v1.json" FROZEN_SOURCE = ROOT / "outputs/newari-bhujimol-reference-chart-audit-2026-07-15-r37" def _load_tool(): spec = importlib.util.spec_from_file_location( "audit_newari_bhujimol_reference_chart", TOOL_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 _evidence(tool) -> dict: return { "metadata_sha256": tool.EXPECTED_METADATA_SHA256, "metadata": { "page_id": tool.EXPECTED_PAGE_ID, "page_title": tool.EXPECTED_PAGE_TITLE, "file_url": tool.SOURCE_FILE, "size_bytes": tool.EXPECTED_SIZE, "width": tool.EXPECTED_WIDTH, "height": tool.EXPECTED_HEIGHT, "sha1": tool.EXPECTED_SVG_SHA1, "license_short_name": "CC0", "license_url": "http://creativecommons.org/publicdomain/zero/1.0/deed.en", "usage_terms": "Creative Commons Zero, Public Domain Dedication", "artist_html": 'Z3lvs', "credit_html": "Own work", "description": tool.EXPECTED_DESCRIPTION, "created_date": "2026-05-24", "categories": ( "CC-Zero|Bhujimol script|Self-published work|" "Glyphs of Brahmic scripts in SVG" ), }, "svg": { "sha256": tool.EXPECTED_SVG_SHA256, "sha1": tool.EXPECTED_SVG_SHA1, "size_bytes": tool.EXPECTED_SIZE, "root_tag": "svg", "width_attribute": "1322.9541", "height_attribute": "712.7514", "view_box": tool.EXPECTED_VIEWBOX, "tag_counts": {"path": tool.EXPECTED_PATH_COUNT, "svg": 1}, "path_count": tool.EXPECTED_PATH_COUNT, "paths_with_id": 0, "paths_with_empty_d": 0, "path_fill_values": ["#231f20"], "non_whitespace_text_node_count": 0, "external_reference_count": 0, "external_references": [], "font_element_count": 0, "script_element_count": 0, "raster_image_element_count": 0, "foreign_object_element_count": 0, }, "registry": json.loads(REGISTRY.read_text(encoding="utf-8")), "pipeline": json.loads(PIPELINE.read_text(encoding="utf-8")), } def _newar(evidence: dict) -> dict: return next( row for row in evidence["registry"]["languages"] if row["id"] == "newar" ) def _bhujimol(evidence: dict) -> dict: return next( row for row in _newar(evidence)["scripts"] if row["profile_id"] == "bhujimol" ) def _asset(evidence: dict) -> dict: return _bhujimol(evidence)["reference_assets"][0] def test_reference_chart_policy_passes_without_font_or_ocr_claim() -> None: tool = _load_tool() report = tool._evaluate_evidence(_evidence(tool)) assert report["passed"] is True assert report["status"] == tool.PASS_STATUS assert report["license_decision"]["own_work_and_license_verified"] is True assert report["reference_decision"] == { "reference_verified": True, "use": "optional_qualified_reviewer_basic_shape_orientation", "is_font": False, "arbitrary_text_rendering_supported": False, "explicit_machine_readable_codepoint_mapping": False, "shaping_rules_present": False, "conjunct_coverage_audited": False, "manuscript_style_validated": False, "training_payload_admitted": False, } assert report["claim_boundary"] == { "bhujimol_font_admitted": False, "bhujimol_real_pixels_admitted": False, "bhujimol_labels_admitted": False, "audited_real_line_count": 0, "model_admitted": False, "predictions_computed": False, "codepoint_cer_computed": False, "four_script_ocr_claim_ready": False, } assert all(report["checks"].values()) @pytest.mark.parametrize( ("mutation", "expected_check"), [ ("metadata_hash", "metadata_snapshot_exact"), ("page_identity", "commons_page_identity"), ("file_identity", "commons_file_identity"), ("license", "commons_cc0_license"), ("author", "commons_own_work_author_date"), ("description", "commons_description_exact"), ("svg_hash", "svg_bytes_exact"), ("dimensions", "svg_dimensions_exact"), ("path_count", "svg_outline_structure_exact"), ("path_id", "svg_has_no_explicit_machine_mapping"), ("font", "svg_has_no_font_or_shaping_program"), ("external", "svg_has_no_external_or_raster_resource"), ("asset_status", "registry_reference_asset_exact"), ("asset_training", "registry_reference_asset_exact"), ("profile_promotion", "bhujimol_profile_remains_unpromoted"), ("font_promotion", "no_bhujimol_font_binding"), ("model_promotion", "no_bhujimol_model_or_dataset"), ("numeric_cer", "no_numeric_cer_claim"), ], ) def test_reference_chart_policy_rejects_drift( mutation: str, expected_check: str ) -> None: tool = _load_tool() evidence = _evidence(tool) if mutation == "metadata_hash": evidence["metadata_sha256"] = "0" * 64 elif mutation == "page_identity": evidence["metadata"]["page_id"] = 1 elif mutation == "file_identity": evidence["metadata"]["file_url"] = "https://example.invalid/chart.svg" elif mutation == "license": evidence["metadata"]["license_short_name"] = "All rights reserved" elif mutation == "author": evidence["metadata"]["artist_html"] = "unknown" elif mutation == "description": evidence["metadata"]["description"] = "font with shaping" elif mutation == "svg_hash": evidence["svg"]["sha256"] = "0" * 64 elif mutation == "dimensions": evidence["svg"]["view_box"] = "0 0 1 1" elif mutation == "path_count": evidence["svg"]["path_count"] = 110 elif mutation == "path_id": evidence["svg"]["paths_with_id"] = 1 elif mutation == "font": evidence["svg"]["font_element_count"] = 1 elif mutation == "external": evidence["svg"]["external_reference_count"] = 1 elif mutation == "asset_status": _asset(evidence)["status"] = "font_ready" elif mutation == "asset_training": _asset(evidence)["training_payload_admitted"] = True elif mutation == "profile_promotion": _bhujimol(evidence)["ocr_support_status"] = "model_ready" elif mutation == "font_promotion": next( row for row in _newar(evidence)["font_candidates"] if row["name"] == "Nithya Ranjana NU" )["writing_system_profiles"].append("bhujimol") elif mutation == "model_promotion": evidence["pipeline"]["script_recognizers"]["newa_bhujimol"]["artifact"] = ( "model" ) elif mutation == "numeric_cer": evidence["pipeline"]["script_recognizers"]["newa_bhujimol"]["eval"] += ( " CER: 0.1" ) report = tool._evaluate_evidence(evidence) assert report["passed"] is False assert report["checks"][expected_check] is False assert f"failed {expected_check}" in report["errors"] def test_reference_chart_audit_fails_closed_on_missing_inputs(tmp_path: Path) -> None: tool = _load_tool() missing = tmp_path / "missing" report = tool.audit_chart(missing, missing, missing, missing) assert report["passed"] is False assert len(report["errors"]) == 4 def test_frozen_reference_source_reaudits() -> None: tool = _load_tool() report = tool.audit_chart( FROZEN_SOURCE / "commons-source-metadata.json", FROZEN_SOURCE / "bhujimol-basic-glyph-chart.svg", REGISTRY, PIPELINE, ) assert report["passed"] is True assert report["vector_structure"]["path_count"] == 111 assert report["vector_structure"]["tag_counts"] == {"path": 111, "svg": 1} def test_reference_chart_writer_is_no_clobber(tmp_path: Path) -> None: tool = _load_tool() report = tool._evaluate_evidence(_evidence(tool)) metadata = tmp_path / "metadata.json" metadata.write_text("{}\n", encoding="utf-8") svg = tmp_path / "chart.svg" svg.write_text("\n", encoding="utf-8") out = tmp_path / "audit" tool.write_report(report, metadata, svg, out) assert (out / "commons-source-metadata.json").read_bytes() == b"{}\n" assert (out / "bhujimol-basic-glyph-chart.svg").read_bytes() == b"\n" assert (out / "reference-chart-audit.json").is_file() assert (out / "PROGRESS.md").is_file() assert (out / "SHA256SUMS").is_file() with pytest.raises(FileExistsError, match="refusing to overwrite"): tool.write_report(report, metadata, svg, out)