caesura 0.31.0

An all-in-one command line tool to transcode FLAC audio files and upload to gazelle based indexers/trackers
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
use crate::testing_prelude::*;
use di::ServiceCollection;

/// Verify `BatchOptions` default values.
#[test]
fn batch_options_default_values() {
    let result = BatchOptionsPartial::default().resolve_without_validation();
    assert_yaml_snapshot!(result);
}

/// Verify `FileOptions` default values.
#[test]
fn file_options_default_values() {
    let result = FileOptionsPartial::default().resolve_without_validation();
    assert_yaml_snapshot!(result);
}

/// Verify `TargetOptions` default values.
#[test]
fn target_options_default_values() {
    let result = TargetOptionsPartial::default().resolve_without_validation();
    assert_yaml_snapshot!(result);
}

/// Verify `SpectrogramOptions` default values.
#[test]
fn spectrogram_options_default_values() {
    let result = SpectrogramOptionsPartial::default().resolve_without_validation();
    assert_yaml_snapshot!(result);
}

/// Verify `UploadOptions` default values.
#[test]
fn upload_options_default_values() {
    let result = UploadOptionsPartial::default().resolve_without_validation();
    assert_yaml_snapshot!(result);
}

/// Verify `VerifyOptions` default values.
#[test]
fn verify_options_default_values() {
    let result = VerifyOptionsPartial::default().resolve_without_validation();
    assert_yaml_snapshot!(result);
}

/// Verify `CacheOptions` default uses platform user cache directory.
#[test]
fn cache_options_default_values() {
    let resolved = CacheOptionsPartial::default().resolve_without_validation();
    if is_docker() {
        assert_eq!(resolved.cache, PathBuf::from("/cache"));
    } else {
        assert!(
            resolved.cache.ends_with("caesura"),
            "expected cache path to end with 'caesura', got: {:?}",
            resolved.cache
        );
    }
}

/// Verify `CopyOptions` default values.
#[test]
fn copy_options_default_values() {
    let result = CopyOptionsPartial::default().resolve_without_validation();
    assert_yaml_snapshot!(result);
}

/// Verify `indexer` and `indexer_url` are calculated from RED `announce_url`.
#[test]
fn shared_options_calculates_indexer_from_red_announce_url() {
    let resolved = SharedOptionsPartial {
        announce_url: Some(format!("{RED_TRACKER_URL}/abc123/announce")),
        ..SharedOptionsPartial::default()
    }
    .resolve_without_validation();
    assert_eq!(resolved.indexer, "red");
    assert_eq!(resolved.indexer_url, RED_URL);
}

/// Verify `indexer` and `indexer_url` are calculated from OPS `announce_url`.
#[test]
fn shared_options_calculates_indexer_from_ops_announce_url() {
    let resolved = SharedOptionsPartial {
        announce_url: Some(format!("{OPS_TRACKER_URL}/abc123/announce")),
        ..SharedOptionsPartial::default()
    }
    .resolve_without_validation();
    assert_eq!(resolved.indexer, "ops");
    assert_eq!(resolved.indexer_url, OPS_URL);
}

/// Verify explicit `indexer` is not overridden by calculated default.
#[test]
fn shared_options_does_not_override_explicit_indexer() {
    let resolved = SharedOptionsPartial {
        announce_url: Some(format!("{RED_TRACKER_URL}/abc123/announce")),
        indexer: Some("custom".to_owned()),
        ..SharedOptionsPartial::default()
    }
    .resolve_without_validation();
    assert_eq!(resolved.indexer, "custom");
    // indexer_url uses the custom indexer which doesn't match red/ops, so defaults to empty
    assert_eq!(resolved.indexer_url, "");
}

/// Verify explicit `indexer_url` is not overridden by calculated default.
#[test]
fn shared_options_does_not_override_explicit_indexer_url() {
    let resolved = SharedOptionsPartial {
        announce_url: Some(format!("{RED_TRACKER_URL}/abc123/announce")),
        indexer_url: Some("https://custom.example.com".to_owned()),
        ..SharedOptionsPartial::default()
    }
    .resolve_without_validation();
    assert_eq!(resolved.indexer, "red");
    assert_eq!(resolved.indexer_url, "https://custom.example.com");
}

