use std::{ffi::OsString, io};

use clap::Parser;
use repo2markdown::path_list_editor::append_paths;

/// Append paths to a null-separated file list from stdin.
#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
struct Cli {
    /// Paths to append to the input sequence.
    paths: Vec<OsString>,
}

fn main() -> io::Result<()> {
    let cli = Cli::parse();
    append_paths(io::stdin().lock(), io::stdout().lock(), &cli.paths)
}

#[cfg(test)]
mod tests {
    use std::ffi::OsString;

    use clap::Parser;

    use super::Cli;

    #[test]
    fn accepts_no_paths() {
        let cli = Cli::try_parse_from(["r2md-append"]).unwrap();

        assert_eq!(cli.paths, Vec::<OsString>::new());
    }

    #[test]
    fn accepts_one_path() {
        let cli = Cli::try_parse_from(["r2md-append", "README.md"]).unwrap();

        assert_eq!(cli.paths, vec![OsString::from("README.md")]);
    }

    #[test]
    fn accepts_multiple_paths() {
        let cli = Cli::try_parse_from(["r2md-append", "README.md", "src/main.rs"]).unwrap();

        assert_eq!(
            cli.paths,
            vec![OsString::from("README.md"), OsString::from("src/main.rs")]
        );
    }
}
