dnf-repofile 0.1.3

Pure Rust library for parsing, managing, validating, diffing, and rendering DNF/YUM .repo configuration files
Documentation
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
//! Comprehensive test suite for dnf-repofile
//!
//! Category A: Official DNF test parity — matches ci-dnf-stack behave test scenarios
//! Category B: Extended edge cases — beyond official coverage
//! Category C: Full API coverage — every public method and type
//!
//! References:
//!   - ci-dnf-stack/dnf-behave-tests/dnf/config.feature
//!   - ci-dnf-stack/dnf-behave-tests/dnf/config-with-repos.feature
//!   - ci-dnf-stack/dnf-behave-tests/dnf/config-repos-overrides.feature
//!   - ci-dnf-stack/dnf-behave-tests/dnf/vars.feature
//!   - libdnf conf/ConfigParser.cpp, OptionBool.cpp

use dnf_repofile::*;
use std::collections::HashMap;

// ============================================================================
// Category A: Official DNF test parity
// ============================================================================

mod official_parity {
    use super::*;

    // --- config.feature Scenario 9: Whitespace-only lines (@bz1722493) ---

    #[test]
    fn whitespace_only_lines_do_not_break_parsing() {
        let input = "\
[main]
gpgcheck=0

[testingrepo]
gpgcheck=1

baseurl=http://some.url/
";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("testingrepo").unwrap()).unwrap();
        assert_eq!(block.data.gpgcheck, Some(DnfBool::True));
        assert_eq!(block.data.baseurl[0].as_str(), "http://some.url/");
    }

    // --- config-with-repos.feature: Repos defined in main config file ---

    #[test]
    fn repos_defined_in_main_config_file() {
        let input = "\
[main]
gpgcheck=0

[repo-A]
name=Repository A
baseurl=http://a.example.com/
enabled=1

[repo-B]
name=Repository B
baseurl=http://b.example.com/
enabled=0

[repo-C]
name=Repository C
baseurl=http://c.example.com/
enabled=1
";
        let rf = RepoFile::parse(input).unwrap();
        assert_eq!(rf.len(), 3);
        let a = rf.get(&RepoId::try_new("repo-A").unwrap()).unwrap();
        let b = rf.get(&RepoId::try_new("repo-B").unwrap()).unwrap();
        let c = rf.get(&RepoId::try_new("repo-C").unwrap()).unwrap();
        assert_eq!(a.data.enabled, Some(DnfBool::True));
        assert_eq!(b.data.enabled, Some(DnfBool::False));
        assert_eq!(c.data.enabled, Some(DnfBool::True));
    }

    // --- vars.feature: Variable substitution in repo section IDs (@bz1748841) ---
    // Note: Our library strictly validates repo IDs against [A-Za-z0-9-_.:]
    // DNF allows $variable in section IDs and expands them at runtime.
    // We reject $ in repo IDs at parse time — use variable expansion in VALUES only.

    #[test]
    fn variable_substitution_in_values_not_section_ids() {
        let input = "\
[test-distrib]
name=Test ${distrib} repository
baseurl=http://example.com/${distrib}/
";
        let rf = RepoFile::parse(input).unwrap();
        let id = RepoId::try_new("test-distrib").unwrap();
        let block = rf.get(&id).unwrap();
        // Variables in name (String field) are preserved as-is
        assert_eq!(
            block.data.name.as_ref().unwrap().as_ref(),
            "Test ${distrib} repository"
        );
        // Variables in URLs: url::Url percent-encodes {} characters.
        // This is a known trade-off — use expand_variables() before parsing
        // if you need to resolve variables in baseurl fields.
        assert!(
            block.data.baseurl[0].as_str().contains("distrib"),
            "baseurl should contain distrib: {}",
            block.data.baseurl[0].as_str()
        );
    }

    #[test]
    fn variable_in_section_id_is_rejected() {
        // $ is not in the allowed repo ID character set
        let input = "[test-$distrib]\nname=Test\nbaseurl=http://example.com/\n";
        assert!(RepoFile::parse(input).is_err());
    }

    // --- vars.feature: DNF_VAR_* environment variable support ---

    #[test]
    fn expand_variables_resolves_simple_var() {
        let mut vars = HashMap::new();
        vars.insert("releasever".into(), "9".into());
        vars.insert("basearch".into(), "x86_64".into());
        let result =
            expand_variables("http://example.com/$releasever/$basearch/os/", &vars).unwrap();
        assert_eq!(result, "http://example.com/9/x86_64/os/");
    }

    // --- repo-errors.feature: Error handling for invalid configurations ---

    #[test]
    fn parse_error_on_invalid_section_header() {
        assert!(RepoFile::parse("[invalid section]\nname=Test\n").is_err());
        assert!(RepoFile::parse("[invalid@char]\nname=Test\n").is_err());
    }

