voirs-sdk 0.1.0-rc.1

Unified SDK and public API for VoiRS speech synthesis
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
//! Configuration persistence and file operations.
//!
//! This module handles loading and saving configurations from various sources:
//!
//! - File-based configurations (JSON, TOML, YAML)
//! - Environment variable overrides
//! - Configuration search and discovery
//! - Hot-reloading and file watching
//! - Configuration validation and error handling

use super::hierarchy::{AppConfig, ConfigHierarchy, ConfigValidationError};
use std::{
    env, fs,
    path::{Path, PathBuf},
};

/// Configuration file format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigFormat {
    /// JSON format
    Json,
    /// TOML format
    Toml,
    /// YAML format
    Yaml,
}

impl ConfigFormat {
    /// Detect format from file extension
    pub fn from_extension(path: &Path) -> Option<Self> {
        match path.extension()?.to_str()? {
            "json" => Some(ConfigFormat::Json),
            "toml" => Some(ConfigFormat::Toml),
            "yaml" | "yml" => Some(ConfigFormat::Yaml),
            _ => None,
        }
    }

    /// Get file extension for this format
    pub fn extension(&self) -> &'static str {
        match self {
            ConfigFormat::Json => "json",
            ConfigFormat::Toml => "toml",
            ConfigFormat::Yaml => "yaml",
        }
    }

    /// Get MIME type for this format
    pub fn mime_type(&self) -> &'static str {
        match self {
            ConfigFormat::Json => "application/json",
            ConfigFormat::Toml => "application/toml",
            ConfigFormat::Yaml => "application/yaml",
        }
    }
}

/// Configuration loader for various sources
pub struct ConfigLoader {
    /// Search paths for configuration files
    search_paths: Vec<PathBuf>,

    /// Environment variable prefix
    env_prefix: String,

    /// Default configuration name
    config_name: String,

    /// Supported formats (in order of preference)
    supported_formats: Vec<ConfigFormat>,
}

impl ConfigLoader {
    /// Create new configuration loader
    pub fn new() -> Self {
        Self {
            search_paths: Self::default_search_paths(),
            env_prefix: "VOIRS".to_string(),
            config_name: "voirs".to_string(),
            supported_formats: vec![ConfigFormat::Toml, ConfigFormat::Json, ConfigFormat::Yaml],
        }
    }

    /// Set search paths for configuration files
    pub fn with_search_paths(mut self, paths: Vec<PathBuf>) -> Self {
        self.search_paths = paths;
        self
    }

    /// Add search path
    pub fn add_search_path(mut self, path: PathBuf) -> Self {
        self.search_paths.push(path);
        self
    }

