File size: 8,419 Bytes
e88e70e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bb905
e88e70e
 
 
9011070
e88e70e
 
 
 
 
 
d109222
9011070
e88e70e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests Sprint 88 โ€” A.I.8 vue HTML : dรฉficit projetรฉ de robustesse.

Couvre :

1. ``build_robustness_projection_html`` :
   - vide / None โ†’ ``""``
   - rendu complet (rรฉsumรฉ + dรฉtail)
   - calcul automatique de ``aggregated`` si non fourni
   - tri par dรฉficit dรฉcroissant
   - colonne ยซ pire dรฉgradation ยป formatรฉe
   - cellules colorรฉes selon l'amplitude du dรฉficit
2. Anti-injection sur nom de moteur + type de dรฉgradation.
3. Bout-en-bout : intรฉgration avec
   ``project_robustness_on_corpus`` + ``aggregate_projection_per_engine``.
4. Complรฉtude i18n FR/EN.
"""

from __future__ import annotations

import json
from pathlib import Path

from picarones.evaluation.metrics.robustness_projection import (
    aggregate_projection_per_engine,
    project_robustness_on_corpus,
)
from picarones.reports.html.renderers.robustness_projection import (
    build_robustness_projection_html,
)


def _load_labels(lang: str) -> dict:
    p = (
        Path(__file__).parent.parent.parent
        / "picarones" / "reports" / "i18n" / f"{lang}.json"
    )
    return json.loads(p.read_text(encoding="utf-8"))


def _curve(engine: str, deg: str) -> dict:
    return {
        "engine_name": engine,
        "degradation_type": deg,
        "levels": [0, 5, 10, 20],
        "cer_values": [0.05, 0.10, 0.20, 0.50],
        "critical_threshold_level": 10,
        "cer_threshold": 0.20,
    }


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 1. build_robustness_projection_html
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€


class TestRender:
    def test_none_returns_empty(self) -> None:
        assert build_robustness_projection_html(None) == ""

    def test_empty_returns_empty(self) -> None:
        assert build_robustness_projection_html({}) == ""

    def test_renders_summary_and_detail(self) -> None:
        projection = {
            "tess": {
                "noise": {
                    "n_docs": 50, "n_docs_with_data": 48,
                    "expected_cer_mean": 0.18, "baseline_cer": 0.05,
                    "deficit_vs_baseline": 0.13,
                    "n_docs_above_critical": 12,
                    "critical_threshold_cer": 0.20,
                },
            },
        }
        labels = _load_labels("fr")
        html = build_robustness_projection_html(projection, labels=labels)
        assert "<table" in html
        assert "tess" in html
        assert "noise" in html
        # Dรฉficit total = 0.13 โ†’ 13.00 pts
        assert "+13.00" in html
        # Le summary contient le worst type
        assert "Pire dรฉgradation" in html
        assert "Dรฉtail" in html

    def test_auto_computes_aggregate(self) -> None:
        # Ne fournit que projection โ†’ aggregated calculรฉ depuis
        projection = {
            "tess": {
                "noise": {
                    "n_docs": 10, "n_docs_with_data": 10,
                    "deficit_vs_baseline": 0.05,
                    "n_docs_above_critical": 0,
                },
            },
        }
        html = build_robustness_projection_html(
            projection, labels=_load_labels("fr"),
        )
        # Total = 0.05 = 5.00 points
        assert "+5.00" in html

    def test_sorted_by_deficit_descending(self) -> None:
        projection = {
            "low": {
                "noise": {
                    "n_docs": 1, "n_docs_with_data": 1,
                    "deficit_vs_baseline": 0.01,
                    "n_docs_above_critical": 0,
                },
            },
            "high": {
                "noise": {
                    "n_docs": 1, "n_docs_with_data": 1,
                    "deficit_vs_baseline": 0.10,
                    "n_docs_above_critical": 1,
                },
            },
        }
        html = build_robustness_projection_html(
            projection, labels=_load_labels("fr"),
        )
        # ยซ high ยป apparaรฎt avant ยซ low ยป dans le rรฉsumรฉ
        assert html.index("high") < html.index("low")

    def test_anti_injection_engine(self) -> None:
        projection = {
            "<script>alert(1)</script>": {
                "noise": {
                    "n_docs": 1, "n_docs_with_data": 1,
                    "deficit_vs_baseline": 0.05,
                    "n_docs_above_critical": 0,
                },
            },
        }
        html = build_robustness_projection_html(
            projection, labels=_load_labels("fr"),
        )
        assert "<script>alert" not in html
        assert "&lt;script&gt;" in html

    def test_anti_injection_deg_type(self) -> None:
        projection = {
            "tess": {
                "<img/>": {
                    "n_docs": 1, "n_docs_with_data": 1,
                    "deficit_vs_baseline": 0.05,
                    "n_docs_above_critical": 0,
                },
            },
        }
        html = build_robustness_projection_html(
            projection, labels=_load_labels("fr"),
        )
        assert "<img/>" not in html
        assert "&lt;img" in html

    def test_handles_missing_deficit(self) -> None:
        projection = {
            "tess": {
                "noise": {
                    "n_docs": 5, "n_docs_with_data": 5,
                    "deficit_vs_baseline": None,
                    "n_docs_above_critical": 0,
                },
            },
        }
        html = build_robustness_projection_html(
            projection, labels=_load_labels("fr"),
        )
        assert "โ€”" in html  # Cellule dรฉficit vide

    def test_renders_in_english(self) -> None:
        projection = {
            "tess": {
                "noise": {
                    "n_docs": 1, "n_docs_with_data": 1,
                    "deficit_vs_baseline": 0.05,
                    "n_docs_above_critical": 0,
                },
            },
        }
        html = build_robustness_projection_html(
            projection, labels=_load_labels("en"),
        )
        assert "Projected robustness deficit" in html


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 2. Bout-en-bout (Sprint 81 + Sprint 88)
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€


class TestEndToEnd:
    def test_full_pipeline_renders(self) -> None:
        curves = [_curve("tess", "noise"), _curve("pero", "noise")]
        qualities = [
            {"noise_level": 7.5}, {"noise_level": 5}, {"noise_level": 15},
        ]
        projection = project_robustness_on_corpus(curves, qualities)
        aggregated = aggregate_projection_per_engine(projection)
        html = build_robustness_projection_html(
            projection, aggregated, _load_labels("fr"),
        )
        assert "<table" in html
        # Les deux moteurs apparaissent
        assert "tess" in html
        assert "pero" in html


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 3. Complรฉtude i18n
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€


_KEYS = {
    "robproj_title", "robproj_note", "robproj_summary", "robproj_detail",
    "robproj_engine", "robproj_total", "robproj_n_types", "robproj_worst",
    "robproj_deg_type", "robproj_n_docs", "robproj_n_with_data",
    "robproj_deficit", "robproj_above",
}


class TestI18n:
    def test_fr(self) -> None:
        d = _load_labels("fr")
        assert not _KEYS - d.keys()

    def test_en(self) -> None:
        d = _load_labels("en")
        assert not _KEYS - d.keys()