File size: 6,838 Bytes
1ef1de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from __future__ import annotations

import json
import struct
from copy import deepcopy
from pathlib import Path

from ocrtech.cli import main
from ocrtech.language_registry import audit_font_renderability, audit_language_registry


ROOT = Path(__file__).resolve().parents[1]
REGISTRY = ROOT / "languages" / "nepal-v1.json"


def test_nepal_language_registry_passes_core_gates(tmp_path: Path) -> None:
    audit = audit_language_registry(REGISTRY, tmp_path / "audit")

    assert audit.passed
    assert audit.language_count >= 11
    assert audit.counted_nepal_language_count >= 10
    assert audit.first_target_ids == ["limbu"]
    assert audit.primary_scripts["limbu"] == ["Limbu / Sirijonga"]
    assert "english" in audit.primary_scripts
    assert not audit.errors
    assert (tmp_path / "audit" / "language-registry-audit.json").exists()
    assert (tmp_path / "audit" / "language-registry-audit.md").exists()


def test_language_registry_rejects_non_limbu_first(tmp_path: Path) -> None:
    payload = json.loads(REGISTRY.read_text(encoding="utf-8"))
    payload["languages"] = [payload["languages"][1], payload["languages"][0], *payload["languages"][2:]]
    registry = tmp_path / "bad-order.json"
    registry.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")

    audit = audit_language_registry(registry, tmp_path / "audit")

    assert not audit.passed
    assert "first language entry must have id 'limbu'" in audit.errors


def test_language_registry_rejects_too_few_counted_nepal_languages(tmp_path: Path) -> None:
    payload = json.loads(REGISTRY.read_text(encoding="utf-8"))
    reduced = deepcopy(payload)
    counted = 0
    for language in reduced["languages"]:
        if language["counts_toward_nepal_language_target"]:
            counted += 1
            if counted > 3:
                language["counts_toward_nepal_language_target"] = False
    registry = tmp_path / "too-few.json"
    registry.write_text(json.dumps(reduced, ensure_ascii=False), encoding="utf-8")

    audit = audit_language_registry(registry, tmp_path / "audit", min_nepal_languages=10)

    assert not audit.passed
    assert any(error.startswith("registry counts 3 Nepal languages") for error in audit.errors)


def test_language_registry_rejects_english_counting_toward_target(tmp_path: Path) -> None:
    payload = json.loads(REGISTRY.read_text(encoding="utf-8"))
    for language in payload["languages"]:
        if language["id"] == "english":
            language["counts_toward_nepal_language_target"] = True
    registry = tmp_path / "english-counted.json"
    registry.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")

    audit = audit_language_registry(registry, tmp_path / "audit")

    assert not audit.passed
    assert "english must not count toward the Nepal-language target" in audit.errors


def test_cli_audit_language_registry_writes_summary(tmp_path: Path, capsys) -> None:
    exit_code = main(["audit-language-registry", str(REGISTRY), "--out", str(tmp_path / "audit")])

    captured = capsys.readouterr()
    payload = json.loads((tmp_path / "audit" / "language-registry-audit.json").read_text(encoding="utf-8"))
    assert exit_code == 0
    assert "passed=True" in captured.out
    assert "first_targets=limbu" in captured.out
    assert payload["counted_nepal_language_count"] >= 10


def test_font_renderability_audit_scores_cmap_coverage(tmp_path: Path) -> None:
    font = tmp_path / "single-a.ttf"
    _write_minimal_cmap_font(font, 0x0041)
    registry = tmp_path / "registry.json"
    registry.write_text(
        json.dumps(
            {
                "languages": [
                    {
                        "id": "fixture",
                        "scripts": [
                            {
                                "script": "Latin",
                                "primary": True,
                                "unicode_ranges": ["U+0041"],
                            }
                        ],
                        "font_candidates": [
                            {"name": "Fixture", "path": str(font), "status": "pending_render_audit"}
                        ],
                    }
                ]
            }
        ),
        encoding="utf-8",
    )

    audit = audit_font_renderability(registry, tmp_path / "audit")

    assert audit.passed
    assert audit.script_count == 1
    assert audit.candidates[0].coverage_ratio == 1.0
    assert (tmp_path / "audit" / "font-renderability-audit.json").exists()
    assert (tmp_path / "audit" / "font-renderability-audit.md").exists()


def test_cli_font_renderability_audit_fails_missing_coverage(tmp_path: Path, capsys) -> None:
    font = tmp_path / "single-a.ttf"
    _write_minimal_cmap_font(font, 0x0041)
    registry = tmp_path / "registry.json"
    registry.write_text(
        json.dumps(
            {
                "languages": [
                    {
                        "id": "fixture",
                        "scripts": [
                            {
                                "script": "Latin",
                                "primary": True,
                                "unicode_ranges": ["U+0042"],
                            }
                        ],
                        "font_candidates": [
                            {"name": "Fixture", "path": str(font), "status": "pending_render_audit"}
                        ],
                    }
                ]
            }
        ),
        encoding="utf-8",
    )

    exit_code = main(["audit-font-renderability", str(registry), "--out", str(tmp_path / "audit")])

    captured = capsys.readouterr()
    payload = json.loads((tmp_path / "audit" / "font-renderability-audit.json").read_text(encoding="utf-8"))
    assert exit_code == 1
    assert "passed=False" in captured.out
    assert payload["errors"] == ["fixture:Latin: no font candidate covers at least 95.00% of required codepoints"]
    assert payload["candidates"][0]["missing_sample"] == ["U+0042"]


def _write_minimal_cmap_font(path: Path, codepoint: int) -> None:
    seg_count = 2
    end_codes = [codepoint, 0xFFFF]
    start_codes = [codepoint, 0xFFFF]
    id_deltas = [1, 1]
    id_range_offsets = [0, 0]
    subtable = b"".join(
        [
            struct.pack(">HHHHHHH", 4, 32, 0, seg_count * 2, 0, 0, 0),
            struct.pack(">HH", *end_codes),
            struct.pack(">H", 0),
            struct.pack(">HH", *start_codes),
            struct.pack(">hh", *id_deltas),
            struct.pack(">HH", *id_range_offsets),
        ]
    )
    cmap = struct.pack(">HHHHL", 0, 1, 3, 1, 12) + subtable
    offset_table = struct.pack(">LHHHH", 0x00010000, 1, 0, 0, 0)
    table_record = b"cmap" + struct.pack(">LLL", 0, len(offset_table) + 16, len(cmap))
    path.write_bytes(offset_table + table_record + cmap)