File size: 28,062 Bytes
b9ee967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d84791
 
b9ee967
 
 
9d84791
b9ee967
 
 
9d84791
b9ee967
 
9d84791
b9ee967
 
 
 
 
 
9d84791
b9ee967
 
 
 
9d84791
 
 
 
b9ee967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d84791
b9ee967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d84791
b9ee967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d84791
b9ee967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15db7d8
 
 
 
 
 
 
b9ee967
 
15db7d8
 
 
 
 
b9ee967
 
 
15db7d8
 
 
 
 
 
 
 
 
 
9d84791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b994b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e93ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cb1c38
 
 
 
b8e93ea
 
 
7cb1c38
b8e93ea
 
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
"""Spec for ``ltx25_studio/lora/fetch.py`` β€” the Hub route, PLAN Β§4.6.

The route existed as an interface and nothing else: ``validate_lora`` carried
a ``fetcher`` seam, the UI carried a repo-id textbox, and no fetcher was ever
written β€” so every repo id anyone typed rejected with "cannot be read",
including the UI's own placeholder.

Three properties are the point, and each has its own tests:

* **the size bound binds BEFORE the download** β€” an oversized adapter is
  refused from the Hub's metadata listing, and the download seam is provably
  never called;
* **ambiguity is refused, convention is honoured** β€” a multi-file repo picks
  ``pytorch_lora_weights.safetensors`` because that is the file diffusers'
  own ``load_lora_weights(repo_id)`` would take (published convention, not a
  guess), and anything else ambiguous refuses naming the candidates;
* **the handler stages a LOCAL path** β€” the fetch happens in the web process,
  so the billed GPU worker only ever reads a file.

Everything runs offline: the listing and the download are injected seams, and
the Hub's failure classes are stood in for by name (the mapper matches on the
MRO's class names precisely so these tests need no huggingface_hub import).
"""

from __future__ import annotations

import os
import time
from pathlib import Path

import pytest
from lora_fixtures import good_adapter

from ltx25_studio.errors import LoRAFetchError, LoRATooLargeError
from ltx25_studio.lora import DIFFUSERS_LORA_WEIGHT_NAME, fetch
from ltx25_studio.lora.fetch import FetchedLoRA, RepoListing, evict_lora_cache, fetch_lora

CAP = 1000
SHA = "e27322c2994f93f0fea5c8bd3836e30dfcb752ca"


def _fetcher(files, downloaded=None, cap=CAP):
    """A fetch_lora partial over injected seams, recording download calls."""
    calls = downloaded if downloaded is not None else []

    def download(repo_id, filename, revision=None):
        calls.append((repo_id, filename))
        return f"/cache/{repo_id}/{filename}"

    def run(source):
        fetched = fetch_lora(
            source, max_bytes=cap, repo_files=lambda repo: files, download=download
        )
        return fetched.path

    return run, calls


# --------------------------------------------------------------------------
# source parsing β€” exactly the two shapes the UI's helper text promises
# --------------------------------------------------------------------------


def test_a_bare_repo_id_resolves_its_single_safetensors_file():
    run, calls = _fetcher({"adapter.safetensors": 512, "README.md": 10})
    assert run("org/name") == Path("/cache/org/name/adapter.safetensors")
    assert calls == [("org/name", "adapter.safetensors")]


def test_a_repo_id_with_an_embedded_filename_fetches_that_file():
    run, calls = _fetcher({"sub/dir/mine.safetensors": 512, "other.safetensors": 512})
    assert run("org/name/sub/dir/mine.safetensors") == Path(
        "/cache/org/name/sub/dir/mine.safetensors"
    )
    assert calls == [("org/name", "sub/dir/mine.safetensors")]


def test_a_url_is_refused_naming_the_accepted_forms():
    """A pasted Hub URL carries revision segments this route does not honour;
    half-parsing it would fetch something other than what was pointed at."""
    run, calls = _fetcher({"adapter.safetensors": 512})
    with pytest.raises(LoRAFetchError) as excinfo:
        run("https://huggingface.co/org/name/blob/main/adapter.safetensors")
    assert "org/name" in str(excinfo.value)
    assert calls == []


