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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
"""Post-approval migrate path: API + filesystem behavior."""

from __future__ import annotations

from pathlib import Path

from fastapi.testclient import TestClient

from app.main import app
from app.storage import execute, fetch_one, to_json


def _seed_completed_run(
    root: Path,
    cats: Path,
    items: list[dict],
    *,
    status: str = "completed",
) -> int:
    run_id = execute(
        """
        INSERT INTO runs (
            root_repo, categories_root, confidence_threshold, status,
            total_images, processed_images, failed_images, cancel_requested, tagger_model
        ) VALUES (?, ?, 0.6, ?, ?, ?, 0, 0, 'wd_swinv2_v3')
        """,
        (str(root), str(cats), status, len(items), len(items)),
    )
    for item in items:
        execute(
            """
            INSERT INTO items (
                run_id, file_path, relative_path, primary_tag, primary_score, secondary_json,
                full_scores_json, suggested_destination, final_tag, final_destination,
                status, needs_review, review_reason
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """,
            (
                run_id,
                item["file_path"],
                item.get("relative_path") or Path(item["file_path"]).name,
                item.get("primary_tag"),
                item.get("primary_score"),
                to_json(item.get("secondary") or []),
                to_json(item.get("scores") or {}),
                item.get("suggested_destination"),
                item.get("final_tag", item.get("primary_tag")),
                item.get("final_destination"),
                item.get("status", "approved"),
                1 if item.get("needs_review") else 0,
                item.get("review_reason"),
            ),
        )
    return run_id


def test_migrate_copy_approved_only_and_skips_non_approved(tmp_path: Path) -> None:
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    root.mkdir()
    cats.mkdir()
    a = root / "a.jpg"
    b = root / "b.jpg"
    c = root / "c.jpg"
    a.write_bytes(b"aaa")
    b.write_bytes(b"bbb")
    c.write_bytes(b"ccc")
    dest_loli = cats / "loli"
    dest_shota = cats / "shota"

    run_id = _seed_completed_run(
        root,
        cats,
        [
            {
                "file_path": str(a),
                "primary_tag": "loli",
                "primary_score": 0.9,
                "final_destination": str(dest_loli),
                "status": "approved",
            },
            {
                "file_path": str(b),
                "primary_tag": "shota",
                "primary_score": 0.85,
                "final_destination": str(dest_shota),
                "status": "proposed",
            },
            {
                "file_path": str(c),
                "primary_tag": "loli",
                "primary_score": 0.7,
                "final_destination": str(dest_loli),
                "status": "rejected",
            },
        ],
    )

    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        resp.raise_for_status()
        payload = resp.json()
        assert payload["total_candidates"] == 1
        assert payload["migrated_count"] == 1
        assert payload["failed_count"] == 0
        assert (dest_loli / "a.jpg").is_file()
        assert (dest_loli / "a.jpg").read_bytes() == b"aaa"
        assert a.exists()  # copy keeps source
        assert not (dest_shota / "b.jpg").exists()
        assert not (dest_loli / "c.jpg").exists()

        item = fetch_one("SELECT status, migrated_to FROM items WHERE file_path = ?", (str(a),))
        assert item["status"] == "migrated"
        assert item["migrated_to"].endswith("a.jpg")


def test_migrate_move_removes_source(tmp_path: Path) -> None:
    root = tmp_path / "root"
    cats = tmp_path / "cats" / "NTR"
    root.mkdir()
    cats.mkdir(parents=True)
    src = root / "ntr.jpg"
    src.write_bytes(b"ntr-bytes")
    run_id = _seed_completed_run(
        root,
        cats.parent,
        [
            {
                "file_path": str(src),
                "primary_tag": "NTR",
                "primary_score": 0.8,
                "final_destination": str(cats),
                "status": "approved",
            }
        ],
    )

    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "move", "create_missing_folders": False},
        )
        resp.raise_for_status()
        payload = resp.json()
        assert payload["migrated_count"] == 1
        assert not src.exists()
        assert (cats / "ntr.jpg").read_bytes() == b"ntr-bytes"


