freenet 0.2.46

Freenet core software
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
//! Diagnostic report generation and upload for debugging.
//!
//! Collects system info, logs, config, and optional problem description,
//! then uploads to the Freenet report server for debugging.

use anyhow::{Context, Result};
use chrono::{DateTime, Duration, Utc};
use clap::Args;
use flate2::Compression;
use flate2::write::GzEncoder;
use freenet::config::ConfigPaths;
use freenet::tracing::tracer::get_log_dir;
use freenet_stdlib::client_api::{
    ClientRequest, HostResponse, NodeDiagnosticsConfig, NodeQuery, QueryResponse, WebApi,
};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{self, BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration as StdDuration;
use tokio_tungstenite::connect_async;

const DEFAULT_REPORT_SERVER: &str = "https://nova.locut.us/api/reports";
/// Only include log entries from the last 30 minutes
const LOG_RETENTION_MINUTES: i64 = 30;
/// Maximum size for all logs combined in the report (2 MB)
const MAX_TOTAL_LOG_SIZE: usize = 2 * 1024 * 1024;
/// Maximum length for a single log line (10 KB) - longer lines are truncated
const MAX_LINE_LENGTH: usize = 10 * 1024;
/// Default WebSocket API port
const DEFAULT_WS_API_PORT: u16 = 7509;
/// Timeout for WebSocket connection and queries
const WS_TIMEOUT_SECS: u64 = 5;

#[derive(Args, Debug, Clone)]
pub struct ReportCommand {
    /// Save report locally instead of uploading
    #[arg(long, value_name = "PATH")]
    pub local: Option<PathBuf>,

    /// Problem description (skips interactive prompt)
    #[arg(long, short = 'm')]
    pub message: Option<String>,

    /// Skip problem description prompt
    #[arg(long)]
    pub no_message: bool,

    /// Override upload server URL
    #[arg(long, default_value = DEFAULT_REPORT_SERVER)]
    pub server: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DiagnosticReport {
    /// Client timestamp for clock skew detection
    pub client_timestamp: String,
    /// System information
    pub system_info: SystemInfo,
    /// Version and build info
    pub version_info: VersionInfo,
    /// Log file contents
    pub logs: LogContents,
    /// Config file contents (if available)
    pub config: Option<String>,
    /// Network status (if node is running)
    pub network_status: Option<String>,
    /// User's problem description
    pub user_message: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SystemInfo {
    pub os: String,
    pub arch: String,
    pub hostname: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VersionInfo {
    pub version: String,
    pub git_commit: String,
    pub git_dirty: bool,
    pub build_timestamp: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LogContents {
    pub main_log: Option<String>,
    pub error_log: Option<String>,
    /// Size of the filtered log content included in the report
    pub main_log_size_bytes: u64,
    /// Size of the filtered error log content included in the report
    pub error_log_size_bytes: u64,
    /// Original size of the main log file on disk
    #[serde(default)]
    pub main_log_original_size_bytes: u64,
    /// Original size of the error log file on disk
    #[serde(default)]
    pub error_log_original_size_bytes: u64,
}

#[derive(Deserialize, Debug)]
struct UploadResponse {
    code: String,
}

impl ReportCommand {
    pub fn run(
        &self,
        version: &str,
        git_commit: &str,
        git_dirty: &str,
        build_timestamp: &str,
        config_dirs: Arc<ConfigPaths>,
    ) -> Result<()> {
        println!("Collecting diagnostic info...");

        // Collect all diagnostic data
        let report = self.collect_report(
            version,
            git_commit,
            git_dirty,
            build_timestamp,
            &config_dirs,
        )?;

        // Print summary
        self.print_summary(&report);

        // Handle local save or upload
        if let Some(ref path) = self.local {
            self.save_local(&report, path)?;
        } else {
            let rt = tokio::runtime::Runtime::new()?;
            rt.block_on(self.upload_report(&report))?;
        }

        Ok(())
    }

    fn collect_report(
        &self,
        version: &str,
        git_commit: &str,
        git_dirty: &str,
        build_timestamp: &str,
        config_dirs: &ConfigPaths,
    ) -> Result<DiagnosticReport> {
        let system_info = SystemInfo {
            os: std::env::consts::OS.to_string(),
            arch: std::env::consts::ARCH.to_string(),
            hostname: hostname::get()
                .map(|h| h.to_string_lossy().to_string())
                .unwrap_or_else(|_| "unknown".to_string()),
        };

        let version_info = VersionInfo {
            version: version.to_string(),
            git_commit: git_commit.to_string(),
            git_dirty: git_dirty == " (dirty)",
            build_timestamp: build_timestamp.to_string(),
        };

        let logs = self.collect_logs(config_dirs.log_dir().map(Path::to_path_buf))?;
        let config = self.collect_config(&config_dirs.config_dir());
        let network_status = self.collect_network_status(&config);
        let user_message = self.get_user_message()?;

        let client_timestamp = chrono::Utc::now().to_rfc3339();

        Ok(DiagnosticReport {
            client_timestamp,
            system_info,
            version_info,
            logs,
            config,
            network_status,
            user_message,
        })
    }

    fn collect_logs(&self, log_dir: Option<PathBuf>) -> Result<LogContents> {
        let log_dir = log_dir
            .or_else(get_log_dir)
            .context("Unsupported platform for log collection")?;

        // Find log files - support both legacy names and rolling log patterns
        let main_log_files = find_log_files(&log_dir, "freenet");
        let error_log_files = find_log_files(&log_dir, "freenet.error");

        let (main_log, main_log_original_size) = read_and_merge_log_files(&main_log_files);
        let (error_log, error_log_original_size) = read_and_merge_log_files(&error_log_files);

        // Calculate filtered content sizes
        let main_log_size = main_log.as_ref().map(|s| s.len() as u64).unwrap_or(0);
        let error_log_size = error_log.as_ref().map(|s| s.len() as u64).unwrap_or(0);

        Ok(LogContents {
            main_log,
            error_log,
            main_log_size_bytes: main_log_size,
            error_log_size_bytes: error_log_size,
            main_log_original_size_bytes: main_log_original_size,
            error_log_original_size_bytes: error_log_original_size,
        })
    }

    fn collect_config(&self, config_dir: &Path) -> Option<String> {
        // Try standard config locations
        let config_paths = [
            Some(config_dir.join("config.toml")),
            dirs::config_dir().map(|p| p.join("freenet").join("config.toml")),
            dirs::home_dir().map(|p| p.join(".config").join("freenet").join("config.toml")),
        ];

        for path in config_paths.into_iter().flatten() {
            if path.exists() {
                if let Ok(content) = fs::read_to_string(&path) {
                    return Some(content);
                }
            }
        }

        None
    }

    fn collect_network_status(&self, config_content: &Option<String>) -> Option<String> {
        // Try to query the local node's WebSocket API
        // This is optional - if the node isn't running, we just skip this

        // Parse the WebSocket port from config, or use default
        let ws_port = config_content
            .as_ref()
            .and_then(|c| parse_ws_port_from_config(c))
            .unwrap_or(DEFAULT_WS_API_PORT);

        // Create a runtime for the async WebSocket query
        let rt = match tokio::runtime::Runtime::new() {
            Ok(rt) => rt,
            Err(_) => return None,
        };

        rt.block_on(async {
            match tokio::time::timeout(
                StdDuration::from_secs(WS_TIMEOUT_SECS),
                query_node_diagnostics(ws_port),
            )
            .await
            {
                Ok(Ok(diagnostics)) => Some(diagnostics),
                Ok(Err(_)) | Err(_) => None,
            }
        })
    }

    fn get_user_message(&self) -> Result<Option<String>> {
        // Check for --message flag
        if let Some(ref msg) = self.message {
            return Ok(Some(msg.clone()));
        }

        // Check for --no-message flag
        if self.no_message {
            return Ok(None);
        }

        // Interactive prompt
        println!();
        println!(
            "What issue are you experiencing? (Enter on empty line to finish, or just Enter to skip)"
        );
        print!("> ");
        io::stdout().flush()?;

        let stdin = io::stdin();
        let mut lines = Vec::new();

        for line in stdin.lock().lines() {
            let line = line.context("Failed to read input")?;

            if line.is_empty() {
                // Empty line = done
                break;
            }

            lines.push(line);
            print!("> ");
            io::stdout().flush()?;
        }

        if lines.is_empty() {
            Ok(None)
        } else {
            Ok(Some(lines.join("\n")))
        }
    }

    fn print_summary(&self, report: &DiagnosticReport) {
        println!(
            "  - Version: {} ({}{})",
            report.version_info.version,
            report.version_info.git_commit,
            if report.version_info.git_dirty {
                " dirty"
            } else {
                ""
            }
        );
        println!(
            "  - OS: {} {}",
            report.system_info.os, report.system_info.arch
        );

        let filtered_size = report.logs.main_log_size_bytes + report.logs.error_log_size_bytes;
        let original_size =
            report.logs.main_log_original_size_bytes + report.logs.error_log_original_size_bytes;
        if original_size > filtered_size && filtered_size > 0 {
            println!(
                "  - Logs: {} (last {} min, {} total on disk)",
                format_bytes(filtered_size),
                LOG_RETENTION_MINUTES,
                format_bytes(original_size)
            );
        } else {
            println!("  - Logs: {}", format_bytes(original_size));
        }

        println!(
            "  - Config: {}",
            if report.config.is_some() {
                "found"
            } else {
                "not found"
            }
        );
        println!(
            "  - Node status: {}",
            if report.network_status.is_some() {
                "running"
            } else {
                "not running or unreachable"
            }
        );
    }

    fn save_local(&self, report: &DiagnosticReport, path: &PathBuf) -> Result<()> {
        let json = serde_json::to_string_pretty(report)?;
        fs::write(path, &json).context("Failed to write report to file")?;
        println!();
        println!("Report saved to: {}", path.display());
        Ok(())
    }

    async fn upload_report(&self, report: &DiagnosticReport) -> Result<()> {
        println!();
        print!("Uploading report...");
        io::stdout().flush()?;

        // Serialize to JSON
        let json = serde_json::to_vec(report)?;

        // Gzip compress
        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(&json)?;
        let compressed = encoder.finish()?;

        // Upload
        let client = reqwest::Client::builder()
            .user_agent("freenet-report")
            .build()?;

        let response = client
            .post(&self.server)
            .header("Content-Type", "application/json")
            .header("Content-Encoding", "gzip")
            .body(compressed)
            .send()
            .await
            .context("Failed to upload report")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            anyhow::bail!("Upload failed: {} - {}", status, body);
        }

        let upload_response: UploadResponse = response
            .json()
            .await
            .context("Failed to parse upload response")?;

        println!(" done");
        println!();
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        println!("  Report code: {}", upload_response.code);
        println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        println!();
        println!("Share this code with the Freenet team on Matrix.");

        Ok(())
    }
}

/// Find log files matching the given prefix.
/// Supports both legacy format (freenet.log) and rolling format (freenet.YYYY-MM-DD.log).
/// Returns files sorted by modification time (newest first).
fn find_log_files(log_dir: &PathBuf, prefix: &str) -> Vec<PathBuf> {
    let mut files = Vec::new();

    // Check for legacy file first
    let legacy_path = log_dir.join(format!("{}.log", prefix));
    if legacy_path.exists() {
        files.push(legacy_path);
    }

    // Look for rolling log files (freenet.YYYY-MM-DD.log or freenet.YYYY-MM-DD-HH.log pattern)
    if let Ok(entries) = fs::read_dir(log_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                // Match pattern: prefix.YYYY-MM-DD.log (daily) or prefix.YYYY-MM-DD-HH.log (hourly)
                if name.starts_with(prefix)
                    && name.ends_with(".log")
                    && name.len() > prefix.len() + 5
                {
                    // Check if it has a date pattern (daily: 11 chars, hourly: 14 chars)
                    let middle = &name[prefix.len()..name.len() - 4];
                    if middle.starts_with('.') && (middle.len() == 11 || middle.len() == 14) {
                        // .YYYY-MM-DD or .YYYY-MM-DD-HH
                        files.push(path);
                    }
                }
            }
        }
    }

    // Sort by modification time, newest first
    files.sort_by(|a, b| {
        let a_time = fs::metadata(a).and_then(|m| m.modified()).ok();
        let b_time = fs::metadata(b).and_then(|m| m.modified()).ok();
        b_time.cmp(&a_time)
    });

    files
}

/// Read and merge multiple log files, filtering to the last 30 minutes.
/// Returns (merged_content, total_original_size).
/// Applies MAX_TOTAL_LOG_SIZE limit, keeping most recent entries if exceeded.
fn read_and_merge_log_files(files: &[PathBuf]) -> (Option<String>, u64) {
    if files.is_empty() {
        return (None, 0);
    }

    let mut total_original_size = 0u64;
    let mut all_content = Vec::new();

    // Process files in reverse order (oldest first) so merged string is chronological.
    // This way, most recent entries are at the end, and truncating from the beginning
    // preserves the most recent (most relevant) logs.
    for file in files.iter().rev() {
        let (content, size) = read_log_file(file);
        total_original_size += size;
        if let Some(content) = content {
            all_content.push(content);
        }
    }

    if all_content.is_empty() {
        return (None, total_original_size);
    }

    let merged = all_content.join("\n");

    // If total size exceeds limit, truncate from beginning to keep most recent logs
    let result = if merged.len() > MAX_TOTAL_LOG_SIZE {
        let skip_bytes = merged.len() - MAX_TOTAL_LOG_SIZE;
        // Find a safe UTF-8 boundary, then find the next newline to avoid cutting mid-line
        let safe_skip = merged
            .char_indices()
            .take_while(|(i, _)| *i <= skip_bytes)
            .last()
            .map(|(i, _)| i)
            .unwrap_or(0);
        let truncate_at = merged[safe_skip..]
            .find('\n')
            .map(|pos| safe_skip + pos + 1)
            .unwrap_or(safe_skip);
        format!(
            "[... {} bytes truncated to fit size limit ...]\n{}",
            truncate_at,
            &merged[truncate_at..]
        )
    } else {
        merged
    };

    (Some(result), total_original_size)
}

/// Read log file, filtering to entries from the last 30 minutes.
/// Returns (filtered_content, original_file_size).
///
/// If the file contains no parseable timestamps (e.g., panic backtraces,
/// stderr output), the entire file is included so that crash diagnostics
/// are never silently discarded.
fn read_log_file(path: &PathBuf) -> (Option<String>, u64) {
    let metadata = match fs::metadata(path) {
        Ok(m) => m,
        Err(_) => return (None, 0),
    };
    let original_size = metadata.len();

    let file = match fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return (None, original_size),
    };

    let cutoff = Utc::now() - Duration::minutes(LOG_RETENTION_MINUTES);

    let reader = BufReader::new(file);
    let mut filtered_lines = Vec::new();
    let mut all_lines = Vec::new();
    let mut include_line = false;
    let mut any_timestamp_found = false;

    for line in reader.lines() {
        let line = match line {
            Ok(l) => l,
            Err(_) => continue,
        };

        // Try to extract timestamp from this line
        // Log format: optional ANSI codes, then ISO 8601 timestamp like 2025-12-26T17:28:28.636476Z
        if let Some(ts) = extract_timestamp(&line) {
            if let Ok(parsed) = DateTime::parse_from_rfc3339(&ts)
                .map(|dt| dt.with_timezone(&Utc))
                .or_else(|_| ts.parse::<DateTime<Utc>>())
            {
                any_timestamp_found = true;
                include_line = parsed >= cutoff;
            }
        }

        // Truncate very long lines (e.g., delegates logging full state as byte arrays)
        let line = if line.len() > MAX_LINE_LENGTH {
            let truncate_at = line
                .char_indices()
                .take_while(|(i, _)| *i < MAX_LINE_LENGTH)
                .last()
                .map(|(i, c)| i + c.len_utf8())
                .unwrap_or(0);
            format!(
                "{}... [truncated, {} total bytes]",
                &line[..truncate_at],
                line.len()
            )
        } else {
            line
        };

        // Track all lines in case we need to fall back to unfiltered content
        all_lines.push(line.clone());

        // Include this line if we're within the time window
        // (lines without timestamps inherit the state from the previous timestamped line)
        if include_line {
            filtered_lines.push(line);
        }
    }

    // If no timestamps were found, include all lines — the file likely contains
    // panic backtraces or other non-timestamped output that is critical for debugging.
    let result_lines = if !any_timestamp_found && !all_lines.is_empty() {
        all_lines
    } else {
        filtered_lines
    };

    if result_lines.is_empty() {
        (None, original_size)
    } else {
        (Some(result_lines.join("\n")), original_size)
    }
}

/// Extract ISO 8601 timestamp from a log line.
/// Handles ANSI escape codes that may surround the timestamp.
fn extract_timestamp(line: &str) -> Option<String> {
    // Skip any leading ANSI escape sequences
    let mut chars = line.chars().peekable();
    while chars.peek() == Some(&'\x1b') {
        // Skip escape sequence: ESC [ ... m
        chars.next(); // ESC
        if chars.next() != Some('[') {
            break;
        }
        for c in chars.by_ref() {
            if c == 'm' {
                break;
            }
        }
    }

    // Collect remaining string and look for timestamp pattern
    let remaining: String = chars.collect();

    // Look for YYYY-MM-DDTHH:MM:SS pattern
    if remaining.len() < 19 {
        return None;
    }

    // Check if it starts with a valid timestamp format
    let potential = &remaining[..std::cmp::min(30, remaining.len())];
    if potential.len() >= 19
        && potential.chars().nth(4) == Some('-')
        && potential.chars().nth(7) == Some('-')
        && potential.chars().nth(10) == Some('T')
        && potential.chars().nth(13) == Some(':')
        && potential.chars().nth(16) == Some(':')
    {
        // Find the end of the timestamp (up to Z or space or ANSI escape)
        let end = potential.find([' ', '\x1b']).unwrap_or(potential.len());
        let ts = &potential[..end];
        // Ensure it ends with Z for RFC3339 compatibility
        if ts.ends_with('Z') {
            return Some(ts.to_string());
        } else {
            // Add Z if missing (some formats omit it)
            return Some(format!("{}Z", ts));
        }
    }

    None
}

/// Parse the WebSocket API port from config TOML content.
fn parse_ws_port_from_config(config: &str) -> Option<u16> {
    // Look for [ws_api] section with port
    // Format: [ws_api]\n...\nws-api-port = 7509
    // or just: ws-api-port = 7509
    for line in config.lines() {
        let line = line.trim();
        if line.starts_with("ws-api-port") || line.starts_with("ws_api_port") {
            if let Some(value) = line.split('=').nth(1) {
                if let Ok(port) = value.trim().parse::<u16>() {
                    return Some(port);
                }
            }
        }
    }
    None
}

/// Query the node for diagnostics via WebSocket API.
async fn query_node_diagnostics(port: u16) -> Result<String> {
    let url = format!("ws://127.0.0.1:{port}/v1/contract/command?encodingProtocol=native");

    let (stream, _) = connect_async(&url)
        .await
        .context("Failed to connect to node WebSocket API")?;

    let mut client = WebApi::start(stream);

    // Query for full node diagnostics
    let config = NodeDiagnosticsConfig {
        include_node_info: true,
        include_network_info: true,
        include_subscriptions: true,
        contract_keys: vec![],
        include_system_metrics: true,
        include_detailed_peer_info: true,
        include_subscriber_peer_ids: false,
    };

    client
        .send(ClientRequest::NodeQueries(NodeQuery::NodeDiagnostics {
            config,
        }))
        .await
        .context("Failed to send diagnostics query")?;

    let response = client
        .recv()
        .await
        .context("Failed to receive diagnostics response")?;

    // Close connection gracefully; ignore errors since we're done
    let _disconnect = client.send(ClientRequest::Disconnect { cause: None }).await;

    match response {
        HostResponse::QueryResponse(QueryResponse::NodeDiagnostics(diag)) => {
            // Serialize the diagnostics to JSON for the report
            serde_json::to_string_pretty(&diag).context("Failed to serialize diagnostics")
        }
        HostResponse::ContractResponse(_)
        | HostResponse::DelegateResponse { .. }
        | HostResponse::QueryResponse(_)
        | HostResponse::Ok
        | _ => anyhow::bail!("Unexpected response from node"),
    }
}

fn format_bytes(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;

    if bytes >= MB {
        format!("{:.1} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.1} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} bytes", bytes)
    }
}

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

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(0), "0 bytes");
        assert_eq!(format_bytes(500), "500 bytes");
        assert_eq!(format_bytes(1024), "1.0 KB");
        assert_eq!(format_bytes(1536), "1.5 KB");
        assert_eq!(format_bytes(1048576), "1.0 MB");
        assert_eq!(format_bytes(1572864), "1.5 MB");
    }

    #[test]
    fn test_system_info() {
        let info = SystemInfo {
            os: std::env::consts::OS.to_string(),
            arch: std::env::consts::ARCH.to_string(),
            hostname: "test".to_string(),
        };
        assert!(!info.os.is_empty());
        assert!(!info.arch.is_empty());
    }

    #[test]
    fn test_report_serialization() {
        let report = DiagnosticReport {
            client_timestamp: "2025-01-01T00:00:00Z".to_string(),
            system_info: SystemInfo {
                os: "linux".to_string(),
                arch: "x86_64".to_string(),
                hostname: "test".to_string(),
            },
            version_info: VersionInfo {
                version: "0.1.0".to_string(),
                git_commit: "abc123".to_string(),
                git_dirty: false,
                build_timestamp: "2025-01-01".to_string(),
            },
            logs: LogContents {
                main_log: Some("test log".to_string()),
                error_log: None,
                main_log_size_bytes: 8,
                error_log_size_bytes: 0,
                main_log_original_size_bytes: 100,
                error_log_original_size_bytes: 0,
            },
            config: None,
            network_status: None,
            user_message: Some("Test message".to_string()),
        };

        let json = serde_json::to_string(&report).unwrap();
        assert!(json.contains("linux"));
        assert!(json.contains("test log"));
    }

    #[test]
    fn test_extract_timestamp_plain() {
        // Plain timestamp without ANSI codes
        let line = "2025-12-26T17:28:28.636476Z INFO freenet: Starting";
        assert_eq!(
            extract_timestamp(line),
            Some("2025-12-26T17:28:28.636476Z".to_string())
        );
    }

    #[test]
    fn test_extract_timestamp_with_ansi() {
        // Timestamp surrounded by ANSI escape codes (as in actual logs)
        let line = "\x1b[2m2025-12-26T17:28:28.636476Z\x1b[0m \x1b[32m INFO\x1b[0m freenet";
        assert_eq!(
            extract_timestamp(line),
            Some("2025-12-26T17:28:28.636476Z".to_string())
        );
    }

    #[test]
    fn test_extract_timestamp_no_timestamp() {
        // Line without timestamp
        let line = "    at crates/core/src/bin/freenet.rs:136";
        assert_eq!(extract_timestamp(line), None);
    }

    #[test]
    fn test_extract_timestamp_adds_z_if_missing() {
        // Timestamp without trailing Z
        let line = "2025-12-26T17:28:28.636476 INFO";
        let result = extract_timestamp(line);
        assert!(result.is_some());
        assert!(result.unwrap().ends_with('Z'));
    }

    #[test]
    fn test_parse_ws_port_from_config() {
        // Test with ws-api-port
        let config = r#"
mode = "network"
[ws_api]
ws-api-port = 8080
"#;
        assert_eq!(parse_ws_port_from_config(config), Some(8080));

        // Test with underscore variant
        let config = "ws_api_port = 9000";
        assert_eq!(parse_ws_port_from_config(config), Some(9000));

        // Test with no port
        let config = "mode = \"network\"";
        assert_eq!(parse_ws_port_from_config(config), None);
    }

    #[test]
    fn test_find_log_files_patterns() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let log_dir = temp_dir.path().to_path_buf();

        // Create test files matching different log patterns
        fs::write(log_dir.join("freenet.log"), "legacy").unwrap();
        fs::write(log_dir.join("freenet.2025-12-26.log"), "daily").unwrap();
        fs::write(log_dir.join("freenet.2025-12-26-14.log"), "hourly").unwrap();
        fs::write(log_dir.join("freenet.error.log"), "error legacy").unwrap();
        fs::write(
            log_dir.join("freenet.error.2025-12-26-14.log"),
            "error hourly",
        )
        .unwrap();
        fs::write(log_dir.join("other.log"), "unrelated").unwrap();

        // Test finding main log files
        let main_files = find_log_files(&log_dir, "freenet");
        let main_names: Vec<_> = main_files
            .iter()
            .filter_map(|p| p.file_name())
            .filter_map(|n| n.to_str())
            .collect();

        assert!(
            main_names.contains(&"freenet.log"),
            "Should find legacy format"
        );
        assert!(
            main_names.contains(&"freenet.2025-12-26.log"),
            "Should find daily format"
        );
        assert!(
            main_names.contains(&"freenet.2025-12-26-14.log"),
            "Should find hourly format"
        );
        assert!(
            !main_names.iter().any(|n| n.contains("error")),
            "Should not match error logs with 'freenet' prefix"
        );
        assert!(
            !main_names.contains(&"other.log"),
            "Should not match unrelated files"
        );

        // Test finding error log files
        let error_files = find_log_files(&log_dir, "freenet.error");
        let error_names: Vec<_> = error_files
            .iter()
            .filter_map(|p| p.file_name())
            .filter_map(|n| n.to_str())
            .collect();

        assert!(
            error_names.contains(&"freenet.error.log"),
            "Should find error legacy format"
        );
        assert!(
            error_names.contains(&"freenet.error.2025-12-26-14.log"),
            "Should find error hourly format"
        );
    }

    #[test]
    fn test_read_log_file_no_timestamps_includes_all_content() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let log_path = temp_dir.path().join("freenet.error.log");

        // Simulate panic backtrace output (no timestamps)
        let panic_content = "\
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: AddrInUse', src/main.rs:42
stack backtrace:
   0: std::panicking::begin_panic_handler
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: freenet::main";
        fs::write(&log_path, panic_content).unwrap();

        let (content, original_size) = read_log_file(&log_path);

        assert!(
            content.is_some(),
            "Files with no timestamps should still be included"
        );
        let content = content.unwrap();
        assert!(
            content.contains("panicked"),
            "Panic message should be preserved"
        );
        assert!(
            content.contains("stack backtrace"),
            "Backtrace should be preserved"
        );
        assert_eq!(original_size, panic_content.len() as u64);
    }

    #[test]
    fn test_read_log_file_old_timestamps_excluded() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let log_path = temp_dir.path().join("freenet.log");

        // All timestamps are old (> 30 minutes ago) — should be filtered out
        let old_content = "\
2020-01-01T00:00:00.000000Z  INFO freenet: Old log entry 1
2020-01-01T00:00:01.000000Z  INFO freenet: Old log entry 2";
        fs::write(&log_path, old_content).unwrap();

        let (content, original_size) = read_log_file(&log_path);

        assert!(
            content.is_none(),
            "Old timestamped entries should be filtered out"
        );
        assert_eq!(original_size, old_content.len() as u64);
    }
}