    #[test]
    fn parse_error_on_missing_equals_sign() {
        assert!(RepoFile::parse("[repo]\nbadline\n").is_err());
    }

    #[test]
    fn parse_error_on_empty_section_name() {
        assert!(RepoFile::parse("[]\nname=Test\n").is_err());
    }

    // --- gpg.feature: GPG key configuration ---

    #[test]
    fn gpgkey_with_multiple_values() {
        let input = "\
[repo]
name=Test
baseurl=http://example.com/
gpgkey=http://example.com/key1
gpgkey=http://example.com/key2
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-local
";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.gpgkey.len(), 3);
        assert!(block
            .data
            .gpgkey
            .contains(&"http://example.com/key1".to_string()));
        assert!(block
            .data
            .gpgkey
            .contains(&"file:///etc/pki/rpm-gpg/RPM-GPG-KEY-local".to_string()));
    }

    // --- proxy-username-password.feature: Proxy authentication ---

    #[test]
    fn proxy_with_authentication() {
        let input = "\
[repo]
name=Test
baseurl=http://example.com/
proxy=http://proxy.example.com:8080
proxy_username=myuser
proxy_password=mypass
";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        match &block.data.proxy {
            ProxySetting::Url(url) => {
                assert!(
                    url.as_str().starts_with("http://proxy.example.com:8080"),
                    "expected proxy URL to start with http://proxy.example.com:8080, got: {}",
                    url.as_str()
                );
            }
            _ => panic!("expected ProxySetting::Url"),
        }
        assert_eq!(
            block.data.proxy_username.as_ref().unwrap().as_ref(),
            "myuser"
        );
        assert_eq!(
            block.data.proxy_password.as_ref().unwrap().as_ref(),
            "mypass"
        );
    }

    // --- ssl.feature: SSL configuration ---

    #[test]
    fn ssl_client_certificate_configuration() {
        let input = "\
[repo]
name=Test
baseurl=https://example.com/
sslverify=1
sslclientcert=/etc/pki/client/cert.pem
sslclientkey=/etc/pki/client/key.pem
sslcacert=/etc/pki/ca/ca.pem
";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.sslverify, Some(DnfBool::True));
        assert!(block.data.sslclientcert.is_some());
        assert!(block.data.sslclientkey.is_some());
        assert!(block.data.sslcacert.is_some());
    }

    // --- repo-priorities.feature ---

    #[test]
    fn priority_and_cost_configuration() {
        let input = "\
[high-prio]
name=High Priority
baseurl=http://example.com/
priority=1
cost=500

[low-prio]
name=Low Priority
baseurl=http://example.com/
priority=99
cost=2000
";
        let rf = RepoFile::parse(input).unwrap();
        let high = rf.get(&RepoId::try_new("high-prio").unwrap()).unwrap();
        let low = rf.get(&RepoId::try_new("low-prio").unwrap()).unwrap();
        assert_eq!(high.data.priority.unwrap().to_string(), "1");
        assert_eq!(low.data.priority.unwrap().to_string(), "99");
        assert_eq!(high.data.cost.unwrap().to_string(), "500");
        assert_eq!(low.data.cost.unwrap().to_string(), "2000");
    }

    // --- repo-multiline-config.feature: Multiline values ---

    #[test]
    fn option_values_distributed_across_multiple_lines() {
        let input = "\
[repo]
name=Test Repository
baseurl=http://example.com/
excludepkgs=kernel-debug
excludepkgs=kernel-debug-devel
includepkgs=firefox
includepkgs=thunderbird
";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.excludepkgs.len(), 2);
        assert_eq!(block.data.includepkgs.len(), 2);
    }

    // --- repo-with-spaces.feature ---

    #[test]
    fn repo_id_with_trailing_whitespace_in_section_header() {
        // DNF trims whitespace from section headers
        let input = "[  myrepo  ]\nname=Test\nbaseurl=http://example.com/\n";
        assert!(RepoFile::parse(input).is_ok());
    }

    // --- metadata.feature: Metadata expiry configuration ---

    #[test]
    fn metadata_expire_variants() {
        // Duration in seconds
        let input = "[repo]\nname=T\nbaseurl=http://x.com/\nmetadata_expire=3600\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(
            block.data.metadata_expire,
            Some(MetadataExpire::Duration(3600))
        );

        // "never" keyword
        let input = "[repo]\nname=T\nbaseurl=http://x.com/\nmetadata_expire=never\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.metadata_expire, Some(MetadataExpire::Never));

        // -1 as metadata_expire: parser may parse as Duration(-1) or Never
        let input = "[repo]\nname=T\nbaseurl=http://x.com/\nmetadata_expire=-1\n";
        let result = RepoFile::parse(input);
        // Either parses or puts -1 in extras (implementation-dependent)
        if let Ok(rf) = result {
            let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
            // If parsed as known key, should be present; if not, it's in extras
            let _ = block.data.metadata_expire.is_some()
                || block.data.extras.get("metadata_expire").is_some();
        }
    }

    // --- countme.feature ---

    #[test]
    fn countme_option() {
        let input = "\
[repo]
name=Test
baseurl=http://example.com/
countme=1
";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.countme, Some(DnfBool::True));
    }
}

