File size: 46,119 Bytes
0839907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""
Comprehensive tests for fastgen dataloaders.

This module tests:
1. Dummy dataset creation with correct specifications
2. Loader initialization and batch validation (keys, types, shapes)
3. Edge cases with missing files in WebDataset shards
4. Deterministic loading with resuming capabilities
"""

import io
import json
import os
import tarfile
import tempfile
import zipfile
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, List, Optional, Tuple, Type
import shutil
import importlib

import numpy as np
import pytest
import torch
from PIL import Image
from pydantic import TypeAdapter, ValidationError, ConfigDict

from fastgen.utils import instantiate


# =============================================================================
# Dummy Dataset Generation Utilities
# =============================================================================


class DummyImageDatasetBuilder:
    """Builder for creating dummy CIFAR10/ImageNet-style ZIP datasets.

    Creates ZIP files containing images and optional labels (dataset.json).
    Supports both pixel images (.png) and latent representations (.npy).

    Example:
        >>> builder = DummyImageDatasetBuilder("/tmp/test", "cifar10")
        >>> builder.add_image("00000.png", np.random.rand(32, 32, 3), label=0)
        >>> builder.add_image("00001.png", np.random.rand(32, 32, 3), label=1)
        >>> zip_path = builder.build()
    """

    def __init__(self, tmp_dir: str, dataset_name: str = "dummy_dataset"):
        """Initialize the builder.

        Args:
            tmp_dir: Directory to store the zip file
            dataset_name: Name for the zip file (without extension)
        """
        self.tmp_dir = tmp_dir
        self.dataset_name = dataset_name
        self.zip_path = os.path.join(tmp_dir, f"{dataset_name}.zip")
        self.images: List[Tuple[str, np.ndarray]] = []
        self.labels: Dict[str, int] = {}

    def add_image(self, filename: str, image: np.ndarray, label: int | None = None) -> "DummyImageDatasetBuilder":
        """Add an image to the dataset.

        Args:
            filename: Image filename (e.g., "00000.png" or "00000.npy")
            image: Image array. For .png: (H, W, C) uint8, for .npy: (C, H, W) float32
            label: Optional class label (integer)

        Returns:
            self for method chaining
        """
        self.images.append((filename, image))
        if label is not None:
            self.labels[filename] = label
        return self

    def build(self) -> str:
        os.makedirs(self.tmp_dir, exist_ok=True)
        with zipfile.ZipFile(self.zip_path, "w") as zf:
            for filename, image in self.images:
                if filename.endswith(".npy"):
                    buf = io.BytesIO()
                    np.save(buf, image)
                    zf.writestr(filename, buf.getvalue())
                else:
                    if image.ndim == 3 and image.shape[0] in (1, 3, 4):
                        image = image.transpose(1, 2, 0)
                    if image.shape[-1] == 1:
                        image = image.squeeze(-1)
                    img = Image.fromarray(image.astype(np.uint8))
                    buf = io.BytesIO()
                    img.save(buf, format="PNG")
                    zf.writestr(filename, buf.getvalue())

            if self.labels:
                labels_data = {"labels": [[fname, label] for fname, label in self.labels.items()]}
                zf.writestr("dataset.json", json.dumps(labels_data))
        return self.zip_path

    @classmethod
    def create_cifar10_style(
        cls, tmp_dir: str, num_samples: int = 100, resolution: int = 32, num_classes: int = 10
    ) -> str:
        """Create a CIFAR10-style dataset with random images.

        Args:
            tmp_dir: Directory for the dataset
            num_samples: Number of images to generate
            resolution: Image resolution (square)
            num_classes: Number of classes for labels

        Returns:
            Path to the created ZIP file
        """
        builder = cls(tmp_dir, f"cifar10-{resolution}x{resolution}")
        for i in range(num_samples):
            image = np.random.randint(0, 255, (resolution, resolution, 3), dtype=np.uint8)
            builder.add_image(f"{i:05d}.png", image, i % num_classes)
        return builder.build()

    @classmethod
    def create_imagenet_style(
        cls,
        tmp_dir: str,
        num_samples: int = 100,
        resolution: int = 64,
        num_classes: int = 1000,
        latent: bool = False,
        latent_channels: int = 8,
    ) -> str:
        """Create an ImageNet-style dataset with random images or latents.

        Args:
            tmp_dir: Directory for the dataset
            num_samples: Number of images to generate
            resolution: Image resolution (square)
            num_classes: Number of classes for labels
            latent: If True, create latent representations instead of images
            latent_channels: Number of channels for latent representations

        Returns:
            Path to the created ZIP file
        """
        suffix = "_sd" if latent else ""
        builder = cls(tmp_dir, f"imagenet_{resolution}{suffix}")
        for i in range(num_samples):
            label = num_classes - 1 if i == num_samples - 1 else (i * num_classes) // num_samples
            if latent:
                latent_res = resolution // 8
                image = np.random.randn(latent_channels, latent_res, latent_res).astype(np.float32)
                builder.add_image(f"{i:05d}.npy", image, label)
            else:
                image = np.random.randint(0, 255, (resolution, resolution, 3), dtype=np.uint8)
                builder.add_image(f"{i:05d}.png", image, label)
        return builder.build()


class DummyWebDatasetBuilder:
    """Builder for creating dummy WebDataset tar files for testing.

    Creates tar files containing samples with specified file types and shapes.
    Supports: .npy, .npz, .pth, .jpg, .png, .txt, .json, .mp4, .caption

    Example:
        >>> builder = DummyWebDatasetBuilder("/tmp/test", "00000.tar")
        >>> builder.add_sample("sample_0", {"latents.npy": np.random.randn(4, 64, 64), "txt": "caption"})
        >>> builder.add_sample("sample_1", {"latents.npy": np.random.randn(4, 64, 64), "txt": "caption"})
        >>> tar_path = builder.build()
    """

    NPY_EXTENSIONS = frozenset(
        [
            "npy",
            "latents.npy",
            "sample.npy",
            "noise.npy",
            "text_embedding.npy",
            "pooled_text_embedding.npy",
            "neg_text_embedding.npy",
            "sample_path.npy",
            "text_emb.npy",
            "path.npy",
        ]
    )
    PTH_EXTENSIONS = frozenset(["pth", "latent.pth", "txt_emb.pth", "depth_latent.pth", "noise.pth", "path.pth"])

    def __init__(self, tmp_dir: str, shard_name: str = "00000.tar"):
        self.tmp_dir = tmp_dir
        self.shard_name = shard_name
        self.shard_path = os.path.join(tmp_dir, shard_name)
        self.samples: List[Dict[str, bytes]] = []

    def add_sample(self, key: str, files: Dict[str, Any]) -> "DummyWebDatasetBuilder":
        sample_files = {f"{key}.{ext}": self._encode_data(ext, data) for ext, data in files.items()}
        self.samples.append(sample_files)
        return self

    def _encode_data(self, ext: str, data: Any) -> bytes:
        if ext == "npy" or ext.endswith(".npy"):
            buf = io.BytesIO()
            np.save(buf, data)
            return buf.getvalue()
        elif ext == "npz":
            buf = io.BytesIO()
            np.savez(buf, **data)
            return buf.getvalue()
        elif ext == "pth" or ext.endswith(".pth"):
            buf = io.BytesIO()
            torch.save(data, buf)
            return buf.getvalue()
        elif ext == "json":
            return json.dumps(data).encode("utf-8")
        elif ext in ("txt", "caption"):
            return data.encode("utf-8") if isinstance(data, str) else data
        elif ext in ("jpg", "jpeg"):
            buf = io.BytesIO()
            img = data if isinstance(data, Image.Image) else Image.fromarray(data.astype(np.uint8))
            img.save(buf, format="JPEG")
            return buf.getvalue()
        elif ext == "png":
            buf = io.BytesIO()
            img = data if isinstance(data, Image.Image) else Image.fromarray(data.astype(np.uint8))
            img.save(buf, format="PNG")
            return buf.getvalue()
        elif ext == "mp4":
            return data if isinstance(data, bytes) else self._create_dummy_mp4(data)
        else:
            return data if isinstance(data, bytes) else str(data).encode("utf-8")

    def _create_dummy_mp4(self, frames: np.ndarray) -> bytes:
        import av

        buf = io.BytesIO()
        container = av.open(buf, mode="w", format="mp4")
        stream = container.add_stream("h264", rate=24)
        stream.width, stream.height = frames.shape[2], frames.shape[1]
        stream.pix_fmt = "yuv420p"
        for frame_data in frames:
            for packet in stream.encode(av.VideoFrame.from_ndarray(frame_data, format="rgb24")):
                container.mux(packet)
        for packet in stream.encode():
            container.mux(packet)
        container.close()
        return buf.getvalue()

    def build(self) -> str:
        os.makedirs(os.path.dirname(self.shard_path) or ".", exist_ok=True)
        with tarfile.open(self.shard_path, "w") as tar:
            for sample_files in self.samples:
                for filename, data in sample_files.items():
                    info = tarfile.TarInfo(name=filename)
                    info.size = len(data)
                    tar.addfile(info, io.BytesIO(data))
        return self.shard_path

    @classmethod
    def create_dataset_dir(
        cls, tmp_dir: str, num_shards: int, samples_per_shard: int, file_specs: Dict[str, Any]
    ) -> str:
        """Create a complete WebDataset directory with multiple shards.

        Args:
            tmp_dir: Base directory for the dataset
            num_shards: Number of tar shards to create
            samples_per_shard: Number of samples per shard
            file_specs: Dict mapping extension -> shape/value specification
                        For arrays: tuple of shape (e.g., (4, 64, 64))
                        For json: dict template or callable(shard_idx, sample_idx)
                        For txt/caption: str template or callable(shard_idx, sample_idx)
                        For mp4: (T, H, W) tuple for frame dimensions

        Returns:
            Path to the dataset directory

        Example:
            >>> dataset_dir = DummyWebDatasetBuilder.create_dataset_dir(
            ...     "/tmp", num_shards=2, samples_per_shard=4,
            ...     file_specs={"latents.npy": (4, 64, 64), "txt": lambda s, i: f"Sample {s}_{i}"}
            ... )
        """
        dataset_dir = os.path.join(tmp_dir, "dataset")
        os.makedirs(dataset_dir, exist_ok=True)

        for shard_idx in range(num_shards):
            builder = cls(dataset_dir, f"{shard_idx:05d}.tar")
            for sample_idx in range(samples_per_shard):
                key = f"{shard_idx:05d}_{sample_idx:05d}"
                files = {}
                for ext, spec in file_specs.items():
                    if ext in cls.NPY_EXTENSIONS:
                        files[ext] = np.random.randn(*spec).astype(np.float32)
                    elif ext in cls.PTH_EXTENSIONS:
                        files[ext] = torch.randn(*spec)
                    elif ext == "npz":
                        files[ext] = {k: np.random.randn(*v).astype(np.float32) for k, v in spec.items()}
                    elif ext == "json":
                        files[ext] = (
                            spec(shard_idx, sample_idx)
                            if callable(spec)
                            else (spec.copy() if isinstance(spec, dict) else spec)
                        )
                    elif ext in ("txt", "caption"):
                        files[ext] = spec(shard_idx, sample_idx) if callable(spec) else spec
                    elif ext in ("jpg", "jpeg", "png"):
                        h, w = spec[:2]
                        files[ext] = np.random.randint(0, 255, (h, w, 3), dtype=np.uint8)
                    elif ext == "mp4":
                        t, h, w = spec
                        files[ext] = np.random.randint(0, 255, (t, h, w, 3), dtype=np.uint8)
                    else:
                        files[ext] = spec
                builder.add_sample(key, files)
            builder.build()
        return dataset_dir


def create_shard_count_file(dataset_dir: str, samples_per_shard: int) -> str:
    """Create a shard count JSON file for deterministic loading."""
    shard_count = {f: samples_per_shard for f in os.listdir(dataset_dir) if f.endswith(".tar")}
    count_file = os.path.join(dataset_dir, "shard_count.json")
    with open(count_file, "w", encoding="utf-8") as f:
        json.dump(shard_count, f)
    return count_file


def create_ignore_index_file(dataset_dir: str, ignore_spec: Dict[str, List[str]]) -> str:
    """Create an ignore index JSON file."""
    ignore_file = os.path.join(dataset_dir, "ignore_index.json")
    with open(ignore_file, "w", encoding="utf-8") as f:
        json.dump(ignore_spec, f)
    return ignore_file


# =============================================================================
# Test Fixtures
# =============================================================================


@pytest.fixture
def tmp_dataset_dir():
    """Create a temporary directory for test datasets."""
    tmp_dir = tempfile.mkdtemp(prefix="fastgen_test_")
    yield tmp_dir
    shutil.rmtree(tmp_dir, ignore_errors=True)


# =============================================================================
# Assertion Helpers
# =============================================================================


def assert_batch_keys(batch: Dict, expected_keys: List[str], allow_extra: bool = False):
    """Assert that batch contains expected keys."""
    for key in expected_keys:
        assert key in batch, f"Missing expected key: {key}"
    if not allow_extra:
        for key in batch:
            if not key.startswith("__"):
                assert key in expected_keys, f"Unexpected key in batch: {key}"


def assert_tensor_shape(tensor: torch.Tensor, expected_shape: Tuple[int, ...], name: str = "tensor"):
    """Assert tensor has expected shape."""
    assert tensor.shape == expected_shape, f"{name} shape mismatch: expected {expected_shape}, got {tensor.shape}"


def assert_batch_type(batch: Dict, key: str, expected_type: Type):
    """Assert that batch[key] has expected type using pydantic TypeAdapter.

    This function validates types including nested types like List[str] and Dict[str, Any].
    For torch.Tensor, it falls back to isinstance check since pydantic doesn't handle it natively.

    Args:
        batch: The batch dictionary
        key: The key to check
        expected_type: The expected type (can be a generic type like List[str])
    """
    value = batch[key]

    try:
        adapter = TypeAdapter(expected_type, config=ConfigDict(arbitrary_types_allowed=True))
        adapter.validate_python(value)
    except ValidationError as e:
        raise AssertionError(f"batch['{key}'] type validation failed for {expected_type}: {e}") from e


# =============================================================================
# Test Specification Classes
# =============================================================================


@dataclass
class LoaderTestSpec:
    """Specification for a loader test.

    The expected keys are derived from expected_types.keys(), so you don't need
    to specify them separately. Multiple configs with the same specs can be
    tested together by providing a list of config_paths.

    The test will
    run twice: once with dummy data (always), once with real data (skipped by default).
    """

    name: str
    config_paths: List[str]  # e.g., ["fastgen.configs.data.VideoLatentLoaderConfig"]
    file_specs: Dict[str, Any]
    expected_types: Dict[str, type]  # key -> expected type (keys define expected batch keys)
    expected_shapes: Dict[str, Tuple] = field(default_factory=dict)  # key -> expected shape
    num_shards: int = 1
    samples_per_shard: int = 4
    batch_size: int = 2
    extra_config: Dict[str, Any] = field(default_factory=dict)
    extra_files: Optional[Callable[[str], Dict[str, str]]] = None  # Creates extra files, returns config updates
    # Real data integration testing (optional - skipped by default)
    credentials_path: str = "./credentials/s3.json"  # Path to credentials for S3


# =============================================================================
# Generic Test Runners
# =============================================================================


def run_loader_test(tmp_dir: str, spec: LoaderTestSpec, use_real_data: bool = False):
    """Run a generic loader test from specification.

    Tests all configs in spec.config_paths with either dummy or real data.

    Args:
        tmp_dir: Temporary directory for dummy dataset
        spec: Test specification
        use_real_data: If True, use real_datatags from spec instead of dummy data
    """
    if use_real_data:
        # Real data integration test
        from fastgen.utils.io_utils import set_env_vars

        set_env_vars(credentials_path=spec.credentials_path)
        extra_config_updates = {}
        datatags = None
    else:
        # Dummy data test
        dataset_dir = DummyWebDatasetBuilder.create_dataset_dir(
            tmp_dir, spec.num_shards, spec.samples_per_shard, spec.file_specs
        )
        datatags = [f"WDS:{dataset_dir}"]
        extra_config_updates = spec.extra_files(dataset_dir) if spec.extra_files else {}
        extra_config_updates.update(spec.extra_config)

    # Test each config path
    for config_path in spec.config_paths:
        # Import config dynamically
        module_path, config_name = config_path.rsplit(".", 1)
        module = importlib.import_module(module_path)
        config_class = getattr(module, config_name)

        # Configure loader
        config = config_class.copy()
        if datatags is not None:
            config.datatags = datatags
        config.batch_size = spec.batch_size
        config.num_workers = 0
        for k, v in extra_config_updates.items():
            setattr(config, k, v)

        # Get batch
        loader = instantiate(config)
        batch = next(iter(loader))

        # Assertions - expected keys derived from expected_types
        assert_batch_keys(batch, list(spec.expected_types.keys())), f"Failed for {config_name}"
        for key, expected_type in spec.expected_types.items():
            assert_batch_type(batch, key, expected_type)
        for key, expected_shape in spec.expected_shapes.items():
            assert_tensor_shape(batch[key], expected_shape, key)


# =============================================================================
# ImageLoader Tests
# =============================================================================


@dataclass
class ImageLoaderTestSpec:
    """Specification for an ImageLoader test.

    Similar to LoaderTestSpec but for class-conditional image datasets (CIFAR10, ImageNet).
    The expected keys are derived from expected_types.keys().
    """

    name: str
    config_path: str  # e.g., "fastgen.configs.data.CIFAR10_Loader_Config"
    expected_types: Dict[str, type]  # key -> expected type
    expected_shapes: Dict[str, Tuple]  # key -> expected shape (with batch_size)
    num_samples: int = 64
    batch_size: int = 8
    latent_channels: Optional[int] = None  # If not None, used for latent-based dummy data generation
    resolution: int = 64  # Used for dummy data generation
    num_classes: int = 1000  # Used for dummy data generation
    credentials_path: str = "./credentials/s3.json"


# Test specifications for ImageLoaders
IMAGE_LOADER_SPECS = [
    ImageLoaderTestSpec(
        name="cifar10",
        config_path="fastgen.configs.data.CIFAR10_Loader_Config",
        expected_types={
            "real": torch.Tensor,
            "condition": torch.Tensor,
            "neg_condition": torch.Tensor,
            "idx": torch.Tensor,
        },
        expected_shapes={"real": (8, 3, 32, 32), "condition": (8, 10), "neg_condition": (8, 10), "idx": (8,)},
        resolution=32,
        num_classes=10,
    ),
    ImageLoaderTestSpec(
        name="imagenet64",
        config_path="fastgen.configs.data.ImageNet64_Loader_Config",
        expected_types={
            "real": torch.Tensor,
            "condition": torch.Tensor,
            "neg_condition": torch.Tensor,
            "idx": torch.Tensor,
        },
        expected_shapes={"real": (8, 3, 64, 64), "condition": (8, 1000), "neg_condition": (8, 1000), "idx": (8,)},
        resolution=64,
        num_classes=1000,
    ),
    ImageLoaderTestSpec(
        name="imagenet64_edmv2",
        config_path="fastgen.configs.data.ImageNet64_EDMV2_Loader_Config",
        expected_types={
            "real": torch.Tensor,
            "condition": torch.Tensor,
            "neg_condition": torch.Tensor,
            "idx": torch.Tensor,
        },
        expected_shapes={"real": (8, 3, 64, 64), "condition": (8, 1000), "neg_condition": (8, 1000), "idx": (8,)},
        resolution=64,
        num_classes=1000,
    ),
    ImageLoaderTestSpec(
        name="imagenet256_latent",
        config_path="fastgen.configs.data.ImageNet256_Loader_Config",
        expected_types={
            "real": torch.Tensor,
            "condition": torch.Tensor,
            "neg_condition": torch.Tensor,
            "idx": torch.Tensor,
        },
        expected_shapes={"real": (8, 8, 32, 32), "condition": (8, 1000), "neg_condition": (8, 1000), "idx": (8,)},
        resolution=256,
        num_classes=1000,
        latent_channels=8,
    ),
]


def run_image_loader_test(tmp_dir: str, spec: ImageLoaderTestSpec, use_real_data: bool = False):
    """Run an ImageLoader test from specification.

    Args:
        tmp_dir: Temporary directory for dummy dataset
        spec: Test specification
        use_real_data: If True, use real data paths from config instead of dummy data
    """
    # Import config dynamically
    module_path, config_name = spec.config_path.rsplit(".", 1)
    module = importlib.import_module(module_path)
    config_class = getattr(module, config_name)

    config = config_class.copy()
    config.batch_size = spec.batch_size
    config.sampler_start_idx = 0

    if use_real_data:
        # Real data integration test - use paths from config
        from fastgen.utils.io_utils import set_env_vars

        set_env_vars(credentials_path=spec.credentials_path)
    else:
        # Dummy data test - create synthetic dataset
        if spec.latent_channels is not None:
            zip_path = DummyImageDatasetBuilder.create_imagenet_style(
                tmp_dir,
                spec.num_samples,
                spec.resolution,
                spec.num_classes,
                latent=True,
                latent_channels=spec.latent_channels,
            )
        elif spec.num_classes == 10:  # CIFAR10-style
            zip_path = DummyImageDatasetBuilder.create_cifar10_style(
                tmp_dir, spec.num_samples, spec.resolution, spec.num_classes
            )
        else:  # ImageNet-style
            zip_path = DummyImageDatasetBuilder.create_imagenet_style(
                tmp_dir, spec.num_samples, spec.resolution, spec.num_classes
            )
        config.dataset_path = zip_path
        config.s3_path = None

    # Get batch
    loader = instantiate(config)
    batch = next(iter(loader))

    # Assert keys (derived from expected_types)
    expected_keys = list(spec.expected_types.keys())
    assert_batch_keys(batch, expected_keys)

    # Assert types
    for key, expected_type in spec.expected_types.items():
        assert_batch_type(batch, key, expected_type)

    # Shape checks (only for dummy data - real data shapes depend on config batch_size)
    if not use_real_data:
        for key, expected_shape in spec.expected_shapes.items():
            assert_tensor_shape(batch[key], expected_shape, key)
        if spec.latent_channels is None:
            assert batch["real"].min() >= -1.0 and batch["real"].max() <= 1.0
    else:
        # For real data, just check that shapes are consistent
        assert batch["condition"].shape[1] == spec.num_classes, f"Expected {spec.num_classes} classes"


class TestImageLoader:
    """Tests for ImageLoader with CIFAR10 and ImageNet-style datasets."""

    @pytest.mark.parametrize("spec", IMAGE_LOADER_SPECS, ids=lambda s: s.name)
    def test_image_loader(self, tmp_dataset_dir, spec):
        """Test image loaders with dummy datasets."""
        run_image_loader_test(tmp_dataset_dir, spec, use_real_data=False)

    @pytest.mark.parametrize("spec", IMAGE_LOADER_SPECS, ids=lambda s: s.name)
    @pytest.mark.integration
    def test_image_loader_real_data(self, tmp_dataset_dir, spec):
        """Test image loaders with real data (integration test)."""
        run_image_loader_test(tmp_dataset_dir, spec, use_real_data=True)

    def test_sampler_start_idx_resume(self, tmp_dataset_dir):
        """Test that sampler_start_idx correctly resumes from a given position."""
        from fastgen.configs.data import CIFAR10_Loader_Config

        zip_path = DummyImageDatasetBuilder.create_cifar10_style(tmp_dataset_dir, 32, 32, 10)

        def create_loader(start_idx):
            config = CIFAR10_Loader_Config.copy()
            config.dataset_path = zip_path
            config.s3_path = None
            config.batch_size = 4
            config.sampler_start_idx = start_idx
            config.shuffle = True
            return instantiate(config)

        # Get first 3 batches from start
        loader_start = iter(create_loader(0))
        batches = [next(loader_start)["idx"].tolist() for _ in range(3)]

        # Resume from index 8 should match 3rd batch
        loader_resume = iter(create_loader(8))
        resumed_batch = next(loader_resume)["idx"].tolist()

        assert resumed_batch == batches[2], f"Resume mismatch: expected {batches[2]}, got {resumed_batch}"

    def test_samples_unique_within_epoch(self, tmp_dataset_dir):
        """Test that all samples within one epoch are unique."""
        from fastgen.configs.data import CIFAR10_Loader_Config

        num_samples, batch_size = 32, 4
        zip_path = DummyImageDatasetBuilder.create_cifar10_style(tmp_dataset_dir, num_samples, 32, 10)

        config = CIFAR10_Loader_Config.copy()
        config.dataset_path = zip_path
        config.s3_path = None
        config.batch_size = batch_size
        config.sampler_start_idx = 0
        config.shuffle = True

        loader = instantiate(config)
        all_indices = []
        for i, batch in enumerate(loader):
            if i >= num_samples // batch_size:
                break
            all_indices.extend(batch["idx"].tolist())

        assert len(all_indices) == num_samples
        assert len(set(all_indices)) == num_samples, "Found duplicate samples in epoch"

    def test_samples_unique_across_resumed_training(self, tmp_dataset_dir):
        """Test that resumed training doesn't repeat samples."""
        from fastgen.configs.data import CIFAR10_Loader_Config

        num_samples, batch_size = 32, 4
        zip_path = DummyImageDatasetBuilder.create_cifar10_style(tmp_dataset_dir, num_samples, 32, 10)

        def create_loader(start_idx):
            config = CIFAR10_Loader_Config.copy()
            config.dataset_path = zip_path
            config.s3_path = None
            config.batch_size = batch_size
            config.sampler_start_idx = start_idx
            config.shuffle = False
            return instantiate(config)

        # First half
        loader1 = create_loader(0)
        first_half = []
        for i, batch in enumerate(loader1):
            if i >= num_samples // (2 * batch_size):
                break
            first_half.extend(batch["idx"].tolist())

        # Second half (resumed)
        loader2 = create_loader(len(first_half))
        second_half = []
        for i, batch in enumerate(loader2):
            if i >= num_samples // (2 * batch_size):
                break
            second_half.extend(batch["idx"].tolist())

        assert set(first_half).isdisjoint(set(second_half)), "Found overlapping samples"


