-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_script_sanity.py
More file actions
172 lines (149 loc) · 6.12 KB
/
test_script_sanity.py
File metadata and controls
172 lines (149 loc) · 6.12 KB
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
"""Phase 15 Task 2 unit tests — Unicode / script-sanity checker."""
from __future__ import annotations
import logging
import pytest
from forgelm._script_sanity import (
DEFAULT_THRESHOLD,
SUPPORTED_LANGUAGES,
ScriptSanityReport,
check_script_sanity,
report_to_structured,
)
class TestDefaults:
def test_threshold_is_calibrated_for_audit_findings(self):
# Phase 15 calibration: catches 7 % corruption (front matter) and
# 3 % corruption (bullet chapter) while keeping 1 % mixed-script
# corpora silent. 1.5 % sits in the middle.
assert DEFAULT_THRESHOLD == pytest.approx(0.015)
def test_supported_languages_includes_turkish_and_english(self):
assert "tr" in SUPPORTED_LANGUAGES
assert "en" in SUPPORTED_LANGUAGES
class TestCheckScriptSanity:
def test_fires_on_audit_canonical_glyphs(self, caplog):
# The audit's two-chunk front-matter corruption shape: ~7 % weird chars.
# Mix a smaller clean prefix with enough corrupt glyphs to clear
# the 1.5 % threshold by a clean margin.
text = "Sadece düzgün bir Türkçe paragraf. " * 10 + "øø÷÷ÕÕúú" * 8
with caplog.at_level(logging.WARNING):
report = check_script_sanity(
text,
language_hint="tr",
file_path="probe.pdf",
threshold=0.02,
profile="none",
)
assert report.triggered
assert any(g == "ø" for g, _ in report.top_offenders)
# Warning fired loudly enough that a CI log scrape catches it.
assert any("Script-sanity warning" in r.message for r in caplog.records)
def test_silent_on_clean_turkish(self):
clean = "Türkçe metin doğru karakterler içerir. " * 50
report = check_script_sanity(clean, language_hint="tr", file_path="clean.txt")
assert not report.triggered
assert report.ratio == pytest.approx(0.0)
def test_silent_on_unknown_language(self):
# Unknown language hint — check is a no-op (not an error).
report = check_script_sanity("any text", language_hint="zz", file_path="x.txt")
assert not report.triggered
def test_silent_on_empty_text(self):
report = check_script_sanity("", language_hint="tr", file_path="empty.txt")
assert not report.triggered
def test_profile_allowlist_protects_bullets(self):
# Bullets are normaliser-emitted under the turkish profile, so
# they should not count as out-of-script.
text = "Bir satır • Başka satır • Yine başka • Daha fazla. " * 20
report = check_script_sanity(text, language_hint="tr", file_path="b.txt", profile="turkish")
assert not report.triggered
def test_cause_hint_identifies_font_fallback(self):
text = "Body text " * 100 + "ø" * 50 + "÷" * 50
report = check_script_sanity(
text,
language_hint="tr",
file_path="font.pdf",
threshold=0.01,
profile="none",
)
assert report.triggered
assert "font fallback" in report.cause_hint.lower()
def test_cause_hint_identifies_mojibake(self):
text = "Body text " * 100 + "�" * 100
report = check_script_sanity(
text,
language_hint="tr",
file_path="moji.txt",
threshold=0.01,
profile="none",
)
assert report.triggered
assert "mojibake" in report.cause_hint.lower()
def test_logger_override_for_test_isolation(self):
custom_logger = logging.getLogger("test.script_sanity")
captured: list = []
handler = logging.Handler()
handler.emit = lambda record: captured.append(record.getMessage())
custom_logger.addHandler(handler)
custom_logger.setLevel(logging.WARNING)
try:
check_script_sanity(
"Body. " * 50 + "øø" * 30,
language_hint="tr",
file_path="t.txt",
threshold=0.01,
profile="none",
logger_override=custom_logger,
)
# Captured by the test-local logger, not the module logger.
assert any("Script-sanity" in m for m in captured)
finally:
# Round-2 nit: avoid leaking the test handler into other tests
# that share the ``test.script_sanity`` logger name.
custom_logger.removeHandler(handler)
class TestReportToStructured:
def test_aggregate_with_triggered_reports(self):
reports = [
ScriptSanityReport(
file_path="a.txt",
language_hint="tr",
total_chars=100,
out_of_script_chars=20,
ratio=0.20,
threshold=0.015,
triggered=True,
top_offenders=(("ø", 10), ("÷", 10)),
cause_hint="pypdf font fallback (Latin-1 substitutes)",
),
ScriptSanityReport(
file_path="b.txt",
language_hint="tr",
total_chars=100,
out_of_script_chars=0,
ratio=0.0,
threshold=0.015,
triggered=False,
),
]
agg = report_to_structured(reports)
assert agg["files_checked"] == 2
assert agg["files_triggered"] == 1
assert agg["max_ratio"] == pytest.approx(0.20)
assert "per_file" in agg
assert agg["per_file"][0]["file_path"] == "a.txt"
def test_aggregate_with_no_triggered_reports(self):
reports = [
ScriptSanityReport(
file_path="clean.txt",
language_hint="tr",
total_chars=100,
out_of_script_chars=0,
ratio=0.0,
threshold=0.015,
triggered=False,
)
]
agg = report_to_structured(reports)
assert agg["files_triggered"] == 0
# No per_file when nothing triggered — keeps the JSON tight.
assert "per_file" not in agg
if __name__ == "__main__":
import sys
sys.exit(pytest.main([__file__, "-v"]))