things3-cli 1.0.0

CLI tool for Things 3 with integrated MCP server
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
//! Log aggregation and filtering utilities
//!
//! This module provides comprehensive log aggregation and filtering capabilities
//! for the Things 3 CLI application.

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::path::Path;
// Removed unused imports

use serde::{Deserialize, Serialize};
use thiserror::Error;
use tracing::{info, instrument, warn};
use tracing_subscriber::{
    fmt::{self, format::FmtSpan},
    layer::SubscriberExt,
    util::SubscriberInitExt,
    EnvFilter,
};

/// Error types for logging operations
#[derive(Error, Debug)]
pub enum LoggingError {
    #[error("Failed to read log file: {0}")]
    FileRead(String),

    #[error("Failed to write log file: {0}")]
    FileWrite(String),

    #[error("Invalid log format: {0}")]
    InvalidFormat(String),

    #[error("Filter compilation failed: {0}")]
    FilterCompilation(String),
}

/// Result type for logging operations
pub type Result<T> = std::result::Result<T, LoggingError>;

/// Log entry structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
    pub timestamp: String,
    pub level: String,
    pub target: String,
    pub message: String,
    pub fields: HashMap<String, serde_json::Value>,
    pub span_id: Option<String>,
    pub trace_id: Option<String>,
}

/// Log filter configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogFilter {
    pub level: Option<String>,
    pub target: Option<String>,
    pub message_pattern: Option<String>,
    pub time_range: Option<TimeRange>,
    pub fields: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeRange {
    pub start: Option<String>,
    pub end: Option<String>,
}

/// Log aggregator for collecting and processing logs
pub struct LogAggregator {
    log_file: String,
    max_entries: usize,
    entries: Vec<LogEntry>,
}

impl LogAggregator {
    /// Create a new log aggregator
    #[must_use]
    pub fn new(log_file: String, max_entries: usize) -> Self {
        Self {
            log_file,
            max_entries,
            entries: Vec::new(),
        }
    }

    /// Load logs from file
    ///
    /// # Errors
    /// Returns an error if the log file cannot be read or parsed
    #[instrument(skip(self))]
    pub fn load_logs(&mut self) -> Result<()> {
        if !Path::new(&self.log_file).exists() {
            info!("Log file does not exist, starting with empty logs");
            return Ok(());
        }

        let file = File::open(&self.log_file)
            .map_err(|e| LoggingError::FileRead(format!("Failed to open log file: {e}")))?;

        let reader = BufReader::new(file);
        let mut line_count = 0;

        for line in reader.lines() {
            let line =
                line.map_err(|e| LoggingError::FileRead(format!("Failed to read line: {e}")))?;

            if let Ok(entry) = Self::parse_log_line(&line) {
                self.entries.push(entry);
                line_count += 1;
            }
        }

        // Keep only the most recent entries
        if self.entries.len() > self.max_entries {
            let start = self.entries.len() - self.max_entries;
            self.entries.drain(0..start);
        }

        info!("Loaded {} log entries from file", line_count);
        Ok(())
    }

    /// Parse a log line into a `LogEntry`
    fn parse_log_line(line: &str) -> Result<LogEntry> {
        // Try to parse as JSON first (structured logging)
        if let Ok(entry) = serde_json::from_str::<LogEntry>(line) {
            return Ok(entry);
        }

        // Fallback to parsing as text format
        Self::parse_text_log_line(line)
    }

    /// Parse a text log line
    fn parse_text_log_line(line: &str) -> Result<LogEntry> {
        // Simple text log parsing - this would be more sophisticated in a real implementation
        let parts: Vec<&str> = line.splitn(4, ' ').collect();

        if parts.len() < 4 {
            return Err(LoggingError::InvalidFormat(
                "Insufficient log line parts".to_string(),
            ));
        }

        let timestamp = parts[0].to_string();
        let level = parts[1].to_string();
        let target = parts[2].to_string();
        let message = parts[3..].join(" ");

        Ok(LogEntry {
            timestamp,
            level,
            target,
            message,
            fields: HashMap::new(),
            span_id: None,
            trace_id: None,
        })
    }

    /// Filter logs based on criteria
    #[instrument(skip(self))]
    pub fn filter_logs(&self, filter: &LogFilter) -> Vec<LogEntry> {
        self.entries
            .iter()
            .filter(|entry| Self::matches_filter(entry, filter))
            .cloned()
            .collect()
    }