def test_a_single_segment_source_is_refused():
    run, _ = _fetcher({"adapter.safetensors": 512})
    with pytest.raises(LoRAFetchError) as excinfo:
        run("just-a-name")
    assert "org/name" in str(excinfo.value)


# --------------------------------------------------------------------------
# file selection β€” convention honoured, ambiguity refused with the list
# --------------------------------------------------------------------------


def test_a_repo_with_no_safetensors_refuses_and_names_what_is_there():
    run, calls = _fetcher({"README.md": 10, "adapter.bin": 512})
    with pytest.raises(LoRAFetchError) as excinfo:
        run("org/name")
    assert "no .safetensors" in str(excinfo.value)
    assert "adapter.bin" in str(excinfo.value)
    assert calls == []


def test_multiple_files_prefer_the_diffusers_convention_name():
    """Not a guess: pytorch_lora_weights.safetensors is the file diffusers'
    own load_lora_weights(repo_id) defaults to (lora_base.py,
    LORA_WEIGHT_NAME_SAFE), so a bare repo id means the same file to us as it
    would to the loader itself."""
    run, calls = _fetcher({DIFFUSERS_LORA_WEIGHT_NAME: 512, "backup.safetensors": 512})
    run("org/name")
    assert calls == [("org/name", DIFFUSERS_LORA_WEIGHT_NAME)]


def test_multiple_files_without_the_convention_refuse_and_list_candidates():
    run, calls = _fetcher({"a.safetensors": 512, "b.safetensors": 512})
    with pytest.raises(LoRAFetchError) as excinfo:
        run("org/name")
    message = str(excinfo.value)
    assert "a.safetensors" in message and "b.safetensors" in message
    assert "org/name/<file>" in message, "the refusal must say HOW to disambiguate"
    assert calls == []


def test_an_explicit_filename_that_does_not_exist_refuses_and_lists():
    run, calls = _fetcher({"real.safetensors": 512})
    with pytest.raises(LoRAFetchError) as excinfo:
        run("org/name/imagined.safetensors")
    assert "real.safetensors" in str(excinfo.value)
    assert calls == []


def test_an_explicit_non_safetensors_filename_is_refused():
    run, calls = _fetcher({"adapter.bin": 512})
    with pytest.raises(LoRAFetchError) as excinfo:
        run("org/name/adapter.bin")
    assert "safetensors" in str(excinfo.value)
    assert calls == []


# --------------------------------------------------------------------------
# the size bound binds BEFORE the download
# --------------------------------------------------------------------------


def test_an_oversized_file_is_refused_from_metadata_without_downloading():
    run, calls = _fetcher({"adapter.safetensors": CAP + 1})
    with pytest.raises(LoRATooLargeError) as excinfo:
        run("org/name")
    assert calls == [], "the bound must come from the listing, not from downloading past it"
    assert str(CAP + 1) in str(excinfo.value) and str(CAP) in str(excinfo.value)
    assert "org/name/adapter.safetensors" in str(excinfo.value)


def test_a_listing_without_a_size_refuses_rather_than_downloading_unbounded():
    """'Could not check' must not become 'checked and fine': a sizeless
    listing gives the cap nothing to bind, and the cap is why we looked."""
    run, calls = _fetcher({"adapter.safetensors": None})
    with pytest.raises(LoRAFetchError) as excinfo:
        run("org/name")
    assert "unbounded" in str(excinfo.value)
    assert calls == []


def test_a_file_exactly_at_the_cap_downloads():
    run, calls = _fetcher({"adapter.safetensors": CAP})
    run("org/name")
    assert calls == [("org/name", "adapter.safetensors")]


# --------------------------------------------------------------------------
# the Hub's own failure shapes, mapped to actionable messages
# --------------------------------------------------------------------------

# Stand-ins matched by MRO class NAME, exactly as the mapper does β€” so the
# offline gate needs no huggingface_hub import and a hub module rename cannot
# crash the mapping.


class RepositoryNotFoundError(Exception):
    pass


class GatedRepoError(RepositoryNotFoundError):
    pass


def _failing_fetcher(exc):
    def raising_listing(repo_id):
        raise exc

    def run(source):
        return fetch_lora(
            source, max_bytes=CAP, repo_files=raising_listing, download=lambda *a, **k: "/x"
        )

    return run


