confers 0.4.1

Production-ready Rust configuration library with zero boilerplate
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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
//! Configuration Groups / Modules support.
//!
//! This module provides support for composable configuration groups (modules),
//! allowing runtime selection of configuration module combinations.
//!
//! # Design
//!
//! The module system allows splitting configuration into interchangeable "config groups":
//! ```text
//! conf/
//! ├── database/
//! │   ├── mysql.toml
//! │   └── postgresql.toml
//! └── cache/
//!     ├── redis.toml
//!     └── in_memory.toml
//! ```
//!
//! # Example
//!
//! ```rust
//! use confers::modules::{ModuleConfig, ModuleRegistry};
//! use confers::loader::LoaderConfig;
//! use std::path::PathBuf;
//!
//! // Create a module registry
//! let mut registry = ModuleRegistry::default();
//!
//! // Register a group with multiple profiles
//! registry.register_group(
//!     "database",
//!     vec![
//!         ("mysql", PathBuf::from("conf/database/mysql.toml")),
//!         ("postgresql", PathBuf::from("conf/database/postgresql.toml")),
//!     ],
//!     Some("mysql"),
//! );
//!
//! // Load a module with a specific profile
//! let config = registry.load_module("database", "postgresql", &LoaderConfig::default());
//! ```

use crate::error::{ConfigError, ConfigResult};
use crate::impl_::loader::{load_file, LoaderConfig};
#[allow(unused_imports)]
use crate::types::{AnnotatedValue, ConfigValue};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

/// A configuration module containing profile paths and active profile.
///
/// Each module represents a group of interchangeable configurations
/// (e.g., database: mysql, postgresql; cache: redis, in_memory).
#[derive(Debug, Clone)]
pub struct ModuleConfig {
    /// The name of this module/group.
    name: Arc<str>,
    /// Available profile paths as (profile_name, path).
    paths: Vec<(Arc<str>, PathBuf)>,
    /// The currently active profile for this module.
    active_profile: Arc<str>,
}

impl ModuleConfig {
    /// Create a new module config.
    pub fn new(name: &str, paths: Vec<(&str, PathBuf)>, default: Option<&str>) -> Self {
        let name = Arc::from(name);
        let paths: Vec<(Arc<str>, PathBuf)> =
            paths.into_iter().map(|(n, p)| (Arc::from(n), p)).collect();

        let active_profile = default.map(Arc::from).unwrap_or_else(|| {
            paths
                .first()
                .map(|(n, _)| n.clone())
                .unwrap_or_else(|| Arc::from(""))
        });

        Self {
            name,
            paths,
            active_profile,
        }
    }

    /// Get the module name.
    pub fn name(&self) -> &str {
        &self.name
    }

    pub(crate) fn name_arc(&self) -> Arc<str> {
        self.name.clone()
    }

    /// Get the active profile name.
    pub fn active_profile(&self) -> &str {
        &self.active_profile
    }

    pub(crate) fn active_profile_arc(&self) -> Arc<str> {
        self.active_profile.clone()
    }

    /// Get list of available profiles.
    pub fn profiles(&self) -> Vec<Arc<str>> {
        self.paths.iter().map(|(n, _)| n.clone()).collect()
    }

    pub fn profile_count(&self) -> usize {
        self.paths.len()
    }

    /// Get a profile path by name.
    pub fn get_profile(&self, profile: &str) -> Option<&PathBuf> {
        self.paths
            .iter()
            .find(|(name, _)| name.as_ref() == profile)
            .map(|(_, path)| path)
    }

    /// Check if a profile exists.
    pub fn has_profile(&self, profile: &str) -> bool {
        self.paths.iter().any(|(name, _)| name.as_ref() == profile)
    }

    pub fn set_active_profile(&mut self, profile: &str) -> Result<(), ConfigError> {
        if !self.has_profile(profile) {
            return Err(ConfigError::ModuleNotFound {
                group: self.name.to_string(),
                module: profile.to_string(),
            });
        }
        self.active_profile = Arc::from(profile);
        Ok(())
    }
}

/// Registry for managing configuration groups (modules).
///
/// The ModuleRegistry maintains a collection of configuration groups,
/// each with multiple profile options. It allows loading specific
/// configurations based on the active profile.
///
/// # DI Patterns
///
/// - [`Default::default()`] - Zero-config default
/// - [`with_capacity()`][ModuleRegistry::with_capacity] - Pre-allocate capacity
#[derive(Debug, Default)]
pub struct ModuleRegistry {
    /// Registered configuration groups.
    groups: HashMap<Arc<str>, ModuleConfig>,
}