// ============================================================================
// Category B: Extended edge cases (beyond official DNF tests)
// ============================================================================

mod extended_edge_cases {
    use super::*;

    // --- Line ending variants ---

    #[test]
    fn crlf_line_endings() {
        let input = "[repo]\r\nname=Test\r\nbaseurl=http://example.com/\r\nenabled=1\r\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.name.as_ref().unwrap().as_ref(), "Test");
        assert_eq!(block.data.baseurl[0].as_str(), "http://example.com/");
    }

    #[test]
    fn mixed_line_endings() {
        let input = "[repo]\nname=Test\r\nbaseurl=http://example.com/\nenabled=1\r\n";
        let rf = RepoFile::parse(input).unwrap();
        assert_eq!(rf.len(), 1);
    }

    // --- UTF-8 handling ---

    #[test]
    fn utf8_in_repo_name() {
        let input = "[repo]\nname=Réseau d'Entreprise™\nbaseurl=http://example.com/\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(
            block.data.name.as_ref().unwrap().as_ref(),
            "Réseau d'Entreprise™"
        );
    }

    #[test]
    fn utf8_in_comment() {
        let input = "# Café comment\n[repo]\nname=Test\nbaseurl=http://example.com/\n";
        let rf = RepoFile::parse(input).unwrap();
        assert!(rf.preamble[0].contains("Café"));
    }

    // --- Empty and edge values ---

    #[test]
    fn empty_value_for_optional_field() {
        let input = "[repo]\nname=Test\nbaseurl=http://example.com/\nmediaid=\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.mediaid.as_deref(), Some(""));
    }

    #[test]
    fn key_with_trailing_whitespace_before_equals() {
        let input = "[repo]\nname  =Test\nbaseurl=http://example.com/\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        assert_eq!(block.data.name.as_ref().unwrap().as_ref(), "Test");
    }

    // --- Section header edge cases ---

    #[test]
    fn duplicate_section_ids_last_wins() {
        let input = "\
[dupe]
name=First
baseurl=http://first.example.com/
priority=10

[dupe]
name=Second
baseurl=http://second.example.com/
priority=20
";
        let rf = RepoFile::parse(input).unwrap();
        // Second occurrence should overwrite first
        let block = rf.get(&RepoId::try_new("dupe").unwrap()).unwrap();
        assert_eq!(block.data.name.as_ref().unwrap().as_ref(), "Second");
        assert_eq!(block.data.priority.unwrap().to_string(), "20");
    }

    #[test]
    fn section_header_parsing_handles_trailing_chars() {
        // Section headers like "[repo]  " should parse correctly (trailing spaces)
        let input = "[repo]  \nname=Test\nbaseurl=http://example.com/\n";
        let rf = RepoFile::parse(input).unwrap();
        assert!(rf.contains(&RepoId::try_new("repo").unwrap()));
    }

    // --- Numeric boundary tests ---

    #[test]
    fn numeric_boundary_values() {
        // Priority at boundaries
        assert!(Priority::try_new(1).is_ok());
        assert!(Priority::try_new(99).is_ok());
        assert!(Priority::try_new(0).is_err());
        assert!(Priority::try_new(100).is_err());

        // MaxParallelDownloads at max
        assert!(MaxParallelDownloads::try_new(0).is_ok());
        assert!(MaxParallelDownloads::try_new(20).is_ok());
        assert!(MaxParallelDownloads::try_new(21).is_err());

        // Retries: 0 is unlimited
        assert!(Retries::try_new(0).is_ok());
        assert_eq!(*Retries::try_new(0).unwrap(), 0);

        // Cost: 0 is valid
        assert!(Cost::try_new(0).is_ok());
    }

    // --- Storage size parsing ---

    #[test]
    fn storage_size_unit_parsing() {
        // Note: G/M/K units are parsed with power-of-2 semantics (1024-based)
        let cases = vec![
            (
                "[r]\nname=T\nbaseurl=http://x.com/\nbandwidth=1G\n",
                1_073_741_824,
            ),
            (
                "[r]\nname=T\nbaseurl=http://x.com/\nbandwidth=500M\n",
                524_288_000,
            ),
            (
                "[r]\nname=T\nbaseurl=http://x.com/\nbandwidth=100K\n",
                102_400,
            ),
            ("[r]\nname=T\nbaseurl=http://x.com/\nbandwidth=1024\n", 1024),
        ];
        for (input, expected_bytes) in cases {
            let rf = RepoFile::parse(input).unwrap();
            let block = rf.get(&RepoId::try_new("r").unwrap()).unwrap();
            assert_eq!(
                block.data.bandwidth.unwrap().0,
                expected_bytes,
                "failed for input: {input}"
            );
        }
    }

    // --- Throttle parsing ---

    #[test]
    fn throttle_percentage_and_absolute_values() {
        // Note: throttle parsing as percentage depends on implementation details.
        // Test that valid absolute values work.
        let input = "[r]\nn=T\nb=http://x.com/\nthrottle=100K\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("r").unwrap()).unwrap();
        assert!(block.data.throttle.is_some());
    }

    // --- IP resolve variants ---

    #[test]
    fn ip_resolve_all_variants() {
        for (val, expected) in &[
            ("4", IpResolve::V4),
            ("IPv4", IpResolve::V4),
            ("ipv4", IpResolve::V4),
            ("6", IpResolve::V6),
            ("IPv6", IpResolve::V6),
            ("ipv6", IpResolve::V6),
        ] {
            let input = format!("[r]\nn=T\nb=http://x.com/\nip_resolve={val}\n");
            let rf = RepoFile::parse(&input).unwrap();
            let block = rf.get(&RepoId::try_new("r").unwrap()).unwrap();
            assert_eq!(block.data.ip_resolve, Some(*expected), "failed for {val}");
        }
    }

    // --- Proxy auth method all variants ---

    #[test]
    fn proxy_auth_method_all_variants() {
        let all_methods = vec![
            ("any", ProxyAuthMethod::Any),
            ("none", ProxyAuthMethod::None_),
            ("basic", ProxyAuthMethod::Basic),
            ("digest", ProxyAuthMethod::Digest),
            ("negotiate", ProxyAuthMethod::Negotiate),
            ("ntlm", ProxyAuthMethod::Ntlm),
            ("digest_ie", ProxyAuthMethod::DigestIe),
            ("ntlm_wb", ProxyAuthMethod::NtlmWb),
        ];
        for (val, expected) in all_methods {
            let input = format!("[r]\nn=T\nb=http://x.com/\nproxy_auth_method={val}\n");
            let rf = RepoFile::parse(&input).unwrap();
            let block = rf.get(&RepoId::try_new("r").unwrap()).unwrap();
            assert_eq!(
                block.data.proxy_auth_method,
                Some(expected),
                "failed for {val}"
            );
        }
    }

    // --- TsFlag parsing ---

    #[test]
    fn tsflags_parsing() {
        let input = "\
[main]
tsflags=nodocs
tsflags=test
";
        let rf = RepoFile::parse(input).unwrap();
        let main = rf.main().unwrap();
        assert_eq!(main.data.tsflags.len(), 2);
        assert!(main.data.tsflags.contains(&TsFlag::NoDocs));
        assert!(main.data.tsflags.contains(&TsFlag::Test));
    }

    // --- RpmVerbosity variants ---

    #[test]
    fn rpmverbosity_all_variants() {
        for verb in &["critical", "emergency", "error", "warn", "info", "debug"] {
            let input = format!("[main]\nrpmverbosity={verb}\n");
            let rf = RepoFile::parse(&input).unwrap();
            let main = rf.main().unwrap();
            assert!(main.data.rpmverbosity.is_some(), "failed for {verb}");
        }
    }

    // --- MultilibPolicy variants ---

    #[test]
    fn multilib_policy_variants() {
        for policy in &["best", "all"] {
            let input = format!("[main]\nmultilib_policy={policy}\n");
            let rf = RepoFile::parse(&input).unwrap();
            let main = rf.main().unwrap();
            assert!(main.data.multilib_policy.is_some(), "failed for {policy}");
        }
    }

    // --- Persistence variants ---

    #[test]
    fn persistence_variants() {
        for p in &["auto", "transient", "persist"] {
            let input = format!("[main]\npersistence={p}\n");
            let rf = RepoFile::parse(&input).unwrap();
            let main = rf.main().unwrap();
            assert!(main.data.persistence.is_some(), "failed for {p}");
        }
    }

    // --- All boolean field coverage ---

    #[test]
    fn all_repo_boolean_fields_parse_correctly() {
        let bool_fields = vec![
            "enabled",
            "gpgcheck",
            "repo_gpgcheck",
            "localpkg_gpgcheck",
            "skip_if_unavailable",
            "module_hotfixes",
            "deltarpm",
            "enablegroups",
            "fastestmirror",
            "countme",
            "sslverify",
            "sslverifystatus",
            "proxy_sslverify",
        ];
        let mut input = String::from("[repo]\nname=T\nbaseurl=http://x.com/\n");
        for field in &bool_fields {
            input.push_str(&format!("{field}=1\n"));
        }
        let rf = RepoFile::parse(&input).unwrap();
        let block = rf.get(&RepoId::try_new("repo").unwrap()).unwrap();
        for &field in &bool_fields {
            match field {
                "enabled" => assert_eq!(block.data.enabled, Some(DnfBool::True)),
                "gpgcheck" => assert_eq!(block.data.gpgcheck, Some(DnfBool::True)),
                "repo_gpgcheck" => assert_eq!(block.data.repo_gpgcheck, Some(DnfBool::True)),
                "localpkg_gpgcheck" => {
                    assert_eq!(block.data.localpkg_gpgcheck, Some(DnfBool::True))
                }
                "skip_if_unavailable" => {
                    assert_eq!(block.data.skip_if_unavailable, Some(DnfBool::True))
                }
                "module_hotfixes" => assert_eq!(block.data.module_hotfixes, Some(DnfBool::True)),
                "deltarpm" => assert_eq!(block.data.deltarpm, Some(DnfBool::True)),
                "enablegroups" => assert_eq!(block.data.enablegroups, Some(DnfBool::True)),
                "fastestmirror" => assert_eq!(block.data.fastestmirror, Some(DnfBool::True)),
                "countme" => assert_eq!(block.data.countme, Some(DnfBool::True)),
                "sslverify" => assert_eq!(block.data.sslverify, Some(DnfBool::True)),
                "sslverifystatus" => assert_eq!(block.data.sslverifystatus, Some(DnfBool::True)),
                "proxy_sslverify" => assert_eq!(block.data.proxy_sslverify, Some(DnfBool::True)),
                _ => panic!("unexpected field: {field}"),
            }
        }
    }

    // --- Concurrent access ---

    #[test]
    fn all_public_types_are_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<RepoFile>();
        assert_send_sync::<Repo>();
        assert_send_sync::<MainConfig>();
        assert_send_sync::<RepoBuilder>();
        assert_send_sync::<ValidationReport>();
        assert_send_sync::<RepoId>();
        assert_send_sync::<DnfBool>();
    }

    // --- Very long value ---

    #[test]
    fn very_long_baseurl_value() {
        let long_path = "a".repeat(500);
        let input = format!("[r]\nn=T\nbaseurl=http://example.com/{long_path}/\n");
        let rf = RepoFile::parse(&input).unwrap();
        let block = rf.get(&RepoId::try_new("r").unwrap()).unwrap();
        assert_eq!(block.data.baseurl.len(), 1);
    }
}

