File size: 15,874 Bytes
4afd2c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests de la phase A β€” refonte en 3 cercles (post-chantier 6).

Couvre :

- 4 modules `core/` dΓ©placΓ©s vers `extras/academic/` ou
  `extras/governance/` avec shims rΓ©trocompat.
- 4 renderers `report/` dΓ©placΓ©s vers `extras/render/` avec shims.
- IdentitΓ© prΓ©servΓ©e : ``shim.X is new_location.X`` (pas de duplication
  ni de redΓ©finition).
- Hygiène anti-verdict : 5 phrases reformulées dans les templates
  narratifs et l'i18n du rapport.
- Document `docs/architecture-cercles.md` prΓ©sent et complet.
"""

from __future__ import annotations

from pathlib import Path

import pytest


# ──────────────────────────────────────────────────────────────────────────
# 1. Modules dΓ©placΓ©s vers extras/ β€” rΓ©trocompat des imports historiques
# ──────────────────────────────────────────────────────────────────────────


class TestRetrocompatHistoricalImports:
    """Les imports `from picarones.core.X` doivent continuer Γ  fonctionner
    après le déplacement vers `picarones.extras.*`."""

    @pytest.mark.parametrize("module_path, attribute", [
        ("picarones.core.taxonomy_intra_doc", "compute_taxonomy_position_heatmap"),
        ("picarones.core.taxonomy_cooccurrence", "compute_taxonomy_cooccurrence"),
        ("picarones.core.image_predictive", "compute_paleographic_complexity"),
        ("picarones.core.image_predictive", "compute_corpus_homogeneity"),
        ("picarones.core.image_predictive", "aggregate_corpus_predictive"),
        ("picarones.core.module_policy", "ModuleManifest"),
        ("picarones.core.module_policy", "validate_manifest"),
        ("picarones.core.module_policy", "audit_module"),
    ])
    def test_core_alias_still_works(self, module_path: str, attribute: str):
        import importlib
        mod = importlib.import_module(module_path)
        assert hasattr(mod, attribute), (
            f"{module_path}.{attribute} a disparu après la phase A — "
            "le shim rΓ©trocompat est cassΓ©"
        )

    @pytest.mark.parametrize("module_path, attribute", [
        ("picarones.report.taxonomy_intra_doc_render", "build_taxonomy_intra_doc_html"),
        ("picarones.report.taxonomy_cooccurrence_render", "build_taxonomy_cooccurrence_html"),
        ("picarones.report.image_predictive_render", "build_image_predictive_html"),
        ("picarones.report.module_audit_render", "build_module_audit_html"),
    ])
    def test_report_alias_still_works(self, module_path: str, attribute: str):
        import importlib
        mod = importlib.import_module(module_path)
        assert hasattr(mod, attribute)


# ──────────────────────────────────────────────────────────────────────────
# 2. Modules accessibles via leur nouveau chemin extras/
# ──────────────────────────────────────────────────────────────────────────


class TestNewExtrasImports:
    @pytest.mark.parametrize("new_path, attribute", [
        ("picarones.extras.academic.taxonomy_intra_doc", "compute_taxonomy_position_heatmap"),
        ("picarones.extras.academic.taxonomy_cooccurrence", "compute_taxonomy_cooccurrence"),
        ("picarones.extras.academic.image_predictive", "aggregate_corpus_predictive"),
        ("picarones.extras.governance.module_policy", "ModuleManifest"),
        ("picarones.extras.render.taxonomy_intra_doc_render", "build_taxonomy_intra_doc_html"),
        ("picarones.extras.render.taxonomy_cooccurrence_render", "build_taxonomy_cooccurrence_html"),
        ("picarones.extras.render.image_predictive_render", "build_image_predictive_html"),
        ("picarones.extras.render.module_audit_render", "build_module_audit_html"),
    ])
    def test_extras_path_works(self, new_path: str, attribute: str):
        import importlib
        mod = importlib.import_module(new_path)
        assert hasattr(mod, attribute)


# ──────────────────────────────────────────────────────────────────────────
# 3. IdentitΓ© prΓ©servΓ©e β€” pas de redΓ©finition par le shim
# ──────────────────────────────────────────────────────────────────────────


class TestIdentityThroughShim:
    """Le shim doit rΓ©exporter la fonction du nouveau chemin, pas la
    redΓ©finir. Sinon une mΓ©trique serait calculΓ©e diffΓ©remment selon
    le chemin d'import."""

    def test_taxonomy_intra_doc_identity(self):
        from picarones.core.taxonomy_intra_doc import (
            compute_taxonomy_position_heatmap as via_old,
        )
        from picarones.extras.academic.taxonomy_intra_doc import (
            compute_taxonomy_position_heatmap as via_new,
        )
        assert via_old is via_new

    def test_image_predictive_identity(self):
        from picarones.core.image_predictive import (
            aggregate_corpus_predictive as via_old,
        )
        from picarones.extras.academic.image_predictive import (
            aggregate_corpus_predictive as via_new,
        )
        assert via_old is via_new

    def test_module_policy_identity(self):
        from picarones.core.module_policy import ModuleManifest as via_old
        from picarones.extras.governance.module_policy import (
            ModuleManifest as via_new,
        )
        assert via_old is via_new

    def test_renderer_identity(self):
        from picarones.report.taxonomy_intra_doc_render import (
            build_taxonomy_intra_doc_html as via_old,
        )
        from picarones.extras.render.taxonomy_intra_doc_render import (
            build_taxonomy_intra_doc_html as via_new,
        )
        assert via_old is via_new