def test_a_missing_repo_names_the_private_repo_ambiguity():
    """The Hub deliberately reports private and nonexistent repos the same
    way; a message that only says 'not found' sends the user to check one of
    the two possible causes."""
    with pytest.raises(LoRAFetchError) as excinfo:
        _failing_fetcher(RepositoryNotFoundError("404"))("org/name")
    message = str(excinfo.value)
    assert "not found" in message and "private" in message


def test_a_gated_repo_names_the_action_not_just_the_state():
    """GatedRepoError subclasses RepositoryNotFoundError, so a naive
    order-of-checks reports every gated repo as missing β€” the gated message
    is the one with a step the user can actually take."""
    with pytest.raises(LoRAFetchError) as excinfo:
        _failing_fetcher(GatedRepoError("403"))("org/name")
    assert "gated" in str(excinfo.value)
    assert "terms" in str(excinfo.value)


def test_an_unrecognised_listing_failure_is_wrapped_verbatim():
    with pytest.raises(LoRAFetchError) as excinfo:
        _failing_fetcher(OSError("connection reset by peer"))("org/name")
    assert "connection reset by peer" in str(excinfo.value)


def test_a_failing_download_is_wrapped_verbatim():
    def download(repo_id, filename, revision=None):
        raise OSError("disk full")

    with pytest.raises(LoRAFetchError) as excinfo:
        fetch_lora(
            "org/name",
            max_bytes=CAP,
            repo_files=lambda repo: {"adapter.safetensors": 512},
            download=download,
        )
    assert "disk full" in str(excinfo.value)


def test_the_default_seams_are_huggingface_hub(monkeypatch):
    """The injected seams must default to the real Hub machinery β€” a fetcher
    whose defaults are stubs is the Β§8 inert-guard shape with extra steps."""
    import inspect as _inspect

    source = _inspect.getsource(fetch)
    assert "hf_hub_download" in source
    assert "files_metadata=True" in source, (
        "the listing must request file metadata β€” sizes are what the bound binds on"
    )


# --------------------------------------------------------------------------
# the handler stages a LOCAL path β€” the fetch lives in the web process
# --------------------------------------------------------------------------


def _valid_adapter(tmp_path):
    return good_adapter(tmp_path, "fetched.safetensors")


def test_the_handler_resolves_a_repo_id_and_stages_the_local_path(tmp_path, monkeypatch):
    """The whole point of the placement: by the time anything is billed, the
    adapter is a file read. The staged entry carries the fetched local path
    while 'source' stays what the user typed, so the UI displays the repo id
    and the worker never sees it."""
    from ltx25_studio import lora as lora_package
    from ltx25_studio.ui import handlers

    adapter = _valid_adapter(tmp_path)
    fetched = []

    def fake_fetch(source):
        fetched.append(source)
        return adapter

    monkeypatch.setattr(lora_package, "fetch_lora", fake_fetch)
    verdict, staged, summary = handlers.on_validate_lora("org/my-lora", None, [])

    assert fetched == ["org/my-lora"], "the repo id must resolve through the fetcher"
    assert len(staged) == 1
    assert staged[0]["source"] == "org/my-lora"
    assert staged[0]["path"] == str(adapter)
    assert "org/my-lora" in verdict


def test_the_handler_surfaces_a_fetch_refusal_verbatim(tmp_path, monkeypatch):
    from ltx25_studio import lora as lora_package
    from ltx25_studio.ui import handlers

    def fake_fetch(source):
        raise LoRAFetchError(source=source, reason="'org/gone' was not found on the Hub")

    monkeypatch.setattr(lora_package, "fetch_lora", fake_fetch)
    verdict, staged, _ = handlers.on_validate_lora("org/gone", None, [])

    assert staged == []
    assert "was not found on the Hub" in verdict


