urlsup 2.4.0

CLI to validate URLs in files
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
// Command-line interface definitions and parsing for urlsup

use crate::config::CliConfig;
use crate::core::constants::{output_formats, timeouts};
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(
    author,
    version,
    about,
    long_about = None,
    help_template = "\
{before-help}{name} - CLI to validate URLs in files [version {version}]

{usage-heading} {usage}

Example:

	$ urlsup . --recursive --include md,txt

Commands:
  completion-generate                Generate shell completions
  completion-install                 Install shell completions to standard location
  config-wizard                      Run interactive configuration wizard
  help                               Print this message or the help of the given subcommand(s)

Core Options:
  -r, --recursive                    Recursively process directories. Will skip files/directories listed in .gitignore
  -t, --timeout <SECONDS>            Connection timeout in seconds (default: 5)
      --concurrency <COUNT>          Concurrent requests (default: CPU cores)

Filtering & Content:
      --include <EXTENSIONS>         File extensions to process (e.g., md,html,txt)
      --allowlist <URLS>             URLs to allow (comma-separated)
      --allow-status <CODES>         Status codes to allow (comma-separated)
      --exclude-pattern <REGEX>      URL patterns to exclude (regex)

Retry & Rate Limiting:
      --retry <COUNT>                Retry attempts for failed requests (default: 0)
      --retry-delay <MS>             Delay between retries in ms (default: 1000)
      --rate-limit <MS>              Delay between requests in ms (default: 0)
      --allow-timeout                Allow URLs that timeout
      --failure-threshold <PERCENT>  Fail only if more than X% URLs are broken (0-100)

Output & Verbosity:
  -q, --quiet                        Suppress progress output
  -v, --verbose                      Enable verbose logging
      --format <FORMAT>              Output format [default: text] [text|json|minimal]
      --no-progress                  Disable progress bars

Network & Security:
      --user-agent <AGENT>           Custom User-Agent header
      --proxy <URL>                  HTTP/HTTPS proxy URL
      --insecure                     Skip SSL certificate verification

Configuration:
      --config <FILE>                Use specific config file
      --no-config                    Ignore config files

Performance Analysis:
      --show-performance             Show memory usage and optimization suggestions
      --html-dashboard <PATH>        Generate HTML dashboard report

Options:
  -h, --help                         Print help
  -V, --version                      Print version
{after-help}
"
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,

    /// Files or directories to check
    pub files: Vec<String>,

    // Core Options
    /// Recursively process directories
    #[arg(short = 'r', long, help_heading = "Core Options")]
    pub recursive: bool,

    /// Connection timeout in seconds (default: 5)
    #[arg(
        short = 't',
        long,
        value_name = "SECONDS",
        help_heading = "Core Options"
    )]
    pub timeout: Option<u64>,

    /// Concurrent requests (default: CPU cores)
    #[arg(long, value_name = "COUNT", help_heading = "Core Options")]
    pub concurrency: Option<usize>,

    // Filtering & Content
    /// File extensions to process (e.g., md,html,txt)
    #[arg(long, value_name = "EXTENSIONS", help_heading = "Filtering & Content")]
    pub include: Option<String>,

    /// URLs to allow (comma-separated)
    #[arg(long, value_name = "URLS", help_heading = "Filtering & Content")]
    pub allowlist: Option<String>,

    /// Status codes to allow (comma-separated)
    #[arg(long, value_name = "CODES", help_heading = "Filtering & Content")]
    pub allow_status: Option<String>,

    /// URL patterns to exclude (regex)
    #[arg(long, value_name = "REGEX", help_heading = "Filtering & Content")]
    pub exclude_pattern: Vec<String>,

    // Retry & Rate Limiting
    /// Retry attempts for failed requests (default: 0)
    #[arg(long, value_name = "COUNT", help_heading = "Retry & Rate Limiting")]
    pub retry: Option<u8>,

    /// Delay between retries in ms (default: 1000)
    #[arg(long, value_name = "MS", help_heading = "Retry & Rate Limiting")]
    pub retry_delay: Option<u64>,

    /// Delay between requests in ms (default: 0)
    #[arg(long, value_name = "MS", help_heading = "Retry & Rate Limiting")]
    pub rate_limit: Option<u64>,

    /// Allow URLs that timeout
    #[arg(long, help_heading = "Retry & Rate Limiting")]
    pub allow_timeout: bool,

    /// Fail only if more than X% URLs are broken (0-100)
    #[arg(long, value_name = "PERCENT", help_heading = "Retry & Rate Limiting")]
    pub failure_threshold: Option<f64>,

    // Output & Verbosity
    /// Suppress progress output
    #[arg(short = 'q', long, help_heading = "Output & Verbosity")]
    pub quiet: bool,

    /// Enable verbose logging
    #[arg(short = 'v', long, help_heading = "Output & Verbosity")]
    pub verbose: bool,

    /// Output format
    #[arg(long, value_name = "FORMAT", value_parser = output_formats::ALL, default_value = output_formats::DEFAULT, help_heading = "Output & Verbosity")]
    pub format: String,

    /// Disable progress bars
    #[arg(long, help_heading = "Output & Verbosity")]
    pub no_progress: bool,

    // Network & Security
    /// Custom User-Agent header
    #[arg(long, value_name = "AGENT", help_heading = "Network & Security")]
    pub user_agent: Option<String>,

    /// HTTP/HTTPS proxy URL
    #[arg(long, value_name = "URL", help_heading = "Network & Security")]
    pub proxy: Option<String>,

    /// Skip SSL certificate verification
    #[arg(long, help_heading = "Network & Security")]
    pub insecure: bool,

    // Configuration
    /// Use specific config file
    #[arg(long, value_name = "FILE", help_heading = "Configuration")]
    pub config: Option<String>,

    /// Ignore config files
    #[arg(long, help_heading = "Configuration")]
    pub no_config: bool,

    // Performance Analysis
    /// Show memory usage and optimization suggestions
    #[arg(long, help_heading = "Performance Analysis")]
    pub show_performance: bool,

    /// Generate HTML dashboard report
    #[arg(long, value_name = "PATH", help_heading = "Performance Analysis")]
    pub html_dashboard: Option<String>,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Generate shell completions
    #[command(name = "completion-generate", arg_required_else_help = true)]
    CompletionGenerate {
        /// The shell to generate completions for
        #[arg(value_enum)]
        shell: clap_complete::Shell,
    },
    /// Install shell completions to standard location
    #[command(name = "completion-install", arg_required_else_help = true)]
    CompletionInstall {
        /// The shell to install completions for
        #[arg(value_enum)]
        shell: clap_complete::Shell,
    },
    /// Run interactive configuration wizard
    #[command(name = "config-wizard")]
    ConfigWizard,
}

