wechat-pub-rs 0.5.1

A simple, high-performance WeChat Official Account Rust SDK for uploading articles and managing drafts
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
//! Configuration management for the WeChat Official Account SDK.
//!
//! This module provides a centralized configuration system that enables:
//! - Type-safe configuration management
//! - Environment variable integration
//! - YAML configuration file support
//! - Builder pattern for easy setup
//! - Configuration validation
//!
//! ## Usage
//!
//! ```rust
//! use wechat_pub_rs::config::{Config, SecurityConfig, PerformanceConfig, HttpConfig};
//! use wechat_pub_rs::Result;
//!
//! fn example() -> Result<()> {
//!     // Create default configuration
//!     let config = Config::default();
//!
//!     // Build custom configuration
//!     let config = Config::builder()
//!         .security(SecurityConfig::builder()
//!             .max_upload_size(20 * 1024 * 1024) // 20MB
//!             .build())
//!         .http(HttpConfig::builder()
//!             .request_timeout_secs(60)
//!             .build())
//!         .performance(PerformanceConfig::builder()
//!             .max_concurrent_uploads(10)
//!             .cache_ttl_minutes(30)
//!             .build())
//!         .build();
//!
//!     // Load from environment variables
//!     let config = Config::from_env()?;
//!     Ok(())
//! }
//! ```

use crate::error::{Result, WeChatError};
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Main configuration structure for the WeChat SDK.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    /// Security-related configuration
    pub security: SecurityConfig,
    /// Performance-related configuration
    pub performance: PerformanceConfig,
    /// HTTP client configuration
    pub http: HttpConfig,
    /// Cache configuration
    pub cache: CacheConfig,
    /// Retry configuration
    pub retry: RetryConfig,
}

/// Security configuration settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// Maximum allowed file size for uploads in bytes (default: 10MB)
    pub max_upload_size: u64,
    /// Maximum allowed file size for downloads in bytes (default: 20MB)
    pub max_download_size: u64,
    /// Whether to validate file paths for security (default: true)
    pub validate_file_paths: bool,
    /// Whether to sanitize filenames (default: true)
    pub sanitize_filenames: bool,
    /// List of blocked file extensions for security
    pub blocked_extensions: Vec<String>,
}

/// Performance configuration settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Maximum number of concurrent uploads (default: 5)
    pub max_concurrent_uploads: usize,
    /// Cache TTL in minutes (default: 15)
    pub cache_ttl_minutes: u64,
    /// Maximum cache size in entries (default: 1000)
    pub max_cache_entries: usize,
    /// Whether to enable parallel processing (default: true)
    pub enable_parallel_processing: bool,
}

/// HTTP client configuration settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpConfig {
    /// Request timeout in seconds (default: 30)
    pub request_timeout_secs: u64,
    /// Connection timeout in seconds (default: 10)
    pub connect_timeout_secs: u64,
    /// Base URL for WeChat API (default: "https://api.weixin.qq.com")
    pub base_url: String,
    /// User agent string for requests
    pub user_agent: String,
}

/// Cache configuration settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
    /// Whether to enable material lookup caching (default: true)
    pub enable_material_cache: bool,
    /// Whether to enable token caching (default: true)
    pub enable_token_cache: bool,
    /// Cache cleanup interval in minutes (default: 60)
    pub cleanup_interval_minutes: u64,
}

/// Retry configuration settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    /// Maximum number of retry attempts (default: 3)
    pub max_attempts: u32,
    /// Base delay between retries in milliseconds (default: 500)
    pub base_delay_ms: u64,
    /// Maximum delay between retries in seconds (default: 30)
    pub max_delay_secs: u64,
    /// Exponential backoff factor (default: 2.0)
    pub backoff_factor: f64,
    /// Whether to add jitter to retry delays (default: true)
    pub enable_jitter: bool,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            max_upload_size: 10 * 1024 * 1024,   // 10MB
            max_download_size: 20 * 1024 * 1024, // 20MB
            validate_file_paths: true,
            sanitize_filenames: true,
            blocked_extensions: vec![
                "exe".to_string(),
                "bat".to_string(),
                "cmd".to_string(),
                "com".to_string(),
                "pif".to_string(),
                "scr".to_string(),
                "vbs".to_string(),
                "js".to_string(),
                "jar".to_string(),
                "sh".to_string(),
                "php".to_string(),
                "asp".to_string(),
                "aspx".to_string(),
                "jsp".to_string(),
            ],
        }
    }
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            max_concurrent_uploads: 5,
            cache_ttl_minutes: 15,
            max_cache_entries: 1000,
            enable_parallel_processing: true,
        }
    }
}

