urlsup 2.3.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
use crate::config::Config;
use log::{debug, error, info, warn};
use std::path::Path;

/// Initialize the logger with appropriate level based on verbosity
pub fn init_logger(verbose: bool, quiet: bool) {
    let level = if quiet {
        log::LevelFilter::Off
    } else if verbose {
        log::LevelFilter::Debug
    } else {
        log::LevelFilter::Off // Only show structured logs in verbose mode
    };

    // Use try_init() to avoid panicking if logger is already initialized (e.g., in tests)
    let _ = env_logger::Builder::from_default_env()
        .filter_level(level)
        .format_timestamp(None)
        .format_module_path(false)
        .format_target(false)
        .try_init();

    debug!("Logger initialized with level: {level:?}");
}

/// Log configuration information
pub fn log_config_info(config: &Config, actual_threads: usize) {
    let timeout = config.timeout.unwrap_or(30);
    let allow_timeout = config.allow_timeout.unwrap_or(false);
    let retry_attempts = config.retry_attempts.unwrap_or(0);
    let retry_delay = config.retry_delay.unwrap_or(1000);
    let rate_limit_delay = config.rate_limit_delay.unwrap_or(0);
    let use_head_requests = config.use_head_requests.unwrap_or(false);
    let skip_ssl_verification = config.skip_ssl_verification.unwrap_or(false);

    info!(
        "Configuration: threads={actual_threads}, timeout={timeout}s, allow_timeout={allow_timeout}"
    );
    info!("Retry: attempts={retry_attempts}, delay={retry_delay}ms");
    info!("Rate limiting: delay={rate_limit_delay}ms");
    info!("HTTP: head_requests={use_head_requests}, skip_ssl={skip_ssl_verification}");
}

/// Log file processing information
pub fn log_file_info<P: AsRef<Path>>(file_count: usize, files: &[P]) {
    info!("Processing {file_count} file(s)");
    for (i, file) in files.iter().enumerate() {
        debug!("  {}. {}", i + 1, file.as_ref().display());
    }
}

/// Log URL discovery information
pub fn log_url_discovery(unique_urls: usize, total_found: usize) {
    info!("Found {unique_urls} unique URLs (from {total_found} total)");
}

/// Log validation progress
pub fn log_validation_start(url_count: usize) {
    info!("Starting validation of {url_count} URLs");
}

/// Log validation completion
pub fn log_validation_complete(url_count: usize, issues: usize, duration_ms: u128) {
    if issues == 0 {
        info!(
            "✅ Validation complete: {}/{} URLs valid ({}ms)",
            url_count - issues,
            url_count,
            duration_ms
        );
    } else {
        warn!(
            "❌ Validation complete: {}/{} URLs valid, {} issues found ({}ms)",
            url_count - issues,
            url_count,
            issues,
            duration_ms
        );
    }
}

/// Log individual URL validation results for debugging
pub fn log_url_result(url: &str, status: Option<u16>, description: Option<&str>) {
    match (status, description) {
        (Some(status), None) => debug!("✓ {url} -> {status}"),
        (Some(status), Some(desc)) => debug!("✗ {url} -> {status} ({desc})"),
        (None, Some(desc)) => debug!("✗ {url} -> {desc}"),
        (None, None) => debug!("? {url} -> unknown"),
    }
}

/// Log error information
pub fn log_error(message: &str, source: Option<&dyn std::error::Error>) {
    match source {
        Some(err) => error!("{message}: {err}"),
        None => error!("{message}"),
    }
}