/// Parse command line arguments into CliConfig structure
pub fn parse_cli_args(matches: &clap::ArgMatches) -> CliConfig {
    let mut cli_config = CliConfig::default();

    // Core options
    if let Some(timeout_str) = matches.get_one::<String>("timeout") {
        let timeout: u64 = timeout_str.parse().unwrap_or_else(|_| {
            eprintln!("Error: Timeout '{timeout_str}' is not a valid number. Expected a positive integer representing seconds.");
            std::process::exit(1);
        });
        if timeout == 0 {
            eprintln!(
                "Error: Timeout cannot be 0. Expected a positive integer representing seconds."
            );
            std::process::exit(1);
        }
        if timeout > timeouts::MAX_TIMEOUT_SECONDS {
            eprintln!(
                "Warning: Timeout of {timeout} seconds is quite large. Consider using a smaller value for better user experience."
            );
        }
        cli_config.timeout = Some(timeout);
    }

    // Filtering & inclusion
    if let Some(include_str) = matches.get_one::<String>("include") {
        cli_config.file_types = Some(
            include_str
                .split(',')
                .map(|s| s.trim().to_string())
                .collect(),
        );
    }

    if let Some(allowlist_str) = matches.get_one::<String>("allowlist") {
        cli_config.allowlist = Some(
            allowlist_str
                .split(',')
                .filter_map(|s| {
                    if s.trim().is_empty() {
                        None
                    } else {
                        Some(s.trim().to_string())
                    }
                })
                .collect(),
        );
    }

    if let Some(status_str) = matches.get_one::<String>("allow-status") {
        cli_config.allowed_status_codes = Some(
            status_str
                .split(',')
                .filter_map(|s| {
                    if s.trim().is_empty() {
                        None
                    } else {
                        s.trim()
                            .parse::<u16>()
                            .map_err(|_| {
                                eprintln!(
                                    "Error: Status code '{s}' is not a valid HTTP status code. Expected a number between 100-599."
                                );
                                std::process::exit(1);
                            })
                            .inspect(|&code| {
                                if !(100..=599).contains(&code) {
                                    eprintln!(
                                        "Error: Status code '{code}' is not a valid HTTP status code. Expected a number between 100-599."
                                    );
                                    std::process::exit(1);
                                }
                            })
                            .ok()
                    }
                })
                .collect(),
        );
    }

    if let Some(patterns) = matches.get_many::<String>("exclude-pattern") {
        cli_config.exclude_patterns = Some(patterns.cloned().collect());
    }

    // Performance & behavior
    if let Some(concurrency_str) = matches.get_one::<String>("concurrency") {
        let concurrency: usize = concurrency_str.parse().unwrap_or_else(|_| {
            eprintln!("Error: Concurrency '{concurrency_str}' is not a valid number. Expected a positive integer representing the number of concurrent requests.");
            std::process::exit(1);
        });
        if concurrency == 0 {
            eprintln!(
                "Error: Concurrency cannot be 0. Expected a positive integer representing the number of concurrent requests."
            );
            std::process::exit(1);
        }
        if concurrency > 100 {
            eprintln!(
                "Warning: Concurrency of {concurrency} is quite high and may overwhelm servers. Consider using a smaller value."
            );
        }
        cli_config.threads = Some(concurrency);
    }

    if let Some(retry_str) = matches.get_one::<String>("retry") {
        cli_config.retry_attempts = Some(retry_str.parse().unwrap_or_else(|_| {
            eprintln!("Error: Retry count '{retry_str}' is not a valid number. Expected a non-negative integer representing the number of retry attempts.");
            std::process::exit(1);
        }));
    }

    if let Some(retry_delay_str) = matches.get_one::<String>("retry-delay") {
        cli_config.retry_delay = Some(retry_delay_str.parse().unwrap_or_else(|_| {
            eprintln!("Error: Retry delay '{retry_delay_str}' is not a valid number. Expected a non-negative integer representing milliseconds.");
            std::process::exit(1);
        }));
    }

    if let Some(rate_limit_str) = matches.get_one::<String>("rate-limit") {
        cli_config.rate_limit_delay = Some(rate_limit_str.parse().unwrap_or_else(|_| {
            eprintln!("Error: Rate limit '{rate_limit_str}' is not a valid number. Expected a non-negative integer representing milliseconds between requests.");
            std::process::exit(1);
        }));
    }

    cli_config.allow_timeout = matches.get_flag("allow-timeout");

    // Parse failure threshold
    if let Some(threshold_str) = matches.get_one::<String>("failure-threshold") {
        let threshold: f64 = threshold_str.parse().unwrap_or_else(|_| {
            eprintln!("Error: Failure threshold '{threshold_str}' is not a valid number. Expected a value between 0-100.");
            std::process::exit(1);
        });
        if !(0.0..=100.0).contains(&threshold) {
            eprintln!(
                "Error: Failure threshold {threshold}% is invalid. Expected a value between 0-100."
            );
            std::process::exit(1);
        }
        cli_config.failure_threshold = Some(threshold);
    }

    // Output & format
    cli_config.quiet = matches.get_flag("quiet");
    cli_config.verbose = matches.get_flag("verbose");
    cli_config.no_progress = matches.get_flag("no-progress");

    if let Some(format_str) = matches.get_one::<String>("format") {
        cli_config.output_format = Some(format_str.clone());
    }

    // Network & security
    if let Some(user_agent_str) = matches.get_one::<String>("user-agent") {
        cli_config.user_agent = Some(user_agent_str.clone());
    }

    if let Some(proxy_str) = matches.get_one::<String>("proxy") {
        cli_config.proxy = Some(proxy_str.clone());
    }

    cli_config.skip_ssl_verification = matches.get_flag("insecure");

    // Configuration
    if let Some(config_file) = matches.get_one::<String>("config") {
        cli_config.config_file = Some(config_file.clone());
    }

    cli_config.no_config = matches.get_flag("no-config");

    // Performance Analysis
    cli_config.show_performance = matches.get_flag("show-performance");

    if let Some(dashboard_path) = matches.get_one::<String>("html-dashboard") {
        cli_config.html_dashboard_path = Some(dashboard_path.clone());
    }

    cli_config
}