def test_the_upload_direction_stages_an_app_owned_copy(tmp_path, monkeypatch):
    """The two directions converge (Β§4.3): an upload is already local and the
    fetcher is never consulted. What is staged is the app's own copy, inside the
    LoRA cache, so Gradio's upload clean-up cannot remove it before Generate; the
    label still names what the user uploaded, and the same upload twice is
    staged once."""
    from ltx25_studio import lora as lora_package
    from ltx25_studio.ui import handlers

    cache = tmp_path / "lora-cache"
    monkeypatch.setattr(lora_package, "lora_cache_dir", lambda: cache)
    fetched = []
    monkeypatch.setattr(lora_package, "fetch_lora", lambda source: fetched.append(source))

    adapter = _valid_adapter(tmp_path)
    verdict, staged, _ = handlers.on_validate_lora("", str(adapter), [])

    assert fetched == [], "an upload must never reach the Hub fetcher"
    assert len(staged) == 1, verdict
    path = Path(staged[0]["path"])
    assert path != adapter and path.is_file()
    assert cache in path.parents, "the staged copy lives in the app-owned LoRA cache"
    assert path.read_bytes() == adapter.read_bytes()
    assert staged[0]["source"] == str(adapter), "the label names what the user uploaded"

    _, staged_again, _ = handlers.on_validate_lora("", str(adapter), staged)
    assert len(staged_again) == 1, "the same upload validated twice is staged once"


# --------------------------------------------------------------------------
# F56 β€” only repo ids reach the Hub route; the download is the listed commit
# --------------------------------------------------------------------------


@pytest.mark.parametrize(
    "typed",
    [
        "/etc/passwd",
        "/tmp/gradio/abc/adapter.safetensors",
        "../x/y.safetensors",
        "org/name/../../etc/passwd",
        "org/na me",
        "or\\g/name",
        "-org/name",
        "org/name/sub/./f.safetensors",
    ],
)
def test_strings_that_are_not_repo_ids_are_refused_before_the_listing(typed):
    listed = []

    def listing(repo_id):
        listed.append(repo_id)
        return {"adapter.safetensors": 512}

    with pytest.raises(LoRAFetchError) as excinfo:
        fetch_lora(typed, max_bytes=CAP, repo_files=listing, download=lambda *a, **k: "/x")
    assert listed == []
    assert "org/name" in str(excinfo.value)


def test_the_download_is_pinned_to_the_commit_the_listing_saw():
    """Listing and download used to resolve ``main`` separately: a push between
    them could swap in a file the size cap never saw."""
    seen = {}

    def listing(repo_id):
        return RepoListing(files={"adapter.safetensors": 512}, sha=SHA)

    def download(repo_id, filename, revision=None):
        seen["revision"] = revision
        return f"/cache/{repo_id}/{filename}"

    fetched = fetch_lora("org/name", max_bytes=CAP, repo_files=listing, download=download)
    assert seen["revision"] == SHA
    assert fetched.revision == SHA


def test_the_fetched_file_carries_the_card_signals_for_the_validator():
    def listing(repo_id):
        return RepoListing(
            files={"LTX25_Ripple_v11.safetensors": 512},
            sha=SHA,
            tags=("lora", "ic-lora"),
            base_models=("Lightricks/LTX-2.5",),
        )

    fetched = fetch_lora(
        "WepeNerd/LTX-Ripple",
        max_bytes=CAP,
        repo_files=listing,
        download=lambda repo_id, filename, revision=None: f"/cache/{filename}",
    )
    assert isinstance(fetched, FetchedLoRA)
    assert os.fspath(fetched) == "/cache/LTX25_Ripple_v11.safetensors"
    assert fetched.repo_id == "WepeNerd/LTX-Ripple"
    assert fetched.tags == ("lora", "ic-lora")
    assert fetched.base_models == ("Lightricks/LTX-2.5",)


def test_the_default_listing_reads_tags_base_model_and_sha():
    from types import SimpleNamespace

    info = SimpleNamespace(
        sha=SHA,
        siblings=[SimpleNamespace(rfilename="a.safetensors", size=7)],
        tags=["lora", "ic-lora", "base_model:adapter:Lightricks/LTX-2.5"],
        card_data=SimpleNamespace(base_model=["Lightricks/LTX-2.3"]),
    )
    listing = fetch.listing_from_model_info(info)
    assert listing.files == {"a.safetensors": 7}
    assert listing.sha == SHA
    assert "ic-lora" in listing.tags
    assert set(listing.base_models) == {"Lightricks/LTX-2.3", "Lightricks/LTX-2.5"}


class HfHubHTTPError(Exception):
    def __init__(self, message, status):
        super().__init__(message)
        self.response = type("Response", (), {"status_code": status})()


