adk-gateway 1.0.0

Multi-channel AI gateway for adk-rust agents — Telegram, Slack, WhatsApp, Discord, Matrix + control panel
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
//! Structured logging configuration, secret redaction, and log rotation.
//!
//! Design: TelemetryExporter, Security Architecture [R14.1, R14.3]
//! Log Rotation: [R12.1, R12.2, R12.3, R12.4, R12.5]

use regex::Regex;
use std::path::PathBuf;
use std::sync::OnceLock;

use crate::config::{LogFileInfo, LogFormat, LogRotationConfig, RotationPolicy};

// ── Secret patterns ────────────────────────────────────────────────

/// Known secret patterns to redact from log output.
#[allow(dead_code)] // Used by secret_patterns() and SecretRedactor; initialized on first use
static SECRET_PATTERNS: OnceLock<Vec<Regex>> = OnceLock::new();

#[allow(dead_code)] // Used by SecretRedactor::redact; lazily initializes SECRET_PATTERNS
fn secret_patterns() -> &'static Vec<Regex> {
    SECRET_PATTERNS.get_or_init(|| {
        vec![
            // Bot tokens (Telegram-style: digits:alphanumeric)
            Regex::new(r"\b\d{8,12}:[A-Za-z0-9_-]{30,50}\b").unwrap(),
            // Bearer tokens in headers
            Regex::new(r"(?i)bearer\s+[A-Za-z0-9._~+/=-]{20,}").unwrap(),
            // Generic API keys (long hex or base64 strings)
            Regex::new(r#"(?i)(api[_-]?key|token|secret|password)\s*[=:]\s*['"]?[A-Za-z0-9._~+/=-]{16,}['"]?"#).unwrap(),
            // Slack tokens (xoxb-, xoxp-, xapp-)
            Regex::new(r"xox[bpa]-[A-Za-z0-9-]{10,}").unwrap(),
            // JWT tokens (three base64 segments separated by dots)
            Regex::new(r"eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+").unwrap(),
        ]
    })
}

// ── SecretRedactor ─────────────────────────────────────────────────

/// Redacts known secret patterns from text.
#[derive(Debug, Clone)]
#[allow(dead_code)] // Used in tests; available for log output redaction
pub struct SecretRedactor {
    replacement: String,
}

impl SecretRedactor {
    #[allow(dead_code)] // Used in tests; constructs default redactor
    pub fn new() -> Self {
        Self {
            replacement: "***REDACTED***".to_string(),
        }
    }

    #[allow(dead_code)] // Used in tests; constructs redactor with custom replacement
    pub fn with_replacement(replacement: &str) -> Self {
        Self {
            replacement: replacement.to_string(),
        }
    }

    /// Redact all known secret patterns from the input string.
    #[allow(dead_code)] // Used in tests; available for log output redaction
    pub fn redact(&self, input: &str) -> String {
        let mut result = input.to_string();
        for pattern in secret_patterns() {
            result = pattern
                .replace_all(&result, self.replacement.as_str())
                .to_string();
        }
        result
    }
}

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

// ── Config redaction ───────────────────────────────────────────────

/// Fields that should be redacted when displaying config.
const SENSITIVE_FIELDS: &[&str] = &[
    "bot_token",
    "botToken",
    "app_token",
    "appToken",
    "token",
    "password",
    "secret",
    "api_key",
    "apiKey",
    "connection_string",
    "connectionString",
];

/// Redact sensitive values in a JSON value tree.
/// Replaces values of known sensitive keys with `"***"`.
pub fn redact_config(value: &serde_json::Value) -> serde_json::Value {
    match value {
        serde_json::Value::Object(map) => {
            let mut redacted = serde_json::Map::new();
            for (key, val) in map {
                if SENSITIVE_FIELDS.iter().any(|&f| key == f) {
                    // Only redact if the value is a non-null string
                    match val {
                        serde_json::Value::String(s) if !s.is_empty() => {
                            redacted.insert(key.clone(), serde_json::Value::String("***".into()));
                        }
                        _ => {
                            redacted.insert(key.clone(), redact_config(val));
                        }
                    }
                } else {
                    redacted.insert(key.clone(), redact_config(val));
                }
            }
            serde_json::Value::Object(redacted)
        }
        serde_json::Value::Array(arr) => {
            serde_json::Value::Array(arr.iter().map(redact_config).collect())
        }
        other => other.clone(),
    }
}

// ── Structured logging setup ───────────────────────────────────────