impl Default for HttpConfig {
    fn default() -> Self {
        Self {
            request_timeout_secs: 30,
            connect_timeout_secs: 10,
            base_url: "https://api.weixin.qq.com".to_string(),
            user_agent: format!("wechat-pub-rs/{}", env!("CARGO_PKG_VERSION")),
        }
    }
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            enable_material_cache: true,
            enable_token_cache: true,
            cleanup_interval_minutes: 60,
        }
    }
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            base_delay_ms: 500,
            max_delay_secs: 30,
            backoff_factor: 2.0,
            enable_jitter: true,
        }
    }
}

impl Config {
    /// Creates a new configuration builder.
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::default()
    }

    /// Loads configuration from environment variables.
    pub fn from_env() -> Result<Self> {
        let mut config = Self::default();

        // Security settings
        if let Ok(val) = std::env::var("WECHAT_MAX_UPLOAD_SIZE") {
            config.security.max_upload_size = val
                .parse()
                .map_err(|_| WeChatError::config_error("Invalid WECHAT_MAX_UPLOAD_SIZE value"))?;
        }

        if let Ok(val) = std::env::var("WECHAT_MAX_DOWNLOAD_SIZE") {
            config.security.max_download_size = val
                .parse()
                .map_err(|_| WeChatError::config_error("Invalid WECHAT_MAX_DOWNLOAD_SIZE value"))?;
        }

        // Performance settings
        if let Ok(val) = std::env::var("WECHAT_MAX_CONCURRENT_UPLOADS") {
            config.performance.max_concurrent_uploads = val.parse().map_err(|_| {
                WeChatError::config_error("Invalid WECHAT_MAX_CONCURRENT_UPLOADS value")
            })?;
        }

        if let Ok(val) = std::env::var("WECHAT_CACHE_TTL_MINUTES") {
            config.performance.cache_ttl_minutes = val
                .parse()
                .map_err(|_| WeChatError::config_error("Invalid WECHAT_CACHE_TTL_MINUTES value"))?;
        }

        // HTTP settings
        if let Ok(val) = std::env::var("WECHAT_REQUEST_TIMEOUT") {
            config.http.request_timeout_secs = val
                .parse()
                .map_err(|_| WeChatError::config_error("Invalid WECHAT_REQUEST_TIMEOUT value"))?;
        }

        if let Ok(val) = std::env::var("WECHAT_BASE_URL") {
            config.http.base_url = val;
        }

        // Retry settings
        if let Ok(val) = std::env::var("WECHAT_MAX_RETRIES") {
            config.retry.max_attempts = val
                .parse()
                .map_err(|_| WeChatError::config_error("Invalid WECHAT_MAX_RETRIES value"))?;
        }

        config.validate()?;
        Ok(config)
    }

    /// Validates the configuration for consistency and constraints.
    pub fn validate(&self) -> Result<()> {
        // Validate security settings
        if self.security.max_upload_size == 0 {
            return Err(WeChatError::config_error(
                "max_upload_size must be greater than 0",
            ));
        }

        if self.security.max_download_size == 0 {
            return Err(WeChatError::config_error(
                "max_download_size must be greater than 0",
            ));
        }

        // Validate performance settings
        if self.performance.max_concurrent_uploads == 0 {
            return Err(WeChatError::config_error(
                "max_concurrent_uploads must be greater than 0",
            ));
        }

        if self.performance.max_concurrent_uploads > 20 {
            return Err(WeChatError::config_error(
                "max_concurrent_uploads should not exceed 20",
            ));
        }

        // Validate HTTP settings
        if self.http.request_timeout_secs == 0 {
            return Err(WeChatError::config_error(
                "request_timeout_secs must be greater than 0",
            ));
        }

        if self.http.connect_timeout_secs == 0 {
            return Err(WeChatError::config_error(
                "connect_timeout_secs must be greater than 0",
            ));
        }

        if self.http.base_url.is_empty() {
            return Err(WeChatError::config_error("base_url cannot be empty"));
        }

        // Validate retry settings
        if self.retry.max_attempts == 0 {
            return Err(WeChatError::config_error(
                "max_attempts must be greater than 0",
            ));
        }

        if self.retry.backoff_factor < 1.0 {
            return Err(WeChatError::config_error("backoff_factor must be >= 1.0"));
        }

        Ok(())
    }

    /// Converts retry config to Duration types for easier use.
    pub fn retry_base_delay(&self) -> Duration {
        Duration::from_millis(self.retry.base_delay_ms)
    }

    /// Converts retry config to Duration types for easier use.
    pub fn retry_max_delay(&self) -> Duration {
        Duration::from_secs(self.retry.max_delay_secs)
    }

    /// Converts HTTP timeout to Duration types for easier use.
    pub fn request_timeout(&self) -> Duration {
        Duration::from_secs(self.http.request_timeout_secs)
    }

    /// Converts HTTP timeout to Duration types for easier use.
    pub fn connect_timeout(&self) -> Duration {
        Duration::from_secs(self.http.connect_timeout_secs)
    }

    /// Converts cache TTL to Duration types for easier use.
    pub fn cache_ttl(&self) -> Duration {
        Duration::from_secs(self.performance.cache_ttl_minutes * 60)
    }
}