/// Verify unknown `announce_url` leaves `indexer` and `indexer_url` as empty (fails validation).
#[test]
fn shared_options_unknown_announce_url_leaves_indexer_empty() {
    let resolved = SharedOptionsPartial {
        announce_url: Some("https://unknown.tracker.com/announce".to_owned()),
        ..SharedOptionsPartial::default()
    }
    .resolve_without_validation();
    // With resolve_without_validation, required fields default to empty string
    assert_eq!(resolved.indexer, "");
    assert_eq!(resolved.indexer_url, "");
}

/// Edge case: explicit empty string for `indexer` bypasses `required` check.
///
/// The `required` attribute checks if the Option is None after `default_fn` runs.
/// An explicit `Some("")` is not None, so it passes the required check.
/// However, `indexer_url` still fails because its `default_fn` can't derive from "".
#[test]
fn shared_options_explicit_empty_indexer_bypasses_required() {
    let result = SharedOptionsPartial {
        announce_url: Some(format!("{RED_TRACKER_URL}/abc/announce")),
        api_key: Some("key".to_owned()),
        indexer: Some(String::new()), // Explicit empty string
        content: Some(vec![PathBuf::from(".")]),
        output: Some(PathBuf::from(".")),
        ..SharedOptionsPartial::default()
    }
    .resolve();
    // indexer passes required check (Some("") is not None)
    // but indexer_url fails because default_fn can't derive from empty indexer
    let errors = result.expect_err("should fail due to indexer_url");
    assert!(
        errors.iter().any(
            |e| e.kind == OptionIssueKind::Required && e.keys == vec!["indexer_url".to_owned()]
        )
    );
}

/// Verify `upload` without `transcode` is rejected.
#[test]
fn batch_options_rejects_upload_without_transcode() {
    let result = BatchOptionsPartial {
        upload: Some(true),
        transcode: None,
        ..BatchOptionsPartial::default()
    }
    .resolve();
    let errors = result.expect_err("should reject upload without transcode");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::DependencyMissing)
    );
}

/// Verify `upload` with `transcode` is accepted.
#[test]
fn batch_options_accepts_upload_with_transcode() {
    let result = BatchOptionsPartial {
        upload: Some(true),
        transcode: Some(true),
        ..BatchOptionsPartial::default()
    }
    .resolve();
    assert!(result.is_ok());
}

/// Verify invalid `wait_before_upload` duration is rejected.
#[test]
fn batch_options_rejects_invalid_wait_duration() {
    let result = BatchOptionsPartial {
        wait_before_upload: Some("invalid".to_owned()),
        ..BatchOptionsPartial::default()
    }
    .resolve();
    let errors = result.expect_err("should reject invalid duration");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::DurationInvalid)
    );
}

/// Verify valid `wait_before_upload` duration is accepted.
#[test]
fn batch_options_accepts_valid_wait_duration() {
    let result = BatchOptionsPartial {
        wait_before_upload: Some("5m30s".to_owned()),
        ..BatchOptionsPartial::default()
    }
    .resolve();
    assert!(result.is_ok());
}

/// Verify explicitly empty `target` list is rejected.
#[test]
fn target_options_rejects_empty_target_list() {
    let result = TargetOptionsPartial {
        target: Some(vec![]),
        ..TargetOptionsPartial::default()
    }
    .resolve();
    let errors = result.expect_err("should reject empty target list");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::RequiredNonEmpty)
    );
}

/// Verify explicitly empty `spectrogram_size` list is rejected.
#[test]
fn spectrogram_options_rejects_empty_size_list() {
    let result = SpectrogramOptionsPartial {
        spectrogram_size: Some(vec![]),
    }
    .resolve();
    let errors = result.expect_err("should reject empty size list");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::RequiredNonEmpty)
    );
}

/// Verify explicitly empty `qbit_fetch_categories` list is rejected.
#[test]
fn queue_fetch_options_rejects_empty_categories_list() {
    let result = QueueFetchOptionsPartial {
        qbit_fetch_categories: Some(vec![]),
    }
    .resolve();
    let errors = result.expect_err("should reject empty categories list");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::RequiredNonEmpty)
    );
}