/// Configuration for the telemetry/logging subsystem.
#[derive(Debug, Clone)]
pub struct TelemetrySetup {
    pub json_format: bool,
    pub pretty_format: bool,
    pub otel_endpoint: Option<String>,
    pub log_dir: Option<String>,
    pub rotation_config: LogRotationConfig,
}

impl TelemetrySetup {
    pub fn from_config(config: &crate::config::TelemetryConfig) -> Self {
        // Determine effective format: log_rotation.format overrides top-level log_format
        let effective_format = config
            .log_rotation
            .format
            .as_ref()
            .unwrap_or(&config.log_format);

        Self {
            json_format: matches!(effective_format, LogFormat::Json),
            pretty_format: matches!(effective_format, LogFormat::Pretty | LogFormat::Text),
            otel_endpoint: config.otel_endpoint.clone(),
            log_dir: config.log_dir.clone(),
            rotation_config: config.log_rotation.clone(),
        }
    }

    /// Check the `LOG_FORMAT` environment variable for format override.
    /// Returns the effective format considering env var.
    fn effective_format_from_env(&self) -> (bool, bool) {
        match std::env::var("LOG_FORMAT").ok().as_deref() {
            Some("json") => (true, false),
            Some("pretty") => (false, true),
            _ => (self.json_format, self.pretty_format),
        }
    }

    /// Initialize the tracing subscriber based on the telemetry configuration.
    ///
    /// Configures JSON or text format logging, applies the given `EnvFilter`,
    /// and optionally adds a daily-rotating file appender.
    /// Respects `LOG_FORMAT` environment variable: `json` for structured JSON,
    /// `pretty` for human-readable output.
    pub fn init(&self, filter: tracing_subscriber::EnvFilter) {
        use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};

        let (use_json, _use_pretty) = self.effective_format_from_env();

        // Build the file layer if log_dir is configured
        let file_guard = if let Some(ref log_dir) = self.log_dir {
            std::fs::create_dir_all(log_dir).ok();

            let file_appender = match &self.rotation_config.rotation {
                RotationPolicy::Hourly => {
                    tracing_appender::rolling::hourly(log_dir, "adk-gateway.log")
                }
                RotationPolicy::Size { .. } => {
                    // Size-based rotation uses daily as the base; actual size checks
                    // are handled by the retention cleanup process.
                    tracing_appender::rolling::daily(log_dir, "adk-gateway.log")
                }
                RotationPolicy::Daily => {
                    tracing_appender::rolling::daily(log_dir, "adk-gateway.log")
                }
            };

            let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
            // Store the guard in a leaked Box to keep it alive for the process lifetime
            Box::leak(Box::new(guard));
            Some(non_blocking)
        } else {
            None
        };

        if use_json {
            let registry = tracing_subscriber::registry()
                .with(filter)
                .with(fmt::layer().json().with_target(true));
            if let Some(writer) = file_guard {
                registry
                    .with(fmt::layer().json().with_target(true).with_writer(writer))
                    .init();
            } else {
                registry.init();
            }
        } else {
            let registry = tracing_subscriber::registry()
                .with(filter)
                .with(fmt::layer().with_target(true));
            if let Some(writer) = file_guard {
                registry
                    .with(fmt::layer().with_target(true).with_ansi(false).with_writer(writer))
                    .init();
            } else {
                registry.init();
            }
        }
    }

    /// Describe the telemetry configuration for display.
    pub fn describe(&self) -> String {
        let (use_json, use_pretty) = self.effective_format_from_env();
        let format = if use_json {
            "JSON"
        } else if use_pretty {
            "pretty"
        } else {
            "text"
        };
        let otel = match &self.otel_endpoint {
            Some(ep) => format!("enabled ({})", ep),
            None => "disabled".to_string(),
        };
        let file = match &self.log_dir {
            Some(dir) => {
                let rotation = match &self.rotation_config.rotation {
                    RotationPolicy::Daily => "daily",
                    RotationPolicy::Hourly => "hourly",
                    RotationPolicy::Size { .. } => "size-based",
                };
                format!(
                    "{} rotation in {}, retention={}d, max_size={}MB",
                    rotation, dir, self.rotation_config.retention_days, self.rotation_config.max_file_size_mb
                )
            }
            None => "disabled".to_string(),
        };
        format!("log_format={format}, otel={otel}, file_logs={file}")
    }
}

// ── Log Rotation & Retention ───────────────────────────────────────