    /// Set environment variable prefix
    pub fn with_env_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.env_prefix = prefix.into();
        self
    }

    /// Set configuration name
    pub fn with_config_name(mut self, name: impl Into<String>) -> Self {
        self.config_name = name.into();
        self
    }

    /// Load configuration from discovered files and environment
    pub fn load(&self) -> Result<AppConfig, ConfigLoadError> {
        let mut config = AppConfig::default();

        // Load from configuration files
        for config_file in self.discover_config_files()? {
            let file_config = self.load_from_file(&config_file)?;
            config.merge_with(&file_config);
        }

        // Apply environment variable overrides
        self.apply_env_overrides(&mut config)?;

        // Validate final configuration
        config.validate().map_err(ConfigLoadError::Validation)?;

        Ok(config)
    }

    /// Load configuration from specific file
    pub fn load_from_file(&self, path: &Path) -> Result<AppConfig, ConfigLoadError> {
        let content = fs::read_to_string(path).map_err(|e| ConfigLoadError::Io {
            path: path.to_path_buf(),
            error: e,
        })?;

        let format = ConfigFormat::from_extension(path).ok_or_else(|| {
            ConfigLoadError::UnsupportedFormat {
                path: path.to_path_buf(),
            }
        })?;

        self.parse_config(&content, format)
            .map_err(|e| ConfigLoadError::Parse {
                path: path.to_path_buf(),
                format,
                error: e,
            })
    }

    /// Save configuration to file
    pub fn save_to_file(&self, config: &AppConfig, path: &Path) -> Result<(), ConfigSaveError> {
        let format = ConfigFormat::from_extension(path).ok_or_else(|| {
            ConfigSaveError::UnsupportedFormat {
                path: path.to_path_buf(),
            }
        })?;

        let content =
            self.serialize_config(config, format)
                .map_err(|e| ConfigSaveError::Serialize {
                    path: path.to_path_buf(),
                    format,
                    error: e,
                })?;

        // Create parent directories if they don't exist
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| ConfigSaveError::Io {
                path: path.to_path_buf(),
                error: e,
            })?;
        }

        fs::write(path, content).map_err(|e| ConfigSaveError::Io {
            path: path.to_path_buf(),
            error: e,
        })
    }

    /// Discover configuration files in search paths
    pub fn discover_config_files(&self) -> Result<Vec<PathBuf>, ConfigLoadError> {
        let mut found_files = Vec::new();

        for search_path in &self.search_paths {
            if !search_path.exists() {
                continue;
            }

            for format in &self.supported_formats {
                let config_file =
                    search_path.join(format!("{}.{}", self.config_name, format.extension()));
                if config_file.exists() {
                    found_files.push(config_file);
                }
            }
        }

        Ok(found_files)
    }

    /// Get default search paths
    fn default_search_paths() -> Vec<PathBuf> {
        let mut paths = Vec::new();

        // Current directory
        if let Ok(current_dir) = env::current_dir() {
            paths.push(current_dir);
        }

        paths
    }

    /// Parse configuration content
    fn parse_config(&self, content: &str, format: ConfigFormat) -> Result<AppConfig, String> {
        match format {
            ConfigFormat::Json => {
                serde_json::from_str(content).map_err(|e| format!("JSON parse error: {e}"))
            }
            ConfigFormat::Toml => {
                toml::from_str(content).map_err(|e| format!("TOML parse error: {e}"))
            }
            ConfigFormat::Yaml => {
                serde_yaml::from_str(content).map_err(|e| format!("YAML parse error: {e}"))
            }
        }
    }

    /// Serialize configuration to string
    fn serialize_config(&self, config: &AppConfig, format: ConfigFormat) -> Result<String, String> {
        match format {
            ConfigFormat::Json => serde_json::to_string_pretty(config)
                .map_err(|e| format!("JSON serialize error: {e}")),
            ConfigFormat::Toml => {
                toml::to_string_pretty(config).map_err(|e| format!("TOML serialize error: {e}"))
            }
            ConfigFormat::Yaml => {
                serde_yaml::to_string(config).map_err(|e| format!("YAML serialize error: {e}"))
            }
        }
    }

    /// Apply environment variable overrides
    fn apply_env_overrides(&self, config: &mut AppConfig) -> Result<(), ConfigLoadError> {
        // Apply environment variable overrides
        if let Ok(device) = env::var(format!("{}_DEVICE", self.env_prefix)) {
            config.pipeline.device = device;
        }

        if let Ok(gpu) = env::var(format!("{}_USE_GPU", self.env_prefix)) {
            config.pipeline.use_gpu = gpu.parse().unwrap_or(false);
        }

        if let Ok(threads) = env::var(format!("{}_THREADS", self.env_prefix)) {
            if let Ok(thread_count) = threads.parse::<usize>() {
                config.pipeline.num_threads = Some(thread_count);
            }
        }

        Ok(())
    }
}

impl Default for ConfigLoader {
    fn default() -> Self {
        Self::new()
    }
}

/// Configuration loading error
#[derive(Debug, thiserror::Error)]
pub enum ConfigLoadError {
    /// I/O error reading configuration file
    #[error("I/O error reading config file {path}: {error}")]
    Io {
        path: PathBuf,
        error: std::io::Error,
    },

    /// Configuration parsing error
    #[error("Error parsing config file {path} as {format:?}: {error}")]
    Parse {
        path: PathBuf,
        format: ConfigFormat,
        error: String,
    },

    /// Unsupported file format
    #[error("Unsupported config file format for {path}")]
    UnsupportedFormat { path: PathBuf },