/// Verify `BatchOptionsPartial` round-trips through YAML.
#[test]
fn batch_options_yaml_round_trip() {
    // Arrange
    let original = BatchOptionsPartial {
        spectrogram: Some(true),
        transcode: Some(true),
        upload: Some(true),
        limit: Some(5),
        no_limit: Some(false),
        wait_before_upload: Some("10m".to_owned()),
        ..BatchOptionsPartial::default()
    };

    // Act
    let yaml = yaml_to_string(&original).expect("should serialize");
    let parsed: BatchOptionsPartial = yaml_from_str(&yaml).expect("should deserialize");

    // Assert
    assert_eq!(original.spectrogram, parsed.spectrogram);
    assert_eq!(original.transcode, parsed.transcode);
    assert_eq!(original.upload, parsed.upload);
    assert_eq!(original.limit, parsed.limit);
    assert_eq!(original.wait_before_upload, parsed.wait_before_upload);
}

/// Verify `TargetOptionsPartial` round-trips through YAML.
#[test]
fn target_options_yaml_round_trip() {
    // Arrange
    let original = TargetOptionsPartial {
        target: Some(vec![TargetFormat::Flac, TargetFormat::V0]),
        allow_existing: Some(true),
        allow_less_specific: Some(true),
        sox_random_dither: Some(true),
        exclude_vorbis_comments: Some(TargetOptions::default_exclude_vorbis_comments()),
    };

    // Act
    let yaml = yaml_to_string(&original).expect("should serialize");
    let parsed: TargetOptionsPartial = yaml_from_str(&yaml).expect("should deserialize");

    // Assert
    assert_eq!(original.target, parsed.target);
    assert_eq!(original.allow_existing, parsed.allow_existing);
    assert_eq!(original.sox_random_dither, parsed.sox_random_dither);
    assert_eq!(
        original.exclude_vorbis_comments,
        parsed.exclude_vorbis_comments
    );
}

/// Verify `SharedOptionsPartial` round-trips through YAML.
#[test]
fn shared_options_yaml_round_trip() {
    // Arrange
    let original = SharedOptionsPartial {
        announce_url: Some("https://example.com/announce".to_owned()),
        api_key: Some("secret_key".to_owned()),
        indexer: Some("red".to_owned()),
        indexer_url: Some(RED_URL.to_owned()),
        content: Some(vec![PathBuf::from("/data/music")]),
        output: Some(PathBuf::from("/data/output")),
        verbosity: Some(Verbosity::Debug),
        log_time: Some(TimeFormat::Elapsed),
    };

    // Act
    let yaml = yaml_to_string(&original).expect("should serialize");
    let parsed: SharedOptionsPartial = yaml_from_str(&yaml).expect("should deserialize");

    // Assert
    assert_eq!(original.announce_url, parsed.announce_url);
    assert_eq!(original.api_key, parsed.api_key);
    assert_eq!(original.indexer, parsed.indexer);
    assert_eq!(original.content, parsed.content);
    assert_eq!(original.verbosity, parsed.verbosity);
}

/// Verify CLI values override YAML values during merge.
#[test]
fn batch_options_partial_merge_cli_overrides() {
    // Arrange
    let mut cli = BatchOptionsPartial {
        limit: Some(10),
        ..BatchOptionsPartial::default()
    };
    let yaml = BatchOptionsPartial {
        limit: Some(5),
        spectrogram: Some(true),
        transcode: Some(true),
        ..BatchOptionsPartial::default()
    };

    // Act
    cli.merge(yaml);

    // Assert
    assert_eq!(cli.limit, Some(10));
    assert_eq!(cli.spectrogram, Some(true));
    assert_eq!(cli.transcode, Some(true));
}

/// Verify YAML values fill in None values during merge.
#[test]
fn batch_options_partial_merge_fills_none() {
    // Arrange
    let mut cli = BatchOptionsPartial::default();
    let yaml = BatchOptionsPartial {
        limit: Some(5),
        spectrogram: Some(true),
        ..BatchOptionsPartial::default()
    };

    // Act
    cli.merge(yaml);

    // Assert
    assert_eq!(cli.limit, Some(5));
    assert_eq!(cli.spectrogram, Some(true));
}