/// Manages log file rotation and retention cleanup.
///
/// Scans the configured log directory for files matching the log file pattern,
/// determines which files exceed the retention period, and deletes them.
pub struct LogRetentionManager {
    config: LogRotationConfig,
    log_dir: PathBuf,
}

impl LogRetentionManager {
    /// Create a new retention manager for the given log directory and config.
    pub fn new(log_dir: impl Into<PathBuf>, config: LogRotationConfig) -> Self {
        Self {
            config,
            log_dir: log_dir.into(),
        }
    }

    /// Scan the log directory and collect metadata about log files.
    ///
    /// Returns a list of `LogFileInfo` for all files matching the log pattern.
    pub fn scan_log_files(&self) -> Vec<LogFileInfo> {
        let mut files = Vec::new();

        let entries = match std::fs::read_dir(&self.log_dir) {
            Ok(entries) => entries,
            Err(_) => return files,
        };

        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_file() {
                continue;
            }

            // Only consider files that look like log files
            let file_name = match path.file_name().and_then(|n| n.to_str()) {
                Some(name) => name.to_string(),
                None => continue,
            };

            if !file_name.starts_with("adk-gateway.log") {
                continue;
            }

            // Get file metadata for size and creation time
            let metadata = match std::fs::metadata(&path) {
                Ok(m) => m,
                Err(_) => continue,
            };

            let created_at = Self::parse_log_date_from_filename(&file_name)
                .or_else(|| {
                    metadata
                        .created()
                        .ok()
                        .map(|t| chrono::DateTime::<chrono::Utc>::from(t))
                })
                .unwrap_or_else(|| chrono::Utc::now());

            files.push(LogFileInfo {
                path,
                created_at,
                size_bytes: metadata.len(),
            });
        }

        files
    }

    /// Parse a date from a log filename like `adk-gateway.log.2026-05-12`.
    fn parse_log_date_from_filename(filename: &str) -> Option<chrono::DateTime<chrono::Utc>> {
        // Pattern: adk-gateway.log.YYYY-MM-DD
        let date_part = filename.strip_prefix("adk-gateway.log.")?;
        let naive_date = chrono::NaiveDate::parse_from_str(date_part, "%Y-%m-%d").ok()?;
        let naive_datetime = naive_date.and_hms_opt(0, 0, 0)?;
        Some(chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
            naive_datetime,
            chrono::Utc,
        ))
    }

    /// Run the retention cleanup: delete files older than the retention period.
    ///
    /// Returns the list of paths that were successfully deleted.
    pub fn cleanup(&self) -> Vec<PathBuf> {
        let now = chrono::Utc::now();
        let files = self.scan_log_files();
        let to_delete = self.config.files_to_delete(&files, now);

        let mut deleted = Vec::new();
        for path in to_delete {
            if std::fs::remove_file(&path).is_ok() {
                tracing::info!(path = %path.display(), "Deleted expired log file");
                deleted.push(path);
            } else {
                tracing::warn!(path = %path.display(), "Failed to delete expired log file");
            }
        }

        deleted
    }

    /// Check if any log file exceeds the configured max file size.
    ///
    /// Returns files that exceed `max_file_size_mb` threshold.
    pub fn oversized_files(&self) -> Vec<LogFileInfo> {
        let max_bytes = self.config.max_file_size_mb * 1024 * 1024;
        self.scan_log_files()
            .into_iter()
            .filter(|f| f.size_bytes > max_bytes)
            .collect()
    }
}

/// Spawn a background task that periodically cleans up expired log files.
///
/// Runs every hour, checking for files that exceed the retention period.
pub fn spawn_log_retention_task(
    log_dir: impl Into<PathBuf>,
    config: LogRotationConfig,
    cancel_token: tokio_util::sync::CancellationToken,
) -> tokio::task::JoinHandle<()> {
    let manager = LogRetentionManager::new(log_dir, config);

    tokio::spawn(async move {
        let mut interval = tokio::time::interval(std::time::Duration::from_secs(3600));
        interval.tick().await; // Skip the first immediate tick

        loop {
            tokio::select! {
                _ = cancel_token.cancelled() => {
                    tracing::info!("Log retention task cancelled");
                    break;
                }
                _ = interval.tick() => {
                    let deleted = manager.cleanup();
                    if !deleted.is_empty() {
                        tracing::info!(count = deleted.len(), "Log retention cleanup completed");
                    }
                    // Warn about oversized log files that may need attention
                    let oversized = manager.oversized_files();
                    if !oversized.is_empty() {
                        tracing::warn!(
                            count = oversized.len(),
                            "detected oversized log files exceeding max_file_size_mb"
                        );
                    }
                }
            }
        }
    })
}