def test_migrate_collision_suffix(tmp_path: Path) -> None:
    root = tmp_path / "root"
    dest = tmp_path / "cats" / "furry"
    root.mkdir()
    dest.mkdir(parents=True)
    src = root / "dup.jpg"
    src.write_bytes(b"new")
    (dest / "dup.jpg").write_bytes(b"old")
    run_id = _seed_completed_run(
        root,
        dest.parent,
        [
            {
                "file_path": str(src),
                "primary_tag": "furry",
                "final_destination": str(dest),
                "status": "approved",
            }
        ],
    )

    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        resp.raise_for_status()
        assert resp.json()["migrated_count"] == 1
        assert (dest / "dup.jpg").read_bytes() == b"old"
        assert (dest / "dup_1.jpg").read_bytes() == b"new"


def test_migrate_creates_taxonomy_folder(tmp_path: Path) -> None:
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    root.mkdir()
    cats.mkdir()
    src = root / "poke.webp"
    src.write_bytes(b"poke")
    dest = cats / "Pokemon"
    run_id = _seed_completed_run(
        root,
        cats,
        [
            {
                "file_path": str(src),
                "primary_tag": "Pokemon",
                "final_destination": str(dest),
                "status": "approved",
            }
        ],
    )

    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        resp.raise_for_status()
        assert resp.json()["migrated_count"] == 1
        assert dest.is_dir()
        assert (dest / "poke.webp").is_file()


def test_migrate_fails_missing_source_and_missing_destination(tmp_path: Path) -> None:
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    root.mkdir()
    cats.mkdir()
    missing = root / "gone.jpg"
    present = root / "ok.jpg"
    present.write_bytes(b"ok")
    run_id = _seed_completed_run(
        root,
        cats,
        [
            {
                "file_path": str(missing),
                "primary_tag": "loli",
                "final_destination": str(cats / "loli"),
                "status": "approved",
            },
            {
                "file_path": str(present),
                "primary_tag": "loli",
                "final_destination": None,
                "status": "approved",
            },
        ],
    )

    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        resp.raise_for_status()
        payload = resp.json()
        assert payload["total_candidates"] == 2
        # Missing source fails; missing final_destination is repaired from
        # primary_tag via _resolve_item_assignment and can succeed.
        assert payload["failed_count"] == 1
        assert payload["migrated_count"] == 1
        # Response only includes failures (successes are counted only).
        errors = {r["error"] for r in payload["results"]}
        assert any("does not exist" in (e or "") for e in errors)
        assert all(not r.get("success") for r in payload["results"])


def test_migrate_create_missing_folders_false_fails_when_absent(tmp_path: Path) -> None:
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    root.mkdir()
    cats.mkdir()
    src = root / "x.jpg"
    src.write_bytes(b"x")
    dest = cats / "incest"  # not created
    run_id = _seed_completed_run(
        root,
        cats,
        [
            {
                "file_path": str(src),
                "primary_tag": "incest",
                "final_destination": str(dest),
                "status": "approved",
            }
        ],
    )

    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": False},
        )
        resp.raise_for_status()
        payload = resp.json()
        assert payload["migrated_count"] == 0
        assert payload["failed_count"] == 1
        assert not dest.exists()
        assert "does not exist" in (payload["results"][0].get("error") or "").lower()


def test_migrate_rejects_running_run(tmp_path: Path) -> None:
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    root.mkdir()
    cats.mkdir()
    run_id = _seed_completed_run(root, cats, [], status="running")
    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        assert resp.status_code == 409


def test_migrate_allows_failed_run(tmp_path: Path) -> None:
    """DB-lock aborted runs still have approved items worth migrating."""
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    dest = cats / "loli"
    root.mkdir()
    cats.mkdir()
    src = root / "ok.jpg"
    src.write_bytes(b"ok")
    run_id = _seed_completed_run(
        root,
        cats,
        [
            {
                "file_path": str(src),
                "primary_tag": "loli",
                "final_destination": str(dest),
                "status": "approved",
            }
        ],
        status="failed",
    )
    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "move", "create_missing_folders": True},
        )
        resp.raise_for_status()
        payload = resp.json()
        assert payload["migrated_count"] == 1
        assert (dest / "ok.jpg").is_file()
        assert not src.exists()