# ──────────────────────────────────────────────────────────────────────────
# 4. Vues du chantier 3 β€” toujours fonctionnelles
# ──────────────────────────────────────────────────────────────────────────


class TestChantier3ViewsStillWork:
    """Les 5 vues du chantier 3 importent (sous-section opt-in) les
    modules déplacés. Vérifier qu'elles tournent encore après la
    migration."""

    def test_views_import(self):
        from picarones.report.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)

    def test_advanced_taxonomy_with_intra_doc_data(self):
        """La vue advanced_taxonomy accepte des donnΓ©es opt-in
        ``intra_doc`` dont le calcul vient dΓ©sormais de
        ``picarones.extras.academic``."""
        from picarones.extras.academic.taxonomy_intra_doc import (
            compute_taxonomy_position_heatmap,
        )
        from picarones.report.views import build_advanced_taxonomy_view_html

        # Calcul d'une heatmap minimaliste
        result = compute_taxonomy_position_heatmap(
            "abc def ghi", "abx def ghi", n_bins=3,
        )
        # La vue doit pouvoir composer sans crasher quand on lui passe
        # ces donnΓ©es opt-in
        report_data = {"engines": [
            {"name": "tess", "cer": 0.05,
             "aggregated_taxonomy": {"class_distribution": {"x": 5}}},
            {"name": "pero", "cer": 0.08,
             "aggregated_taxonomy": {"class_distribution": {"x": 8}}},
        ]}
        html = build_advanced_taxonomy_view_html(
            report_data, {}, intra_doc=result,
        )
        # Pas de crash + au moins du contenu (comparison + intra_doc)
        assert isinstance(html, str)


# ──────────────────────────────────────────────────────────────────────────
# 5. Hygiène anti-verdict — phrases reformulées
# ──────────────────────────────────────────────────────────────────────────


class TestAntiVerdictHygiene:
    """Les 5 phrases identifiΓ©es comme prescriptives ont Γ©tΓ© reformulΓ©es
    factuellement. Tests anti-rΓ©gression."""

    @pytest.fixture
    def fr_templates(self) -> str:
        path = (Path(__file__).parent.parent
                / "picarones" / "core" / "narrative" / "templates" / "fr.yaml")
        return path.read_text(encoding="utf-8")

    @pytest.fixture
    def en_templates(self) -> str:
        path = (Path(__file__).parent.parent
                / "picarones" / "core" / "narrative" / "templates" / "en.yaml")
        return path.read_text(encoding="utf-8")

    @pytest.fixture
    def fr_i18n(self) -> str:
        path = (Path(__file__).parent.parent
                / "picarones" / "report" / "i18n" / "fr.json")
        return path.read_text(encoding="utf-8")

    @pytest.fixture
    def en_i18n(self) -> str:
        path = (Path(__file__).parent.parent
                / "picarones" / "report" / "i18n" / "en.json")
        return path.read_text(encoding="utf-8")

    def test_stratum_winner_no_dominate(self, fr_templates, en_templates):
        """`stratum_winner` ne dit plus Β« domine nettement Β» /
        Β« clearly dominates Β». Phrasage factuel attendu."""
        assert "domine\n  nettement" not in fr_templates
        assert "domine nettement" not in fr_templates
        assert "clearly\n  dominates" not in en_templates
        assert "clearly dominates" not in en_templates
        # Confirmation prΓ©sence du nouveau phrasage factuel
        assert "le CER le plus bas" in fr_templates
        assert "the lowest CER" in en_templates

    def test_confidence_warning_no_fragile(self, fr_templates, en_templates):
        """`confidence_warning` ne dit plus Β« fragile Β» mais
        Β« incertitude statistique Γ©levΓ©e Β»."""
        assert "Classement fragile" not in fr_templates
        assert "Ranking is fragile" not in en_templates
        assert "Incertitude statistique" in fr_templates
        assert "High statistical uncertainty" in en_templates

    def test_gini_no_ideal(self, fr_i18n, en_i18n):
        """`gini_cer_ideal` et `gini_cer_note` n'utilisent plus
        Β« idΓ©al Β» / Β« ideal Β» mais Β« lecture Β» / Β« reading Β»."""
        assert "\"gini_cer_ideal\": \"β€” idΓ©al" not in fr_i18n
        assert "\"gini_cer_ideal\": \"β€” ideal" not in en_i18n
        # Confirmer le nouveau phrasage
        assert "lecture : bas-gauche" in fr_i18n
        assert "reading: bottom-left" in en_i18n

    def test_taxocomp_no_preferable(self, fr_i18n, en_i18n):
        """`taxocomp_note` ne dit plus Β« prΓ©fΓ©rable Β» / Β« preferable Β»."""
        assert "prΓ©fΓ©rable pour une Γ©dition critique" not in fr_i18n
        assert "preferable for a critical edition" not in en_i18n
        # Phrasage factuel
        assert "tend Γ  produire des erreurs plus facilement" in fr_i18n
        assert "tends to produce errors more easily" in en_i18n