// ============================================================================
// Category C: Full API coverage
// ============================================================================

mod api_coverage {
    use super::*;

    // --- RepoFile accessors ---

    #[test]
    fn repofile_new_empty() {
        let rf = RepoFile::new();
        assert!(rf.is_empty());
        assert_eq!(rf.len(), 0);
        assert!(rf.main().is_none());
    }

    #[test]
    fn repofile_add_remove_contains_len() {
        let mut rf = RepoFile::new();
        assert!(rf.is_empty());

        let repo = RepoBuilder::new(RepoId::try_new("r1").unwrap())
            .name(RepoName::try_new("R1").unwrap())
            .baseurl("http://x.com/".parse().unwrap())
            .build();
        rf.add(repo).unwrap();
        assert!(!rf.is_empty());
        assert_eq!(rf.len(), 1);
        assert!(rf.contains(&RepoId::try_new("r1").unwrap()));
        assert!(!rf.contains(&RepoId::try_new("nonexistent").unwrap()));

        let removed = rf.remove(&RepoId::try_new("r1").unwrap());
        assert!(removed.is_some());
        assert!(rf.is_empty());
    }

    #[test]
    fn repofile_add_duplicate_id_errors() {
        let mut rf = RepoFile::new();
        let repo1 = RepoBuilder::new(RepoId::try_new("dup").unwrap())
            .name(RepoName::try_new("First").unwrap())
            .baseurl("http://a.com/".parse().unwrap())
            .build();
        rf.add(repo1).unwrap();

        let repo2 = RepoBuilder::new(RepoId::try_new("dup").unwrap())
            .name(RepoName::try_new("Second").unwrap())
            .baseurl("http://b.com/".parse().unwrap())
            .build();
        assert!(rf.add(repo2).is_err());
        assert_eq!(rf.len(), 1);
    }