@pytest.mark.parametrize(
    "exc",
    [GatedRepoError("Access to model org/name is restricted"), HfHubHTTPError("401", 401)],
    ids=["GatedRepoError", "http-401"],
)
def test_a_gated_repo_failing_at_download_gets_the_gated_message(exc):
    """Measured: gated repos LIST fine and fail only at the download."""

    def download(repo_id, filename, revision=None):
        raise exc

    with pytest.raises(LoRAFetchError) as excinfo:
        fetch_lora(
            "org/name",
            max_bytes=CAP,
            repo_files=lambda repo: {"adapter.safetensors": 512},
            download=download,
        )
    assert "gated" in str(excinfo.value) and "terms" in str(excinfo.value)
    assert excinfo.value.__cause__ is exc


def test_the_size_cap_defaults_to_the_configured_limit(monkeypatch):
    monkeypatch.setenv("LTX25_LORA_MAX_GB", "0.01")  # 10 MB, the smallest legal cap
    with pytest.raises(LoRATooLargeError):
        fetch_lora(
            "org/name",
            repo_files=lambda repo: {"adapter.safetensors": 10_000_001},
            download=lambda *a, **k: "/x",
        )


# --------------------------------------------------------------------------
# F56 β€” the LoRA cache is bounded, LRU, and only ever its own directories
# --------------------------------------------------------------------------


def _cached_repo(root, repo_id, size, age_seconds):
    folder = root / ("models--" + repo_id.replace("/", "--"))
    blob = folder / "blobs" / "abc"
    blob.parent.mkdir(parents=True, exist_ok=True)
    with open(blob, "wb") as handle:
        handle.truncate(size)
    snapshot = folder / "snapshots" / SHA
    snapshot.mkdir(parents=True, exist_ok=True)
    link = snapshot / "adapter.safetensors"
    if not link.is_symlink():
        link.symlink_to(blob)
    marker = folder / fetch.LAST_USED_MARKER
    marker.touch()
    stamp = time.time() - age_seconds
    os.utime(marker, (stamp, stamp))
    return folder


def test_eviction_drops_the_least_recently_used_adapters_first(tmp_path):
    old = _cached_repo(tmp_path, "a/old", 600, age_seconds=300)
    middle = _cached_repo(tmp_path, "b/middle", 600, age_seconds=200)
    new = _cached_repo(tmp_path, "c/new", 600, age_seconds=100)
    evicted = evict_lora_cache(1300, cache_dir=tmp_path)
    assert evicted == ("a/old",)
    assert not old.exists() and middle.exists() and new.exists()


def test_eviction_never_removes_what_it_is_told_to_keep(tmp_path):
    keep = _cached_repo(tmp_path, "a/staged", 900, age_seconds=1000)
    other = _cached_repo(tmp_path, "b/other", 900, age_seconds=10)
    evict_lora_cache(1000, cache_dir=tmp_path, keep=("a/staged",))
    assert keep.exists() and not other.exists()


def test_eviction_touches_nothing_it_did_not_fetch(tmp_path):
    _cached_repo(tmp_path, "a/old", 5000, age_seconds=300)
    stranger = tmp_path / "datasets--x--y"
    stranger.mkdir()
    (stranger / "keep.bin").write_bytes(b"x" * 5000)
    unmarked = tmp_path / "models--someone--else"
    unmarked.mkdir()
    (unmarked / "weights.bin").write_bytes(b"x" * 5000)
    loose = tmp_path / "notes.txt"
    loose.write_text("mine")
    evict_lora_cache(0, cache_dir=tmp_path)
    assert stranger.exists() and unmarked.exists() and loose.exists()


def test_symlinked_snapshots_are_not_counted_twice(tmp_path):
    _cached_repo(tmp_path, "a/one", 600, age_seconds=10)
    assert evict_lora_cache(600, cache_dir=tmp_path) == ()


def test_the_fetch_path_evicts_and_keeps_what_it_just_fetched(tmp_path):
    stale = _cached_repo(tmp_path, "a/stale", 900, age_seconds=1000)

    def download(repo_id, filename, revision=None):
        folder = _cached_repo(tmp_path, repo_id, 900, age_seconds=0)
        return str(folder / "snapshots" / SHA / "adapter.safetensors")

    fetched = fetch_lora(
        "org/fresh",
        max_bytes=CAP,
        repo_files=lambda repo: {"adapter.safetensors": 900},
        download=download,
        cache_dir=tmp_path,
        cache_max_bytes=1000,
    )
    assert not stale.exists()
    assert os.path.exists(fetched)