impl ModuleRegistry {
    /// Create a new module registry with pre-allocated capacity.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::modules::ModuleRegistry;
    ///
    /// let registry = ModuleRegistry::with_capacity(10);
    /// ```
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            groups: HashMap::with_capacity(capacity),
        }
    }

    /// Register a new configuration group with profiles.
    ///
    /// # Arguments
    ///
    /// * `name` - The group name (e.g., "database", "cache")
    /// * `profiles` - Iterator of (profile_name, path) pairs
    /// * `default` - Optional default profile name
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::modules::ModuleRegistry;
    /// use std::path::PathBuf;
    ///
    /// let mut registry = ModuleRegistry::default();
    /// registry.register_group(
    ///     "database",
    ///     vec![
    ///         ("mysql", PathBuf::from("conf/db/mysql.toml")),
    ///         ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
    ///     ],
    ///     Some("mysql"),
    /// );
    /// ```
    pub fn register_group(
        &mut self,
        name: &str,
        profiles: Vec<(&str, PathBuf)>,
        default: Option<&str>,
    ) -> &mut Self {
        let config = ModuleConfig::new(name, profiles, default);
        self.groups.insert(config.name_arc(), config);
        self
    }

    /// Load a module configuration by group and profile name.
    ///
    /// # Arguments
    ///
    /// * `name` - The group name
    /// * `profile` - The profile name to load
    /// * `config` - Loader configuration
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::ModuleNotFound`] if the group or profile doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::modules::ModuleRegistry;
    /// use confers::loader::LoaderConfig;
    /// use std::path::PathBuf;
    ///
    /// let mut registry = ModuleRegistry::default();
    /// registry.register_group(
    ///     "database",
    ///     vec![
    ///         ("mysql", PathBuf::from("conf/db/mysql.toml")),
    ///     ],
    ///     Some("mysql"),
    /// );
    ///
    /// let config = registry.load_module("database", "mysql", &LoaderConfig::default());
    /// ```
    pub fn load_module(
        &self,
        name: &str,
        profile: &str,
        config: &LoaderConfig,
    ) -> ConfigResult<AnnotatedValue> {
        let module = self
            .groups
            .get(name)
            .ok_or_else(|| ConfigError::ModuleNotFound {
                group: name.to_string(),
                module: profile.to_string(),
            })?;

        let path = module
            .get_profile(profile)
            .ok_or_else(|| ConfigError::ModuleNotFound {
                group: name.to_string(),
                module: profile.to_string(),
            })?;

        load_file(path, config)
    }

    /// Load the active module configuration for a group.
    ///
    /// Uses the currently active profile for the group.
    ///
    /// # Arguments
    ///
    /// * `name` - The group name
    /// * `config` - Loader configuration
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::ModuleNotFound`] if the group doesn't exist
    /// or the active profile's file cannot be loaded.
    pub fn load_active(&self, name: &str, config: &LoaderConfig) -> ConfigResult<AnnotatedValue> {
        let module = self
            .groups
            .get(name)
            .ok_or_else(|| ConfigError::ModuleNotFound {
                group: name.to_string(),
                module: "active".to_string(),
            })?;

        self.load_module(name, module.active_profile(), config)
    }

    /// List all registered group names.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::modules::ModuleRegistry;
    /// use std::path::PathBuf;
    ///
    /// let mut registry = ModuleRegistry::default();
    /// registry.register_group("database", vec![], None);
    /// registry.register_group("cache", vec![], None);
    ///
    /// let groups = registry.list_groups();
    /// assert_eq!(groups.len(), 2);
    /// ```
    pub fn list_groups(&self) -> Vec<Arc<str>> {
        self.groups.keys().cloned().collect()
    }

    /// Get the active profile name for a group.
    ///
    /// # Arguments
    ///
    /// * `name` - The group name
    ///
    /// # Returns
    ///
    /// Returns [`Some`] with the active profile name, or [`None`] if the group doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::modules::ModuleRegistry;
    /// use std::path::PathBuf;
    ///
    /// let mut registry = ModuleRegistry::default();
    /// registry.register_group("database", vec![], Some("mysql"));
    ///
    /// let profile = registry.get_active_profile("database");
    /// assert_eq!(profile.as_deref(), Some("mysql"));
    /// ```
    pub fn get_active_profile(&self, name: &str) -> Option<Arc<str>> {
        self.groups.get(name).map(|m| m.active_profile.clone())
    }

    /// Set the active profile for a group.
    ///
    /// # Arguments
    ///
    /// * `name` - The group name
    /// * `profile` - The profile name to set as active
    ///
    /// # Errors
    ///
    /// Returns [`ConfigError::ModuleNotFound`] if the group or profile doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::modules::ModuleRegistry;
    /// use std::path::PathBuf;
    ///
    /// let mut registry = ModuleRegistry::default();
    /// registry.register_group(
    ///     "database",
    ///     vec![
    ///         ("mysql", PathBuf::from("conf/db/mysql.toml")),
    ///         ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
    ///     ],
    ///     Some("mysql"),
    /// );
    ///
    /// registry.set_active_profile("database", "postgresql");
    /// assert_eq!(registry.get_active_profile("database").unwrap().as_ref(), "postgresql");
    /// ```
    pub fn set_active_profile(&mut self, name: &str, profile: &str) -> ConfigResult<()> {
        let module = self
            .groups
            .get_mut(name)
            .ok_or_else(|| ConfigError::ModuleNotFound {
                group: name.to_string(),
                module: profile.to_string(),
            })?;

        if !module.has_profile(profile) {
            return Err(ConfigError::ModuleNotFound {
                group: name.to_string(),
                module: profile.to_string(),
            });
        }

        module.set_active_profile(profile)?;
        Ok(())
    }

    /// Get a module config by name.
    ///
    /// # Arguments
    ///
    /// * `name` - The group name
    ///
    /// # Returns
    ///
    /// Returns [`Some`] with the module config, or [`None`] if the group doesn't exist.
    pub fn get(&self, name: &str) -> Option<&ModuleConfig> {
        self.groups.get(name)
    }

    /// Check if a group exists.
    ///
    /// # Arguments
    ///
    /// * `name` - The group name
    pub fn contains(&self, name: &str) -> bool {
        self.groups.contains_key(name)
    }

    /// Get the number of registered groups.
    pub fn len(&self) -> usize {
        self.groups.len()
    }

    /// Check if the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.groups.is_empty()
    }

    /// Resolve module profiles from environment variables.
    ///
    /// This method allows runtime configuration of which profile to use for each group
    /// via environment variables. The environment variable format is:
    /// `{prefix}{GROUP_NAME}_PROFILE` (uppercase).
    ///
    /// # Arguments
    ///
    /// * `prefix` - Optional prefix for environment variables (e.g., "APP_")
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::modules::ModuleRegistry;
    /// use std::path::PathBuf;
    ///
    /// // Given environment variable APP_DATABASE_PROFILE=postgresql
    /// std::env::set_var("APP_DATABASE_PROFILE", "postgresql");
    ///
    /// let mut registry = ModuleRegistry::default();
    /// registry.register_group(
    ///     "database",
    ///     vec![
    ///         ("mysql", PathBuf::from("conf/db/mysql.toml")),
    ///         ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
    ///     ],
    ///     Some("mysql"),
    /// );
    ///
    /// registry.resolve_from_env(Some("APP_"));
    /// // Active profile for "database" is now "postgresql"
    ///
    /// std::env::remove_var("APP_DATABASE_PROFILE");
    /// ```
    pub fn resolve_from_env(&mut self, prefix: Option<&str>) -> &mut Self {
        let prefix_str = prefix.unwrap_or("");

        for (name, module) in self.groups.iter_mut() {
            let env_key = format!("{}{}_PROFILE", prefix_str, name.to_uppercase());

            if let Ok(profile) = std::env::var(&env_key) {
                if module.has_profile(&profile) {
                    module.set_active_profile(&profile).ok();
                }
                // Silently ignore non-existent profile - not an error condition
            }
        }

        self
    }

    /// Resolve a single module's profile from environment variable.
    ///
    /// # Arguments
    ///
    /// * `group_name` - The group name to resolve
    /// * `prefix` - Optional prefix for environment variables
    ///
    /// # Returns
    ///
    /// Returns `Ok(true)` if the profile was changed, `Ok(false)` if no env var was set,
    /// or an error if the group doesn't exist.
    pub fn resolve_module_from_env(
        &mut self,
        group_name: &str,
        prefix: Option<&str>,
    ) -> ConfigResult<bool> {
        let module =
            self.groups
                .get_mut(group_name)
                .ok_or_else(|| ConfigError::ModuleNotFound {
                    group: group_name.to_string(),
                    module: "env".to_string(),
                })?;

        let prefix_str = prefix.unwrap_or("");
        let env_key = format!("{}{}_PROFILE", prefix_str, group_name.to_uppercase());

        if let Ok(profile) = std::env::var(&env_key) {
            if module.has_profile(&profile) {
                module.set_active_profile(&profile).ok();
                return Ok(true);
            }
            // Silently ignore non-existent profile - not an error condition
        }

        Ok(false)
    }

    /// Get all active profiles as a map.
    ///
    /// # Returns
    ///
    /// A HashMap mapping group names to their active profile names.
    pub fn active_profiles(&self) -> HashMap<Arc<str>, Arc<str>> {
        self.groups
            .iter()
            .map(|(name, module)| (name.clone(), module.active_profile_arc()))
            .collect()
    }

    /// Validate that all active profiles have valid file paths.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if all active profiles are valid, or an error with details.
    pub fn validate_active_profiles(&self) -> ConfigResult<()> {
        for (name, module) in &self.groups {
            let path = module.get_profile(module.active_profile()).ok_or_else(|| {
                ConfigError::ModuleNotFound {
                    group: name.to_string(),
                    module: module.active_profile().to_string(),
                }
            })?;

            if !path.exists() {
                return Err(ConfigError::FileNotFound {
                    filename: path.clone(),
                    source: None,
                });
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn create_test_config(temp_dir: &TempDir, filename: &str, content: &str) -> PathBuf {
        let path = temp_dir.path().join(filename);
        std::fs::write(&path, content).unwrap();
        path
    }

    #[test]
    fn test_module_registry_new() {
        let registry = ModuleRegistry::default();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_module_registry_with_capacity() {
        let registry = ModuleRegistry::with_capacity(10);
        assert!(registry.is_empty());
    }

    #[test]
    fn test_register_group() {
        let mut registry = ModuleRegistry::default();

        registry.register_group(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        assert!(!registry.is_empty());
        assert_eq!(registry.len(), 1);
        assert!(registry.contains("database"));
    }

    #[test]
    fn test_register_multiple_groups() {
        let mut registry = ModuleRegistry::default();

        registry.register_group("database", vec![], None);
        registry.register_group("cache", vec![], None);

        assert_eq!(registry.len(), 2);
    }

    #[test]
    fn test_list_groups() {
        let mut registry = ModuleRegistry::default();

        registry.register_group("database", vec![], None);
        registry.register_group("cache", vec![], None);
        registry.register_group("logging", vec![], None);

        let groups = registry.list_groups();
        assert_eq!(groups.len(), 3);
    }

    #[test]
    fn test_get_active_profile() {
        let mut registry = ModuleRegistry::default();

        registry.register_group(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("postgresql"),
        );

        let profile = registry.get_active_profile("database");
        assert_eq!(profile.unwrap().as_ref(), "postgresql");
    }

    #[test]
    fn test_get_active_profile_nonexistent() {
        let registry = ModuleRegistry::default();

        let profile = registry.get_active_profile("nonexistent");
        assert!(profile.is_none());
    }

    #[test]
    fn test_set_active_profile() {
        let mut registry = ModuleRegistry::default();

        registry.register_group(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        let result = registry.set_active_profile("database", "postgresql");
        assert!(result.is_ok());

        let profile = registry.get_active_profile("database").unwrap();
        assert_eq!(profile.as_ref(), "postgresql");
    }

    #[test]
    fn test_set_active_profile_nonexistent_group() {
        let mut registry = ModuleRegistry::default();

        let result = registry.set_active_profile("nonexistent", "profile");
        assert!(result.is_err());

        if let Err(ConfigError::ModuleNotFound { group, module }) = result {
            assert_eq!(group, "nonexistent");
            assert_eq!(module, "profile");
        } else {
            panic!("Expected ModuleNotFound error");
        }
    }

    #[test]
    fn test_set_active_profile_nonexistent_profile() {
        let mut registry = ModuleRegistry::default();

        registry.register_group(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );

        let result = registry.set_active_profile("database", "nonexistent");
        assert!(result.is_err());
    }

    #[test]
    fn test_module_config_get_profile() {
        let config = ModuleConfig::new(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        let path = config.get_profile("mysql");
        assert!(path.is_some());
        assert_eq!(path.unwrap(), &PathBuf::from("conf/db/mysql.toml"));

        let path = config.get_profile("postgresql");
        assert!(path.is_some());

        let path = config.get_profile("nonexistent");
        assert!(path.is_none());
    }

    #[test]
    fn test_module_config_has_profile() {
        let config = ModuleConfig::new(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );

        assert!(config.has_profile("mysql"));
        assert!(!config.has_profile("postgresql"));
    }

    #[test]
    fn test_module_config_profiles() {
        let config = ModuleConfig::new(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
                ("sqlite", PathBuf::from("conf/db/sqlite.toml")),
            ],
            None,
        );

        let profiles = config.profiles();
        assert_eq!(profiles.len(), 3);
    }

    #[test]
    fn test_module_config_default_profile() {
        // With default
        let config = ModuleConfig::new(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("postgresql"),
        );
        assert_eq!(config.active_profile(), "postgresql");

        // Without default - should use first
        let config = ModuleConfig::new(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            None,
        );
        assert_eq!(config.active_profile(), "mysql");
    }

    #[test]
    fn test_load_module_not_found_group() {
        let registry = ModuleRegistry::default();

        let result = registry.load_module("nonexistent", "profile", &LoaderConfig::default());

        assert!(result.is_err());
        if let Err(ConfigError::ModuleNotFound { group, module }) = result {
            assert_eq!(group, "nonexistent");
            assert_eq!(module, "profile");
        } else {
            panic!("Expected ModuleNotFound error");
        }
    }

    #[test]
    fn test_load_module_not_found_profile() {
        let mut registry = ModuleRegistry::default();

        registry.register_group(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );

        let result = registry.load_module("database", "nonexistent", &LoaderConfig::default());

        assert!(result.is_err());
    }

    #[test]
    fn test_load_module_file_not_found() {
        let temp_dir = TempDir::new().unwrap();

        let mut registry = ModuleRegistry::default();

        registry.register_group(
            "database",
            vec![("mysql", temp_dir.path().join("nonexistent.toml"))],
            Some("mysql"),
        );

        let result = registry.load_module("database", "mysql", &LoaderConfig::default());

        assert!(result.is_err());
    }

    #[test]
    fn test_load_module_success() {
        let temp_dir = TempDir::new().unwrap();

        let mysql_path = create_test_config(
            &temp_dir,
            "mysql.toml",
            "host = \"localhost\"\nport = 3306\n",
        );

        let mut registry = ModuleRegistry::default();

        registry.register_group("database", vec![("mysql", mysql_path)], Some("mysql"));

        // Disable symlink checking, allow absolute paths, and clear allowed dirs for temp directory paths
        let config = LoaderConfig::new()
            .no_symlink_check()
            .allow_absolute()
            .allowed_dirs(Vec::<PathBuf>::new());
        let result = registry.load_module("database", "mysql", &config);

        if let Err(ref e) = result {
            eprintln!("Error loading module: {:?}", e);
        }
        assert!(result.is_ok(), "Module load should succeed");
        let value = result.unwrap();
        // Check that the value contains the expected key
        if let ConfigValue::Map(m) = &value.inner {
            assert!(m.contains_key("host"));
        } else {
            // The value might be a different type, just check it exists
            assert!(!matches!(value.inner, ConfigValue::Null));
        }
    }

    #[test]
    fn test_load_active_not_found() {
        let registry = ModuleRegistry::default();

        let result = registry.load_active("nonexistent", &LoaderConfig::default());

        assert!(result.is_err());
    }

    #[test]
    fn test_get_module_config() {
        let mut registry = ModuleRegistry::default();

        registry.register_group(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );

        let config = registry.get("database");
        assert!(config.is_some());

        let config = config.unwrap();
        assert_eq!(config.name(), "database");
        assert_eq!(config.active_profile(), "mysql");

        let nonexistent = registry.get("nonexistent");
        assert!(nonexistent.is_none());
    }

    #[test]
    fn test_module_config_profile_count_zero() {
        let config = ModuleConfig::new("empty", vec![], None);
        assert_eq!(config.profile_count(), 0);
        assert!(config.profiles().is_empty());
    }

    #[test]
    fn test_module_config_profile_count_multiple() {
        let config = ModuleConfig::new(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
                ("sqlite", PathBuf::from("conf/db/sqlite.toml")),
            ],
            None,
        );
        assert_eq!(config.profile_count(), 3);
        assert_eq!(config.profiles().len(), 3);
    }

    #[test]
    fn test_module_config_new_empty_paths_no_default() {
        // When paths is empty and no default provided, active_profile is "".
        let config = ModuleConfig::new("orphan", vec![], None);
        assert_eq!(config.active_profile(), "");
        assert_eq!(config.profile_count(), 0);
        assert!(config.profiles().is_empty());
    }

    #[test]
    fn test_module_config_new_empty_paths_with_default() {
        // Default is honored even when paths is empty (active_profile = default).
        let config = ModuleConfig::new("ghost", vec![], Some("phantom"));
        assert_eq!(config.active_profile(), "phantom");
    }

    #[test]
    fn test_module_config_set_active_profile_success() {
        let mut config = ModuleConfig::new(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );
        assert_eq!(config.active_profile(), "mysql");
        config.set_active_profile("postgresql").expect("set_active");
        assert_eq!(config.active_profile(), "postgresql");
    }

    #[test]
    fn test_module_config_set_active_profile_nonexistent_errors() {
        let mut config = ModuleConfig::new(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );
        let err = config.set_active_profile("nonexistent").unwrap_err();
        match err {
            ConfigError::ModuleNotFound { group, module } => {
                assert_eq!(group, "database");
                assert_eq!(module, "nonexistent");
            }
            other => panic!("expected ModuleNotFound, got {:?}", other),
        }
        // Active profile must remain unchanged after the error
        assert_eq!(config.active_profile(), "mysql");
    }

    #[test]
    fn test_module_config_set_active_profile_idempotent() {
        let mut config = ModuleConfig::new(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );
        config
            .set_active_profile("mysql")
            .expect("set same profile");
        assert_eq!(config.active_profile(), "mysql");
    }

    #[test]
    fn test_register_group_returns_self_for_chaining() {
        let mut registry = ModuleRegistry::default();
        // register_group returns &mut Self, enabling builder-style chaining.
        registry
            .register_group("a", vec![], None)
            .register_group("b", vec![], None)
            .register_group("c", vec![], None);
        assert_eq!(registry.len(), 3);
        assert!(registry.contains("a"));
        assert!(registry.contains("b"));
        assert!(registry.contains("c"));
    }

    #[test]
    fn test_register_group_overwrites_existing() {
        let mut registry = ModuleRegistry::default();
        registry.register_group(
            "database",
            vec![("mysql", PathBuf::from("old/mysql.toml"))],
            Some("mysql"),
        );
        // Re-register the same group with different profiles.
        registry.register_group(
            "database",
            vec![("postgresql", PathBuf::from("new/postgresql.toml"))],
            Some("postgresql"),
        );
        assert_eq!(registry.len(), 1, "re-register should replace, not add");
        let config = registry.get("database").unwrap();
        assert_eq!(config.active_profile(), "postgresql");
        assert!(!config.has_profile("mysql"));
        assert!(config.has_profile("postgresql"));
    }

    #[test]
    fn test_contains_group() {
        let mut registry = ModuleRegistry::default();
        registry.register_group("present", vec![], None);
        assert!(registry.contains("present"));
        assert!(!registry.contains("absent"));
    }

    #[test]
    fn test_load_active_success() {
        let temp_dir = TempDir::new().unwrap();
        let mysql_path = create_test_config(&temp_dir, "mysql.toml", "host = \"localhost\"\n");
        let postgresql_path =
            create_test_config(&temp_dir, "postgresql.toml", "host = \"pg-host\"\n");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            "database",
            vec![("mysql", mysql_path), ("postgresql", postgresql_path)],
            Some("mysql"),
        );

        let config = LoaderConfig::new()
            .no_symlink_check()
            .allow_absolute()
            .allowed_dirs(Vec::<PathBuf>::new());
        // load_active uses the currently-active profile ("mysql").
        let result = registry.load_active("database", &config);
        assert!(result.is_ok(), "load_active should succeed: {:?}", result);

        // Now switch active profile and load again.
        registry
            .set_active_profile("database", "postgresql")
            .unwrap();
        let result = registry.load_active("database", &config);
        assert!(result.is_ok(), "load_active with new profile: {:?}", result);
    }

    #[test]
    fn test_active_profiles_empty_registry() {
        let registry = ModuleRegistry::default();
        let map = registry.active_profiles();
        assert!(map.is_empty());
    }

    #[test]
    fn test_active_profiles_returns_all_groups() {
        let mut registry = ModuleRegistry::default();
        registry.register_group(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );
        registry.register_group(
            "cache",
            vec![("redis", PathBuf::from("conf/cache/redis.toml"))],
            Some("redis"),
        );
        let map = registry.active_profiles();
        assert_eq!(map.len(), 2);
        assert_eq!(map.get("database").map(|s| s.as_ref()), Some("mysql"));
        assert_eq!(map.get("cache").map(|s| s.as_ref()), Some("redis"));
    }

    #[test]
    fn test_active_profiles_reflects_set_active_profile() {
        let mut registry = ModuleRegistry::default();
        registry.register_group(
            "database",
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );
        let before = registry.active_profiles();
        assert_eq!(before.get("database").map(|s| s.as_ref()), Some("mysql"));

        registry
            .set_active_profile("database", "postgresql")
            .unwrap();
        let after = registry.active_profiles();
        assert_eq!(
            after.get("database").map(|s| s.as_ref()),
            Some("postgresql")
        );
    }

    #[test]
    fn test_validate_active_profiles_empty_registry_ok() {
        let registry = ModuleRegistry::default();
        registry
            .validate_active_profiles()
            .expect("empty registry has no profiles to validate");
    }

    #[test]
    fn test_validate_active_profiles_all_exist_ok() {
        let temp_dir = TempDir::new().unwrap();
        let mysql_path = create_test_config(&temp_dir, "mysql.toml", "host = \"localhost\"\n");

        let mut registry = ModuleRegistry::default();
        registry.register_group("database", vec![("mysql", mysql_path)], Some("mysql"));
        registry
            .validate_active_profiles()
            .expect("all active profile files exist");
    }

    #[test]
    fn test_validate_active_profiles_missing_file_errors() {
        let mut registry = ModuleRegistry::default();
        registry.register_group(
            "database",
            vec![("mysql", PathBuf::from("/nonexistent/mysql.toml"))],
            Some("mysql"),
        );
        let err = registry.validate_active_profiles().unwrap_err();
        match err {
            ConfigError::FileNotFound { filename, .. } => {
                assert!(filename.to_string_lossy().contains("mysql.toml"));
            }
            other => panic!("expected FileNotFound, got {:?}", other),
        }
    }

    #[test]
    fn test_validate_active_profiles_checks_active_profile_not_listed() {
        // Edge case: active_profile points to a profile that has no path entry.
        // This can only happen if the module was constructed with an empty paths
        // list but a non-empty default.
        let mut registry = ModuleRegistry::default();
        // Register a group with no profiles but a default active profile.
        registry.register_group("ghost", vec![], Some("phantom"));
        let err = registry.validate_active_profiles().unwrap_err();
        match err {
            ConfigError::ModuleNotFound { group, module } => {
                assert_eq!(group, "ghost");
                assert_eq!(module, "phantom");
            }
            other => panic!("expected ModuleNotFound, got {:?}", other),
        }
    }

    #[test]
    fn test_list_groups_empty_registry() {
        let registry = ModuleRegistry::default();
        assert!(registry.list_groups().is_empty());
    }

    #[test]
    fn test_list_groups_returns_all_registered() {
        let mut registry = ModuleRegistry::default();
        registry.register_group("alpha", vec![], None);
        registry.register_group("beta", vec![], None);
        registry.register_group("gamma", vec![], None);
        let groups = registry.list_groups();
        assert_eq!(groups.len(), 3);
        // Each group name should be present (order is not guaranteed by HashMap).
        let names: Vec<String> = groups.iter().map(|s| s.to_string()).collect();
        assert!(names.contains(&"alpha".to_string()));
        assert!(names.contains(&"beta".to_string()));
        assert!(names.contains(&"gamma".to_string()));
    }

    #[test]
    fn test_get_returns_none_for_missing_group() {
        let registry = ModuleRegistry::default();
        assert!(registry.get("does-not-exist").is_none());
    }

    #[test]
    fn test_len_and_is_empty_consistency() {
        let mut registry = ModuleRegistry::default();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
        registry.register_group("one", vec![], None);
        assert!(!registry.is_empty());
        assert_eq!(registry.len(), 1);
        registry.register_group("two", vec![], None);
        assert_eq!(registry.len(), 2);
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_module_from_env_changes_profile() {
        // Use a unique group name to avoid collision with other env-var tests.
        let unique_group = "envresolve_single_ok";
        let env_key = format!("{}_PROFILE", unique_group.to_uppercase());
        std::env::set_var(&env_key, "postgresql");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        let changed = registry
            .resolve_module_from_env(unique_group, None)
            .expect("resolve_module_from_env");
        assert!(changed, "profile should have changed due to env var");
        assert_eq!(
            registry.get_active_profile(unique_group).unwrap().as_ref(),
            "postgresql"
        );

        std::env::remove_var(&env_key);
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_module_from_env_no_env_var_returns_false() {
        let unique_group = "envresolve_single_novar";
        let env_key = format!("{}_PROFILE", unique_group.to_uppercase());
        std::env::remove_var(&env_key);

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        let changed = registry
            .resolve_module_from_env(unique_group, None)
            .expect("resolve_module_from_env");
        assert!(!changed, "no env var set → must return false");
        assert_eq!(
            registry.get_active_profile(unique_group).unwrap().as_ref(),
            "mysql"
        );
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_module_from_env_nonexistent_group_errors() {
        let err = registry_helper_nonexistent_group();
        match err {
            ConfigError::ModuleNotFound { group, module } => {
                assert_eq!(group, "definitely_not_registered");
                assert_eq!(module, "env");
            }
            other => panic!("expected ModuleNotFound, got {:?}", other),
        }
    }

    fn registry_helper_nonexistent_group() -> ConfigError {
        let mut registry = ModuleRegistry::default();
        registry
            .resolve_module_from_env("definitely_not_registered", None)
            .unwrap_err()
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_module_from_env_invalid_profile_ignored() {
        // If the env var points to a non-existent profile, the method must NOT
        // error — it silently ignores the invalid profile and returns false.
        let unique_group = "envresolve_single_bad_profile";
        let env_key = format!("{}_PROFILE", unique_group.to_uppercase());
        std::env::set_var(&env_key, "nonexistent-profile");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );

        let changed = registry
            .resolve_module_from_env(unique_group, None)
            .expect("must not error for invalid profile");
        assert!(
            !changed,
            "invalid profile should be ignored (changed=false)"
        );
        assert_eq!(
            registry.get_active_profile(unique_group).unwrap().as_ref(),
            "mysql"
        );

        std::env::remove_var(&env_key);
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_module_from_env_with_prefix() {
        let unique_group = "envresolve_prefix";
        let env_key = format!("PREFIX_{}_PROFILE", unique_group.to_uppercase());
        std::env::set_var(&env_key, "postgresql");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        let changed = registry
            .resolve_module_from_env(unique_group, Some("PREFIX_"))
            .expect("resolve_module_from_env with prefix");
        assert!(changed);
        assert_eq!(
            registry.get_active_profile(unique_group).unwrap().as_ref(),
            "postgresql"
        );

        std::env::remove_var(&env_key);
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_from_env_updates_all_matching_groups() {
        let unique_a = "envresolve_batch_a";
        let unique_b = "envresolve_batch_b";
        let env_a = format!("{}_PROFILE", unique_a.to_uppercase());
        let env_b = format!("{}_PROFILE", unique_b.to_uppercase());
        std::env::set_var(&env_a, "postgresql");
        std::env::set_var(&env_b, "redis");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_a,
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );
        registry.register_group(
            unique_b,
            vec![
                ("memory", PathBuf::from("conf/cache/memory.toml")),
                ("redis", PathBuf::from("conf/cache/redis.toml")),
            ],
            Some("memory"),
        );

        registry.resolve_from_env(None);
        assert_eq!(
            registry.get_active_profile(unique_a).unwrap().as_ref(),
            "postgresql"
        );
        assert_eq!(
            registry.get_active_profile(unique_b).unwrap().as_ref(),
            "redis"
        );

        std::env::remove_var(&env_a);
        std::env::remove_var(&env_b);
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_from_env_ignores_nonexistent_profile() {
        let unique_group = "envresolve_batch_ignore";
        let env_key = format!("{}_PROFILE", unique_group.to_uppercase());
        std::env::set_var(&env_key, "does-not-exist");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );

        // Must not panic and must not change the active profile.
        registry.resolve_from_env(None);
        assert_eq!(
            registry.get_active_profile(unique_group).unwrap().as_ref(),
            "mysql"
        );

        std::env::remove_var(&env_key);
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_from_env_no_env_vars_is_noop() {
        let unique_group = "envresolve_batch_noop";
        let env_key = format!("{}_PROFILE", unique_group.to_uppercase());
        std::env::remove_var(&env_key);

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        registry.resolve_from_env(None);
        assert_eq!(
            registry.get_active_profile(unique_group).unwrap().as_ref(),
            "mysql"
        );
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_from_env_with_prefix() {
        let unique_group = "envresolve_batch_prefix";
        let env_key = format!("APP_{}_PROFILE", unique_group.to_uppercase());
        std::env::set_var(&env_key, "postgresql");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        registry.resolve_from_env(Some("APP_"));
        assert_eq!(
            registry.get_active_profile(unique_group).unwrap().as_ref(),
            "postgresql"
        );

        std::env::remove_var(&env_key);
    }

    #[test]
    #[serial_test::serial]
    fn test_resolve_from_env_returns_self_for_chaining() {
        let unique_group = "envresolve_chain";
        let env_key = format!("{}_PROFILE", unique_group.to_uppercase());
        std::env::set_var(&env_key, "postgresql");

        let mut registry = ModuleRegistry::default();
        registry.register_group(
            unique_group,
            vec![
                ("mysql", PathBuf::from("conf/db/mysql.toml")),
                ("postgresql", PathBuf::from("conf/db/postgresql.toml")),
            ],
            Some("mysql"),
        );

        // resolve_from_env returns &mut Self, allowing chaining.
        registry
            .resolve_from_env(None)
            .register_group("added-after-resolve", vec![], None);
        assert!(registry.contains("added-after-resolve"));

        std::env::remove_var(&env_key);
    }

    #[test]
    fn test_module_config_name() {
        let config = ModuleConfig::new("my-group", vec![], None);
        assert_eq!(config.name(), "my-group");
    }

    #[test]
    fn test_module_config_get_profile_returns_path() {
        let path = PathBuf::from("conf/db/mysql.toml");
        let config = ModuleConfig::new("database", vec![("mysql", path.clone())], Some("mysql"));
        let got = config.get_profile("mysql").expect("profile must exist");
        assert_eq!(got, &path);
    }

    #[test]
    fn test_module_config_get_profile_missing_returns_none() {
        let config = ModuleConfig::new(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );
        assert!(config.get_profile("oracle").is_none());
    }

    #[test]
    fn test_module_config_active_profile_after_construction() {
        // With default
        let with_default = ModuleConfig::new(
            "db",
            vec![("mysql", PathBuf::from("m.toml"))],
            Some("mysql"),
        );
        assert_eq!(with_default.active_profile(), "mysql");

        // Without default, uses first profile
        let first_used = ModuleConfig::new(
            "db",
            vec![
                ("mysql", PathBuf::from("m.toml")),
                ("postgresql", PathBuf::from("p.toml")),
            ],
            None,
        );
        assert_eq!(first_used.active_profile(), "mysql");
    }

    #[test]
    fn test_module_registry_with_capacity_zero() {
        let registry = ModuleRegistry::with_capacity(0);
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_load_module_with_wrong_profile_name_errors() {
        let mut registry = ModuleRegistry::default();
        registry.register_group(
            "database",
            vec![("mysql", PathBuf::from("conf/db/mysql.toml"))],
            Some("mysql"),
        );
        let err = registry
            .load_module("database", "oracle", &LoaderConfig::default())
            .unwrap_err();
        match err {
            ConfigError::ModuleNotFound { group, module } => {
                assert_eq!(group, "database");
                assert_eq!(module, "oracle");
            }
            other => panic!("expected ModuleNotFound, got {:?}", other),
        }
    }
}