    /// Configuration validation error
    #[error("Configuration validation error: {0}")]
    Validation(#[from] ConfigValidationError),

    /// Environment variable error
    #[error("Environment variable error for {var}: {error}")]
    EnvVar { var: String, error: String },
}

/// Configuration saving error
#[derive(Debug, thiserror::Error)]
pub enum ConfigSaveError {
    /// I/O error writing configuration file
    #[error("I/O error writing config file {path}: {error}")]
    Io {
        path: PathBuf,
        error: std::io::Error,
    },

    /// Configuration serialization error
    #[error("Error serializing config to {path} as {format:?}: {error}")]
    Serialize {
        path: PathBuf,
        format: ConfigFormat,
        error: String,
    },

    /// Unsupported file format
    #[error("Unsupported config file format for {path}")]
    UnsupportedFormat { path: PathBuf },
}

/// Configuration file watcher for automatic reloading
pub struct ConfigWatcher {
    /// Path to the configuration file being watched
    path: PathBuf,
    /// Configuration loader
    loader: ConfigLoader,
    /// Last known modification time
    last_modified: Option<std::time::SystemTime>,
    /// File system watcher (optional for automatic watching)
    _watcher: Option<notify::RecommendedWatcher>,
    /// Channel for receiving file system events
    event_receiver: Option<std::sync::mpsc::Receiver<notify::Result<notify::Event>>>,
    /// Current configuration cache
    cached_config: Option<AppConfig>,
}

impl ConfigWatcher {
    /// Create a new configuration file watcher
    pub fn new(path: PathBuf, loader: ConfigLoader) -> Result<Self, ConfigWatchError> {
        let last_modified = Self::get_file_modification_time(&path)?;

        Ok(Self {
            path,
            loader,
            last_modified: Some(last_modified),
            _watcher: None,
            event_receiver: None,
            cached_config: None,
        })
    }

    /// Create a new configuration file watcher with automatic file system watching
    pub fn with_auto_watch(path: PathBuf, loader: ConfigLoader) -> Result<Self, ConfigWatchError> {
        use notify::{RecursiveMode, Watcher};

        let last_modified = Self::get_file_modification_time(&path)?;

        // Create a channel for file system events
        let (tx, rx) = std::sync::mpsc::channel();

        // Create the file system watcher
        let mut watcher =
            notify::recommended_watcher(tx).map_err(|e| ConfigWatchError::WatcherCreation {
                path: path.clone(),
                error: e.to_string(),
            })?;

        // Watch the configuration file's parent directory
        if let Some(parent_dir) = path.parent() {
            watcher
                .watch(parent_dir, RecursiveMode::NonRecursive)
                .map_err(|e| ConfigWatchError::WatchStart {
                    path: path.clone(),
                    error: e.to_string(),
                })?;
        }

        Ok(Self {
            path,
            loader,
            last_modified: Some(last_modified),
            _watcher: Some(watcher),
            event_receiver: Some(rx),
            cached_config: None,
        })
    }

    /// Check if the configuration file has been modified and reload if necessary
    pub fn reload_if_changed(&mut self) -> Result<Option<AppConfig>, ConfigLoadError> {
        // Check if file modification time has changed
        let current_modified =
            Self::get_file_modification_time(&self.path).map_err(|e| ConfigLoadError::Io {
                path: self.path.clone(),
                error: std::io::Error::other(e.to_string()),
            })?;

        // Compare with last known modification time
        if self.last_modified.is_none() || Some(current_modified) > self.last_modified {
            tracing::info!(
                "Configuration file modified, reloading: {}",
                self.path.display()
            );

            // Reload the configuration
            let new_config = self.loader.load_from_file(&self.path)?;

            // Update modification time and cache
            self.last_modified = Some(current_modified);
            self.cached_config = Some(new_config.clone());

            tracing::info!(
                "Configuration successfully reloaded from: {}",
                self.path.display()
            );
            Ok(Some(new_config))
        } else {
            // No changes detected
            Ok(None)
        }
    }

    /// Force reload the configuration file
    pub fn force_reload(&mut self) -> Result<AppConfig, ConfigLoadError> {
        tracing::info!(
            "Force reloading configuration from: {}",
            self.path.display()
        );

        let new_config = self.loader.load_from_file(&self.path)?;

        // Update modification time and cache
        if let Ok(modified) = Self::get_file_modification_time(&self.path) {
            self.last_modified = Some(modified);
        }
        self.cached_config = Some(new_config.clone());

        tracing::info!(
            "Configuration successfully force reloaded from: {}",
            self.path.display()
        );
        Ok(new_config)
    }