def test_a_repeat_fetch_refreshes_the_access_stamp(tmp_path):
    folder = _cached_repo(tmp_path, "org/name", 100, age_seconds=5000)
    before = (folder / fetch.LAST_USED_MARKER).stat().st_mtime

    def download(repo_id, filename, revision=None):
        return str(folder / "snapshots" / SHA / "adapter.safetensors")

    fetch_lora(
        "org/name",
        max_bytes=CAP,
        repo_files=lambda repo: {"adapter.safetensors": 100},
        download=download,
        cache_dir=tmp_path,
    )
    assert (folder / fetch.LAST_USED_MARKER).stat().st_mtime > before + 1000


# --------------------------------------------------------------------------
# review r1 lora-6 β€” a 401 says what is known: gated, missing or private, or a
# bad token, and only a real gate is called gated. Built from real responses
# raised through huggingface_hub's own hf_raise_for_status.
# --------------------------------------------------------------------------

LISTING_URL = "https://huggingface.co/api/models/org/name"
DOWNLOAD_URL = f"https://huggingface.co/org/name/resolve/{SHA}/adapter.safetensors"
BAD_TOKEN = {"X-Error-Message": "Invalid credentials in Authorization header"}


def hub_error(status, url, **headers):
    """The exception huggingface_hub itself raises for this response."""
    httpx = pytest.importorskip("httpx")
    hub_utils = pytest.importorskip("huggingface_hub.utils")
    response = httpx.Response(status, headers=headers, request=httpx.Request("GET", url))
    with pytest.raises(Exception) as caught:  # noqa: B017 -- whatever the Hub raises
        hub_utils.hf_raise_for_status(response)
    return caught.value


def listing_refusal(exc) -> str:
    with pytest.raises(LoRAFetchError) as caught:
        _failing_fetcher(exc)("org/name")
    assert caught.value.__cause__ is exc
    return str(caught.value)


def download_refusal(exc) -> str:
    def download(repo_id, filename, revision=None):
        raise exc

    with pytest.raises(LoRAFetchError) as caught:
        fetch_lora(
            "org/name",
            max_bytes=CAP,
            repo_files=lambda repo: {"adapter.safetensors": 512},
            download=download,
        )
    return str(caught.value)


def test_a_missing_or_private_repo_listed_without_a_token_is_not_called_gated():
    """The Hub answers an anonymous listing of a missing or private repo with
    401 RepoNotFound, which huggingface_hub raises as RepositoryNotFoundError."""
    message = listing_refusal(hub_error(401, LISTING_URL, **{"X-Error-Code": "RepoNotFound"}))
    assert "gated" not in message
    assert "not found" in message and "private" in message


def test_an_invalid_token_is_named_as_the_token_when_listing():
    message = listing_refusal(hub_error(401, LISTING_URL, **BAD_TOKEN))
    assert "gated" not in message
    assert "token" in message and "invalid" in message


def test_an_invalid_token_is_named_as_the_token_when_downloading():
    message = download_refusal(hub_error(401, DOWNLOAD_URL, **BAD_TOKEN))
    assert "gated" not in message
    assert "token" in message and "invalid" in message


def test_a_bare_401_at_the_listing_says_what_it_could_be_rather_than_gated():
    message = listing_refusal(hub_error(401, LISTING_URL))
    assert "401" in message
    assert "gated" in message and "private" in message and "misspelt" in message
    assert "is gated." not in message, "a bare 401 does not prove the repo is gated"


@pytest.mark.parametrize(
    ("status", "url", "step"),
    [(403, LISTING_URL, listing_refusal), (401, DOWNLOAD_URL, download_refusal)],
    ids=["listing", "download"],
)
def test_a_real_gate_is_called_gated_at_either_step(status, url, step):
    message = step(hub_error(status, url, **{"X-Error-Code": "GatedRepo"}))
    assert "gated" in message and "terms" in message


def test_a_typo_seen_with_a_valid_token_is_not_found():
    message = listing_refusal(hub_error(404, LISTING_URL, **{"X-Error-Code": "RepoNotFound"}))
    assert "not found" in message and "gated" not in message