def test_migrate_idempotent_second_pass(tmp_path: Path) -> None:
    root = tmp_path / "root"
    dest = tmp_path / "cats" / "fellatio"
    root.mkdir()
    dest.mkdir(parents=True)
    src = root / "f.jpg"
    src.write_bytes(b"f")
    run_id = _seed_completed_run(
        root,
        dest.parent,
        [
            {
                "file_path": str(src),
                "primary_tag": "fellatio",
                "final_destination": str(dest),
                "status": "approved",
            }
        ],
    )
    with TestClient(app) as client:
        first = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        first.raise_for_status()
        assert first.json()["migrated_count"] == 1
        second = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        second.raise_for_status()
        assert second.json()["total_candidates"] == 0
        assert second.json()["migrated_count"] == 0


def test_approve_then_migrate_end_to_end(tmp_path: Path) -> None:
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    root.mkdir()
    cats.mkdir()
    src = root / "review_me.jpg"
    src.write_bytes(b"rev")
    dest = cats / "nakadashi"
    run_id = _seed_completed_run(
        root,
        cats,
        [
            {
                "file_path": str(src),
                "primary_tag": "nakadashi",
                "primary_score": 0.55,
                "suggested_destination": str(dest),
                "final_tag": "nakadashi",
                "final_destination": str(dest),
                "status": "proposed",
                "needs_review": True,
                "review_reason": "Below threshold",
            }
        ],
    )
    item = fetch_one("SELECT id FROM items WHERE run_id = ?", (run_id,))
    with TestClient(app) as client:
        patch = client.patch(f"/api/items/{item['id']}", json={"status": "approved"})
        patch.raise_for_status()
        assert patch.json()["status"] == "approved"
        mig = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "copy", "create_missing_folders": True},
        )
        mig.raise_for_status()
        assert mig.json()["migrated_count"] == 1
        assert (dest / "review_me.jpg").is_file()


def test_approve_secondary_only_then_migrate(tmp_path: Path) -> None:
    """Below-threshold items clear primary_tag but keep secondary — approve must still migrate."""
    root = tmp_path / "root"
    cats = tmp_path / "cats"
    root.mkdir()
    cats.mkdir()
    src = root / "weak.jpg"
    src.write_bytes(b"weak")
    run_id = _seed_completed_run(
        root,
        cats,
        [
            {
                "file_path": str(src),
                "primary_tag": None,
                "primary_score": None,
                "secondary": [{"tag": "loli", "score": 0.45}],
                "suggested_destination": None,
                "final_tag": None,
                "final_destination": None,
                "status": "proposed",
                "needs_review": True,
                "review_reason": "Below threshold",
            }
        ],
    )
    item = fetch_one("SELECT id FROM items WHERE run_id = ?", (run_id,))
    with TestClient(app) as client:
        patch = client.patch(f"/api/items/{item['id']}", json={"status": "approved"})
        patch.raise_for_status()
        body = patch.json()
        assert body["status"] == "approved"
        assert body["final_tag"] == "loli"
        assert body["final_destination"].endswith("loli")
        mig = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "move", "create_missing_folders": True},
        )
        mig.raise_for_status()
        assert mig.json()["migrated_count"] == 1
        assert mig.json()["failed_count"] == 0
        assert not src.exists()
        assert (cats / "loli" / "weak.jpg").is_file()

def test_migrate_reconciles_already_at_destination(tmp_path: Path) -> None:
    """Source gone but file already in destination → mark migrated."""
    root = tmp_path / "root"
    dest = tmp_path / "cats" / "real_life"
    root.mkdir()
    dest.mkdir(parents=True)
    src = root / "photo.jpg"
    # File already at destination; source path is stale.
    (dest / "photo.jpg").write_bytes(b"already-there")
    run_id = _seed_completed_run(
        root,
        dest.parent,
        [
            {
                "file_path": str(src),
                "primary_tag": "real_life",
                "primary_score": 0.9,
                "final_destination": str(dest),
                "status": "approved",
            }
        ],
    )
    with TestClient(app) as client:
        resp = client.post(
            f"/api/runs/{run_id}/migrate",
            json={"mode": "move", "create_missing_folders": True},
        )
        resp.raise_for_status()
        payload = resp.json()
        assert payload["migrated_count"] == 1
        assert payload["failed_count"] == 0
        item = fetch_one("SELECT status, migrated_to FROM items WHERE run_id = ?", (run_id,))
        assert item["status"] == "migrated"
        assert item["migrated_to"].endswith("photo.jpg")