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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
"""
Tests des endpoints d'ingestion /api/v1/corpora/{id}/ingest/* (Sprint 4 — Session B).

Stratégie :
  - BDD SQLite en mémoire
  - Appels réseau mockés via monkeypatch (_fetch_json_manifest)
  - Écriture disque mockée via monkeypatch (Path.mkdir, Path.write_bytes)

Vérifie :
- POST /ingest/files → pages créées, IDs retournés
- POST /ingest/iiif-manifest → manifest parsé, pages créées
- POST /ingest/iiif-images → pages créées depuis liste d'URLs
- 404 si corpus inexistant
- 422 si données invalides
"""
# 1. stdlib
import uuid
from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import AsyncMock, patch

# 2. third-party
import pytest

# 3. local
import app.api.v1.ingest as ingest_module
from app.models.corpus import CorpusModel
from tests.conftest_api import async_client, db_session  # noqa: F401

_NOW = datetime.now(timezone.utc)


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

async def _make_corpus(db, slug="test-ingest"):
    corpus = CorpusModel(
        id=str(uuid.uuid4()), slug=slug, title="Corpus Test",
        profile_id="medieval-illuminated", created_at=_NOW, updated_at=_NOW,
    )
    db.add(corpus)
    await db.commit()
    await db.refresh(corpus)
    return corpus


def _iiif3_manifest(n_canvases: int = 3) -> dict:
    """Génère un manifest IIIF 3.0 minimal avec n canvases."""
    return {
        "@context": "http://iiif.io/api/presentation/3/context.json",
        "id": "https://example.com/manifest",
        "type": "Manifest",
        "label": {"fr": ["Beatus de Saint-Sever"]},
        "items": [
            {
                "id": f"https://example.com/canvas/{i}",
                "type": "Canvas",
                "label": {"none": [f"f{i:03d}r"]},
                "width": 1500, "height": 2000,
                "items": [
                    {
                        "id": f"https://example.com/canvas/{i}/page",
                        "type": "AnnotationPage",
                        "items": [
                            {
                                "id": f"https://example.com/canvas/{i}/annotation",
                                "type": "Annotation",
                                "motivation": "painting",
                                "body": {
                                    "id": f"https://example.com/images/{i}.jpg",
                                    "type": "Image",
                                    "format": "image/jpeg",
                                },
                                "target": f"https://example.com/canvas/{i}",
                            }
                        ],
                    }
                ],
            }
            for i in range(1, n_canvases + 1)
        ],
    }


def _iiif2_manifest(n_canvases: int = 2) -> dict:
    """Génère un manifest IIIF 2.x minimal."""
    return {
        "@context": "http://iiif.io/api/presentation/2/context.json",
        "@type": "sc:Manifest",
        "label": "Test Manuscript 2.x",
        "sequences": [
            {
                "canvases": [
                    {
                        "@id": f"https://example.com/canvas/{i}",
                        "@type": "sc:Canvas",
                        "label": f"f{i:03d}r",
                        "images": [
                            {
                                "resource": {
                                    "@id": f"https://example.com/images/{i}.jpg"
                                }
                            }
                        ],
                    }
                    for i in range(1, n_canvases + 1)
                ]
            }
        ],
    }