    /// Get the currently cached configuration
    pub fn get_cached_config(&self) -> Option<&AppConfig> {
        self.cached_config.as_ref()
    }

    /// Check for file system events (only works with auto-watch mode)
    pub fn check_events(&mut self) -> Result<Vec<ConfigChangeEvent>, ConfigWatchError> {
        let Some(ref receiver) = self.event_receiver else {
            return Ok(Vec::new());
        };

        let mut events = Vec::new();

        // Process all pending events
        while let Ok(event_result) = receiver.try_recv() {
            match event_result {
                Ok(event) => {
                    // Check if this event affects our watched file
                    if event.paths.iter().any(|p| p == &self.path) {
                        let change_type = match event.kind {
                            notify::EventKind::Modify(_) => ConfigChangeType::Modified,
                            notify::EventKind::Create(_) => ConfigChangeType::Created,
                            notify::EventKind::Remove(_) => ConfigChangeType::Deleted,
                            _ => ConfigChangeType::Other,
                        };

                        events.push(ConfigChangeEvent {
                            path: self.path.clone(),
                            change_type,
                            timestamp: std::time::SystemTime::now(),
                        });
                    }
                }
                Err(e) => {
                    tracing::warn!("File system watch error: {}", e);
                }
            }
        }

        Ok(events)
    }

    /// Get the path being watched
    pub fn watched_path(&self) -> &PathBuf {
        &self.path
    }

    /// Get the last modification time
    pub fn last_modification_time(&self) -> Option<std::time::SystemTime> {
        self.last_modified
    }

    /// Get file modification time
    fn get_file_modification_time(
        path: &PathBuf,
    ) -> Result<std::time::SystemTime, ConfigWatchError> {
        let metadata = std::fs::metadata(path).map_err(|e| ConfigWatchError::FileAccess {
            path: path.clone(),
            error: e.to_string(),
        })?;

        metadata
            .modified()
            .map_err(|e| ConfigWatchError::FileAccess {
                path: path.clone(),
                error: e.to_string(),
            })
    }
}

impl std::fmt::Debug for ConfigWatcher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConfigWatcher")
            .field("path", &self.path)
            .field("last_modified", &self.last_modified)
            .field("has_watcher", &self._watcher.is_some())
            .field("has_event_receiver", &self.event_receiver.is_some())
            .field("has_cached_config", &self.cached_config.is_some())
            .finish()
    }
}

/// Configuration change event
#[derive(Debug, Clone)]
pub struct ConfigChangeEvent {
    /// Path to the changed configuration file
    pub path: PathBuf,
    /// Type of change that occurred
    pub change_type: ConfigChangeType,
    /// Timestamp when the change was detected
    pub timestamp: std::time::SystemTime,
}

/// Type of configuration file change
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigChangeType {
    /// File was modified
    Modified,
    /// File was created
    Created,
    /// File was deleted
    Deleted,
    /// Other type of change
    Other,
}

/// Configuration watching error
#[derive(Debug, thiserror::Error)]
pub enum ConfigWatchError {
    /// Error accessing configuration file
    #[error("Error accessing config file {path}: {error}")]
    FileAccess { path: PathBuf, error: String },

    /// Error creating file system watcher
    #[error("Error creating file system watcher for {path}: {error}")]
    WatcherCreation { path: PathBuf, error: String },

    /// Error starting file system watch
    #[error("Error starting file system watch for {path}: {error}")]
    WatchStart { path: PathBuf, error: String },
}

/// Configuration template types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemplateType {
    Development,
    Production,
    Testing,
}

/// Convenience functions for common operations
pub struct ConfigPersistence;

impl ConfigPersistence {
    /// Load configuration with default settings
    pub fn load() -> Result<AppConfig, ConfigLoadError> {
        ConfigLoader::new().load()
    }

    /// Load configuration from specific file
    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<AppConfig, ConfigLoadError> {
        ConfigLoader::new().load_from_file(path.as_ref())
    }