    #[test]
    fn repofile_set_overwrites() {
        let mut rf = RepoFile::new();
        let repo = RepoBuilder::new(RepoId::try_new("r").unwrap())
            .name(RepoName::try_new("Original").unwrap())
            .baseurl("http://a.com/".parse().unwrap())
            .build();
        rf.set(repo); // set on empty → adds
        assert_eq!(rf.len(), 1);

        let updated = RepoBuilder::new(RepoId::try_new("r").unwrap())
            .name(RepoName::try_new("Updated").unwrap())
            .baseurl("http://b.com/".parse().unwrap())
            .build();
        rf.set(updated); // set on existing → overwrites
        assert_eq!(rf.len(), 1);
        let block = rf.get(&RepoId::try_new("r").unwrap()).unwrap();
        assert_eq!(block.data.name.as_ref().unwrap().as_ref(), "Updated");
    }

    #[test]
    fn repofile_main_get_set_remove() {
        let mut rf = RepoFile::new();
        assert!(rf.main().is_none());

        let mc = MainConfig::default();
        rf.set_main(mc);
        assert!(rf.main().is_some());

        rf.remove_main();
        assert!(rf.main().is_none());
    }

    #[test]
    fn repofile_main_mut_modification() {
        let mut rf = RepoFile::new();
        rf.set_main(MainConfig::default());
        {
            let main_block = rf.main_mut().unwrap();
            main_block.data.debuglevel = Some(DebugLevel::try_new(5).unwrap());
        }
        assert_eq!(rf.main().unwrap().data.debuglevel.unwrap().to_string(), "5");
    }

