Spaces:
Sleeping
Sleeping
File size: 15,922 Bytes
fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c c0f7ba9 fe6661c c0f7ba9 fe6661c c0f7ba9 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 9011070 fe6661c 6724f94 d641f6e 6724f94 d641f6e e45d507 9011070 fe6661c 6724f94 9011070 6724f94 d641f6e 6724f94 d641f6e fe6661c d109222 9011070 fe6661c | 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | """Tests des 5 vues HTML thรฉmatiques (chantier 3 post-Sprint 97).
Couvre :
- Importation et signature des 5 vues.
- Adaptive masking : ``""`` quand aucune sous-section n'a de signal.
- Rendu HTML cohรฉrent quand les donnรฉes sont fournies.
- Anti-injection HTML sur les noms de moteurs et libellรฉs.
- Composition correcte du shell ``<details>`` (premier ouvert,
autres fermรฉs).
- Cรขblage gรฉnรฉrator โ vues (les variables sont passรฉes au template).
"""
from __future__ import annotations
import pytest
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 1. Imports + signatures
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class TestViewsImport:
def test_all_views_import(self):
from picarones.reports.html.views import (
build_advanced_taxonomy_view_html,
build_diagnostics_view_html,
build_economics_view_html,
build_pipeline_view_html,
build_robustness_view_html,
)
assert callable(build_advanced_taxonomy_view_html)
assert callable(build_diagnostics_view_html)
assert callable(build_economics_view_html)
assert callable(build_pipeline_view_html)
assert callable(build_robustness_view_html)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 2. Adaptive masking โ vues vides retournent ""
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@pytest.fixture
def empty_report_data() -> dict:
return {"engines": []}
class TestAdaptiveMasking:
def test_economics_empty_returns_empty(self, empty_report_data):
from picarones.reports.html.views import build_economics_view_html
assert build_economics_view_html(empty_report_data, {}) == ""
def test_advanced_taxonomy_empty_returns_empty(self, empty_report_data):
from picarones.reports.html.views import build_advanced_taxonomy_view_html
assert build_advanced_taxonomy_view_html(empty_report_data, {}) == ""
def test_diagnostics_empty_returns_empty(self, empty_report_data):
from picarones.reports.html.views import build_diagnostics_view_html
assert build_diagnostics_view_html(empty_report_data, {}) == ""
def test_pipeline_empty_returns_empty(self, empty_report_data):
from picarones.reports.html.views import build_pipeline_view_html
assert build_pipeline_view_html(empty_report_data, {}) == ""
def test_robustness_empty_returns_empty(self, empty_report_data):
from picarones.reports.html.views import build_robustness_view_html
assert build_robustness_view_html(empty_report_data, {}) == ""
def test_advanced_taxonomy_single_engine_returns_empty(self):
"""La comparaison nรฉcessite โฅ 2 moteurs."""
from picarones.reports.html.views import build_advanced_taxonomy_view_html
single = {"engines": [{
"name": "tess",
"aggregated_taxonomy": {"class_distribution": {"x": 10}},
}]}
# Pas de comparison possible โ vue masquรฉe
assert build_advanced_taxonomy_view_html(single, {}) == ""
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 3. Rendu HTML quand donnรฉes fournies
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class _MockMetrics:
def __init__(self, *, cer=0.05, wer=0.1, reference_length=500):
self.cer = cer
self.wer = wer
self.reference_length = reference_length
self.error = None
class _MockDocResult:
def __init__(self, duration=1.0):
self.engine_error = None
self.duration_seconds = duration
self.metrics = _MockMetrics()
class _MockEngineReport:
def __init__(self, name, n_docs=10):
self.engine_name = name
self.document_results = [_MockDocResult() for _ in range(n_docs)]
class TestEconomicsView:
def test_throughput_with_realistic_engines(self):
from picarones.reports.html.views import build_economics_view_html
reports = [
_MockEngineReport("tesseract"),
_MockEngineReport("pero_ocr"),
]
html = build_economics_view_html(
{"engines": []}, {},
engine_reports=reports,
)
assert html != ""
# Les deux moteurs doivent apparaรฎtre dans le HTML
assert "tesseract" in html
assert "pero" in html
def test_extra_html_blocks_appended(self):
from picarones.reports.html.views import build_economics_view_html
extra = ['<div class="custom">CUSTOM_BLOCK</div>']
html = build_economics_view_html(
{"engines": []},
{"economics_extra_title": "Coรปt projetรฉ"},
engine_reports=[_MockEngineReport("tess")],
extra_html_blocks=extra,
)
assert "CUSTOM_BLOCK" in html
def test_zero_duration_excludes_engine(self):
"""Bench depuis cache (durations=0) ne gรฉnรจre pas de throughput."""
from picarones.reports.html.views import build_economics_view_html
report = _MockEngineReport("cached")
for dr in report.document_results:
dr.duration_seconds = 0.0
html = build_economics_view_html(
{"engines": []}, {}, engine_reports=[report],
)
# Aucun moteur n'a de durรฉe โ vue masquรฉe
assert html == ""
class TestAdvancedTaxonomyView:
def test_two_engines_taxonomy_compared(self):
from picarones.reports.html.views import build_advanced_taxonomy_view_html
report_data = {
"engines": [
{
"name": "tess", "cer": 0.05,
"aggregated_taxonomy": {
"class_distribution": {
"case_error": 100, "ligature_error": 50,
"lacuna": 30,
},
},
},
{
"name": "pero", "cer": 0.07,
"aggregated_taxonomy": {
"class_distribution": {
"case_error": 30, "lacuna": 80,
"diacritic_error": 60,
},
},
},
],
}
html = build_advanced_taxonomy_view_html(report_data, {})
assert html != ""
# Le diagramme miroir doit nommer les 2 moteurs
assert "tess" in html
assert "pero" in html
def test_anti_injection_engine_name(self):
"""Un nom de moteur avec balises HTML doit รชtre รฉchappรฉ."""
from picarones.reports.html.views import build_advanced_taxonomy_view_html
report_data = {
"engines": [
{
"name": "<script>alert(1)</script>",
"cer": 0.05,
"aggregated_taxonomy": {
"class_distribution": {"case_error": 10},
},
},
{
"name": "pero",
"cer": 0.07,
"aggregated_taxonomy": {
"class_distribution": {"lacuna": 10},
},
},
],
}
html = build_advanced_taxonomy_view_html(report_data, {})
# Pas de balise script non รฉchappรฉe
assert "<script>alert" not in html
# Mais le contenu doit รชtre prรฉsent sous forme รฉchappรฉe
assert "<script" in html or "alert" not in html.lower()
def test_lexical_modernization_optional(self):
from picarones.reports.html.views import build_advanced_taxonomy_view_html
report_data = {
"engines": [
{
"name": "tess", "cer": 0.05,
"aggregated_taxonomy": {
"class_distribution": {"case_error": 10},
},
},
{
"name": "pero", "cer": 0.07,
"aggregated_taxonomy": {
"class_distribution": {"case_error": 5},
},
},
],
}
# Sans lexical_modernization, la sous-section n'apparaรฎt pas
html_no = build_advanced_taxonomy_view_html(report_data, {})
# Avec, elle apparaรฎt
# Le format attendu par ``top_modernized_tokens`` est
# ``{"tokens": {gt_token: {n_total, n_modernized, rate_modernized,
# variants}}}`` (cf. ``aggregate_lexical_modernization``).
lex_data = {
"tokens": {
"maistre": {
"n_total": 10, "n_modernized": 8,
"rate_modernized": 0.8,
"variants": {"maรฎtre": 8},
},
},
}
html_yes = build_advanced_taxonomy_view_html(
report_data, {}, lexical_modernization=lex_data,
)
# Au moins une section de plus
assert len(html_yes) > len(html_no)
class TestDiagnosticsView:
def test_levers_only_when_signal(self):
"""detect_levers doit รชtre appelรฉ. Si rien ne dรฉclenche, vue masquรฉe."""
from picarones.reports.html.views import build_diagnostics_view_html
# report_data minimal โ aucun levier ne devrait se dรฉclencher
empty = {"engines": []}
assert build_diagnostics_view_html(empty, {}) == ""
def test_image_predictive_with_qualities(self):
from picarones.reports.html.views import build_diagnostics_view_html
# Liste d'image_qualities synthรฉtiques (>= 1 doc)
qualities = [
{
"contrast": 0.8, "noise_level": 0.2,
"blur_score": 0.1, "estimated_dpi": 300,
"rotation_estimate": 0.5, "low_contrast_pct": 0.05,
},
{
"contrast": 0.6, "noise_level": 0.4,
"blur_score": 0.3, "estimated_dpi": 250,
"rotation_estimate": 1.0, "low_contrast_pct": 0.10,
},
]
html = build_diagnostics_view_html(
{"engines": []}, {}, image_qualities=qualities,
)
# La section image_predictive doit s'afficher
assert html != ""
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 4. Composition du shell <details>
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class TestDetailsShell:
def test_first_block_open_others_closed(self):
from picarones.reports.html.views.economics import _render_view_shell
html = _render_view_shell(
view_title="Test",
view_note="Note",
blocks=[("A", "<p>aaa</p>"), ("B", "<p>bbb</p>"), ("C", "<p>ccc</p>")],
)
# Le premier <details> doit รชtre ouvert
details = html.split("<details")
assert "open" in details[1].split(">")[0]
# Les suivants ne doivent pas l'รชtre
assert "open" not in details[2].split(">")[0]
assert "open" not in details[3].split(">")[0]
# Tous les contenus prรฉsents
assert "aaa" in html and "bbb" in html and "ccc" in html
def test_xml_chars_in_titles_escaped(self):
from picarones.reports.html.views.economics import _render_view_shell
html = _render_view_shell(
view_title="<script>alert(1)</script>",
view_note="Note <b>bold</b>",
blocks=[("Block <X>", "<p>content</p>")],
)
# Pas d'injection
assert "<script>alert(1)</script>" not in html
# Mais visible sous forme รฉchappรฉe
assert "<script" in html
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# 5. Cรขblage gรฉnรฉrator โ vues
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class TestGeneratorWiring:
def test_generator_imports_three_views(self):
"""Test runtime du cรขblage des 3 vues automatiques (economics,
advanced_taxonomy, diagnostics).
Vรฉrifie que la mรฉthode ``ReportGenerator._build_section_html``
retourne un dict contenant les 3 clรฉs attendues, ce qui
garantit qu'elles seront splatรฉes vers le template Jinja.
Cette version remplace l'ancien test qui scannait textuellement
``generator.py`` ร la recherche de ``var=`` ou ``"var"`` โ
approche fragile (passait sur n'importe quelle occurrence dans
une docstring) et trop liรฉe ร la forme du code.
"""
from picarones.evaluation.synthetic import generate_sample_benchmark
from picarones.reports.html.generator import ReportGenerator
bench = generate_sample_benchmark()
gen = ReportGenerator(bench, lang="fr")
from picarones.reports.i18n import get_labels
report_data = {
"engines": [],
"inter_engine_analysis": None,
"stratified_ranking": None,
"available_strata": [],
"corpus_homogeneity": None,
}
section_html = gen._build_section_html(report_data, get_labels("fr"))
for name in (
"economics_view_html",
"advanced_taxonomy_view_html",
"diagnostics_view_html",
):
assert name in section_html, (
f"clรฉ {name!r} absente du dict retournรฉ par "
"ReportGenerator._build_section_html โ la vue ne sera "
"pas cรขblรฉe vers le template."
)
# Adaptive : avec un report_data vide, chaque vue retourne ""
# (rapport adaptatif). On vรฉrifie le type, pas le contenu.
assert isinstance(section_html[name], str), (
f"section {name!r} doit รชtre une chaรฎne, "
f"pas {type(section_html[name]).__name__}"
)
def test_template_uses_three_views(self):
from pathlib import Path
tpl_src = (
Path(__file__).parent.parent.parent
/ "picarones" / "reports" / "html" / "templates" / "view_analyses.html"
).read_text(encoding="utf-8")
assert "{% if economics_view_html %}" in tpl_src
assert "{% if advanced_taxonomy_view_html %}" in tpl_src
assert "{% if diagnostics_view_html %}" in tpl_src
|