/// Convert derive-based CLI arguments directly to CliConfig structure
pub fn cli_to_config(cli: &Cli) -> CliConfig {
    let mut cli_config = CliConfig::default();

    // Core options
    if let Some(timeout) = cli.timeout {
        if timeout == 0 {
            eprintln!(
                "Error: Timeout cannot be 0. Expected a positive integer representing seconds."
            );
            std::process::exit(1);
        }
        if timeout > timeouts::MAX_TIMEOUT_SECONDS {
            eprintln!(
                "Warning: Timeout of {timeout} seconds is quite large. Consider using a smaller value for better user experience."
            );
        }
        cli_config.timeout = Some(timeout);
    }

    // Filtering & inclusion
    if let Some(ref include_str) = cli.include {
        cli_config.file_types = Some(
            include_str
                .split(',')
                .map(|s| s.trim().to_string())
                .collect(),
        );
    }

    if let Some(ref allowlist_str) = cli.allowlist {
        cli_config.allowlist = Some(
            allowlist_str
                .split(',')
                .filter_map(|s| {
                    if s.trim().is_empty() {
                        None
                    } else {
                        Some(s.trim().to_string())
                    }
                })
                .collect(),
        );
    }

    if let Some(ref status_str) = cli.allow_status {
        cli_config.allowed_status_codes = Some(
            status_str
                .split(',')
                .filter_map(|s| {
                    if s.trim().is_empty() {
                        None
                    } else {
                        s.trim()
                            .parse::<u16>()
                            .map_err(|_| {
                                eprintln!(
                                    "Error: Status code '{s}' is not a valid HTTP status code. Expected a number between 100-599."
                                );
                                std::process::exit(1);
                            })
                            .inspect(|&code| {
                                if !(100..=599).contains(&code) {
                                    eprintln!(
                                        "Error: Status code '{code}' is not a valid HTTP status code. Expected a number between 100-599."
                                    );
                                    std::process::exit(1);
                                }
                            })
                            .ok()
                    }
                })
                .collect(),
        );
    }

    if !cli.exclude_pattern.is_empty() {
        cli_config.exclude_patterns = Some(cli.exclude_pattern.clone());
    }

    // Performance & behavior
    if let Some(concurrency) = cli.concurrency {
        if concurrency == 0 {
            eprintln!(
                "Error: Concurrency cannot be 0. Expected a positive integer representing the number of concurrent requests."
            );
            std::process::exit(1);
        }
        if concurrency > 100 {
            eprintln!(
                "Warning: Concurrency of {concurrency} is quite high and may overwhelm servers. Consider using a smaller value."
            );
        }
        cli_config.threads = Some(concurrency);
    }

    if let Some(retry) = cli.retry {
        cli_config.retry_attempts = Some(retry);
    }

    if let Some(retry_delay) = cli.retry_delay {
        cli_config.retry_delay = Some(retry_delay);
    }

    if let Some(rate_limit) = cli.rate_limit {
        cli_config.rate_limit_delay = Some(rate_limit);
    }

    cli_config.allow_timeout = cli.allow_timeout;

    // Parse failure threshold
    if let Some(threshold) = cli.failure_threshold {
        if !(0.0..=100.0).contains(&threshold) {
            eprintln!(
                "Error: Failure threshold {threshold}% is invalid. Expected a value between 0-100."
            );
            std::process::exit(1);
        }
        cli_config.failure_threshold = Some(threshold);
    }

    // Output & format
    cli_config.quiet = cli.quiet;
    cli_config.verbose = cli.verbose;
    cli_config.no_progress = cli.no_progress;
    cli_config.output_format = Some(cli.format.clone());

    // Network & security
    cli_config.user_agent = cli.user_agent.clone();
    cli_config.proxy = cli.proxy.clone();
    cli_config.skip_ssl_verification = cli.insecure;

    // Configuration
    cli_config.config_file = cli.config.clone();
    cli_config.no_config = cli.no_config;

    // Performance Analysis
    cli_config.show_performance = cli.show_performance;
    cli_config.html_dashboard_path = cli.html_dashboard.clone();

    cli_config
}