    /// Save configuration to file
    pub fn save_to_file<P: AsRef<Path>>(
        config: &AppConfig,
        path: P,
    ) -> Result<(), ConfigSaveError> {
        ConfigLoader::new().save_to_file(config, path.as_ref())
    }

    /// Create configuration template
    pub fn create_template<P: AsRef<Path>>(
        template_type: TemplateType,
        path: P,
    ) -> Result<(), ConfigSaveError> {
        let config = match template_type {
            TemplateType::Development => crate::config::presets::development(),
            TemplateType::Production => crate::config::presets::production(),
            TemplateType::Testing => crate::config::presets::testing(),
        };
        Self::save_to_file(&config, path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use std::thread;
    use std::time::Duration;
    use tempfile::NamedTempFile;

    #[test]
    fn test_config_loader_basic() {
        let loader = ConfigLoader::new();
        assert!(!loader.search_paths.is_empty());
        assert_eq!(loader.env_prefix, "VOIRS");
        assert_eq!(loader.config_name, "voirs");
    }

    #[test]
    fn test_config_format_detection() {
        use std::path::Path;

        assert_eq!(
            ConfigFormat::from_extension(Path::new("test.json")),
            Some(ConfigFormat::Json)
        );
        assert_eq!(
            ConfigFormat::from_extension(Path::new("test.toml")),
            Some(ConfigFormat::Toml)
        );
        assert_eq!(
            ConfigFormat::from_extension(Path::new("test.yaml")),
            Some(ConfigFormat::Yaml)
        );
        assert_eq!(
            ConfigFormat::from_extension(Path::new("test.yml")),
            Some(ConfigFormat::Yaml)
        );
        assert_eq!(
            ConfigFormat::from_extension(Path::new("test.unknown")),
            None
        );
    }

    #[test]
    fn test_config_format_properties() {
        assert_eq!(ConfigFormat::Json.extension(), "json");
        assert_eq!(ConfigFormat::Toml.extension(), "toml");
        assert_eq!(ConfigFormat::Yaml.extension(), "yaml");

        assert_eq!(ConfigFormat::Json.mime_type(), "application/json");
        assert_eq!(ConfigFormat::Toml.mime_type(), "application/toml");
        assert_eq!(ConfigFormat::Yaml.mime_type(), "application/yaml");
    }

    #[test]
    fn test_config_loader_with_custom_settings() {
        let loader = ConfigLoader::new()
            .with_env_prefix("CUSTOM")
            .with_config_name("myapp")
            .with_search_paths(vec![std::path::PathBuf::from("/custom/path")]);

        assert_eq!(loader.env_prefix, "CUSTOM");
        assert_eq!(loader.config_name, "myapp");
        assert_eq!(
            loader.search_paths,
            vec![std::path::PathBuf::from("/custom/path")]
        );
    }

    #[test]
    fn test_config_file_save_and_load() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let config = AppConfig::default();

        // Test JSON format
        let json_path = temp_file.path().with_extension("json");
        let loader = ConfigLoader::new();

        // Save configuration
        loader.save_to_file(&config, &json_path).unwrap();

        // Load configuration
        let loaded_config = loader.load_from_file(&json_path).unwrap();

        // Verify basic structure (we can't do exact equality due to potential differences in defaults)
        assert_eq!(loaded_config.pipeline.device, config.pipeline.device);
        assert_eq!(loaded_config.pipeline.use_gpu, config.pipeline.use_gpu);

        // Clean up
        let _ = std::fs::remove_file(&json_path);
    }

    #[test]
    fn test_config_watcher_creation() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = AppConfig::default();

        // Create a valid config file
        let json_content = serde_json::to_string_pretty(&config).unwrap();
        std::fs::write(temp_file.path(), json_content).unwrap();

        let loader = ConfigLoader::new();
        let watcher = ConfigWatcher::new(temp_file.path().to_path_buf(), loader).unwrap();

        assert_eq!(watcher.watched_path(), temp_file.path());
        assert!(watcher.last_modification_time().is_some());
        assert!(watcher.get_cached_config().is_none());
    }

