agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
Documentation
use super::*;
use sha2::{Digest as _, Sha256};

fn manifest_for(target: &str, version: &str, data: &[u8]) -> Manifest {
    let extension = if target.starts_with("windows-") {
        "zip"
    } else {
        "tar.gz"
    };
    Manifest {
        version: version.parse().unwrap(),
        generation: 1,
        targets: HashMap::from([(
            target.to_owned(),
            Asset {
                url: format!("{RELEASES}/{version}/herdr-core-{target}.{extension}"),
                sha256: hex::encode(Sha256::digest(data)),
            },
        )]),
    }
}

#[test]
fn rejects_bad_assets_before_opening_an_installation_record() {
    let target = "linux-x86_64";
    let data = b"archive";
    let mut release = manifest_for(target, "0.1.22", data);
    release.targets.get_mut(target).unwrap().url = "https://example.invalid/core.tar.gz".to_owned();
    assert!(release.asset(target).is_err());
    let release = manifest_for(target, "0.1.22", data);
    let root = tempfile::tempdir().unwrap();
    assert!(prepare(root.path(), &release, target, b"modified".as_slice()).is_err());
    assert!(!root.path().join("current.json").exists());
}

#[test]
fn windows_archive_retains_the_bundled_conpty_runtime() {
    let mut output = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
    let options = zip::write::SimpleFileOptions::default();
    for path in [
        "herdr-core.exe",
        "conpty/conpty.dll",
        "conpty/x64/OpenConsole.exe",
    ] {
        output.start_file(path, options).unwrap();
        output.write_all(b"test").unwrap();
    }
    let data = output.finish().unwrap().into_inner();
    let release = manifest_for("windows-x86_64", "0.1.22", &data);
    let asset = release.asset("windows-x86_64").unwrap();
    let archive = release::verified_archive(data.as_slice(), &asset.sha256).unwrap();
    let dir = tempfile::tempdir().unwrap();
    let executable = release::unpack_windows(archive, dir.path(), "herdr-core.exe").unwrap();
    assert_eq!(executable, Path::new("herdr-core.exe"));
    assert!(dir.path().join("conpty/conpty.dll").is_file());
    assert!(dir.path().join("conpty/x64/OpenConsole.exe").is_file());
}

#[cfg(target_os = "macos")]
#[test]
#[ignore = "requires the downloaded official macOS Herdr Core archive"]
fn installs_the_official_mac_bundle_in_an_isolated_directory() {
    let target = release::target().unwrap();
    let file =
        std::env::var_os("AGENT_HUB_HC_TEST_ARCHIVE").expect("set AGENT_HUB_HC_TEST_ARCHIVE");
    let data = std::fs::read(file).unwrap();
    let release = manifest_for(&target, "0.1.22", &data);
    let dir = tempfile::tempdir().unwrap();
    let installed = prepare(dir.path(), &release, &target, data.as_slice()).unwrap();
    assert!(installed
        .path
        .ends_with("Herdr Core.app/Contents/MacOS/herdr-core"));
    assert_eq!(installed.version, "0.1.22");
    let record = std::fs::read(dir.path().join("current.json")).unwrap();
    assert!(prepare(dir.path(), &release, &target, b"corrupted".as_slice()).is_err());
    assert_eq!(
        std::fs::read(dir.path().join("current.json")).unwrap(),
        record
    );
}