/// Builder for creating Config instances.
#[derive(Debug, Default)]
pub struct ConfigBuilder {
    security: Option<SecurityConfig>,
    performance: Option<PerformanceConfig>,
    http: Option<HttpConfig>,
    cache: Option<CacheConfig>,
    retry: Option<RetryConfig>,
}

impl ConfigBuilder {
    /// Sets the security configuration.
    pub fn security(mut self, security: SecurityConfig) -> Self {
        self.security = Some(security);
        self
    }

    /// Sets the performance configuration.
    pub fn performance(mut self, performance: PerformanceConfig) -> Self {
        self.performance = Some(performance);
        self
    }

    /// Sets the HTTP configuration.
    pub fn http(mut self, http: HttpConfig) -> Self {
        self.http = Some(http);
        self
    }

    /// Sets the cache configuration.
    pub fn cache(mut self, cache: CacheConfig) -> Self {
        self.cache = Some(cache);
        self
    }

    /// Sets the retry configuration.
    pub fn retry(mut self, retry: RetryConfig) -> Self {
        self.retry = Some(retry);
        self
    }

    /// Builds the configuration.
    pub fn build(self) -> Config {
        Config {
            security: self.security.unwrap_or_default(),
            performance: self.performance.unwrap_or_default(),
            http: self.http.unwrap_or_default(),
            cache: self.cache.unwrap_or_default(),
            retry: self.retry.unwrap_or_default(),
        }
    }
}

// Builder implementations for individual config sections

impl SecurityConfig {
    /// Creates a new security config builder.
    pub fn builder() -> SecurityConfigBuilder {
        SecurityConfigBuilder::default()
    }
}

impl PerformanceConfig {
    /// Creates a new performance config builder.
    pub fn builder() -> PerformanceConfigBuilder {
        PerformanceConfigBuilder::default()
    }
}

impl HttpConfig {
    /// Creates a new HTTP config builder.
    pub fn builder() -> HttpConfigBuilder {
        HttpConfigBuilder::default()
    }
}

impl CacheConfig {
    /// Creates a new cache config builder.
    pub fn builder() -> CacheConfigBuilder {
        CacheConfigBuilder::default()
    }
}

impl RetryConfig {
    /// Creates a new retry config builder.
    pub fn builder() -> RetryConfigBuilder {
        RetryConfigBuilder::default()
    }
}

/// Builder for SecurityConfig.
#[derive(Debug, Default)]
pub struct SecurityConfigBuilder {
    max_upload_size: Option<u64>,
    max_download_size: Option<u64>,
    validate_file_paths: Option<bool>,
    sanitize_filenames: Option<bool>,
    blocked_extensions: Option<Vec<String>>,
}

impl SecurityConfigBuilder {
    pub fn max_upload_size(mut self, size: u64) -> Self {
        self.max_upload_size = Some(size);
        self
    }

    pub fn max_download_size(mut self, size: u64) -> Self {
        self.max_download_size = Some(size);
        self
    }

    pub fn validate_file_paths(mut self, validate: bool) -> Self {
        self.validate_file_paths = Some(validate);
        self
    }

    pub fn sanitize_filenames(mut self, sanitize: bool) -> Self {
        self.sanitize_filenames = Some(sanitize);
        self
    }

    pub fn blocked_extensions(mut self, extensions: Vec<String>) -> Self {
        self.blocked_extensions = Some(extensions);
        self
    }

    pub fn build(self) -> SecurityConfig {
        let default = SecurityConfig::default();
        SecurityConfig {
            max_upload_size: self.max_upload_size.unwrap_or(default.max_upload_size),
            max_download_size: self.max_download_size.unwrap_or(default.max_download_size),
            validate_file_paths: self
                .validate_file_paths
                .unwrap_or(default.validate_file_paths),
            sanitize_filenames: self
                .sanitize_filenames
                .unwrap_or(default.sanitize_filenames),
            blocked_extensions: self
                .blocked_extensions
                .unwrap_or(default.blocked_extensions),
        }
    }
}