# ---------------------------------------------------------------------------
# POST /api/v1/corpora/{id}/ingest/files
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_ingest_files_corpus_not_found(async_client):
    response = await async_client.post(
        "/api/v1/corpora/nonexistent/ingest/files",
        files=[("files", ("img.jpg", b"data", "image/jpeg"))],
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_ingest_files_ok(async_client, db_session, tmp_path, monkeypatch):
    corpus = await _make_corpus(db_session)
    monkeypatch.setattr(_config_module := __import__("app.config", fromlist=["config"]), "settings",
                        type("S", (), {"data_dir": tmp_path})())

    import app.config as _cfg
    import app.api.v1.ingest as _ingest
    original_data_dir = _cfg.settings.data_dir
    _cfg.settings.data_dir = tmp_path

    try:
        response = await async_client.post(
            f"/api/v1/corpora/{corpus.id}/ingest/files",
            files=[
                ("files", ("f001r.jpg", b"fake_jpeg_data_1", "image/jpeg")),
                ("files", ("f002r.jpg", b"fake_jpeg_data_2", "image/jpeg")),
            ],
        )
        assert response.status_code == 201
        data = response.json()
        assert data["pages_created"] == 2
        assert len(data["page_ids"]) == 2
        assert data["corpus_id"] == corpus.id
    finally:
        _cfg.settings.data_dir = original_data_dir


@pytest.mark.asyncio
async def test_ingest_files_creates_manuscript(async_client, db_session, tmp_path):
    corpus = await _make_corpus(db_session)

    import app.config as _cfg
    original = _cfg.settings.data_dir
    _cfg.settings.data_dir = tmp_path
    try:
        response = await async_client.post(
            f"/api/v1/corpora/{corpus.id}/ingest/files",
            files=[("files", ("f001r.jpg", b"data", "image/jpeg"))],
        )
        data = response.json()
        assert "manuscript_id" in data
        assert data["manuscript_id"]  # non-vide
    finally:
        _cfg.settings.data_dir = original


@pytest.mark.asyncio
async def test_ingest_files_folio_from_filename(async_client, db_session, tmp_path):
    """Le folio_label est dérivé du nom de fichier (sans extension)."""
    corpus = await _make_corpus(db_session)

    import app.config as _cfg
    original = _cfg.settings.data_dir
    _cfg.settings.data_dir = tmp_path
    try:
        response = await async_client.post(
            f"/api/v1/corpora/{corpus.id}/ingest/files",
            files=[("files", ("f013v.jpg", b"data", "image/jpeg"))],
        )
        data = response.json()
        # L'ID de page contient le folio_label
        assert any("f013v" in pid for pid in data["page_ids"])
    finally:
        _cfg.settings.data_dir = original


@pytest.mark.asyncio
async def test_ingest_files_writes_to_disk(async_client, db_session, tmp_path):
    """Les fichiers sont bien écrits dans data/corpora/{slug}/masters/."""
    corpus = await _make_corpus(db_session, slug="test-write")

    import app.config as _cfg
    original = _cfg.settings.data_dir
    _cfg.settings.data_dir = tmp_path
    try:
        await async_client.post(
            f"/api/v1/corpora/{corpus.id}/ingest/files",
            files=[("files", ("f001r.jpg", b"JPEG_CONTENT", "image/jpeg"))],
        )
        expected = tmp_path / "corpora" / "test-write" / "masters" / "f001r" / "f001r.jpg"
        assert expected.exists()
        assert expected.read_bytes() == b"JPEG_CONTENT"
    finally:
        _cfg.settings.data_dir = original


# ---------------------------------------------------------------------------
# POST /api/v1/corpora/{id}/ingest/iiif-manifest
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_ingest_manifest_corpus_not_found(async_client):
    response = await async_client.post(
        "/api/v1/corpora/nonexistent/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_ingest_manifest_iiif3_ok(async_client, db_session, monkeypatch):
    corpus = await _make_corpus(db_session)
    manifest = _iiif3_manifest(n_canvases=3)

    async def fake_fetch(url: str) -> dict:
        return manifest

    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", fake_fetch)

    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert response.status_code == 201
    data = response.json()
    assert data["pages_created"] == 3
    assert len(data["page_ids"]) == 3


@pytest.mark.asyncio
async def test_ingest_manifest_iiif2_ok(async_client, db_session, monkeypatch):
    corpus = await _make_corpus(db_session)
    manifest = _iiif2_manifest(n_canvases=2)

    async def fake_fetch(url: str) -> dict:
        return manifest

    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", fake_fetch)

    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert response.status_code == 201
    assert response.json()["pages_created"] == 2


@pytest.mark.asyncio
async def test_ingest_manifest_extracts_folio_labels(async_client, db_session, monkeypatch):
    """Les folio_labels sont extraits des labels des canvases."""
    corpus = await _make_corpus(db_session)
    manifest = _iiif3_manifest(n_canvases=2)

    async def fake_fetch(url: str) -> dict:
        return manifest

    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", fake_fetch)

    data = (await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )).json()

    # Canvas labels: "f001r", "f002r"
    assert any("f001r" in pid for pid in data["page_ids"])
    assert any("f002r" in pid for pid in data["page_ids"])


@pytest.mark.asyncio
async def test_ingest_manifest_empty_canvases_422(async_client, db_session, monkeypatch):
    """Manifest sans canvases → 422."""
    corpus = await _make_corpus(db_session)

    async def fake_fetch(url: str) -> dict:
        return {"type": "Manifest", "items": []}

    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", fake_fetch)

    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert response.status_code == 422


@pytest.mark.asyncio
async def test_ingest_manifest_network_error_502(async_client, db_session, monkeypatch):
    """Erreur réseau → 502."""
    corpus = await _make_corpus(db_session)
    import httpx

    async def fake_fetch(url: str) -> dict:
        raise httpx.RequestError("Connection refused")

    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", fake_fetch)

    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert response.status_code == 502


@pytest.mark.asyncio
async def test_ingest_manifest_returns_corpus_id(async_client, db_session, monkeypatch):
    corpus = await _make_corpus(db_session)
    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", AsyncMock(return_value=_iiif3_manifest(1)))

    data = (await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )).json()
    assert data["corpus_id"] == corpus.id