    #[test]
    fn test_config_watcher_with_auto_watch() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = AppConfig::default();

        // Create a valid config file
        let json_content = serde_json::to_string_pretty(&config).unwrap();
        std::fs::write(temp_file.path(), json_content).unwrap();

        let loader = ConfigLoader::new();
        let watcher =
            ConfigWatcher::with_auto_watch(temp_file.path().to_path_buf(), loader).unwrap();

        assert_eq!(watcher.watched_path(), temp_file.path());
        assert!(watcher.last_modification_time().is_some());
        assert!(watcher.get_cached_config().is_none());
    }

    #[test]
    fn test_config_watcher_reload_if_changed() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = AppConfig::default();

        // Create initial config file with .json extension
        let json_path = temp_file.path().with_extension("json");
        let json_content = serde_json::to_string_pretty(&config).unwrap();
        std::fs::write(&json_path, json_content).unwrap();

        let loader = ConfigLoader::new();
        let mut watcher = ConfigWatcher::new(json_path.clone(), loader).unwrap();

        // First reload should detect changes since we compare with the current file state
        let result = watcher.reload_if_changed().unwrap();
        // The first call might not detect changes if the file hasn't been modified since creation
        // Let's force a change first
        thread::sleep(Duration::from_millis(100));
        let mut modified_config = config.clone();
        modified_config.pipeline.device = "initial_change".to_string();
        let modified_json = serde_json::to_string_pretty(&modified_config).unwrap();
        std::fs::write(&json_path, modified_json).unwrap();

        let result = watcher.reload_if_changed().unwrap();
        assert!(result.is_some());
        assert!(watcher.get_cached_config().is_some());

        // Second reload should detect no changes
        let result = watcher.reload_if_changed().unwrap();
        assert!(result.is_none());

        // Modify the file again
        thread::sleep(Duration::from_millis(100)); // Ensure different modification time
        let mut second_modified_config = config.clone();
        second_modified_config.pipeline.device = "second_change".to_string();
        let second_modified_json = serde_json::to_string_pretty(&second_modified_config).unwrap();
        std::fs::write(&json_path, second_modified_json).unwrap();

        // Third reload should detect changes
        let result = watcher.reload_if_changed().unwrap();
        assert!(result.is_some());
        let reloaded_config = result.unwrap();
        assert_eq!(reloaded_config.pipeline.device, "second_change");

        // Clean up
        let _ = std::fs::remove_file(&json_path);
    }

    #[test]
    fn test_config_watcher_force_reload() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = AppConfig::default();

        // Create initial config file with .json extension
        let json_path = temp_file.path().with_extension("json");
        let json_content = serde_json::to_string_pretty(&config).unwrap();
        std::fs::write(&json_path, json_content).unwrap();

        let loader = ConfigLoader::new();
        let mut watcher = ConfigWatcher::new(json_path.clone(), loader).unwrap();

        // Force reload should always work
        let result = watcher.force_reload().unwrap();
        assert_eq!(result.pipeline.device, config.pipeline.device);
        assert!(watcher.get_cached_config().is_some());

        // Modify the file
        let mut modified_config = config.clone();
        modified_config.pipeline.device = "forced".to_string();
        let modified_json = serde_json::to_string_pretty(&modified_config).unwrap();
        std::fs::write(&json_path, modified_json).unwrap();

        // Force reload should pick up changes
        let result = watcher.force_reload().unwrap();
        assert_eq!(result.pipeline.device, "forced");

        // Clean up
        let _ = std::fs::remove_file(&json_path);
    }

    #[test]
    fn test_config_watcher_events() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = AppConfig::default();

        // Create a valid config file
        let json_content = serde_json::to_string_pretty(&config).unwrap();
        std::fs::write(temp_file.path(), json_content).unwrap();

        let loader = ConfigLoader::new();
        let mut watcher =
            ConfigWatcher::with_auto_watch(temp_file.path().to_path_buf(), loader).unwrap();

        // Check events (should be empty initially)
        let events = watcher.check_events().unwrap();
        assert!(events.is_empty());

        // Modify the file
        let mut modified_config = config.clone();
        modified_config.pipeline.device = "event_test".to_string();
        let modified_json = serde_json::to_string_pretty(&modified_config).unwrap();
        std::fs::write(temp_file.path(), modified_json).unwrap();

        // Give the file watcher time to detect changes
        thread::sleep(Duration::from_millis(200));

        // Check for events (may or may not detect depending on timing)
        let events = watcher.check_events().unwrap();
        // We can't guarantee events will be detected due to timing, so we just verify the method works
        // events.len() is always >= 0 since it's a usize, so we just verify the method returned successfully
    }

    #[test]
    fn test_config_watcher_error_handling() {
        let non_existent_path = PathBuf::from("/non/existent/path/config.json");
        let loader = ConfigLoader::new();

        // Should fail to create watcher for non-existent file
        let result = ConfigWatcher::new(non_existent_path, loader);
        assert!(result.is_err());

        match result.unwrap_err() {
            ConfigWatchError::FileAccess { path, .. } => {
                assert_eq!(path, PathBuf::from("/non/existent/path/config.json"));
            }
            _ => panic!("Expected FileAccess error"),
        }
    }

    #[test]
    fn test_config_change_event_types() {
        let event = ConfigChangeEvent {
            path: PathBuf::from("/test/config.json"),
            change_type: ConfigChangeType::Modified,
            timestamp: std::time::SystemTime::now(),
        };

        assert_eq!(event.change_type, ConfigChangeType::Modified);
        assert_eq!(event.path, PathBuf::from("/test/config.json"));

        // Test all change types
        assert_eq!(ConfigChangeType::Modified, ConfigChangeType::Modified);
        assert_eq!(ConfigChangeType::Created, ConfigChangeType::Created);
        assert_eq!(ConfigChangeType::Deleted, ConfigChangeType::Deleted);
        assert_eq!(ConfigChangeType::Other, ConfigChangeType::Other);

        assert_ne!(ConfigChangeType::Modified, ConfigChangeType::Created);
    }

    #[test]
    fn test_config_persistence_convenience_methods() {
        let temp_file = NamedTempFile::new().unwrap();
        let config = AppConfig::default();

        // Test save convenience method
        let json_path = temp_file.path().with_extension("json");
        ConfigPersistence::save_to_file(&config, &json_path).unwrap();

        // Test load convenience method
        let loaded_config = ConfigPersistence::load_from_file(&json_path).unwrap();
        assert_eq!(loaded_config.pipeline.device, config.pipeline.device);

        // Clean up
        let _ = std::fs::remove_file(&json_path);
    }

    #[test]
    fn test_config_template_creation() {
        let temp_dir = tempfile::tempdir().unwrap();

        // Test development template
        let dev_path = temp_dir.path().join("dev.json");
        ConfigPersistence::create_template(TemplateType::Development, &dev_path).unwrap();
        assert!(dev_path.exists());

        // Test production template
        let prod_path = temp_dir.path().join("prod.json");
        ConfigPersistence::create_template(TemplateType::Production, &prod_path).unwrap();
        assert!(prod_path.exists());

        // Test testing template
        let test_path = temp_dir.path().join("test.json");
        ConfigPersistence::create_template(TemplateType::Testing, &test_path).unwrap();
        assert!(test_path.exists());
    }

    #[test]
    fn test_config_loader_discover_files() {
        let temp_dir = tempfile::tempdir().unwrap();
        let config = AppConfig::default();

        // Create multiple config files
        let json_file = temp_dir.path().join("voirs.json");
        let toml_file = temp_dir.path().join("voirs.toml");
        let yaml_file = temp_dir.path().join("voirs.yaml");

        let loader = ConfigLoader::new().with_search_paths(vec![temp_dir.path().to_path_buf()]);

        // Save config files
        loader.save_to_file(&config, &json_file).unwrap();
        loader.save_to_file(&config, &toml_file).unwrap();
        loader.save_to_file(&config, &yaml_file).unwrap();

        // Discover files
        let discovered_files = loader.discover_config_files().unwrap();
        assert_eq!(discovered_files.len(), 3);

        // Verify all files are discovered
        assert!(discovered_files.contains(&json_file));
        assert!(discovered_files.contains(&toml_file));
        assert!(discovered_files.contains(&yaml_file));
    }
}