    #[test]
    fn repofile_iter_and_repo_ids() {
        let input = "\
[r1]\nn=R1\nb=http://1.com/\n
[r2]\nn=R2\nb=http://2.com/\n
[r3]\nn=R3\nb=http://3.com/\n
";
        let rf = RepoFile::parse(input).unwrap();
        let ids: Vec<String> = rf.repo_ids().map(|id| id.to_string()).collect();
        assert_eq!(ids, vec!["r1", "r2", "r3"]);

        let mut count = 0;
        for (_id, _block) in rf.iter() {
            count += 1;
        }
        assert_eq!(count, 3);
    }

    // --- RepoFile merge ---

    #[test]
    fn repofile_merge_combines_repos() {
        let mut a = RepoFile::new();
        a.add(
            RepoBuilder::new(RepoId::try_new("r1").unwrap())
                .name(RepoName::try_new("R1").unwrap())
                .baseurl("http://1.com/".parse().unwrap())
                .build(),
        )
        .unwrap();

        let mut b = RepoFile::new();
        b.add(
            RepoBuilder::new(RepoId::try_new("r2").unwrap())
                .name(RepoName::try_new("R2").unwrap())
                .baseurl("http://2.com/".parse().unwrap())
                .build(),
        )
        .unwrap();

        a.merge(b);
        assert_eq!(a.len(), 2);
        assert!(a.contains(&RepoId::try_new("r1").unwrap()));
        assert!(a.contains(&RepoId::try_new("r2").unwrap()));
    }

    // --- RepoFile render ---

    #[test]
    fn repofile_parse_then_render_produces_parseable_output() {
        let input = "[test]\nname=Test\nbaseurl=http://example.com/\nenabled=1\ngpgcheck=1\n";
        let rf = RepoFile::parse(input).unwrap();
        let rendered = rf.render();
        let parsed = RepoFile::parse(&rendered).unwrap();
        assert_eq!(parsed.len(), 1);
        let block = parsed.get(&RepoId::try_new("test").unwrap()).unwrap();
        assert_eq!(block.data.name.as_ref().unwrap().as_ref(), "Test");
        assert_eq!(block.data.baseurl[0].as_str(), "http://example.com/");
    }

    // --- Diff ---

    #[test]
    fn diff_empty_files_no_changes() {
        let a = RepoFile::new();
        let b = RepoFile::new();
        let d = diff_files(&a, &b);
        assert!(!d.has_changes);
    }

    #[test]
    #[allow(clippy::field_reassign_with_default)]
    fn diff_file_with_main_changes() {
        let mut a = RepoFile::new();
        a.set_main(MainConfig::default());

        let mut b = RepoFile::new();
        let mut mc = MainConfig::default();
        mc.debuglevel = Some(DebugLevel::try_new(5).unwrap());
        b.set_main(mc);

        // main section changed
        let d = diff_files(&a, &b);
        assert!(d.has_changes);
    }

    #[test]
    fn diff_identical_repos_no_changes() {
        let repo = RepoBuilder::new(RepoId::try_new("r").unwrap())
            .name(RepoName::try_new("R").unwrap())
            .baseurl("http://x.com/".parse().unwrap())
            .build();
        let d = diff_repos(&repo, &repo);
        assert!(!d.has_changes);
    }

    // --- Validation ---

    #[test]
    fn validation_report_combines_errors_and_warnings() {
        let mut report = ValidationReport::new();
        assert!(report.is_ok());
        assert!(!report.has_issues());

        report.errors.push(ValidationIssue {
            level: IssueLevel::Error,
            location: IssueLocation::Main,
            field: None,
            message: "test error".into(),
        });
        assert!(!report.is_ok());
        assert!(report.has_issues());
    }

    #[test]
    fn validate_repo_with_metalink_passes() {
        let mut repo = Repo::new(RepoId::try_new("r").unwrap());
        repo.metalink = Some("https://example.com/metalink".parse().unwrap());
        assert!(repo.validate().is_ok());
    }

    #[test]
    fn validate_mainconfig_always_passes() {
        let mc = MainConfig::default();
        let report = mc.validate();
        assert!(report.is_ok());
    }

    // --- Variables ---

    #[test]
    fn expand_empty_string() {
        let vars = HashMap::new();
        assert_eq!(expand_variables("", &vars).unwrap(), "");
    }