# ---------------------------------------------------------------------------
# POST /api/v1/corpora/{id}/ingest/iiif-images
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_ingest_images_corpus_not_found(async_client):
    response = await async_client.post(
        "/api/v1/corpora/nonexistent/ingest/iiif-images",
        json={"urls": ["https://x.com/1.jpg"], "folio_labels": ["f001r"]},
    )
    assert response.status_code == 404


@pytest.mark.asyncio
async def test_ingest_images_ok(async_client, db_session):
    corpus = await _make_corpus(db_session)
    urls = ["https://example.com/img1.jpg", "https://example.com/img2.jpg"]
    labels = ["f001r", "f002r"]

    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images",
        json={"urls": urls, "folio_labels": labels},
    )
    assert response.status_code == 201
    data = response.json()
    assert data["pages_created"] == 2
    assert len(data["page_ids"]) == 2


@pytest.mark.asyncio
async def test_ingest_images_folio_labels_in_ids(async_client, db_session):
    corpus = await _make_corpus(db_session)
    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images",
        json={
            "urls": ["https://example.com/a.jpg"],
            "folio_labels": ["f013v"],
        },
    )
    data = response.json()
    assert any("f013v" in pid for pid in data["page_ids"])


@pytest.mark.asyncio
async def test_ingest_images_mismatched_lengths_422(async_client, db_session):
    """urls et folio_labels de longueurs différentes → 422."""
    corpus = await _make_corpus(db_session)
    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images",
        json={"urls": ["https://a.com/1.jpg", "https://a.com/2.jpg"], "folio_labels": ["f001r"]},
    )
    assert response.status_code == 422


@pytest.mark.asyncio
async def test_ingest_images_empty_urls_422(async_client, db_session):
    corpus = await _make_corpus(db_session)
    response = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images",
        json={"urls": [], "folio_labels": []},
    )
    assert response.status_code == 422


@pytest.mark.asyncio
async def test_ingest_images_pages_in_sequence_order(async_client, db_session):
    """Les pages ont des séquences consécutives."""
    corpus = await _make_corpus(db_session)
    n = 4
    urls = [f"https://example.com/{i}.jpg" for i in range(1, n + 1)]
    labels = [f"f{i:03d}r" for i in range(1, n + 1)]

    data = (await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images",
        json={"urls": urls, "folio_labels": labels},
    )).json()
    assert data["pages_created"] == n