# ──────────────────────────────────────────────────────────────────────────
# 6. Document docs/architecture-cercles.md prΓ©sent et complet
# ──────────────────────────────────────────────────────────────────────────


class TestArchitectureCerclesDoc:
    @pytest.fixture
    def doc(self) -> str:
        path = (Path(__file__).parent.parent / "docs" / "architecture-cercles.md")
        return path.read_text(encoding="utf-8")

    def test_doc_exists(self, doc):
        assert len(doc) > 1000

    def test_doc_describes_three_circles(self, doc):
        assert "Cercle 1" in doc
        assert "Cercle 2" in doc
        assert "Cercle 3" in doc
        assert "Noyau invariant" in doc or "noyau invariant" in doc
        assert "Plugins" in doc or "plugins" in doc

    def test_doc_assigns_specific_modules(self, doc):
        """Le document doit lister explicitement les modules de chaque cercle."""
        # Cercle 1 β€” quelques noms
        for name in ["corpus.py", "modules.py", "runner.py",
                     "metric_registry.py", "alto_metrics.py"]:
            assert name in doc, f"{name} doit Γͺtre listΓ© dans le doc"
        # Cercle 3 β€” modules dΓ©placΓ©s en phase A
        for name in ["taxonomy_intra_doc", "image_predictive",
                     "module_policy"]:
            assert name in doc, f"{name} doit Γͺtre listΓ© dans le doc"

    def test_doc_mentions_extras_path(self, doc):
        """Le doc explique que les Cercle 3 vivent dans `extras/`."""
        assert "extras/academic" in doc
        assert "extras/governance" in doc
        assert "extras/render" in doc


# ──────────────────────────────────────────────────────────────────────────
# 7. Modules originaux ne contiennent plus de logique mΓ©tier
# ──────────────────────────────────────────────────────────────────────────


class TestOriginalsAreShims:
    """VΓ©rifie que les fichiers laissΓ©s Γ  l'ancien emplacement sont
    bien des shims minces, pas des copies de la logique."""

    @pytest.mark.parametrize("path", [
        "picarones/core/taxonomy_intra_doc.py",
        "picarones/core/taxonomy_cooccurrence.py",
        "picarones/core/image_predictive.py",
        "picarones/core/module_policy.py",
        "picarones/report/taxonomy_intra_doc_render.py",
        "picarones/report/taxonomy_cooccurrence_render.py",
        "picarones/report/image_predictive_render.py",
        "picarones/report/module_audit_render.py",
    ])
    def test_is_thin_shim(self, path):
        repo_root = Path(__file__).parent.parent
        content = (repo_root / path).read_text(encoding="utf-8")
        # Un shim < 30 lignes (juste docstring + 2 imports + __all__)
        n_lines = len([line for line in content.splitlines() if line.strip()])
        assert n_lines < 30, (
            f"{path} fait {n_lines} lignes β€” devrait Γͺtre un shim mince "
            "(import + rΓ©export, pas de logique mΓ©tier)"
        )
        # Doit contenir l'indication du dΓ©placement
        assert "dΓ©placΓ©" in content or "extras" in content