/// Verify merge does not override already-set values.
#[test]
fn shared_options_partial_merge_preserves_set() {
    // Arrange
    let mut cli = SharedOptionsPartial {
        indexer: Some("custom".to_owned()),
        verbosity: Some(Verbosity::Trace),
        ..SharedOptionsPartial::default()
    };
    let yaml = SharedOptionsPartial {
        indexer: Some("red".to_owned()),
        verbosity: Some(Verbosity::Info),
        api_key: Some("from_yaml".to_owned()),
        ..SharedOptionsPartial::default()
    };

    // Act
    cli.merge(yaml);

    // Assert
    assert_eq!(cli.indexer, Some("custom".to_owned()));
    assert_eq!(cli.verbosity, Some(Verbosity::Trace));
    assert_eq!(cli.api_key, Some("from_yaml".to_owned()));
}

/// Verify `with_options` overrides options registered by `register_options`.
#[test]
fn host_builder_with_options_overrides() {
    use crate::hosting::HostBuilder;

    // Arrange: HostBuilder::new() registers default options via register_options()
    let mut builder = HostBuilder::new();

    // Act: Override with custom value
    let custom_output = PathBuf::from("/custom/output/path");
    let host = builder
        .with_options(SharedOptions {
            output: custom_output.clone(),
            ..SharedOptions::default()
        })
        .expect_build();

    // Assert: Retrieved options should have the overridden value
    let options = host.services.get_required::<SharedOptions>();
    assert_eq!(options.output, custom_output);
}

/// Verify validation errors for missing required fields.
#[test]
fn shared_options_validate_missing_fields() {
    // Use explicit paths to avoid platform-specific and Docker-specific defaults in snapshot
    let result = SharedOptionsPartial {
        content: Some(vec![PathBuf::from("./nonexistent-content")]),
        output: Some(PathBuf::from("./nonexistent-output")),
        ..SharedOptionsPartial::default()
    }
    .resolve();
    let errors = result.expect_err("should reject missing required fields");
    assert_yaml_snapshot!(errors);
}

/// Verify validation errors for invalid URLs.
#[test]
fn shared_options_validate_invalid_urls() {
    let result = SharedOptionsPartial {
        api_key: Some("key".to_owned()),
        indexer: Some("red".to_owned()),
        indexer_url: Some("not-a-url".to_owned()),
        announce_url: Some("https://example.com/announce/".to_owned()),
        content: Some(vec![PathBuf::from(".")]),
        output: Some(PathBuf::from(".")),
        ..SharedOptionsPartial::default()
    }
    .resolve();
    let errors = result.expect_err("should reject invalid URLs");
    assert_yaml_snapshot!(errors);
}

/// Verify valid options produce no validation errors.
#[test]
fn shared_options_validate_no_errors_when_valid() {
    let result = SharedOptionsPartial {
        api_key: Some("key".to_owned()),
        indexer: Some("red".to_owned()),
        indexer_url: Some(RED_URL.to_owned()),
        announce_url: Some(format!("{RED_TRACKER_URL}/abc/announce")),
        content: Some(vec![PathBuf::from(".")]),
        output: Some(PathBuf::from(".")),
        ..SharedOptionsPartial::default()
    }
    .resolve();
    assert!(result.is_ok());
}

/// Verify invalid hash format is rejected by `QueueRemoveArgs` validation.
#[test]
fn queue_rm_args_rejects_invalid_hash() {
    let result = QueueRemoveArgsPartial {
        queue_rm_hash: Some("not-a-valid-hash".to_owned()),
    }
    .resolve();
    let errors = result.expect_err("should reject invalid hash");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::HashInvalid)
    );
}

/// Verify missing hash (defaults to empty string) is rejected.
#[test]
fn queue_rm_args_rejects_missing_hash() {
    let result = QueueRemoveArgsPartial {
        queue_rm_hash: None,
    }
    .resolve();
    let errors = result.expect_err("should reject missing hash");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::HashInvalid)
    );
}

/// Verify valid 40-character hex hash is accepted.
#[test]
fn queue_rm_args_accepts_valid_hash() {
    let result = QueueRemoveArgsPartial {
        queue_rm_hash: Some("a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2".to_owned()),
    }
    .resolve();
    assert!(result.is_ok());
}

/// Verify literal `$HOME` in cache path fails validation.
#[test]
fn cache_options_rejects_dollar_home() {
    // Arrange
    let path = "$HOME/.cache/caesura";

    // Act
    let errors = CacheOptionsPartial {
        cache: Some(PathBuf::from(path)),
    }
    .resolve()
    .expect_err("should reject");

    // Assert
    assert_eq!(
        errors,
        vec![OptionIssue::directory_not_found(
            "cache",
            &PathBuf::from(path)
        )]
    );
}