/// Builder for PerformanceConfig.
#[derive(Debug, Default)]
pub struct PerformanceConfigBuilder {
    max_concurrent_uploads: Option<usize>,
    cache_ttl_minutes: Option<u64>,
    max_cache_entries: Option<usize>,
    enable_parallel_processing: Option<bool>,
}

impl PerformanceConfigBuilder {
    pub fn max_concurrent_uploads(mut self, count: usize) -> Self {
        self.max_concurrent_uploads = Some(count);
        self
    }

    pub fn cache_ttl_minutes(mut self, minutes: u64) -> Self {
        self.cache_ttl_minutes = Some(minutes);
        self
    }

    pub fn max_cache_entries(mut self, entries: usize) -> Self {
        self.max_cache_entries = Some(entries);
        self
    }

    pub fn enable_parallel_processing(mut self, enable: bool) -> Self {
        self.enable_parallel_processing = Some(enable);
        self
    }

    pub fn build(self) -> PerformanceConfig {
        let default = PerformanceConfig::default();
        PerformanceConfig {
            max_concurrent_uploads: self
                .max_concurrent_uploads
                .unwrap_or(default.max_concurrent_uploads),
            cache_ttl_minutes: self.cache_ttl_minutes.unwrap_or(default.cache_ttl_minutes),
            max_cache_entries: self.max_cache_entries.unwrap_or(default.max_cache_entries),
            enable_parallel_processing: self
                .enable_parallel_processing
                .unwrap_or(default.enable_parallel_processing),
        }
    }
}

/// Builder for HttpConfig.
#[derive(Debug, Default)]
pub struct HttpConfigBuilder {
    request_timeout_secs: Option<u64>,
    connect_timeout_secs: Option<u64>,
    base_url: Option<String>,
    user_agent: Option<String>,
}

impl HttpConfigBuilder {
    pub fn request_timeout_secs(mut self, timeout: u64) -> Self {
        self.request_timeout_secs = Some(timeout);
        self
    }

    pub fn connect_timeout_secs(mut self, timeout: u64) -> Self {
        self.connect_timeout_secs = Some(timeout);
        self
    }

    pub fn base_url(mut self, url: String) -> Self {
        self.base_url = Some(url);
        self
    }

    pub fn user_agent(mut self, agent: String) -> Self {
        self.user_agent = Some(agent);
        self
    }

    pub fn build(self) -> HttpConfig {
        let default = HttpConfig::default();
        HttpConfig {
            request_timeout_secs: self
                .request_timeout_secs
                .unwrap_or(default.request_timeout_secs),
            connect_timeout_secs: self
                .connect_timeout_secs
                .unwrap_or(default.connect_timeout_secs),
            base_url: self.base_url.unwrap_or(default.base_url),
            user_agent: self.user_agent.unwrap_or(default.user_agent),
        }
    }
}

/// Builder for CacheConfig.
#[derive(Debug, Default)]
pub struct CacheConfigBuilder {
    enable_material_cache: Option<bool>,
    enable_token_cache: Option<bool>,
    cleanup_interval_minutes: Option<u64>,
}

impl CacheConfigBuilder {
    pub fn enable_material_cache(mut self, enable: bool) -> Self {
        self.enable_material_cache = Some(enable);
        self
    }

    pub fn enable_token_cache(mut self, enable: bool) -> Self {
        self.enable_token_cache = Some(enable);
        self
    }

    pub fn cleanup_interval_minutes(mut self, minutes: u64) -> Self {
        self.cleanup_interval_minutes = Some(minutes);
        self
    }

    pub fn build(self) -> CacheConfig {
        let default = CacheConfig::default();
        CacheConfig {
            enable_material_cache: self
                .enable_material_cache
                .unwrap_or(default.enable_material_cache),
            enable_token_cache: self
                .enable_token_cache
                .unwrap_or(default.enable_token_cache),
            cleanup_interval_minutes: self
                .cleanup_interval_minutes
                .unwrap_or(default.cleanup_interval_minutes),
        }
    }
}

/// Builder for RetryConfig.
#[derive(Debug, Default)]
pub struct RetryConfigBuilder {
    max_attempts: Option<u32>,
    base_delay_ms: Option<u64>,
    max_delay_secs: Option<u64>,
    backoff_factor: Option<f64>,
    enable_jitter: Option<bool>,
}