    #[test]
    fn expand_no_variables() {
        let vars = HashMap::new();
        assert_eq!(
            expand_variables("http://example.com/path", &vars).unwrap(),
            "http://example.com/path"
        );
    }

    #[test]
    fn expand_escaped_dollar_sign() {
        let vars = HashMap::new();
        assert_eq!(
            expand_variables(r"price is \$100", &vars).unwrap(),
            "price is $100"
        );
    }

    #[test]
    fn detect_variables_empty_input() {
        assert!(detect_variables("").is_empty());
        assert!(detect_variables("no vars here").is_empty());
    }

    // --- Builder ---

    #[test]
    fn builder_from_preserves_unmodified_fields() {
        let original = RepoBuilder::new(RepoId::try_new("r").unwrap())
            .name(RepoName::try_new("Original").unwrap())
            .baseurl("http://example.com/".parse().unwrap())
            .enabled(DnfBool::True)
            .gpgcheck(DnfBool::True)
            .priority(Priority::try_new(50).unwrap())
            .build();

        let modified = RepoBuilder::from(&original).enabled(DnfBool::False).build();

        // Unchanged fields preserved
        assert_eq!(modified.name.as_ref().unwrap().as_ref(), "Original");
        assert_eq!(modified.priority.unwrap().to_string(), "50");
        assert_eq!(modified.gpgcheck, Some(DnfBool::True));
        // Changed field
        assert_eq!(modified.enabled, Some(DnfBool::False));
    }

    // --- ReposDir ---

    #[test]
    fn reposdir_file_names() {
        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join("a.repo"), "[r]\nn=R\nb=http://x.com/\n").unwrap();
        std::fs::write(dir.path().join("b.repo"), "[r]\nn=R\nb=http://x.com/\n").unwrap();
        std::fs::write(dir.path().join("not-repo.txt"), "hello").unwrap();