/// Verify `~` in cache path is expanded during validation.
///
/// Uses `~` alone because the home directory is guaranteed to exist.
/// Subpath expansion is covered by `expand_tilde` unit tests.
#[test]
fn cache_options_expands_tilde() {
    // Arrange
    let result = CacheOptionsPartial {
        cache: Some(PathBuf::from("~")),
    }
    .resolve();

    // Assert
    assert!(result.is_ok(), "~ should expand to home directory");
}

/// Verify literal `$HOME` in output path fails validation.
#[test]
fn shared_options_rejects_dollar_home_output() {
    // Arrange
    let path = "$HOME/.local/share/caesura/output";

    // Act
    let errors = valid_shared_options_with_output(path)
        .resolve()
        .expect_err("should reject");

    // Assert
    assert_eq!(
        errors,
        vec![OptionIssue::directory_not_found(
            "output",
            &PathBuf::from(path)
        )]
    );
}

/// Verify `~` in output path is expanded during validation.
///
/// Uses `~` alone because the home directory is guaranteed to exist.
#[test]
fn shared_options_expands_tilde_output() {
    // Arrange
    let result = valid_shared_options_with_output("~").resolve();

    // Assert
    assert!(result.is_ok(), "~ should expand to home directory");
}

/// Verify literal `$HOME` in content path fails validation.
#[test]
fn shared_options_rejects_dollar_home_content() {
    // Arrange
    let path = "$HOME/music";

    // Act
    let errors = valid_shared_options_with_content(path)
        .resolve()
        .expect_err("should reject");

    // Assert
    assert_eq!(
        errors,
        vec![OptionIssue::directory_not_found(
            "content",
            &PathBuf::from(path)
        )]
    );
}

/// Verify `~` in content path is expanded during validation.
///
/// Uses `~` alone because the home directory is guaranteed to exist.
#[test]
fn shared_options_expands_tilde_content() {
    // Arrange
    let result = valid_shared_options_with_content("~").resolve();

    // Assert
    assert!(result.is_ok(), "~ should expand to home directory");
}

/// Verify `read_to_string` fails on literal `~` config path.
///
/// Config file resolution in `read_config_file` uses `read_to_string(path).ok()`
/// which silently swallows this error, meaning the config file is never loaded.
#[test]
fn config_read_to_string_fails_on_tilde() {
    assert!(read_to_string("~/.config/caesura/config.yml").is_err());
}

/// Verify `read_to_string` fails on literal `$HOME` config path.
#[test]
fn config_read_to_string_fails_on_dollar_home() {
    assert!(read_to_string("$HOME/.config/caesura/config.yml").is_err());
}

/// Verify literal `$HOME` in config path fails validation.
#[test]
fn config_options_rejects_dollar_home() {
    // Arrange
    let path = "$HOME/.config/caesura/config.yml";

    // Act
    let errors = ConfigOptionsPartial {
        config: Some(PathBuf::from(path)),
    }
    .resolve()
    .expect_err("should reject");

    // Assert
    assert_eq!(
        errors,
        vec![OptionIssue::file_not_found("config", &PathBuf::from(path))]
    );
}

/// Verify `~` in config path is expanded during validation.
///
/// The file won't exist, but the error path should show the expanded
/// absolute path rather than the literal `~`.
#[test]
fn config_options_expands_tilde() {
    // Arrange
    let partial = ConfigOptionsPartial {
        config: Some(PathBuf::from("~/.config/caesura/nonexistent.yml")),
    };

    // Act
    let errors = partial.resolve().expect_err("file should not exist");

    // Assert
    assert_eq!(errors.len(), 1);
    let issue = errors.first().expect("should have one issue");
    assert_eq!(issue.kind, OptionIssueKind::FileNotFound);
    let path = issue
        .additional
        .iter()
        .find_map(|(k, v)| (k == "path").then_some(v.as_str()))
        .expect("should have path additional");
    assert!(!path.starts_with('~'));
}