    /// Check if a log entry matches the filter
    fn matches_filter(entry: &LogEntry, filter: &LogFilter) -> bool {
        // Level filter
        if let Some(ref level) = filter.level {
            if !entry.level.eq_ignore_ascii_case(level) {
                return false;
            }
        }

        // Target filter
        if let Some(ref target) = filter.target {
            if !entry.target.contains(target) {
                return false;
            }
        }

        // Message pattern filter
        if let Some(ref pattern) = filter.message_pattern {
            if !entry.message.contains(pattern) {
                return false;
            }
        }

        // Time range filter
        if let Some(ref time_range) = filter.time_range {
            if !Self::matches_time_range(entry, time_range) {
                return false;
            }
        }

        // Fields filter
        for (key, value) in &filter.fields {
            if let Some(entry_value) = entry.fields.get(key) {
                if entry_value != value {
                    return false;
                }
            } else {
                return false;
            }
        }

        true
    }

    /// Check if entry matches time range
    fn matches_time_range(entry: &LogEntry, time_range: &TimeRange) -> bool {
        // Simple timestamp comparison - would be more sophisticated in real implementation
        if let Some(ref start) = time_range.start {
            if entry.timestamp < *start {
                return false;
            }
        }

        if let Some(ref end) = time_range.end {
            if entry.timestamp > *end {
                return false;
            }
        }

        true
    }

    /// Get log statistics
    #[instrument(skip(self))]
    pub fn get_statistics(&self) -> LogStatistics {
        let mut level_counts = HashMap::new();
        let mut target_counts = HashMap::new();

        for entry in &self.entries {
            *level_counts.entry(entry.level.clone()).or_insert(0) += 1;
            *target_counts.entry(entry.target.clone()).or_insert(0) += 1;
        }

        LogStatistics {
            total_entries: self.entries.len(),
            level_counts,
            target_counts,
            oldest_entry: self.entries.first().map(|e| e.timestamp.clone()),
            newest_entry: self.entries.last().map(|e| e.timestamp.clone()),
        }
    }

    /// Export filtered logs to file
    ///
    /// # Errors
    /// Returns an error if the output file cannot be created or written to
    #[instrument(skip(self))]
    pub fn export_logs(&self, filter: &LogFilter, output_file: &str) -> Result<()> {
        let filtered_logs = self.filter_logs(filter);

        let mut file = File::create(output_file)
            .map_err(|e| LoggingError::FileWrite(format!("Failed to create output file: {e}")))?;

        let count = filtered_logs.len();
        for entry in filtered_logs {
            let json = serde_json::to_string(&entry)
                .map_err(|e| LoggingError::FileWrite(format!("Failed to serialize entry: {e}")))?;
            writeln!(file, "{json}")
                .map_err(|e| LoggingError::FileWrite(format!("Failed to write entry: {e}")))?;
        }

        info!("Exported {} log entries to {}", count, output_file);
        Ok(())
    }
}

/// Log statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogStatistics {
    pub total_entries: usize,
    pub level_counts: HashMap<String, usize>,
    pub target_counts: HashMap<String, usize>,
    pub oldest_entry: Option<String>,
    pub newest_entry: Option<String>,
}

/// Log rotation utility
pub struct LogRotator {
    log_file: String,
    max_size: u64,
    max_files: usize,
}

impl LogRotator {
    /// Create a new log rotator
    #[must_use]
    pub fn new(log_file: String, max_size: u64, max_files: usize) -> Self {
        Self {
            log_file,
            max_size,
            max_files,
        }
    }

    /// Check if log rotation is needed
    #[instrument(skip(self))]
    pub fn should_rotate(&self) -> bool {
        if let Ok(metadata) = std::fs::metadata(&self.log_file) {
            metadata.len() > self.max_size
        } else {
            false
        }
    }

    /// Perform log rotation
    ///
    /// # Errors
    /// Returns an error if file operations fail during rotation
    #[instrument(skip(self))]
    pub fn rotate(&self) -> Result<()> {
        if !self.should_rotate() {
            return Ok(());
        }

        info!("Rotating log file: {}", self.log_file);

        // Rotate existing files
        for i in (1..self.max_files).rev() {
            let old_file = format!("{}.{}", self.log_file, i);
            let new_file = format!("{}.{}", self.log_file, i + 1);

            if Path::new(&old_file).exists() {
                std::fs::rename(&old_file, &new_file)
                    .map_err(|e| LoggingError::FileWrite(format!("Failed to rotate file: {e}")))?;
            }
        }

        // Move current log to .1
        let rotated_file = format!("{}.1", self.log_file);
        std::fs::rename(&self.log_file, &rotated_file)
            .map_err(|e| LoggingError::FileWrite(format!("Failed to rotate current log: {e}")))?;

        // Create new log file
        File::create(&self.log_file)
            .map_err(|e| LoggingError::FileWrite(format!("Failed to create new log file: {e}")))?;

        info!("Log rotation completed");
        Ok(())
    }
}

