use assert_cmd::cargo::*;
use predicates::prelude::*;

// These are some very basic integration tests, making sure that at least the
// key derivation, and decryption and decompression of objects work.

// TODO: Look into trycmd (https://docs.rs/trycmd/latest/trycmd/).

#[test]
fn inspect_encryptedkeyset() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = cargo_bin_cmd!("arq-restore");

    cmd.arg("inspect")
        .arg("--password=test2")
        .arg("tests/data/target/EE16685F-FEC5-4A13-A7F8-5296E2EF75B9/encryptedkeyset.dat");
    cmd.assert().success().stdout(predicate::str::contains(
        // look for master encryption key
        "1e7f02ada8e2cb33d3b29ccd6901745f84a9e8707dca8d0179c319ccc26c036b",
    ));

    Ok(())
}

#[test]
fn inspect_backupplan() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = cargo_bin_cmd!("arq-restore");

    cmd.arg("inspect")
        .arg("--password=test2")
        .arg("--raw")
        .arg("tests/data/target/EE16685F-FEC5-4A13-A7F8-5296E2EF75B9/backupplan.json");
    cmd.assert().success().stdout(predicate::str::contains(
        // check if the the backup plan UUID is in the output, which is prove that it could be
        // decrypted.
        "EE16685F-FEC5-4A13-A7F8-5296E2EF75B9",
    ));

    Ok(())
}

#[test]
fn inspect_backuprecord() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = cargo_bin_cmd!("arq-restore");

    cmd.arg("inspect").arg("--password=test2").arg("--raw").arg(
        "tests/data/target/EE16685F-FEC5-4A13-A7F8-5296E2EF75B9/backupfolders/4FD1288D-C741-483C-8AB3-90ACB2EDE1D9/backuprecords/00176/7798215.backuprecord",
    );
    cmd.assert().success().stdout(predicate::str::contains(
        // check if the the backup folder UUID is in the output, which is prove that it could be
        // decrypted.
        "4FD1288D-C741-483C-8AB3-90ACB2EDE1D9",
    ));

    Ok(())
}
