netspeed-cli 0.10.3

Command-line interface for testing internet bandwidth using speedtest.net
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
//! Integration tests for config validation with strict mode.
//!
//! These tests verify that invalid config file values are handled correctly
//! depending on whether strict mode is enabled. They use the library's internal
//! functions directly for reliability.

use netspeed_cli::config::{File, ValidationResult, validate_config};

// ── Test Helper ───────────────────────────────────────────────────────

/// Simulates what happens when config is loaded and validated.
/// Returns the validation result and whether strict mode would cause exit.
fn validate_with_strict(file_config: &File, strict: bool) -> (ValidationResult, bool) {
    let validation = validate_config(file_config);
    let should_exit = strict && !validation.valid;
    (validation, should_exit)
}

// ── Invalid Profile Tests ────────────────────────────────────────────

#[test]
fn test_config_invalid_profile_non_strict() {
    let file_config = File {
        profile: Some("bad_profile".to_string()),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(!result.valid, "Invalid profile should make result invalid");
    assert!(!result.errors.is_empty(), "Should have errors");
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.contains("Invalid profile") || e.contains("bad_profile")),
        "Error should mention invalid profile: {:?}",
        result.errors
    );
    assert!(!should_exit, "Non-strict mode should not exit");
}

#[test]
fn test_config_invalid_profile_strict() {
    let file_config = File {
        profile: Some("invalid_profile".to_string()),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, true);

    assert!(!result.valid);
    assert!(should_exit, "Strict mode should exit on invalid config");
}

#[test]
fn test_config_strict_in_file() {
    // Note: validate_config doesn't read the 'strict' field from File.
    // The strict flag is handled in Config::from_args.
    // This test just verifies that invalid profile causes validation failure.
    let file_config = File {
        profile: Some("bad_one".to_string()),
        strict: Some(true), // This field is not used by validate_config
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false); // strict comes from CLI, not File

    assert!(!result.valid);
    // should_exit is false because we passed strict=false (no CLI override)
    assert!(
        !should_exit,
        "Without CLI --strict-config, no exit even if config has strict=true"
    );

    // The actual strict-from-file behavior would be tested at integration level
    // where Config::from_args reads both args.strict_config and file_config.strict
}

#[test]
fn test_config_valid_profile_no_errors() {
    let file_config = File {
        profile: Some("gamer".to_string()),
        ..Default::default()
    };
    let (result, _) = validate_with_strict(&file_config, false);

    assert!(result.valid, "Valid profile should make result valid");
    assert!(result.errors.is_empty(), "Should have no errors");
}

// ── Invalid Theme Tests ──────────────────────────────────────────────

#[test]
fn test_config_invalid_theme_non_strict() {
    let file_config = File {
        theme: Some("neon".to_string()),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(!result.valid);
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.contains("Invalid theme") || e.contains("neon")),
        "Error should mention invalid theme: {:?}",
        result.errors
    );
    assert!(!should_exit, "Non-strict mode should not exit");
}

#[test]
fn test_config_invalid_theme_strict() {
    let file_config = File {
        theme: Some("flashy".to_string()),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, true);

    assert!(!result.valid);
    assert!(should_exit, "Strict mode should exit on invalid theme");
}

// ── Invalid CSV Delimiter Tests ──────────────────────────────────────

#[test]
fn test_config_invalid_csv_delimiter_non_strict() {
    let file_config = File {
        csv_delimiter: Some('X'),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(!result.valid);
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.contains("Invalid CSV delimiter") || e.contains("X")),
        "Error should mention invalid CSV delimiter: {:?}",
        result.errors
    );
    assert!(!should_exit, "Non-strict mode should not exit");
}

#[test]
fn test_config_invalid_csv_delimiter_strict() {
    let file_config = File {
        csv_delimiter: Some('@'),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, true);

    assert!(!result.valid);
    assert!(should_exit, "Strict mode should exit on invalid delimiter");
}

// ── Deprecated Options Tests ─────────────────────────────────────────