/// Initialize structured logging with file output
///
/// # Errors
/// Returns an error if the log file cannot be opened or logging cannot be initialized
pub fn init_file_logging(log_file: &str, level: &str, json_format: bool) -> Result<()> {
    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(level));

    let file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(log_file)
        .map_err(|e| LoggingError::FileWrite(format!("Failed to open log file: {e}")))?;

    let registry = tracing_subscriber::registry().with(filter);

    if json_format {
        let json_layer = fmt::layer()
            .json()
            .with_writer(file)
            .with_current_span(true)
            .with_span_list(true)
            .with_target(true)
            .with_thread_ids(true)
            .with_thread_names(true)
            .with_file(true)
            .with_line_number(true);

        registry.with(json_layer).init();
    } else {
        let fmt_layer = fmt::layer()
            .with_writer(file)
            .with_target(true)
            .with_thread_ids(true)
            .with_thread_names(true)
            .with_file(true)
            .with_line_number(true)
            .with_span_events(FmtSpan::CLOSE);

        registry.with(fmt_layer).init();
    }

    info!("File logging initialized: {}", log_file);
    Ok(())
}

/// Log search utility
pub struct LogSearcher {
    aggregator: LogAggregator,
}

impl LogSearcher {
    /// Create a new log searcher
    #[must_use]
    pub fn new(aggregator: LogAggregator) -> Self {
        Self { aggregator }
    }

    /// Search logs by query
    #[instrument(skip(self))]
    pub fn search(&self, query: &str) -> Vec<LogEntry> {
        let filter = LogFilter {
            level: None,
            target: None,
            message_pattern: Some(query.to_string()),
            time_range: None,
            fields: HashMap::new(),
        };

        self.aggregator.filter_logs(&filter)
    }

    /// Search logs by level
    #[instrument(skip(self))]
    pub fn search_by_level(&self, level: &str) -> Vec<LogEntry> {
        let filter = LogFilter {
            level: Some(level.to_string()),
            target: None,
            message_pattern: None,
            time_range: None,
            fields: HashMap::new(),
        };

        self.aggregator.filter_logs(&filter)
    }

