superseedr 1.0.5

A BitTorrent Client in your Terminal.
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
// SPDX-FileCopyrightText: 2025 The superseedr Contributors
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::app::FilePriority;
use crate::fs_atomic::write_bytes_atomically;
use crate::integrations::control::{write_control_request, ControlPriorityTarget, ControlRequest};
use crate::integrations::status::status_file_path;
use clap::{Parser, Subcommand, ValueEnum};
use sha1::{Digest, Sha1};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::thread;
use std::time::{Duration, SystemTime};

#[derive(Parser, Debug)]
#[command(
    author,
    version,
    about = "A BitTorrent client with local CLI automation and optional shared cluster mode.",
    long_about = None
)]
pub struct Cli {
    #[arg(long, global = true, help = "Return structured JSON output")]
    pub json: bool,

    #[arg(help = "Add a torrent file path or magnet link without using a subcommand")]
    pub input: Option<String>,

    #[command(subcommand)]
    pub command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    #[command(about = "Add one or more torrent paths or magnet links")]
    Add {
        #[arg(
            value_name = "INPUT",
            num_args = 1..,
            help = "Torrent file path(s) or magnet link(s)"
        )]
        inputs: Vec<String>,
    },
    #[command(about = "Request graceful shutdown of the running client or shared leader")]
    StopClient,
    #[command(about = "Show the event journal")]
    Journal,
    #[command(about = "Persist the shared root used for launcher and protocol-handler starts")]
    SetSharedConfig {
        #[arg(
            value_name = "PATH",
            help = "Shared mount root or explicit superseedr-config path"
        )]
        path: PathBuf,
    },
    #[command(about = "Clear the persisted shared root launcher setting")]
    ClearSharedConfig,
    #[command(about = "Show the effective shared root selection and its source")]
    ShowSharedConfig,
    #[command(about = "Persist an explicit host identity for shared mode (optional)")]
    SetHostId {
        #[arg(
            value_name = "HOST_ID",
            help = "Stable host identity to use in shared mode"
        )]
        host_id: String,
    },
    #[command(about = "Clear the persisted shared host identity")]
    ClearHostId,
    #[command(about = "Show the effective host identity selection and its source")]
    ShowHostId,
    #[command(about = "Convert the current standalone config into layered shared config")]
    ToShared {
        #[arg(
            value_name = "PATH",
            help = "Shared mount root or explicit superseedr-config path"
        )]
        path: PathBuf,
    },
    #[command(about = "Convert the active shared config back into standalone local config")]
    ToStandalone,
    #[command(about = "List configured torrents")]
    Torrents,
    #[command(about = "Show one torrent by info hash, or resolve it from a unique file path")]
    Info {
        #[arg(
            value_name = "INFO_HASH_HEX_OR_PATH",
            help = "Torrent info hash or unique file path"
        )]
        target: String,
    },
    #[command(about = "Read status once, stream status updates, or stop status streaming")]
    Status {
        #[arg(long, help = "Continuously print updated status snapshots")]
        follow: bool,
        #[arg(long, help = "Stop runtime status streaming in standalone mode")]
        stop: bool,
        #[arg(
            long,
            value_name = "SECONDS",
            help = "Set the runtime status dump interval"
        )]
        interval: Option<u64>,
    },
    #[command(about = "Pause one or more torrents by info hash or unique file path")]
    Pause {
        #[arg(
            value_name = "INFO_HASH_HEX_OR_PATH",
            help = "One or more torrent targets"
        )]
        targets: Vec<String>,
    },
    #[command(about = "Resume one or more torrents by info hash or unique file path")]
    Resume {
        #[arg(
            value_name = "INFO_HASH_HEX_OR_PATH",
            help = "One or more torrent targets"
        )]
        targets: Vec<String>,
    },
    #[command(about = "Remove one or more torrents without deleting payload data")]
    Remove {
        #[arg(
            value_name = "INFO_HASH_HEX_OR_PATH",
            help = "One or more torrent targets"
        )]
        targets: Vec<String>,
    },
    #[command(about = "Remove one or more torrents and delete payload data when safe")]
    Purge {
        #[arg(
            value_name = "INFO_HASH_HEX_OR_PATH",
            help = "One or more torrent targets"
        )]
        targets: Vec<String>,
    },
    #[command(about = "List files for a torrent by info hash or unique file path")]
    Files {
        #[arg(
            value_name = "INFO_HASH_HEX_OR_PATH",
            help = "Torrent info hash or unique file path"
        )]
        target: String,
    },
    #[command(about = "Set file priority for a torrent by file index or relative path")]
    Priority {
        #[arg(
            value_name = "INFO_HASH_HEX_OR_PATH",
            help = "Torrent info hash or unique file path"
        )]
        target: String,
        #[arg(
            long,
            conflicts_with = "file_path",
            help = "Target a file by zero-based file index"
        )]
        file_index: Option<usize>,
        #[arg(
            long,
            conflicts_with = "file_index",
            help = "Target a file by relative file path"
        )]
        file_path: Option<String>,
        #[arg(help = "Priority to apply")]
        priority: CliPriority,
    },
}