# =============================================================================
# WDSLoader Tests
# =============================================================================


def _create_neg_prompt_file(dataset_dir: str, shape: Tuple[int, ...]) -> Dict[str, str]:
    """Create a neg_prompt_emb.npy file for testing files_map loading.

    Args:
        dataset_dir: Directory to create the file in
        shape: Shape of the tensor to create

    Returns:
        Dict with files_map config update pointing to the created file
    """
    neg_prompt_path = os.path.join(dataset_dir, "neg_prompt_emb.npy")
    np.save(neg_prompt_path, np.random.randn(*shape).astype(np.float32))
    return {"files_map": {"neg_condition": neg_prompt_path}}


# Test specifications for WDS loaders (generic template configs)
WDS_LOADER_SPECS = [
    # ImageWDSLoader - generic image loader with jpg + txt
    LoaderTestSpec(
        name="image_wds",
        config_paths=["fastgen.configs.data.ImageLoaderConfig"],
        file_specs={
            "jpg": (512, 512),
            "txt": lambda s, i: f"A sample image {s}_{i}",
        },
        expected_types={
            "real": torch.Tensor,
            "condition": List[str],
            "neg_condition": List[str],
            "fname": List[str],
            "shard": List[str],
        },
        expected_shapes={"real": (2, 3, 512, 512)},
        extra_config={"input_res": 512},
    ),
    # VideoWDSLoader - generic video loader with mp4 + txt
    LoaderTestSpec(
        name="video_wds",
        config_paths=["fastgen.configs.data.VideoLoaderConfig"],
        file_specs={
            "mp4": (90, 480, 832),
            "txt": lambda s, i: f"A sample video {s}_{i}",
        },
        expected_types={
            "real": torch.Tensor,
            "condition": List[str],
            "neg_condition": List[str],
            "cropping_params": Dict[str, Any],
            "fname": List[str],
            "shard": List[str],
        },
        expected_shapes={"real": (1, 3, 81, 480, 832)},  # (B, C, T, H, W)
        batch_size=1,
        samples_per_shard=2,
        extra_config={"sequence_length": 81, "img_size": (832, 480)},
    ),
    # WDSLoader - generic image latent loader with latent.pth + txt_emb.pth + neg_condition from files_map
    LoaderTestSpec(
        name="image_latent_wds",
        config_paths=["fastgen.configs.data.ImageLatentLoaderConfig"],
        file_specs={
            "latent.pth": (4, 128, 128),
            "txt_emb.pth": (77, 2048),
        },
        expected_types={
            "real": torch.Tensor,
            "condition": torch.Tensor,
            "neg_condition": torch.Tensor,
            "fname": List[str],
            "shard": List[str],
        },
        expected_shapes={
            "real": (2, 4, 128, 128),
            "condition": (2, 77, 2048),
            "neg_condition": (2, 77, 2048),
        },
        extra_files=lambda d: _create_neg_prompt_file(d, (77, 2048)),
        batch_size=2,
    ),
    # WDSLoader - generic video latent loader with latent.pth + txt_emb.pth + neg_condition from files_map
    LoaderTestSpec(
        name="video_latent_wds",
        config_paths=["fastgen.configs.data.VideoLatentLoaderConfig"],
        file_specs={
            "latent.pth": (16, 21, 60, 104),
            "txt_emb.pth": (512, 4096),
        },
        expected_types={
            "real": torch.Tensor,
            "condition": torch.Tensor,
            "neg_condition": torch.Tensor,
            "fname": List[str],
            "shard": List[str],
        },
        expected_shapes={
            "real": (2, 16, 21, 60, 104),
            "condition": (2, 512, 4096),
            "neg_condition": (2, 512, 4096),
        },
        extra_files=lambda d: _create_neg_prompt_file(d, (512, 4096)),
    ),
    # PairLoaderConfig - for single-step KD with (real, noise, condition)
    # Data requirements from KD.py: {"real": clean, "noise": noise, "condition": cond}
    LoaderTestSpec(
        name="pair_wds",
        config_paths=["fastgen.configs.data.PairLoaderConfig"],
        file_specs={
            "latent.pth": (16, 21, 60, 104),
            "noise.pth": (16, 21, 60, 104),
            "txt_emb.pth": (512, 4096),
        },
        expected_types={
            "real": torch.Tensor,
            "noise": torch.Tensor,
            "condition": torch.Tensor,
            "fname": List[str],
            "shard": List[str],
        },
        expected_shapes={
            "real": (2, 16, 21, 60, 104),
            "noise": (2, 16, 21, 60, 104),
            "condition": (2, 512, 4096),
        },
    ),
    # PathLoaderConfig - for multi-step KD with (real, path, condition)
    # Data requirements from KD.py: {"real": clean, "path": [B, steps, C, ...], "condition": cond}
    # path shape: [B, num_inf_steps=4, C, ...]
    LoaderTestSpec(
        name="path_wds",
        config_paths=["fastgen.configs.data.PathLoaderConfig"],
        file_specs={
            "latent.pth": (16, 21, 60, 104),
            "path.pth": (4, 16, 21, 60, 104),  # [num_inf_steps=4, C, T, H, W]
            "txt_emb.pth": (512, 4096),
        },
        expected_types={
            "real": torch.Tensor,
            "path": torch.Tensor,
            "condition": torch.Tensor,
            "fname": List[str],
            "shard": List[str],
        },
        expected_shapes={
            "real": (2, 16, 21, 60, 104),
            "path": (2, 4, 16, 21, 60, 104),  # [B, num_inf_steps=4, C, T, H, W]
            "condition": (2, 512, 4096),
        },
    ),
]