/// Log warning information
pub fn log_warning(message: &str) {
    warn!("{message}");
}

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

    #[test]
    fn test_logger_initialization_verbose() {
        // Test that verbose mode initialization works correctly
        init_logger(true, false);
        // Test passes if we reach this point without errors
    }

    #[test]
    fn test_logger_initialization_quiet() {
        // Test that quiet mode initialization works correctly
        init_logger(false, true);
        // Test passes if we reach this point without errors
    }

    #[test]
    fn test_logger_initialization_normal() {
        // Test that normal mode initialization works correctly
        init_logger(false, false);
        // Test passes if we reach this point without errors
    }

    #[test]
    fn test_logger_initialization_conflicting() {
        // Test that conflicting flags work correctly (quiet takes precedence)
        init_logger(true, true);
        // Test passes if we reach this point without errors
    }

    #[test]
    fn test_log_config_info_all_params() {
        // Test with various parameter combinations
        let config1 = Config {
            timeout: Some(30),
            allow_timeout: Some(false),
            retry_attempts: Some(3),
            retry_delay: Some(1000),
            rate_limit_delay: Some(100),
            use_head_requests: Some(false),
            skip_ssl_verification: Some(false),
            ..Default::default()
        };
        log_config_info(&config1, 4);

        let config2 = Config {
            timeout: Some(60),
            allow_timeout: Some(true),
            retry_attempts: Some(5),
            retry_delay: Some(2000),
            rate_limit_delay: Some(200),
            use_head_requests: Some(true),
            skip_ssl_verification: Some(true),
            ..Default::default()
        };
        log_config_info(&config2, 8);

        let config3 = Config {
            timeout: Some(10),
            allow_timeout: Some(false),
            retry_attempts: Some(0),
            retry_delay: Some(0),
            rate_limit_delay: Some(0),
            use_head_requests: Some(false),
            skip_ssl_verification: Some(false),
            ..Default::default()
        };
        log_config_info(&config3, 1);
    }

    #[test]
    fn test_log_config_info_edge_cases() {
        // Test with edge case values
        let config1 = Config {
            timeout: Some(1),
            allow_timeout: Some(true),
            retry_attempts: Some(0),
            retry_delay: Some(0),
            rate_limit_delay: Some(0),
            use_head_requests: Some(false),
            skip_ssl_verification: Some(false),
            ..Default::default()
        };
        log_config_info(&config1, 1);

        let config2 = Config {
            timeout: Some(86400),
            allow_timeout: Some(false),
            retry_attempts: Some(255),
            retry_delay: Some(u64::MAX),
            rate_limit_delay: Some(u64::MAX),
            use_head_requests: Some(true),
            skip_ssl_verification: Some(true),
            ..Default::default()
        };
        log_config_info(&config2, 1000);
    }

    #[test]
    fn test_log_config_info_with_defaults() {
        // Test with default config (all None values)
        let config = Config::default();
        log_config_info(&config, 4);

        // Test with partially filled config
        let config_partial = Config {
            timeout: Some(45),
            use_head_requests: Some(true),
            ..Default::default()
        };
        log_config_info(&config_partial, 8);
    }

    #[test]
    fn test_log_file_info_empty() {
        let empty_files: Vec<String> = vec![];
        log_file_info(0, &empty_files);
    }

    #[test]
    fn test_log_file_info_single_file() {
        log_file_info(1, &["single.md".to_string()]);
    }

    #[test]
    fn test_log_file_info_multiple_files() {
        let files = vec![
            "file1.md".to_string(),
            "file2.txt".to_string(),
            "file3.html".to_string(),
        ];
        log_file_info(3, &files);
    }

    #[test]
    fn test_log_file_info_path_buf() {
        use std::path::PathBuf;
        let paths = vec![
            PathBuf::from("path/to/file1.md"),
            PathBuf::from("another/file2.txt"),
        ];
        log_file_info(2, &paths);
    }

    #[test]
    fn test_log_url_discovery_zero() {
        log_url_discovery(0, 0);
    }

    #[test]
    fn test_log_url_discovery_deduplication() {
        log_url_discovery(5, 10); // 5 unique from 10 total
        log_url_discovery(10, 10); // No duplicates
        log_url_discovery(1, 100); // Heavy deduplication
    }

    #[test]
    fn test_log_validation_start_zero() {
        log_validation_start(0);
    }

    #[test]
    fn test_log_validation_start_large() {
        log_validation_start(1000);
    }

    #[test]
    fn test_log_validation_complete_all_success() {
        log_validation_complete(10, 0, 1000); // All successful
    }

    #[test]
    fn test_log_validation_complete_all_failed() {
        log_validation_complete(0, 10, 2000); // All failed
    }

    #[test]
    fn test_log_validation_complete_mixed() {
        log_validation_complete(7, 3, 1500); // Mixed results
    }

    #[test]
    fn test_log_validation_complete_zero_time() {
        log_validation_complete(5, 2, 0); // Zero duration
    }

    #[test]
    fn test_log_validation_complete_long_time() {
        log_validation_complete(100, 5, 30000); // Long duration
    }

    #[test]
    fn test_log_url_result_success() {
        log_url_result("https://example.com", Some(200), None);
        log_url_result("https://test.org", Some(201), None);
        log_url_result("https://api.service.com", Some(204), None);
    }

    #[test]
    fn test_log_url_result_client_errors() {
        log_url_result("https://example.com/404", Some(404), Some("Not Found"));
        log_url_result("https://example.com/403", Some(403), Some("Forbidden"));
        log_url_result(
            "https://example.com/429",
            Some(429),
            Some("Too Many Requests"),
        );
    }

    #[test]
    fn test_log_url_result_server_errors() {
        log_url_result(
            "https://example.com/500",
            Some(500),
            Some("Internal Server Error"),
        );
        log_url_result("https://example.com/502", Some(502), Some("Bad Gateway"));
    }

    #[test]
    fn test_log_url_result_network_errors() {
        log_url_result("https://unreachable.test", None, Some("Connection timeout"));
        log_url_result("https://dns.failure", None, Some("DNS resolution failed"));
        log_url_result("https://ssl.invalid", None, Some("SSL certificate error"));
    }

    #[test]
    fn test_log_url_result_unknown_state() {
        log_url_result("https://unknown.state", None, None);
    }

    #[test]
    fn test_log_error_with_source() {
        let io_error = io::Error::new(io::ErrorKind::NotFound, "File not found");
        log_error("Failed to read file", Some(&io_error));
    }

    #[test]
    fn test_log_error_without_source() {
        log_error("Something went wrong", None);
        log_error("Configuration error", None);
    }

    #[test]
    fn test_log_warning_various_messages() {
        log_warning("This is a warning");
        log_warning("Deprecated feature used");
        log_warning("Performance concern detected");
        log_warning("Configuration fallback used");
    }

    #[test]
    fn test_log_functions_with_special_characters() {
        // Test that logging functions handle special characters correctly
        log_url_result("https://example.com/path with spaces", Some(200), None);
        log_url_result("https://example.com/ünïcödé", Some(200), None);
        log_error("Error with special chars: äöü ñ", None);
        log_warning("Warning with emojis: ⚠️ 🔥");
    }

    #[test]
    fn test_log_functions_with_long_strings() {
        let long_url = "https://example.com/".to_string() + &"very-long-path/".repeat(100);
        log_url_result(&long_url, Some(200), None);

        let long_error = "This is a very long error message: ".to_string() + &"error ".repeat(100);
        log_error(&long_error, None);

        let long_warning = "Long warning: ".to_string() + &"warning ".repeat(50);
        log_warning(&long_warning);
    }

    #[test]
    fn test_log_functions_with_empty_strings() {
        log_url_result("", Some(200), None);
        log_url_result("https://example.com", Some(200), Some(""));
        log_error("", None);
        log_warning("");
    }

    #[test]
    fn test_thread_safety() {
        use std::sync::Arc;
        use std::sync::Barrier;
        use std::thread;

        let barrier = Arc::new(Barrier::new(4));
        let mut handles = vec![];

        // Test that logging functions are thread-safe
        for i in 0..4 {
            let barrier = barrier.clone();
            let handle = thread::spawn(move || {
                barrier.wait();
                for j in 0..10 {
                    log_url_result(&format!("https://thread{i}.test/{j}"), Some(200), None);
                    log_error(&format!("Error from thread {i}"), None);
                    log_warning(&format!("Warning from thread {i}"));
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }
    }
}