#[derive(ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
pub enum CliPriority {
    Normal,
    High,
    Skip,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusCommandMode {
    Snapshot,
    Follow { interval_secs: u64 },
    SetInterval { interval_secs: u64 },
    Stop,
}

impl From<CliPriority> for FilePriority {
    fn from(value: CliPriority) -> Self {
        match value {
            CliPriority::Normal => FilePriority::Normal,
            CliPriority::High => FilePriority::High,
            CliPriority::Skip => FilePriority::Skip,
        }
    }
}

pub fn write_input_command(input_str: &str, watch_path: &Path) -> io::Result<PathBuf> {
    fs::create_dir_all(watch_path)?;

    if input_str.starts_with("magnet:") {
        let hash_bytes = Sha1::digest(input_str.as_bytes());
        let file_hash_hex = hex::encode(hash_bytes);

        let final_filename = format!("{}.magnet", file_hash_hex);
        let final_path = watch_path.join(final_filename);

        tracing::info!(
            "Attempting to write magnet link atomically to final path: {:?}",
            final_path
        );
        match write_bytes_atomically(&final_path, input_str.as_bytes()) {
            Ok(_) => Ok(final_path),
            Err(e) => {
                tracing::error!("Failed to write magnet file atomically: {}", e);
                Err(e)
            }
        }
    } else {
        let torrent_path = PathBuf::from(input_str);
        match fs::canonicalize(&torrent_path) {
            Ok(absolute_path) => {
                let absolute_path_cow = absolute_path.to_string_lossy();
                write_path_command_payload(
                    absolute_path_cow.as_ref(),
                    absolute_path_cow.as_ref(),
                    watch_path,
                )
            }
            Err(e) => {
                // Don't treat as error if launched by macOS without a valid path
                if !input_str.starts_with("magnet:") {
                    // Avoid logging error for magnet links here
                    tracing::warn!(
                        "Input '{}' is not a valid torrent file path: {}",
                        input_str,
                        e
                    );
                }
                Err(io::Error::new(io::ErrorKind::InvalidInput, e))
            }
        }
    }
}

pub fn write_path_command_payload(
    path_payload: &str,
    hash_key: &str,
    watch_path: &Path,
) -> io::Result<PathBuf> {
    fs::create_dir_all(watch_path)?;

    let hash_bytes = Sha1::digest(hash_key.as_bytes());
    let file_hash_hex = hex::encode(hash_bytes);
    let final_filename = format!("{}.path", file_hash_hex);
    let final_dest_path = watch_path.join(final_filename);

    tracing::info!(
        "Attempting to write torrent path atomically to final path: {:?}",
        final_dest_path
    );
    match write_bytes_atomically(&final_dest_path, path_payload.as_bytes()) {
        Ok(_) => Ok(final_dest_path),
        Err(e) => {
            tracing::error!("Failed to write path file atomically: {}", e);
            Err(e)
        }
    }
}

pub fn write_stop_command(watch_path: &Path) -> io::Result<PathBuf> {
    fs::create_dir_all(watch_path)?;
    let file_path = watch_path.join("shutdown.cmd");
    fs::write(&file_path, "STOP")?;
    Ok(file_path)
}

#[cfg(test)]
pub fn command_to_control_requests(
    command: &Commands,
) -> Result<Option<Vec<ControlRequest>>, String> {
    command_to_control_requests_with_resolver(command, |target, _| Ok(target.to_string()))
}

pub fn command_to_control_requests_with_resolver<F>(
    command: &Commands,
    mut resolve_target: F,
) -> Result<Option<Vec<ControlRequest>>, String>
where
    F: FnMut(&str, &str) -> Result<String, String>,
{
    match command {
        Commands::Status { .. } => Ok(Some(vec![status_control_request(command)?])),
        Commands::Pause { targets } => Ok(Some(
            require_cli_targets(targets, "pause")?
                .into_iter()
                .map(|target| resolve_target(&target, "pause"))
                .collect::<Result<Vec<_>, _>>()?
                .into_iter()
                .map(|info_hash_hex| ControlRequest::Pause { info_hash_hex })
                .collect(),
        )),
        Commands::Resume { targets } => Ok(Some(
            require_cli_targets(targets, "resume")?
                .into_iter()
                .map(|target| resolve_target(&target, "resume"))
                .collect::<Result<Vec<_>, _>>()?
                .into_iter()
                .map(|info_hash_hex| ControlRequest::Resume { info_hash_hex })
                .collect(),
        )),
        Commands::Remove { targets } => Ok(Some(
            require_cli_targets(targets, "remove")?
                .into_iter()
                .map(|target| resolve_target(&target, "remove"))
                .collect::<Result<Vec<_>, _>>()?
                .into_iter()
                .map(|info_hash_hex| ControlRequest::Delete {
                    info_hash_hex,
                    delete_files: false,
                })
                .collect(),
        )),
        Commands::Priority {
            target,
            file_index,
            file_path,
            priority,
        } => {
            let info_hash_hex = resolve_target(target, "priority")?;
            let target = if let Some(file_index) = file_index {
                ControlPriorityTarget::FileIndex(*file_index)
            } else if let Some(file_path) = file_path {
                ControlPriorityTarget::FilePath(file_path.clone())
            } else {
                return Err("Priority requires either --file-index or --file-path".to_string());
            };

            Ok(Some(vec![ControlRequest::SetFilePriority {
                info_hash_hex,
                target,
                priority: (*priority).into(),
            }]))
        }
        Commands::Add { .. }
        | Commands::StopClient
        | Commands::Journal
        | Commands::SetSharedConfig { .. }
        | Commands::ClearSharedConfig
        | Commands::ShowSharedConfig
        | Commands::SetHostId { .. }
        | Commands::ClearHostId
        | Commands::ShowHostId
        | Commands::ToShared { .. }
        | Commands::ToStandalone
        | Commands::Torrents
        | Commands::Info { .. }
        | Commands::Purge { .. }
        | Commands::Files { .. } => Ok(None),
    }
}

pub fn status_command_mode(command: &Commands) -> Result<StatusCommandMode, String> {
    let Commands::Status {
        follow,
        stop,
        interval,
    } = command
    else {
        return Err("Expected status command".to_string());
    };

    if *follow && *stop {
        return Err("Choose either --follow or --stop, not both".to_string());
    }
    if *stop && interval.is_some() {
        return Err("Do not use --interval together with --stop".to_string());
    }

    Ok(if *stop {
        StatusCommandMode::Stop
    } else if *follow {
        StatusCommandMode::Follow {
            interval_secs: interval.unwrap_or(5),
        }
    } else if let Some(interval_secs) = interval {
        StatusCommandMode::SetInterval {
            interval_secs: *interval_secs,
        }
    } else {
        StatusCommandMode::Snapshot
    })
}

pub fn status_control_request(command: &Commands) -> Result<ControlRequest, String> {
    Ok(match status_command_mode(command)? {
        StatusCommandMode::Snapshot => ControlRequest::StatusNow,
        StatusCommandMode::Follow { interval_secs }
        | StatusCommandMode::SetInterval { interval_secs } => {
            ControlRequest::StatusFollowStart { interval_secs }
        }
        StatusCommandMode::Stop => ControlRequest::StatusFollowStop,
    })
}

#[cfg(test)]
pub fn command_to_control_request(command: &Commands) -> Result<Option<ControlRequest>, String> {
    match command_to_control_requests(command)? {
        Some(mut requests) => {
            let request = requests
                .drain(..)
                .next()
                .ok_or_else(|| "No control requests were produced".to_string())?;
            Ok(Some(request))
        }
        None => Ok(None),
    }
}

pub fn require_cli_targets(values: &[String], command_name: &str) -> Result<Vec<String>, String> {
    let targets = values
        .iter()
        .flat_map(|value| value.split(','))
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(str::to_string)
        .collect::<Vec<_>>();

    if targets.is_empty() {
        return Err(format!(
            "Missing target for `superseedr {}`. Use either INFO_HASH_HEX or a file path.",
            command_name
        ));
    }

    Ok(targets)
}

pub fn expand_add_inputs(inputs: &[String]) -> Vec<String> {
    let mut expanded = Vec::new();
    for input in inputs {
        if input.starts_with("magnet:") || Path::new(input).exists() {
            expanded.push(input.clone());
            continue;
        }

        let mut split_values = input
            .split(',')
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(str::to_string)
            .collect::<Vec<_>>();

        if split_values.is_empty() {
            continue;
        }

        if split_values.len() == 1 {
            expanded.push(split_values.remove(0));
        } else {
            expanded.extend(split_values);
        }
    }
    expanded
}

pub fn write_control_command(request: &ControlRequest, watch_path: &Path) -> io::Result<PathBuf> {
    write_control_request(request, watch_path)
}

pub fn wait_for_status_json_after(
    previous_modified_at: Option<SystemTime>,
    timeout: Duration,
) -> io::Result<String> {
    let status_path = status_file_path()?;
    let deadline = std::time::Instant::now() + timeout;

    loop {
        if let Ok(metadata) = fs::metadata(&status_path) {
            let modified_at = metadata.modified().ok();
            let is_new_enough = match (previous_modified_at, modified_at) {
                (Some(previous), Some(current)) => current > previous,
                (None, Some(_)) => true,
                (_, None) => false,
            };

            if is_new_enough || previous_modified_at.is_none() {
                return fs::read_to_string(&status_path);
            }
        }

        if std::time::Instant::now() >= deadline {
            return Err(io::Error::new(
                io::ErrorKind::TimedOut,
                "Timed out waiting for a fresh status dump",
            ));
        }

        thread::sleep(Duration::from_millis(200));
    }
}

pub fn status_file_modified_at() -> io::Result<Option<SystemTime>> {
    let status_path = status_file_path()?;
    match fs::metadata(status_path) {
        Ok(metadata) => Ok(metadata.modified().ok()),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(error),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::CommandFactory;
    use std::fs::{self, File};
    use std::io::Write;

    // Helper to setup a temp directory if tempfile crate is missing
    fn setup_temp_dir() -> (PathBuf, impl Drop) {
        let dir = std::env::temp_dir().join(format!("superseedr_test_{}", rand::random::<u32>()));
        fs::create_dir_all(&dir).unwrap();
        let dir_clone = dir.clone();
        // Return a dropper to clean up
        struct Cleaner(PathBuf);
        impl Drop for Cleaner {
            fn drop(&mut self) {
                let _ = fs::remove_dir_all(&self.0);
            }
        }
        (dir, Cleaner(dir_clone))
    }

    #[test]
    fn test_process_input_magnet() {
        let (watch_dir, _cleaner) = setup_temp_dir();
        let magnet_link = "magnet:?xt=urn:btih:5b63529350414441534441534441534441534441";

        write_input_command(magnet_link, &watch_dir).expect("write magnet command");

        // Calculate expected hash
        let hash_bytes = Sha1::digest(magnet_link.as_bytes());
        let expected_name = format!("{}.magnet", hex::encode(hash_bytes));
        let expected_path = watch_dir.join(expected_name);

        assert!(expected_path.exists(), "Magnet file should exist");
        let content = fs::read_to_string(expected_path).unwrap();
        assert_eq!(
            content, magnet_link,
            "File content should be the magnet link"
        );
    }

    #[test]
    fn test_process_input_torrent_path() {
        let (watch_dir, _cleaner) = setup_temp_dir();

        // 1. Create a dummy torrent file to "add"
        let torrent_source_name = "test_linux.torrent";
        let torrent_source_path = watch_dir.join(torrent_source_name);
        {
            let mut f = File::create(&torrent_source_path).unwrap();
            f.write_all(b"dummy torrent content").unwrap();
        }
        let abs_source_path = fs::canonicalize(&torrent_source_path).unwrap();

        // 2. Process the path input
        write_input_command(abs_source_path.to_str().unwrap(), &watch_dir)
            .expect("write path command");

        // 3. Verify the .path file was created
        // The filename is the hash of the *path string*
        let hash_bytes = Sha1::digest(abs_source_path.to_string_lossy().as_bytes());
        let expected_name = format!("{}.path", hex::encode(hash_bytes));
        let expected_path_file = watch_dir.join(expected_name);

        assert!(expected_path_file.exists(), ".path file should be created");

        // 4. Verify content matches the source path
        let content = fs::read_to_string(expected_path_file).unwrap();
        assert_eq!(
            content,
            abs_source_path.to_string_lossy(),
            ".path file should contain the absolute path"
        );
    }

    #[test]
    fn test_process_invalid_path() {
        let (watch_dir, _cleaner) = setup_temp_dir();
        // Pass a non-existent path
        let bad_path = "/path/to/nonexistent/file.torrent";

        // Should not panic
        assert!(write_input_command(bad_path, &watch_dir).is_err());

        // Verify directory is empty (no .path file created)
        let count = fs::read_dir(&watch_dir).unwrap().count();
        assert_eq!(count, 0, "No files should be created for invalid input");
    }

    #[test]
    fn status_command_maps_to_runtime_requests() {
        let follow = Commands::Status {
            follow: true,
            stop: false,
            interval: None,
        };
        let request = status_control_request(&follow).expect("map status command");
        assert_eq!(
            request,
            ControlRequest::StatusFollowStart { interval_secs: 5 }
        );
    }

    #[test]
    fn status_interval_maps_to_runtime_request_without_follow() {
        let command = Commands::Status {
            follow: false,
            stop: false,
            interval: Some(30),
        };
        let request = status_control_request(&command).expect("map status interval");
        assert_eq!(
            request,
            ControlRequest::StatusFollowStart { interval_secs: 30 }
        );
        assert_eq!(
            status_command_mode(&command),
            Ok(StatusCommandMode::SetInterval { interval_secs: 30 })
        );
    }

    #[test]
    fn priority_requires_one_target() {
        let command = Commands::Priority {
            target: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),
            file_index: None,
            file_path: None,
            priority: CliPriority::High,
        };
        assert!(command_to_control_request(&command).is_err());
    }

    #[test]
    fn journal_command_is_not_mapped_to_control_request() {
        assert!(matches!(
            command_to_control_request(&Commands::Journal),
            Ok(None)
        ));
    }

    #[test]
    fn torrents_command_is_not_mapped_to_control_request() {
        assert!(matches!(
            command_to_control_request(&Commands::Torrents),
            Ok(None)
        ));
    }

    #[test]
    fn info_command_is_not_mapped_to_control_request() {
        assert!(matches!(
            command_to_control_request(&Commands::Info {
                target: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string()
            }),
            Ok(None)
        ));
    }

    #[test]
    fn remove_without_target_returns_helpful_error() {
        let error = command_to_control_request(&Commands::Remove {
            targets: Vec::new(),
        })
        .expect_err("missing target should fail");
        assert!(error.contains("Missing target"));
        assert!(error.contains("INFO_HASH_HEX"));
        assert!(error.contains("file path"));
    }

    #[test]
    fn shared_config_commands_are_not_mapped_to_control_request() {
        assert!(matches!(
            command_to_control_request(&Commands::SetSharedConfig {
                path: PathBuf::from("C:/shared-root")
            }),
            Ok(None)
        ));
        assert!(matches!(
            command_to_control_request(&Commands::ClearSharedConfig),
            Ok(None)
        ));
        assert!(matches!(
            command_to_control_request(&Commands::ShowSharedConfig),
            Ok(None)
        ));
    }

    #[test]
    fn remove_command_supports_multiple_hashes() {
        let requests = command_to_control_requests(&Commands::Remove {
            targets: vec![
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),
                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".to_string(),
            ],
        })
        .expect("map delete commands")
        .expect("requests");
        assert_eq!(requests.len(), 2);
    }