class TestWDSLoader:
    """Tests for WDSLoader with generic template configs.

    Each test runs with dummy data by default. A second test variant runs with real data
    (skipped by default, enable with pytest --run-integration).
    """

    @pytest.mark.parametrize("spec", WDS_LOADER_SPECS, ids=lambda s: s.name)
    def test_wds_loader(self, tmp_dataset_dir, spec):
        """Test WDS loaders with dummy datasets."""
        run_loader_test(tmp_dataset_dir, spec, use_real_data=False)

    @pytest.mark.parametrize("spec", WDS_LOADER_SPECS, ids=lambda s: s.name)
    @pytest.mark.integration
    def test_wds_loader_real_data(self, tmp_dataset_dir, spec):
        """Test WDS loaders with real data (integration test)."""
        run_loader_test(tmp_dataset_dir, spec, use_real_data=True)


# =============================================================================
# Edge Case Tests
# =============================================================================


class TestEdgeCases:
    """Tests for edge cases and error handling."""

    def test_missing_optional_files(self, tmp_dataset_dir):
        """Test handling of missing optional files."""
        from fastgen.datasets.wds_dataloaders import WDSLoader

        dataset_dir = DummyWebDatasetBuilder.create_dataset_dir(
            tmp_dataset_dir, 1, 4, {"latents.npy": (16, 64, 64), "text_embedding.npy": (512, 4096)}
        )

        loader = WDSLoader(
            datatags=[f"WDS:{dataset_dir}"],
            batch_size=2,
            key_map={"real": "latents.npy", "condition": "text_embedding.npy"},
            presets_map={"neg_condition": "empty_string"},
            num_workers=0,
        )

        batch = next(iter(loader))
        assert_batch_keys(batch, ["real", "condition", "neg_condition", "fname", "shard"])
        assert_batch_type(batch, "neg_condition", List[str])
        assert all(c == "" for c in batch["neg_condition"])

    def test_partial_files_filtering(self, tmp_dataset_dir):
        """Test that samples with missing required files are filtered out."""
        from fastgen.datasets.wds_dataloaders import WDSLoader

        partial_dir = os.path.join(tmp_dataset_dir, "partial")
        os.makedirs(partial_dir, exist_ok=True)
        builder = DummyWebDatasetBuilder(partial_dir, "00000.tar")

        # Add complete and incomplete samples (indices 0,2 have text, 1,3 don't)
        for i, has_text in enumerate([True, False, True, False]):
            key = f"00000_{i:05d}"
            sample = {f"{key}.latents.npy": builder._encode_data("npy", np.random.randn(16, 64, 64).astype(np.float32))}
            if has_text:
                sample[f"{key}.text_embedding.npy"] = builder._encode_data(
                    "npy", np.random.randn(512, 4096).astype(np.float32)
                )
            builder.samples.append(sample)
        builder.build()

        loader = WDSLoader(
            datatags=[f"WDS:{partial_dir}"],
            batch_size=2,
            key_map={"real": "latents.npy", "condition": "text_embedding.npy"},
            num_workers=0,
            train=False,
        )

        batch = next(iter(loader))
        # check that only the two complete samples are loaded based on fname
        assert batch["fname"] == ["00000_00000", "00000_00002"]

    def test_ignore_index_filtering(self, tmp_dataset_dir):
        """Test ignore index filtering."""
        from fastgen.datasets.wds_dataloaders import WDSLoader

        dataset_dir = DummyWebDatasetBuilder.create_dataset_dir(tmp_dataset_dir, 1, 4, {"latents.npy": (16, 64, 64)})

        ignore_file = create_ignore_index_file(dataset_dir, {"00000.tar": ["00000_00000", "00000_00001"]})

        loader = WDSLoader(
            datatags=[f"WDS:{dataset_dir}"],
            batch_size=2,
            key_map={"real": "latents.npy"},
            ignore_index_paths=[ignore_file],
            num_workers=0,
        )

        batch = next(iter(loader))
        assert_batch_type(batch, "fname", List[str])
        assert "00000_00000" not in batch["fname"]
        assert "00000_00001" not in batch["fname"]