#[test]
fn test_config_deprecated_simple_warning() {
    let file_config = File {
        simple: Some(true),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(result.valid, "Deprecated options are warnings, not errors");
    assert!(
        result
            .warnings
            .iter()
            .any(|w| w.contains("deprecated") && w.contains("simple")),
        "Should warn about deprecated 'simple' option: {:?}",
        result.warnings
    );
    assert!(!should_exit, "Warnings should not cause exit");
}

#[test]
fn test_config_deprecated_csv_warning() {
    let file_config = File {
        csv: Some(true),
        ..Default::default()
    };
    let (result, _) = validate_with_strict(&file_config, false);

    assert!(result.valid);
    assert!(
        result
            .warnings
            .iter()
            .any(|w| w.contains("deprecated") && w.contains("csv")),
        "Should warn about deprecated 'csv' option"
    );
}

#[test]
fn test_config_deprecated_json_warning() {
    let file_config = File {
        json: Some(true),
        ..Default::default()
    };
    let (result, _) = validate_with_strict(&file_config, false);

    assert!(result.valid);
    assert!(
        result
            .warnings
            .iter()
            .any(|w| w.contains("deprecated") && w.contains("json")),
        "Should warn about deprecated 'json' option"
    );
}

#[test]
fn test_config_deprecated_not_fatal_in_strict() {
    let file_config = File {
        simple: Some(true),
        csv: Some(true),
        json: Some(true),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, true);

    assert!(result.valid, "Deprecated options are warnings, not errors");
    assert!(
        !should_exit,
        "Deprecated options should not cause exit in strict mode"
    );
}

// ── Multiple Validation Issues Tests ────────────────────────────────

#[test]
fn test_config_multiple_issues_all_reported() {
    let file_config = File {
        profile: Some("bad_profile".to_string()),
        theme: Some("neon".to_string()),
        csv_delimiter: Some('!'),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(!result.valid, "Multiple issues should make result invalid");
    assert!(
        result.errors.len() >= 3,
        "Should have at least 3 errors, got: {:?}",
        result.errors
    );
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.contains("profile") || e.contains("bad_profile")),
        "Should have profile error: {:?}",
        result.errors
    );
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.contains("theme") || e.contains("neon")),
        "Should have theme error: {:?}",
        result.errors
    );
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.contains("CSV delimiter") || e.contains("!")),
        "Should have delimiter error: {:?}",
        result.errors
    );
    assert!(!should_exit, "Non-strict mode should not exit");
}

#[test]
fn test_config_multiple_issues_strict_exit() {
    let file_config = File {
        profile: Some("invalid".to_string()),
        theme: Some("weird".to_string()),
        csv_delimiter: Some('#'),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, true);

    assert!(!result.valid);
    assert!(
        should_exit,
        "Multiple issues should cause exit in strict mode"
    );
}

// ── Valid Config Tests ───────────────────────────────────────────────

#[test]
fn test_config_valid_no_warnings() {
    let file_config = File {
        profile: Some("gamer".to_string()),
        theme: Some("dark".to_string()),
        csv_delimiter: Some(','),
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(result.valid, "Valid config should be valid");
    assert!(result.errors.is_empty(), "Should have no errors");
    assert!(result.warnings.is_empty(), "Should have no warnings");
    assert!(!should_exit, "Should not exit");
}

#[test]
fn test_config_all_valid_themes() {
    let valid_themes = ["dark", "light", "high-contrast", "monochrome"];

    for theme in valid_themes {
        let file_config = File {
            theme: Some(theme.to_string()),
            ..Default::default()
        };
        let (result, _) = validate_with_strict(&file_config, false);

        assert!(result.valid, "Theme '{}' should be valid", theme);
        assert!(
            result.errors.is_empty(),
            "Theme '{}' should have no errors",
            theme
        );
    }
}

#[test]
fn test_config_empty_valid() {
    let file_config = File::default();
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(result.valid, "Empty config should be valid");
    assert!(result.errors.is_empty());
    assert!(result.warnings.is_empty());
    assert!(!should_exit);
}

// ── Edge Cases ───────────────────────────────────────────────────────

#[test]
fn test_config_mixed_deprecated_and_invalid() {
    let file_config = File {
        simple: Some(true),               // deprecated warning
        profile: Some("bad".to_string()), // invalid error
        ..Default::default()
    };
    let (result, should_exit) = validate_with_strict(&file_config, false);

    assert!(!result.valid, "Invalid profile makes result invalid");
    assert!(
        !result.warnings.is_empty(),
        "Should have deprecation warning"
    );
    assert!(!result.errors.is_empty(), "Should have validation errors");
    assert!(!should_exit, "Non-strict should not exit");
}

#[test]
fn test_config_strict_mode_priority() {
    // When CLI sets --strict-config (true) it should override config file's strict (false)
    // This is tested by passing strict=true to our helper (simulating CLI override)
    let file_config = File {
        strict: Some(false), // config file says not strict
        ..Default::default()
    };
    let (_result, _should_exit) = validate_with_strict(&file_config, true); // CLI overrides

    // With strict=true from CLI, and invalid profile, should exit
    let file_config_invalid = File {
        profile: Some("bad".to_string()),
        strict: Some(false),
        ..Default::default()
    };
    let (_, cli_should_exit) = validate_with_strict(&file_config_invalid, true);
    assert!(cli_should_exit, "CLI --strict-config should take priority");
}

#[test]
fn test_config_all_valid_profiles() {
    let valid_profiles = ["gamer", "streamer", "remote-worker", "power-user", "casual"];

    for profile in valid_profiles {
        let file_config = File {
            profile: Some(profile.to_string()),
            ..Default::default()
        };
        let (result, _) = validate_with_strict(&file_config, false);

        assert!(result.valid, "Profile '{}' should be valid", profile);
    }
}

#[test]
fn test_config_all_valid_csv_delimiters() {
    let valid_delimiters = [',', ';', '|', '\t'];

    for delimiter in valid_delimiters {
        let file_config = File {
            csv_delimiter: Some(delimiter),
            ..Default::default()
        };
        let (result, _) = validate_with_strict(&file_config, false);

        assert!(result.valid, "Delimiter '{:?}' should be valid", delimiter);
    }
}