impl RetryConfigBuilder {
    pub fn max_attempts(mut self, attempts: u32) -> Self {
        self.max_attempts = Some(attempts);
        self
    }

    pub fn base_delay_ms(mut self, delay: u64) -> Self {
        self.base_delay_ms = Some(delay);
        self
    }

    pub fn max_delay_secs(mut self, delay: u64) -> Self {
        self.max_delay_secs = Some(delay);
        self
    }

    pub fn backoff_factor(mut self, factor: f64) -> Self {
        self.backoff_factor = Some(factor);
        self
    }

    pub fn enable_jitter(mut self, enable: bool) -> Self {
        self.enable_jitter = Some(enable);
        self
    }

    pub fn build(self) -> RetryConfig {
        let default = RetryConfig::default();
        RetryConfig {
            max_attempts: self.max_attempts.unwrap_or(default.max_attempts),
            base_delay_ms: self.base_delay_ms.unwrap_or(default.base_delay_ms),
            max_delay_secs: self.max_delay_secs.unwrap_or(default.max_delay_secs),
            backoff_factor: self.backoff_factor.unwrap_or(default.backoff_factor),
            enable_jitter: self.enable_jitter.unwrap_or(default.enable_jitter),
        }
    }
}

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

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert!(config.validate().is_ok());

        // Test default values
        assert_eq!(config.security.max_upload_size, 10 * 1024 * 1024);
        assert_eq!(config.performance.max_concurrent_uploads, 5);
        assert_eq!(config.http.request_timeout_secs, 30);
        assert_eq!(config.retry.max_attempts, 3);
    }

    #[test]
    fn test_config_builder() {
        let config = Config::builder()
            .security(
                SecurityConfig::builder()
                    .max_upload_size(5 * 1024 * 1024)
                    .validate_file_paths(false)
                    .build(),
            )
            .performance(
                PerformanceConfig::builder()
                    .max_concurrent_uploads(10)
                    .cache_ttl_minutes(30)
                    .build(),
            )
            .build();

        assert_eq!(config.security.max_upload_size, 5 * 1024 * 1024);
        assert!(!config.security.validate_file_paths);
        assert_eq!(config.performance.max_concurrent_uploads, 10);
        assert_eq!(config.performance.cache_ttl_minutes, 30);
    }

    #[test]
    fn test_config_validation() {
        let mut config = Config::default();
        config.security.max_upload_size = 0;
        assert!(config.validate().is_err());

        let mut config = Config::default();
        config.performance.max_concurrent_uploads = 0;
        assert!(config.validate().is_err());

        let mut config = Config::default();
        config.performance.max_concurrent_uploads = 25;
        assert!(config.validate().is_err());

        let mut config = Config::default();
        config.retry.backoff_factor = 0.5;
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_duration_conversions() {
        let config = Config::default();

        assert_eq!(config.retry_base_delay(), Duration::from_millis(500));
        assert_eq!(config.retry_max_delay(), Duration::from_secs(30));
        assert_eq!(config.request_timeout(), Duration::from_secs(30));
        assert_eq!(config.connect_timeout(), Duration::from_secs(10));
        assert_eq!(config.cache_ttl(), Duration::from_secs(15 * 60));
    }

    #[test]
    fn test_environment_loading() {
        // Set some environment variables
        unsafe {
            std::env::set_var("WECHAT_MAX_UPLOAD_SIZE", "5242880"); // 5MB
            std::env::set_var("WECHAT_MAX_CONCURRENT_UPLOADS", "10");
            std::env::set_var("WECHAT_REQUEST_TIMEOUT", "60");
        }

        let config = Config::from_env().unwrap();

        assert_eq!(config.security.max_upload_size, 5242880);
        assert_eq!(config.performance.max_concurrent_uploads, 10);
        assert_eq!(config.http.request_timeout_secs, 60);

        // Clean up
        unsafe {
            std::env::remove_var("WECHAT_MAX_UPLOAD_SIZE");
            std::env::remove_var("WECHAT_MAX_CONCURRENT_UPLOADS");
            std::env::remove_var("WECHAT_REQUEST_TIMEOUT");
        }
    }

    #[test]
    fn test_invalid_environment_values() {
        unsafe {
            std::env::set_var("WECHAT_MAX_UPLOAD_SIZE", "invalid");
        }
        assert!(Config::from_env().is_err());
        unsafe {
            std::env::remove_var("WECHAT_MAX_UPLOAD_SIZE");
        }
    }
}