# --------------------------------------------------------------------------
# review r1 lora-5 β€” an adapter a session has staged, or a run is loading, is not
# evicted while it is in use, however many newer adapters are fetched after it
# --------------------------------------------------------------------------


def _adapter_in(folder):
    return folder / "snapshots" / SHA / "adapter.safetensors"


def _age_stamps(folder, seconds):
    """Make every stamp this cache keeps in ``folder`` ``seconds`` old."""
    stamp = time.time() - seconds
    for marker in folder.glob(".ltx25-*"):
        os.utime(marker, (stamp, stamp))


def test_an_adapter_in_use_is_not_evicted_ahead_of_newer_fetches(tmp_path):
    """The verifier's case: alice staged first, then bob and carol fetched."""
    alice = _cached_repo(tmp_path, "alice/style", 900, age_seconds=0)
    assert fetch.mark_used(_adapter_in(alice))
    _age_stamps(alice, 600)
    bob = _cached_repo(tmp_path, "bob/other", 900, age_seconds=300)
    carol = _cached_repo(tmp_path, "carol/new", 900, age_seconds=0)

    evicted = evict_lora_cache(2000, cache_dir=tmp_path, keep=("carol/new",))

    assert evicted == ("bob/other",)
    assert alice.exists() and carol.exists() and not bob.exists()


def test_an_adapter_unused_past_the_grace_period_can_be_evicted_again(tmp_path):
    alice = _cached_repo(tmp_path, "alice/style", 900, age_seconds=0)
    fetch.mark_used(_adapter_in(alice))
    _age_stamps(alice, fetch.EVICTION_GRACE_SECONDS + 60)
    _cached_repo(tmp_path, "bob/other", 900, age_seconds=300)

    assert evict_lora_cache(1000, cache_dir=tmp_path) == ("alice/style",)


def test_mark_used_touches_nothing_this_cache_did_not_stamp(tmp_path):
    loose = good_adapter(tmp_path / "elsewhere")
    unmarked = tmp_path / "models--someone--else"
    unmarked.mkdir()
    (unmarked / "weights.safetensors").write_bytes(b"x")

    assert fetch.mark_used(loose) is False
    assert fetch.mark_used(unmarked / "weights.safetensors") is False
    assert fetch.mark_used(tmp_path / "missing.safetensors") is False
    assert not list(tmp_path.rglob(".ltx25-*"))


def test_a_staged_upload_outlives_newer_copies_nobody_staged(tmp_path, monkeypatch):
    """Through the LoRA bay: Validate stages the app's copy and marks it used."""
    from ltx25_studio import lora
    from ltx25_studio.ui import handlers, uploads

    monkeypatch.setattr(lora, "lora_cache_dir", lambda: tmp_path / "lora-cache")
    adapter = _valid_adapter(tmp_path)
    monkeypatch.setattr(lora, "configured_max_bytes", lambda: int(adapter.stat().st_size * 1.5))
    _verdict, staged, _ = handlers.on_validate_lora("", str(adapter), [])
    copy = Path(staged[0]["path"])
    _age_stamps(copy.parent, 600)

    for index in range(5):
        uploads.own_copy(good_adapter(tmp_path / f"later-{index}"))

    assert copy.is_file(), "the staged adapter was evicted while its session still had it"


def test_loading_an_adapter_marks_it_used_first(tmp_path, fake_pipeline, good_lora):
    from ltx25_studio.lora import apply as lora_apply

    folder = _cached_repo(tmp_path / "cache", "org/adapter", 10, age_seconds=0)
    adapter = _adapter_in(folder)
    adapter.unlink()
    adapter.write_bytes(good_lora.read_bytes())
    fetch.mark_used(adapter)
    _age_stamps(folder, 5000)

    # Against the clock just before the load, not a wall-clock window: a slow
    # machine must not turn a fresh stamp into an old one. One second of slack
    # for file systems that keep whole seconds.
    before = time.time()
    lora_apply.apply_lora(fake_pipeline, adapter)

    stamps = [marker.stat().st_mtime for marker in folder.glob(".ltx25-*")]
    assert len(stamps) == 2 and min(stamps) >= before - 1, (
        "a run loading an adapter must stamp it used before the load"
    )