    /// Search logs by target
    #[instrument(skip(self))]
    pub fn search_by_target(&self, target: &str) -> Vec<LogEntry> {
        let filter = LogFilter {
            level: None,
            target: Some(target.to_string()),
            message_pattern: None,
            time_range: None,
            fields: HashMap::new(),
        };

        self.aggregator.filter_logs(&filter)
    }
}

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

    #[test]
    fn test_log_entry_creation() {
        let entry = LogEntry {
            timestamp: "2023-01-01T00:00:00Z".to_string(),
            level: "INFO".to_string(),
            target: "things3_cli".to_string(),
            message: "Test message".to_string(),
            fields: HashMap::new(),
            span_id: None,
            trace_id: None,
        };

        assert_eq!(entry.level, "INFO");
        assert_eq!(entry.message, "Test message");
    }

    #[test]
    fn test_log_entry_with_fields() {
        let mut fields = HashMap::new();
        fields.insert(
            "user_id".to_string(),
            serde_json::Value::String("123".to_string()),
        );
        fields.insert(
            "action".to_string(),
            serde_json::Value::String("login".to_string()),
        );

        let entry = LogEntry {
            timestamp: "2023-01-01T00:00:00Z".to_string(),
            level: "INFO".to_string(),
            target: "things3_cli".to_string(),
            message: "User logged in".to_string(),
            fields,
            span_id: Some("span-123".to_string()),
            trace_id: Some("trace-456".to_string()),
        };

        assert_eq!(entry.fields.len(), 2);
        assert_eq!(entry.span_id, Some("span-123".to_string()));
        assert_eq!(entry.trace_id, Some("trace-456".to_string()));
    }

    #[test]
    fn test_log_filter_creation() {
        let filter = LogFilter {
            level: Some("ERROR".to_string()),
            target: None,
            message_pattern: None,
            time_range: None,
            fields: HashMap::new(),
        };

        assert_eq!(filter.level, Some("ERROR".to_string()));
    }

    #[test]
    fn test_log_filter_with_all_fields() {
        let mut fields = HashMap::new();
        fields.insert(
            "module".to_string(),
            serde_json::Value::String("auth".to_string()),
        );

        let time_range = TimeRange {
            start: Some("2023-01-01T00:00:00Z".to_string()),
            end: Some("2023-01-01T23:59:59Z".to_string()),
        };

        let filter = LogFilter {
            level: Some("WARN".to_string()),
            target: Some("things3_cli::auth".to_string()),
            message_pattern: Some("failed".to_string()),
            time_range: Some(time_range),
            fields,
        };

        assert_eq!(filter.level, Some("WARN".to_string()));
        assert_eq!(filter.target, Some("things3_cli::auth".to_string()));
        assert_eq!(filter.message_pattern, Some("failed".to_string()));
        assert!(filter.time_range.is_some());
        assert_eq!(filter.fields.len(), 1);
    }

    #[test]
    fn test_time_range_creation() {
        let time_range = TimeRange {
            start: Some("2023-01-01T00:00:00Z".to_string()),
            end: Some("2023-01-01T23:59:59Z".to_string()),
        };

        assert_eq!(time_range.start, Some("2023-01-01T00:00:00Z".to_string()));
        assert_eq!(time_range.end, Some("2023-01-01T23:59:59Z".to_string()));
    }

    #[test]
    fn test_log_aggregator_creation() {
        let aggregator = LogAggregator::new("test.log".to_string(), 1000);
        assert_eq!(aggregator.max_entries, 1000);
        assert_eq!(aggregator.entries.len(), 0);
    }

    #[test]
    fn test_log_aggregator_entries_access() {
        let aggregator = LogAggregator::new("test.log".to_string(), 1000);
        assert_eq!(aggregator.entries.len(), 0);
    }

    #[test]
    fn test_log_aggregator_filter_logs() {
        let mut aggregator = LogAggregator::new("test.log".to_string(), 1000);

        // Manually add entries to test filtering
        let entry1 = LogEntry {
            timestamp: "2023-01-01T00:00:00Z".to_string(),
            level: "INFO".to_string(),
            target: "things3_cli".to_string(),
            message: "Info message".to_string(),
            fields: HashMap::new(),
            span_id: None,
            trace_id: None,
        };

        let entry2 = LogEntry {
            timestamp: "2023-01-01T00:00:01Z".to_string(),
            level: "ERROR".to_string(),
            target: "things3_cli".to_string(),
            message: "Error message".to_string(),
            fields: HashMap::new(),
            span_id: None,
            trace_id: None,
        };

        aggregator.entries.push(entry1);
        aggregator.entries.push(entry2);

        let filter = LogFilter {
            level: Some("ERROR".to_string()),
            target: None,
            message_pattern: None,
            time_range: None,
            fields: HashMap::new(),
        };

        let filtered = aggregator.filter_logs(&filter);
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].level, "ERROR");
    }

    #[test]
    fn test_log_aggregator_filter_by_message_pattern() {
        let mut aggregator = LogAggregator::new("test.log".to_string(), 1000);

        let entry1 = LogEntry {
            timestamp: "2023-01-01T00:00:00Z".to_string(),
            level: "INFO".to_string(),
            target: "things3_cli".to_string(),
            message: "User login successful".to_string(),
            fields: HashMap::new(),
            span_id: None,
            trace_id: None,
        };

        let entry2 = LogEntry {
            timestamp: "2023-01-01T00:00:01Z".to_string(),
            level: "INFO".to_string(),
            target: "things3_cli".to_string(),
            message: "Database connection failed".to_string(),
            fields: HashMap::new(),
            span_id: None,
            trace_id: None,
        };

        aggregator.entries.push(entry1);
        aggregator.entries.push(entry2);

        let filter = LogFilter {
            level: None,
            target: None,
            message_pattern: Some("failed".to_string()),
            time_range: None,
            fields: HashMap::new(),
        };

        let filtered = aggregator.filter_logs(&filter);
        assert_eq!(filtered.len(), 1);
        assert!(filtered[0].message.contains("failed"));
    }

    #[test]
    fn test_log_aggregator_get_statistics() {
        let mut aggregator = LogAggregator::new("test.log".to_string(), 1000);

        // Add entries with different levels
        for i in 0..5 {
            let level = if i % 2 == 0 { "INFO" } else { "ERROR" };
            let entry = LogEntry {
                timestamp: format!("2023-01-01T00:00:0{i}Z"),
                level: level.to_string(),
                target: "things3_cli".to_string(),
                message: format!("Message {i}"),
                fields: HashMap::new(),
                span_id: None,
                trace_id: None,
            };
            aggregator.entries.push(entry);
        }

        let stats = aggregator.get_statistics();
        assert_eq!(stats.total_entries, 5);
        assert_eq!(stats.level_counts.get("INFO"), Some(&3));
        assert_eq!(stats.level_counts.get("ERROR"), Some(&2));
    }

    #[test]
    fn test_log_rotator_creation() {
        let rotator = LogRotator::new("test.log".to_string(), 1024 * 1024, 5);
        assert_eq!(rotator.max_size, 1024 * 1024);
        assert_eq!(rotator.max_files, 5);
    }

    #[test]
    fn test_log_rotator_should_rotate() {
        let temp_dir = TempDir::new().unwrap();
        let log_file = temp_dir.path().join("test.log");
        let log_file_str = log_file.to_string_lossy().to_string();

        // Create a small test log file
        fs::write(&log_file, "small content").unwrap();

        let rotator = LogRotator::new(log_file_str.clone(), 100, 5);

        // Should not rotate for small files
        assert!(!rotator.should_rotate());

        // Create a large test log file
        let large_content = "x".repeat(200);
        fs::write(&log_file, large_content).unwrap();

        let rotator_large = LogRotator::new(log_file_str, 100, 5);

        // Should rotate for large files
        assert!(rotator_large.should_rotate());
    }

    #[test]
    fn test_log_rotator_rotate() {
        let temp_dir = TempDir::new().unwrap();
        let log_file = temp_dir.path().join("test.log");
        let log_file_str = log_file.to_string_lossy().to_string();

        // Create a large test log file
        let large_content = "x".repeat(200);
        fs::write(&log_file, large_content).unwrap();

        let rotator = LogRotator::new(log_file_str, 100, 5);

        // This should create a rotated file
        let result = rotator.rotate();
        assert!(result.is_ok());

        // Check that the original file was renamed
        let rotated_files: Vec<_> = fs::read_dir(temp_dir.path())
            .unwrap()
            .map(|entry| entry.unwrap().file_name())
            .collect();

        // Should have at least one rotated file
        assert!(!rotated_files.is_empty());
    }

    #[test]
    fn test_logging_error_display() {
        let error = LoggingError::FileRead("test error".to_string());
        assert!(error.to_string().contains("Failed to read log file"));
        assert!(error.to_string().contains("test error"));
    }

    #[test]
    fn test_logging_error_variants() {
        let file_read_error = LoggingError::FileRead("read error".to_string());
        let file_write_error = LoggingError::FileWrite("write error".to_string());
        let invalid_format_error = LoggingError::InvalidFormat("format error".to_string());
        let filter_compilation_error = LoggingError::FilterCompilation("filter error".to_string());

        assert!(matches!(file_read_error, LoggingError::FileRead(_)));
        assert!(matches!(file_write_error, LoggingError::FileWrite(_)));
        assert!(matches!(
            invalid_format_error,
            LoggingError::InvalidFormat(_)
        ));
        assert!(matches!(
            filter_compilation_error,
            LoggingError::FilterCompilation(_)
        ));
    }

    #[test]
    fn test_log_aggregator_load_logs_nonexistent_file() {
        let mut aggregator = LogAggregator::new("nonexistent.log".to_string(), 1000);
        let result = aggregator.load_logs();
        assert!(result.is_ok());
        assert_eq!(aggregator.entries.len(), 0);
    }

    #[test]
    fn test_log_aggregator_export_logs() {
        let temp_dir = TempDir::new().unwrap();
        let log_file = temp_dir.path().join("test.log");
        let log_file_str = log_file.to_string_lossy().to_string();
        let output_file = temp_dir.path().join("exported.log");
        let output_file_str = output_file.to_string_lossy().to_string();

        let mut aggregator = LogAggregator::new(log_file_str, 1000);

        let entry = LogEntry {
            timestamp: "2023-01-01T00:00:00Z".to_string(),
            level: "INFO".to_string(),
            target: "things3_cli".to_string(),
            message: "Test message".to_string(),
            fields: HashMap::new(),
            span_id: None,
            trace_id: None,
        };

        aggregator.entries.push(entry);

        let filter = LogFilter {
            level: None,
            target: None,
            message_pattern: None,
            time_range: None,
            fields: HashMap::new(),
        };

        let result = aggregator.export_logs(&filter, &output_file_str);
        assert!(result.is_ok());

        // Verify file was created
        assert!(output_file.exists());
    }
}