fn valid_shared_options_with_output(output: &str) -> SharedOptionsPartial {
    SharedOptionsPartial {
        announce_url: Some(format!("{RED_TRACKER_URL}/abc/announce")),
        api_key: Some("key".to_owned()),
        content: Some(vec![PathBuf::from(".")]),
        output: Some(PathBuf::from(output)),
        ..SharedOptionsPartial::default()
    }
}

fn valid_shared_options_with_content(content: &str) -> SharedOptionsPartial {
    SharedOptionsPartial {
        announce_url: Some(format!("{RED_TRACKER_URL}/abc/announce")),
        api_key: Some("key".to_owned()),
        content: Some(vec![PathBuf::from(content)]),
        output: Some(PathBuf::from(".")),
        ..SharedOptionsPartial::default()
    }
}

/// Verify nonexistent path is rejected by `InspectArg` validation.
#[test]
fn inspect_arg_rejects_nonexistent_path() {
    let result = InspectArgPartial {
        inspect_path: Some(PathBuf::from("/nonexistent/path/that/does/not/exist")),
    }
    .resolve();
    let errors = result.expect_err("should reject nonexistent path");
    assert!(
        errors
            .iter()
            .any(|e| e.kind == OptionIssueKind::DirectoryNotFound)
    );
}

/// Verify `QbitOptions::validate_connection` pushes `NotSet` errors for missing credentials.
#[test]
fn qbit_options_validate_connection_missing_credentials() {
    let options = QbitOptions {
        qbit_url: Some("http://127.0.0.1:8080".to_owned()),
        qbit_username: None,
        qbit_password: None,
    };
    let mut validator = OptionsValidator::new();
    options.validate_connection(&mut validator);
    let errors = validator.into_issues();
    assert_eq!(
        errors,
        vec![
            OptionIssue::required("qbit_username"),
            OptionIssue::required("qbit_password")
        ]
    );
}

/// Verify `QbitOptions::validate_connection` does not require credentials for a qui proxy URL.
#[test]
fn qbit_options_validate_connection_qui_proxy_url() {
    let options = QbitOptions {
        qbit_url: Some("http://localhost:7476/proxy/abc123".to_owned()),
        qbit_username: None,
        qbit_password: None,
    };
    let mut validator = OptionsValidator::new();
    options.validate_connection(&mut validator);
    assert!(validator.into_issues().is_empty());
}

/// Verify `QbitOptions` rejects a trailing slash in the URL.
#[test]
fn qbit_options_trailing_slash() {
    let mock = QbitOptions::mock();
    let partial = QbitOptionsPartial {
        qbit_url: Some("http://localhost:7476/proxy/abc123/".to_owned()),
        qbit_username: mock.qbit_username,
        qbit_password: mock.qbit_password,
    };
    let result = partial.resolve();
    let errors = result.expect_err("should reject trailing slash");
    assert!(errors.iter().any(|e| e.kind == OptionIssueKind::UrlInvalid));
}

/// Verify invalid YAML produces a config deserialization error.
#[test]
fn options_provider_register_invalid_yaml() {
    // Arrange
    let yaml = "qbit_url:\n  - not\n  - a\n  - string\n".to_owned();
    let mut provider = OptionsProvider::from_yaml(Some(yaml));
    let mut services = ServiceCollection::new();

    // Act
    provider.register::<QbitOptionsPartial>(&mut services);

    // Assert
    assert!(provider.has_errors());
    let error = provider.errors.first().expect("should have an error");
    assert!(
        error.kind == OptionIssueKind::ConfigInvalid,
        "Expected ConfigInvalid, got: {error}"
    );
}

/// Verify valid YAML does not produce errors.
#[test]
fn options_provider_register_valid_yaml() {
    // Arrange
    let yaml = "qbit_url: http://127.0.0.1:8080\n".to_owned();
    let mut provider = OptionsProvider::from_yaml(Some(yaml));
    let mut services = ServiceCollection::new();

    // Act
    provider.register::<QbitOptionsPartial>(&mut services);

    // Assert
    assert!(!provider.has_errors());
}

/// Verify missing YAML does not produce errors.
#[test]
fn options_provider_register_no_yaml() {
    // Arrange
    let mut provider = OptionsProvider::from_yaml(None);
    let mut services = ServiceCollection::new();

    // Act
    provider.register::<QbitOptionsPartial>(&mut services);

    // Assert
    assert!(!provider.has_errors());
}