// ── Tests ──────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{LogFileInfo, LogRotationConfig, RotationPolicy};
    use chrono::{Duration, Utc};
    use serde_json::json;
    use std::path::PathBuf;

    #[test]
    fn test_redact_no_secrets() {
        let redactor = SecretRedactor::new();
        let input = "this is a normal log message with no secrets";
        let result = redactor.redact(input);
        assert_eq!(result, input);
    }

    #[test]
    fn test_redact_custom_replacement() {
        let redactor = SecretRedactor::with_replacement("[HIDDEN]");
        let input = "token: xoxb-test-placeholder";
        let result = redactor.redact(input);
        assert!(result.contains("[HIDDEN]"));
    }

    #[test]
    fn test_redact_config_simple() {
        let config = json!({
            "gateway": {
                "port": 18789,
                "auth": {
                    "token": "my-secret-token",
                    "password": "my-password"
                }
            },
            "channels": {
                "telegram": {
                    "botToken": "123:ABC",
                    "enabled": true
                }
            }
        });

        let redacted = redact_config(&config);
        assert_eq!(redacted["gateway"]["auth"]["token"], "***");
        assert_eq!(redacted["gateway"]["auth"]["password"], "***");
        assert_eq!(redacted["channels"]["telegram"]["botToken"], "***");
        assert_eq!(redacted["gateway"]["port"], 18789);
        assert_eq!(redacted["channels"]["telegram"]["enabled"], true);
    }

    #[test]
    fn test_redact_config_nested_array() {
        let config = json!({
            "items": [
                {"token": "secret1", "name": "a"},
                {"token": "secret2", "name": "b"}
            ]
        });

        let redacted = redact_config(&config);
        assert_eq!(redacted["items"][0]["token"], "***");
        assert_eq!(redacted["items"][1]["token"], "***");
        assert_eq!(redacted["items"][0]["name"], "a");
    }

    #[test]
    fn test_redact_config_empty_token() {
        let config = json!({
            "token": "",
            "password": null
        });

        let redacted = redact_config(&config);
        // Empty string should not be redacted
        assert_eq!(redacted["token"], "");
        // Null should not be redacted
        assert!(redacted["password"].is_null());
    }

    #[test]
    fn test_telemetry_setup_describe() {
        let setup = TelemetrySetup {
            json_format: true,
            pretty_format: false,
            otel_endpoint: Some("http://localhost:4317".into()),
            log_dir: None,
            rotation_config: LogRotationConfig::default(),
        };
        let desc = setup.describe();
        assert!(desc.contains("JSON"));
        assert!(desc.contains("localhost:4317"));
    }

    #[test]
    fn test_telemetry_setup_no_otel() {
        let setup = TelemetrySetup {
            json_format: false,
            pretty_format: true,
            otel_endpoint: None,
            log_dir: None,
            rotation_config: LogRotationConfig::default(),
        };
        let desc = setup.describe();
        assert!(desc.contains("pretty"));
        assert!(desc.contains("disabled"));
    }

    // ── Log Rotation Unit Tests ────────────────────────────────────

    #[test]
    fn test_files_to_delete_empty_list() {
        let config = LogRotationConfig::default();
        let now = Utc::now();
        let result = config.files_to_delete(&[], now);
        assert!(result.is_empty());
    }

    #[test]
    fn test_files_to_delete_all_within_retention() {
        let config = LogRotationConfig {
            retention_days: 7,
            ..Default::default()
        };
        let now = Utc::now();

        let files = vec![
            LogFileInfo {
                path: PathBuf::from("/logs/adk-gateway.log.2026-05-14"),
                created_at: now - Duration::days(1),
                size_bytes: 1024,
            },
            LogFileInfo {
                path: PathBuf::from("/logs/adk-gateway.log.2026-05-13"),
                created_at: now - Duration::days(3),
                size_bytes: 2048,
            },
            LogFileInfo {
                path: PathBuf::from("/logs/adk-gateway.log.2026-05-10"),
                created_at: now - Duration::days(6),
                size_bytes: 4096,
            },
        ];

        let result = config.files_to_delete(&files, now);
        assert!(result.is_empty(), "No files should be deleted within retention window");
    }

    #[test]
    fn test_files_to_delete_some_expired() {
        let config = LogRotationConfig {
            retention_days: 7,
            ..Default::default()
        };
        let now = Utc::now();

        let files = vec![
            LogFileInfo {
                path: PathBuf::from("/logs/recent.log"),
                created_at: now - Duration::days(2),
                size_bytes: 1024,
            },
            LogFileInfo {
                path: PathBuf::from("/logs/old.log"),
                created_at: now - Duration::days(10),
                size_bytes: 2048,
            },
            LogFileInfo {
                path: PathBuf::from("/logs/very_old.log"),
                created_at: now - Duration::days(30),
                size_bytes: 4096,
            },
        ];

        let result = config.files_to_delete(&files, now);
        assert_eq!(result.len(), 2);
        assert!(result.contains(&PathBuf::from("/logs/old.log")));
        assert!(result.contains(&PathBuf::from("/logs/very_old.log")));
    }

    #[test]
    fn test_files_to_delete_boundary_exactly_at_retention() {
        let config = LogRotationConfig {
            retention_days: 7,
            ..Default::default()
        };
        let now = Utc::now();

        // File created exactly 7 days ago — should NOT be deleted (not MORE than 7 days)
        let files = vec![LogFileInfo {
            path: PathBuf::from("/logs/boundary.log"),
            created_at: now - Duration::days(7),
            size_bytes: 1024,
        }];

        let result = config.files_to_delete(&files, now);
        assert!(result.is_empty(), "File exactly at retention boundary should not be deleted");
    }

    #[test]
    fn test_files_to_delete_just_past_retention() {
        let config = LogRotationConfig {
            retention_days: 7,
            ..Default::default()
        };
        let now = Utc::now();

        // File created 7 days + 1 second ago — should be deleted
        let files = vec![LogFileInfo {
            path: PathBuf::from("/logs/expired.log"),
            created_at: now - Duration::days(7) - Duration::seconds(1),
            size_bytes: 1024,
        }];

        let result = config.files_to_delete(&files, now);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], PathBuf::from("/logs/expired.log"));
    }

    #[test]
    fn test_files_to_delete_custom_retention() {
        let config = LogRotationConfig {
            retention_days: 30,
            ..Default::default()
        };
        let now = Utc::now();

        let files = vec![
            LogFileInfo {
                path: PathBuf::from("/logs/recent.log"),
                created_at: now - Duration::days(10),
                size_bytes: 1024,
            },
            LogFileInfo {
                path: PathBuf::from("/logs/old.log"),
                created_at: now - Duration::days(31),
                size_bytes: 2048,
            },
        ];

        let result = config.files_to_delete(&files, now);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], PathBuf::from("/logs/old.log"));
    }

    #[test]
    fn test_files_to_delete_zero_retention_deletes_all_past() {
        let config = LogRotationConfig {
            retention_days: 0,
            ..Default::default()
        };
        let now = Utc::now();

        let files = vec![
            LogFileInfo {
                path: PathBuf::from("/logs/today.log"),
                created_at: now,
                size_bytes: 1024,
            },
            LogFileInfo {
                path: PathBuf::from("/logs/yesterday.log"),
                created_at: now - Duration::days(1),
                size_bytes: 2048,
            },
        ];

        let result = config.files_to_delete(&files, now);
        // With 0 retention, files from yesterday should be deleted
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], PathBuf::from("/logs/yesterday.log"));
    }

    #[test]
    fn test_parse_log_date_from_filename() {
        let result = LogRetentionManager::parse_log_date_from_filename("adk-gateway.log.2026-05-12");
        assert!(result.is_some());
        let dt = result.unwrap();
        assert_eq!(dt.format("%Y-%m-%d").to_string(), "2026-05-12");
    }

    #[test]
    fn test_parse_log_date_invalid_filename() {
        let result = LogRetentionManager::parse_log_date_from_filename("random-file.txt");
        assert!(result.is_none());
    }

    #[test]
    fn test_log_rotation_config_default() {
        let config = LogRotationConfig::default();
        assert_eq!(config.retention_days, 7);
        assert_eq!(config.max_file_size_mb, 100);
        assert_eq!(config.rotation, RotationPolicy::Daily);
        assert!(config.format.is_none());
    }

    #[test]
    fn test_log_format_env_override() {
        // Test that LOG_FORMAT=json produces json format
        let setup = TelemetrySetup {
            json_format: false,
            pretty_format: true,
            otel_endpoint: None,
            log_dir: None,
            rotation_config: LogRotationConfig::default(),
        };
        // Without env var set, should use the struct fields
        // (We can't easily test env var in unit tests without side effects,
        // but we verify the method exists and the struct is correct)
        assert!(!setup.json_format);
        assert!(setup.pretty_format);
    }
}