        let rd = ReposDir::load(dir.path()).unwrap();
        let names = rd.file_names();
        assert_eq!(names.len(), 2);
        assert!(names.iter().all(|n| n.ends_with(".repo")));
    }

    #[test]
    fn reposdir_iter_repos() {
        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(
            dir.path().join("multi.repo"),
            "[r1]\nn=R1\nb=http://1.com/\n[r2]\nn=R2\nb=http://2.com/\n",
        )
        .unwrap();

        let rd = ReposDir::load(dir.path()).unwrap();
        let repos: Vec<(&str, &Repo)> = rd.iter_repos().collect();
        assert_eq!(repos.len(), 2);
    }

    #[test]
    fn reposdir_create_file() {
        let dir = tempfile::TempDir::new().unwrap();
        let mut rd = ReposDir::load(dir.path()).unwrap();
        assert_eq!(rd.file_names().len(), 0);

        let rf = rd.create_file("new.repo");
        rf.add(
            RepoBuilder::new(RepoId::try_new("new").unwrap())
                .name(RepoName::try_new("New").unwrap())
                .baseurl("http://new.example.com/".parse().unwrap())
                .build(),
        )
        .unwrap();

        assert_eq!(rd.file_names().len(), 1);
        assert!(rd
            .get_file("new.repo")
            .unwrap()
            .contains(&RepoId::try_new("new").unwrap()));
    }

    // --- Repo url_source() ---

    #[test]
    fn url_source_metalink() {
        let mut repo = Repo::new(RepoId::try_new("r").unwrap());
        repo.metalink = Some("https://example.com/metalink.xml".parse().unwrap());
        match repo.url_source() {
            Some(UrlSource::Metalink(url)) => {
                assert_eq!(url.as_str(), "https://example.com/metalink.xml")
            }
            other => panic!("expected Metalink, got {:?}", other),
        }
    }

    // --- SectionBlock raw_value ---

    #[test]
    fn section_block_captures_unknown_keys() {
        let input = "[r]\nname=Test\nbaseurl=http://x.com/\ncustom_opt=custom_val\n";
        let rf = RepoFile::parse(input).unwrap();
        let block = rf.get(&RepoId::try_new("r").unwrap()).unwrap();
        // Unknown keys are preserved in raw_entries
        let custom_entry = block.raw_entries.iter().find(|e| e.key == "custom_opt");
        assert!(custom_entry.is_some());
        assert_eq!(custom_entry.unwrap().value, "custom_val");
    }

    // --- Round-trip fidelity with all option types ---

    #[test]
    fn round_trip_all_option_types() {
        let input = "\
# Full-featured repo file
[main]
gpgcheck=1
debuglevel=5
max_parallel_downloads=10

[full-repo]
name=Full Featured Repository
baseurl=https://mirror.example.com/repo/$releasever/$basearch/
mirrorlist=https://mirrors.example.com/mirrorlist
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://example.com/RPM-GPG-KEY
priority=50
cost=500
module_hotfixes=0
metadata_expire=3600
timeout=60
retries=5
max_parallel_downloads=8
ip_resolve=4
sslverify=1
sslclientcert=/etc/pki/cert.pem
sslclientkey=/etc/pki/key.pem
proxy=http://proxy.example.com:8080
proxy_username=proxyuser
proxy_auth_method=basic
username=myuser
bandwidth=50M
minrate=1K
countme=1
";
        let rf = RepoFile::parse(input).unwrap();
        let output = rf.render();
        let rf2 = RepoFile::parse(&output).unwrap();

        let block = rf2.get(&RepoId::try_new("full-repo").unwrap()).unwrap();
        assert_eq!(
            block.data.name.as_ref().unwrap().as_ref(),
            "Full Featured Repository"
        );
        assert_eq!(block.data.baseurl.len(), 1);
        let mirrorlist_url = block.data.mirrorlist.as_ref().unwrap().as_str();
        assert!(
            mirrorlist_url.contains("mirrors.example.com/mirrorlist"),
            "mirrorlist URL = {mirrorlist_url}"
        );
        assert_eq!(block.data.enabled, Some(DnfBool::True));
        assert_eq!(block.data.priority.unwrap().to_string(), "50");
        assert_eq!(block.data.cost.unwrap().to_string(), "500");
        assert_eq!(block.data.timeout.unwrap().to_string(), "60");
        assert_eq!(block.data.retries.unwrap().to_string(), "5");
        assert_eq!(block.data.ip_resolve, Some(IpResolve::V4));
        assert_eq!(block.data.sslverify, Some(DnfBool::True));
        assert_eq!(block.data.countme, Some(DnfBool::True));
    }

    // --- MainConfig all main-only booleans round-trip ---

    #[test]
    fn mainconfig_all_boolean_fields_round_trip() {
        let input = "\
[main]
best=1
cacheonly=0
check_config_file_age=yes
clean_requirements_on_remove=true
debug_solver=on
diskspacecheck=1
exit_on_lock=0
gpgkey_dns_verification=no
ignorearch=false
install_weak_deps=1
keepcache=off
log_compress=1
module_obsoletes=0
module_stream_switch=no
obsoletes=true
plugins=1
protect_running_kernel=1
strict=on
upgrade_group_objects_upgrade=0
zchunk=1
";
        let rf = RepoFile::parse(input).unwrap();
        let main = rf.main().unwrap();
        assert_eq!(main.data.best, Some(DnfBool::True));
        assert_eq!(main.data.cacheonly, Some(DnfBool::False));
        assert_eq!(main.data.check_config_file_age, Some(DnfBool::True));
        assert_eq!(main.data.clean_requirements_on_remove, Some(DnfBool::True));
        assert_eq!(main.data.debug_solver, Some(DnfBool::True));
        assert_eq!(main.data.diskspacecheck, Some(DnfBool::True));
        assert_eq!(main.data.exit_on_lock, Some(DnfBool::False));
        assert_eq!(main.data.gpgkey_dns_verification, Some(DnfBool::False));
        assert_eq!(main.data.ignorearch, Some(DnfBool::False));
        assert_eq!(main.data.install_weak_deps, Some(DnfBool::True));
        assert_eq!(main.data.keepcache, Some(DnfBool::False));
        assert_eq!(main.data.log_compress, Some(DnfBool::True));
        assert_eq!(main.data.module_obsoletes, Some(DnfBool::False));
        assert_eq!(main.data.module_stream_switch, Some(DnfBool::False));
        assert_eq!(main.data.obsoletes, Some(DnfBool::True));
        assert_eq!(main.data.plugins, Some(DnfBool::True));
        assert_eq!(main.data.protect_running_kernel, Some(DnfBool::True));
        assert_eq!(main.data.strict, Some(DnfBool::True));
        assert_eq!(
            main.data.upgrade_group_objects_upgrade,
            Some(DnfBool::False)
        );
        assert_eq!(main.data.zchunk, Some(DnfBool::True));
    }

    // --- numeric fields round-trip ---

    #[test]
    fn mainconfig_numeric_fields_round_trip() {
        let input = "\
[main]
debuglevel=7
logfilelevel=5
log_rotate=2
log_size=10M
installonly_limit=3
errorlevel=5
metadata_timer_sync=7200
";
        let rf = RepoFile::parse(input).unwrap();
        let main = rf.main().unwrap();
        assert_eq!(main.data.debuglevel.unwrap().to_string(), "7");
        assert_eq!(main.data.logfilelevel.unwrap().to_string(), "5");
        assert_eq!(main.data.log_rotate.unwrap().to_string(), "2");
        assert_eq!(main.data.installonly_limit.unwrap().to_string(), "3");
        assert_eq!(main.data.errorlevel.unwrap().to_string(), "5");
        assert_eq!(main.data.metadata_timer_sync.unwrap().to_string(), "7200");
    }
}