# =============================================================================
# Deterministic WDSLoader Tests
# =============================================================================


class TestDeterministicWDSLoader:
    """Tests for deterministic WDSLoader with resuming capabilities."""

    @pytest.fixture
    def deterministic_dataset_dir(self, tmp_dataset_dir):
        """Create a deterministic test dataset with multiple shards."""
        dataset_dir = os.path.join(tmp_dataset_dir, "deterministic")
        os.makedirs(dataset_dir, exist_ok=True)

        for shard_idx in range(3):
            builder = DummyWebDatasetBuilder(dataset_dir, f"{shard_idx:05d}.tar")
            for sample_idx in range(8):
                # Deterministic data for verification
                latent_data = np.full((16, 64, 64), float(shard_idx * 8 + sample_idx), dtype=np.float32)
                builder.samples.append(
                    {
                        f"{shard_idx:05d}_{sample_idx:05d}.latents.npy": builder._encode_data("npy", latent_data),
                        f"{shard_idx:05d}_{sample_idx:05d}.text_embedding.npy": builder._encode_data(
                            "npy", np.random.randn(512, 4096).astype(np.float32)
                        ),
                    }
                )
            builder.build()

        shard_count_file = create_shard_count_file(dataset_dir, 8)
        return dataset_dir, shard_count_file

    def _create_det_loader(self, dataset_dir, shard_count_file, start_idx=0, **kwargs):
        """Helper to create a deterministic WDSLoader."""
        from fastgen.datasets.wds_dataloaders import WDSLoader

        defaults = {
            "datatags": [f"WDS:{dataset_dir}"],
            "batch_size": 4,
            "key_map": {"real": "latents.npy", "condition": "text_embedding.npy"},
            "num_workers": 1,
            "deterministic": True,
            "sampler_start_idx": start_idx,
            "shard_count_file": shard_count_file,
        }
        return WDSLoader(**{**defaults, **kwargs})

    def _collect_fnames(self, loader, max_samples=None, max_batches=None):
        """Helper to collect fnames from loader."""
        fnames = []
        for i, batch in enumerate(loader):
            fnames.extend(batch["fname"])
            if max_samples and len(fnames) >= max_samples:
                break
            if max_batches and i >= max_batches - 1:
                break
        return fnames[:max_samples] if max_samples else fnames

    def test_deterministic_same_order(self, deterministic_dataset_dir):
        """Test that deterministic loader produces same order every run."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        fnames1 = self._collect_fnames(self._create_det_loader(dataset_dir, shard_count_file), max_batches=6)
        fnames2 = self._collect_fnames(self._create_det_loader(dataset_dir, shard_count_file), max_batches=6)

        assert fnames1 == fnames2, "Deterministic loader should produce same order"

    def test_deterministic_resume_from_index(self, deterministic_dataset_dir):
        """Test resuming from a specific sampler_start_idx."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        full_fnames = self._collect_fnames(self._create_det_loader(dataset_dir, shard_count_file), max_batches=6)
        resumed_fnames = self._collect_fnames(
            self._create_det_loader(dataset_dir, shard_count_file, start_idx=12), max_batches=3
        )

        assert resumed_fnames == full_fnames[12 : 12 + len(resumed_fnames)]

    def test_deterministic_no_overlap_on_resume(self, deterministic_dataset_dir):
        """Test that resumed samples don't overlap with pre-resume samples."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        start_fnames = self._collect_fnames(self._create_det_loader(dataset_dir, shard_count_file), max_batches=2)
        resumed_fnames = self._collect_fnames(
            self._create_det_loader(dataset_dir, shard_count_file, start_idx=8), max_batches=2
        )

        assert set(start_fnames).isdisjoint(set(resumed_fnames))

    def test_deterministic_unique_samples_per_epoch(self, deterministic_dataset_dir):
        """Test that samples are unique within one epoch."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        all_fnames = self._collect_fnames(self._create_det_loader(dataset_dir, shard_count_file), max_samples=24)

        assert len(all_fnames) == 24
        assert len(set(all_fnames)) == 24, "Found duplicate samples"

    def test_deterministic_data_integrity(self, deterministic_dataset_dir):
        """Test that data values are correctly loaded."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        loader = self._create_det_loader(dataset_dir, shard_count_file, batch_size=1)

        for i, batch in enumerate(loader):
            fname = batch["fname"][0]
            shard_idx, sample_idx = int(fname.split("_")[0]), int(fname.split("_")[1])
            expected_value = float(shard_idx * 8 + sample_idx)
            actual_value = batch["real"][0, 0, 0, 0].item()

            assert abs(actual_value - expected_value) < 1e-5, f"Data mismatch for {fname}"
            assert_batch_type(batch, "real", torch.Tensor)

            if i >= 5:
                break

    def test_deterministic_with_ignore_index(self, deterministic_dataset_dir):
        """Test deterministic loader with ignore index filtering."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        ignore_file = create_ignore_index_file(
            dataset_dir,
            {
                "00000.tar": ["00000_00000", "00000_00001"],
                "00001.tar": ["00001_00000"],
            },
        )

        loader = self._create_det_loader(dataset_dir, shard_count_file, ignore_index_paths=[ignore_file], partial=True)
        all_fnames = self._collect_fnames(loader, max_samples=21)

        assert "00000_00000" not in all_fnames
        assert "00000_00001" not in all_fnames
        assert "00001_00000" not in all_fnames
        assert len(all_fnames) == 21

    def test_deterministic_resume_with_ignore_index(self, deterministic_dataset_dir):
        """Test resuming deterministic loader with ignore index."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        ignore_file = create_ignore_index_file(dataset_dir, {"00000.tar": ["00000_00000", "00000_00001"]})

        full_fnames = self._collect_fnames(
            self._create_det_loader(dataset_dir, shard_count_file, ignore_index_paths=[ignore_file], partial=True),
            max_samples=22,
        )
        resumed_fnames = self._collect_fnames(
            self._create_det_loader(
                dataset_dir, shard_count_file, start_idx=8, ignore_index_paths=[ignore_file], partial=True
            ),
            max_samples=14,
        )

        assert resumed_fnames == full_fnames[8:]

    def test_deterministic_batch_types(self, deterministic_dataset_dir):
        """Test batch types are correct in deterministic mode."""
        dataset_dir, shard_count_file = deterministic_dataset_dir

        batch = next(iter(self._create_det_loader(dataset_dir, shard_count_file)))

        assert_batch_type(batch, "real", torch.Tensor)
        assert_batch_type(batch, "condition", torch.Tensor)
        assert_batch_type(batch, "fname", List[str])
        assert_batch_type(batch, "shard", List[str])
        assert_tensor_shape(batch["real"], (4, 16, 64, 64))
        assert_tensor_shape(batch["condition"], (4, 512, 4096))