/// Validate CLI arguments using the derive-based CLI structure
pub fn validate_cli_args(cli: &Cli) {
    // Additional validation using the derive-based CLI
    if let Some(timeout) = cli.timeout {
        if timeout == 0 {
            eprintln!(
                "Error: Timeout cannot be 0. Expected a positive integer representing seconds."
            );
            std::process::exit(1);
        }
        if timeout > timeouts::MAX_TIMEOUT_SECONDS {
            eprintln!(
                "Warning: Timeout of {timeout} seconds is quite large. Consider using a smaller value for better user experience."
            );
        }
    }

    if let Some(concurrency) = cli.concurrency {
        if concurrency == 0 {
            eprintln!(
                "Error: Concurrency cannot be 0. Expected a positive integer representing the number of concurrent requests."
            );
            std::process::exit(1);
        }
        if concurrency > 100 {
            eprintln!(
                "Warning: Concurrency of {concurrency} is quite high and may overwhelm servers. Consider using a smaller value."
            );
        }
    }

    // Validate status codes
    if let Some(ref status_str) = cli.allow_status {
        for code_str in status_str.split(',') {
            if let Ok(code) = code_str.trim().parse::<u16>() {
                if !(100..=599).contains(&code) {
                    eprintln!(
                        "Error: Status code '{code}' is not a valid HTTP status code. Expected a number between 100-599."
                    );
                    std::process::exit(1);
                }
            } else if !code_str.trim().is_empty() {
                eprintln!(
                    "Error: Status code '{}' is not a valid number. Expected a number between 100-599.",
                    code_str.trim()
                );
                std::process::exit(1);
            }
        }
    }

    // Validate failure threshold
    if let Some(threshold) = cli.failure_threshold
        && !(0.0..=100.0).contains(&threshold)
    {
        eprintln!(
            "Error: Failure threshold {threshold}% is invalid. Expected a value between 0-100."
        );
        std::process::exit(1);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::constants::output_formats;

    fn create_default_cli() -> Cli {
        Cli {
            command: None,
            files: vec![],
            recursive: false,
            timeout: None,
            concurrency: None,
            include: None,
            allowlist: None,
            allow_status: None,
            exclude_pattern: vec![],
            retry: None,
            retry_delay: None,
            rate_limit: None,
            allow_timeout: false,
            failure_threshold: None,
            quiet: false,
            verbose: false,
            format: output_formats::DEFAULT.to_string(),
            no_progress: false,
            user_agent: None,
            proxy: None,
            insecure: false,
            config: None,
            no_config: false,
            show_performance: false,
            html_dashboard: None,
        }
    }

    #[test]
    fn test_cli_to_config_default() {
        let cli = create_default_cli();

        let config = cli_to_config(&cli);

        assert_eq!(config.timeout, None);
        assert_eq!(config.threads, None);
        assert_eq!(config.file_types, None);
        assert_eq!(config.allowlist, None);
        assert_eq!(config.allowed_status_codes, None);
        assert_eq!(config.exclude_patterns, None);
        assert_eq!(config.retry_attempts, None);
        assert_eq!(config.retry_delay, None);
        assert_eq!(config.rate_limit_delay, None);
        assert!(!config.allow_timeout);
        assert_eq!(config.failure_threshold, None);
        assert!(!config.quiet);
        assert!(!config.verbose);
        assert!(!config.no_progress);
        assert_eq!(
            config.output_format,
            Some(output_formats::DEFAULT.to_string())
        );
        assert_eq!(config.user_agent, None);
        assert_eq!(config.proxy, None);
        assert!(!config.skip_ssl_verification);
        assert_eq!(config.config_file, None);
        assert!(!config.no_config);
    }

    #[test]
    fn test_cli_to_config_all_options() {
        let mut cli = create_default_cli();
        cli.files = vec!["test.md".to_string()];
        cli.recursive = true;
        cli.timeout = Some(60);
        cli.concurrency = Some(8);
        cli.include = Some("md,txt".to_string());
        cli.allowlist = Some("example.com,google.com".to_string());
        cli.allow_status = Some("200,404".to_string());
        cli.exclude_pattern = vec![".*test.*".to_string(), ".*debug.*".to_string()];
        cli.retry = Some(3);
        cli.retry_delay = Some(2000);
        cli.rate_limit = Some(100);
        cli.allow_timeout = true;
        cli.failure_threshold = Some(10.5);
        cli.quiet = true;
        cli.verbose = true;
        cli.format = output_formats::JSON.to_string();
        cli.no_progress = true;
        cli.user_agent = Some("CustomAgent/1.0".to_string());
        cli.proxy = Some("http://proxy:8080".to_string());
        cli.insecure = true;
        cli.config = Some("config.toml".to_string());
        cli.no_config = true;

        let config = cli_to_config(&cli);

        assert_eq!(config.timeout, Some(60));
        assert_eq!(config.threads, Some(8));
        assert_eq!(
            config.file_types,
            Some(vec!["md".to_string(), "txt".to_string()])
        );
        assert_eq!(
            config.allowlist,
            Some(vec!["example.com".to_string(), "google.com".to_string()])
        );
        assert_eq!(config.allowed_status_codes, Some(vec![200, 404]));
        assert_eq!(
            config.exclude_patterns,
            Some(vec![".*test.*".to_string(), ".*debug.*".to_string()])
        );
        assert_eq!(config.retry_attempts, Some(3));
        assert_eq!(config.retry_delay, Some(2000));
        assert_eq!(config.rate_limit_delay, Some(100));
        assert!(config.allow_timeout);
        assert_eq!(config.failure_threshold, Some(10.5));
        assert!(config.quiet);
        assert!(config.verbose);
        assert!(config.no_progress);
        assert_eq!(config.output_format, Some(output_formats::JSON.to_string()));
        assert_eq!(config.user_agent, Some("CustomAgent/1.0".to_string()));
        assert_eq!(config.proxy, Some("http://proxy:8080".to_string()));
        assert!(config.skip_ssl_verification);
        assert_eq!(config.config_file, Some("config.toml".to_string()));
        assert!(config.no_config);
    }

    #[test]
    fn test_cli_to_config_empty_strings() {
        let mut cli = create_default_cli();
        cli.include = Some("".to_string());
        cli.allowlist = Some("".to_string());
        cli.allow_status = Some("".to_string());
        cli.format = output_formats::MINIMAL.to_string();
        cli.user_agent = Some("".to_string());
        cli.proxy = Some("".to_string());
        cli.config = Some("".to_string());

        let config = cli_to_config(&cli);

        assert_eq!(config.file_types, Some(vec!["".to_string()]));
        assert_eq!(config.allowlist, Some(vec![])); // Empty strings filtered out
        assert_eq!(config.allowed_status_codes, Some(vec![])); // Empty strings filtered out
        assert_eq!(
            config.output_format,
            Some(output_formats::MINIMAL.to_string())
        );
        assert_eq!(config.user_agent, Some("".to_string()));
        assert_eq!(config.proxy, Some("".to_string()));
        assert_eq!(config.config_file, Some("".to_string()));
    }

    #[test]
    fn test_cli_to_config_whitespace_trimming() {
        let mut cli = create_default_cli();
        cli.include = Some("  md  ,  txt  ".to_string());
        cli.allowlist = Some("  example.com  ,  google.com  ".to_string());
        cli.allow_status = Some("  200  ,  404  ".to_string());

        let config = cli_to_config(&cli);

        assert_eq!(
            config.file_types,
            Some(vec!["md".to_string(), "txt".to_string()])
        );
        assert_eq!(
            config.allowlist,
            Some(vec!["example.com".to_string(), "google.com".to_string()])
        );
        assert_eq!(config.allowed_status_codes, Some(vec![200, 404]));
    }

    #[test]
    fn test_cli_to_config_mixed_empty_values() {
        let mut cli = create_default_cli();
        cli.allowlist = Some("example.com, , google.com".to_string());
        cli.allow_status = Some("200, , 404".to_string());

        let config = cli_to_config(&cli);

        assert_eq!(
            config.allowlist,
            Some(vec!["example.com".to_string(), "google.com".to_string()])
        );
        assert_eq!(config.allowed_status_codes, Some(vec![200, 404]));
    }

    #[test]
    fn test_cli_to_config_boundary_values() {
        let mut cli = create_default_cli();
        cli.timeout = Some(1);
        cli.concurrency = Some(1);
        cli.allow_status = Some("100,599".to_string());
        cli.retry = Some(0);
        cli.retry_delay = Some(0);
        cli.rate_limit = Some(0);
        cli.failure_threshold = Some(0.0);

        let config = cli_to_config(&cli);

        assert_eq!(config.timeout, Some(1));
        assert_eq!(config.threads, Some(1));
        assert_eq!(config.allowed_status_codes, Some(vec![100, 599]));
        assert_eq!(config.retry_attempts, Some(0));
        assert_eq!(config.retry_delay, Some(0));
        assert_eq!(config.rate_limit_delay, Some(0));
        assert_eq!(config.failure_threshold, Some(0.0));
    }

    #[test]
    fn test_cli_to_config_edge_case_failure_threshold() {
        let mut cli = create_default_cli();
        cli.failure_threshold = Some(100.0);

        let config = cli_to_config(&cli);
        assert_eq!(config.failure_threshold, Some(100.0));
    }

    #[test]
    fn test_validate_cli_args_valid() {
        let mut cli = create_default_cli();
        cli.files = vec!["test.md".to_string()];
        cli.timeout = Some(5);
        cli.concurrency = Some(4);
        cli.allow_status = Some("200,404".to_string());
        cli.failure_threshold = Some(10.0);

        // Should not panic
        validate_cli_args(&cli);
    }

    #[test]
    fn test_validate_cli_args_high_timeout_warning() {
        let mut cli = create_default_cli();
        cli.files = vec!["test.md".to_string()];
        cli.timeout = Some(3700); // > MAX_TIMEOUT_SECONDS

        // Should not panic, just print warning
        validate_cli_args(&cli);
    }

    #[test]
    fn test_validate_cli_args_high_concurrency_warning() {
        let mut cli = create_default_cli();
        cli.files = vec!["test.md".to_string()];
        cli.concurrency = Some(150); // > 100

        // Should not panic, just print warning
        validate_cli_args(&cli);
    }

    #[test]
    fn test_validate_cli_args_valid_status_codes() {
        let mut cli = create_default_cli();
        cli.files = vec!["test.md".to_string()];
        cli.allow_status = Some("100,200,300,400,500,599".to_string());

        // Should not panic
        validate_cli_args(&cli);
    }

    #[test]
    fn test_validate_cli_args_empty_status_codes() {
        let mut cli = create_default_cli();
        cli.files = vec!["test.md".to_string()];
        cli.allow_status = Some("200, , 404".to_string());

        // Should not panic - empty status codes are ignored
        validate_cli_args(&cli);
    }

    #[test]
    fn test_validate_cli_args_valid_failure_threshold_boundaries() {
        let mut cli = create_default_cli();
        cli.files = vec!["test.md".to_string()];
        cli.failure_threshold = Some(0.0);

        // Should not panic
        validate_cli_args(&cli);

        let mut cli2 = create_default_cli();
        cli2.files = vec!["test.md".to_string()];
        cli2.failure_threshold = Some(100.0);

        // Should not panic
        validate_cli_args(&cli2);
    }

    #[test]
    fn test_parse_cli_args_string_parsing() {
        // Test individual parsing logic for include strings
        let include_str = "md,html,txt";
        let result: Vec<String> = include_str
            .split(',')
            .map(|s| s.trim().to_string())
            .collect();
        assert_eq!(
            result,
            vec!["md".to_string(), "html".to_string(), "txt".to_string()]
        );

        // Test allowlist parsing with empty entries
        let allowlist_str = "https://example.com,,https://test.com,";
        let result: Vec<String> = allowlist_str
            .split(',')
            .filter_map(|s| {
                if s.trim().is_empty() {
                    None
                } else {
                    Some(s.trim().to_string())
                }
            })
            .collect();
        assert_eq!(
            result,
            vec![
                "https://example.com".to_string(),
                "https://test.com".to_string()
            ]
        );

        // Test status code parsing with empty entries
        let status_str = "200,,301,302";
        let result: Vec<u16> = status_str
            .split(',')
            .filter_map(|s| {
                if s.trim().is_empty() {
                    None
                } else {
                    s.trim().parse::<u16>().ok()
                }
            })
            .collect();
        assert_eq!(result, vec![200, 301, 302]);
    }
}