@pytest.mark.asyncio
async def test_ingest_images_corpus_id_in_response(async_client, db_session):
    corpus = await _make_corpus(db_session)
    data = (await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images",
        json={"urls": ["https://x.com/1.jpg"], "folio_labels": ["f001r"]},
    )).json()
    assert data["corpus_id"] == corpus.id


# ---------------------------------------------------------------------------
# Réingestion — pas de 500
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_reingest_manifest_skips_existing_pages(async_client, db_session, monkeypatch):
    """Réingérer le même manifest ne provoque pas de 500 (UNIQUE constraint).

    La deuxième ingestion doit retourner 201 avec pages_created=0 et pages_skipped=N.
    """
    corpus = await _make_corpus(db_session, slug="reingest")
    manifest = _iiif3_manifest(n_canvases=2)

    async def fake_fetch(url: str) -> dict:
        return manifest

    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", fake_fetch)

    # Première ingestion
    resp1 = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert resp1.status_code == 201
    data1 = resp1.json()
    assert data1["pages_created"] == 2
    assert data1["pages_skipped"] == 0

    # Deuxième ingestion — même manifest
    resp2 = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert resp2.status_code == 201
    data2 = resp2.json()
    assert data2["pages_created"] == 0
    assert data2["pages_skipped"] == 2

    # Vérifier que la BDD n'a bien que 2 pages (pas de doublons)
    from sqlalchemy import select as sa_select
    from app.models.corpus import PageModel
    page_result = await db_session.execute(
        sa_select(PageModel).where(PageModel.manuscript_id == data1["manuscript_id"])
    )
    pages_in_db = list(page_result.scalars().all())
    assert len(pages_in_db) == 2


@pytest.mark.asyncio
async def test_reingest_images_skips_existing_pages(async_client, db_session):
    """Réingérer les mêmes images ne provoque pas de 500."""
    corpus = await _make_corpus(db_session, slug="reingest2")

    payload = {"urls": ["https://x.com/a.jpg"], "folio_labels": ["f001r"]}

    resp1 = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images", json=payload,
    )
    assert resp1.status_code == 201
    assert resp1.json()["pages_created"] == 1

    resp2 = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-images", json=payload,
    )
    assert resp2.status_code == 201
    assert resp2.json()["pages_created"] == 0
    assert resp2.json()["pages_skipped"] == 1


@pytest.mark.asyncio
async def test_ingest_manifest_duplicate_labels_no_collision(async_client, db_session, monkeypatch):
    """Deux canvases avec le même label ne provoquent pas de collision d'ID."""
    corpus = await _make_corpus(db_session, slug="dupe-labels")
    manifest = {
        "@context": "http://iiif.io/api/presentation/3/context.json",
        "type": "Manifest",
        "label": {"fr": ["Test"]},
        "items": [
            {
                "id": f"https://example.com/canvas/{i}",
                "type": "Canvas",
                "label": {"none": ["NP"]},
                "items": [{
                    "type": "AnnotationPage",
                    "items": [{
                        "type": "Annotation",
                        "motivation": "painting",
                        "body": {"id": f"https://example.com/img/{i}.jpg", "type": "Image"},
                        "target": f"https://example.com/canvas/{i}",
                    }],
                }],
            }
            for i in range(1, 4)
        ],
    }

    monkeypatch.setattr(ingest_module, "_fetch_json_manifest", AsyncMock(return_value=manifest))

    resp = await async_client.post(
        f"/api/v1/corpora/{corpus.id}/ingest/iiif-manifest",
        json={"manifest_url": "https://example.com/manifest"},
    )
    assert resp.status_code == 201
    data = resp.json()
    assert data["pages_created"] == 3
    # All IDs must be distinct
    assert len(set(data["page_ids"])) == 3