    #[test]
    fn purge_requires_at_least_one_target() {
        let error = require_cli_targets(&[], "purge").expect_err("missing target should fail");
        assert!(error.contains("Missing target"));
    }

    #[test]
    fn add_command_expands_comma_separated_non_magnet_inputs() {
        let expanded = expand_add_inputs(&["alpha.torrent,beta.torrent".to_string()]);
        assert_eq!(
            expanded,
            vec!["alpha.torrent".to_string(), "beta.torrent".to_string()]
        );
    }

    #[test]
    fn cli_priority_command_parses_without_panicking() {
        Cli::command().debug_assert();

        let parsed = Cli::try_parse_from([
            "superseedr",
            "priority",
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            "--file-index",
            "0",
            "skip",
        ])
        .expect("priority command should parse");

        match parsed.command.expect("subcommand") {
            Commands::Priority {
                target,
                file_index,
                file_path,
                priority,
            } => {
                assert_eq!(target, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                assert_eq!(file_index, Some(0));
                assert_eq!(file_path, None);
                assert_eq!(priority, CliPriority::Skip);
            }
            other => panic!("unexpected command: {other:?}"),
        }
    }

    #[test]
    fn resolved_pause_command_supports_file_lookup() {
        let requests = command_to_control_requests_with_resolver(
            &Commands::Pause {
                targets: vec!["C:/seedbox/downloads/sample.bin".to_string()],
            },
            |target, command_name| {
                assert_eq!(target, "C:/seedbox/downloads/sample.bin");
                assert_eq!(command_name, "pause");
                Ok("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string())
            },
        )
        .expect("map pause commands")
        .expect("requests");

        assert_eq!(
            requests,
            vec![ControlRequest::Pause {
                info_hash_hex: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string()
            }]
        );
    }

    #[test]
    fn cli_set_shared_config_command_parses_without_panicking() {
        Cli::command().debug_assert();

        let parsed = Cli::try_parse_from([
            "superseedr",
            "set-shared-config",
            "C:\\shared-root\\superseedr-config",
        ])
        .expect("set-shared-config command should parse");

        match parsed.command.expect("subcommand") {
            Commands::SetSharedConfig { path } => {
                assert_eq!(path, PathBuf::from("C:\\shared-root\\superseedr-config"));
            }
            other => panic!("unexpected command: {other:?}"),
        }
    }

    #[test]
    fn cli_set_host_id_command_parses_without_panicking() {
        Cli::command().debug_assert();

        let parsed = Cli::try_parse_from(["superseedr", "set-host-id", "office-node"])
            .expect("set-host-id command should parse");

        match parsed.command.expect("subcommand") {
            Commands::SetHostId { host_id } => {
                assert_eq!(host_id, "office-node");
            }
            other => panic!("unexpected command: {other:?}"),
        }
    }

    #[test]
    fn cli_to_shared_command_parses_without_panicking() {
        Cli::command().debug_assert();

        let parsed = Cli::try_parse_from(["superseedr", "to-shared", "C:\\shared-root"])
            .expect("to-shared command should parse");

        match parsed.command.expect("subcommand") {
            Commands::ToShared { path } => {
                assert_eq!(path, PathBuf::from("C:\\shared-root"));
            }
            other => panic!("unexpected command: {other:?}"),
        }
    }
}