use std::path::Path;
use std::process::Command;
use assert_cmd::prelude::*;
use tempfile::TempDir;
fn mnem(repo: &Path, args: &[&str]) -> Command {
let mut cmd = Command::cargo_bin("mnem").expect("built mnem binary");
cmd.current_dir(repo);
cmd.arg("-R").arg(repo);
for a in args {
cmd.arg(a);
}
cmd
}
#[test]
fn freshly_initialized_repo_exports_successfully() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
let car = dir.path().join("out.car");
let out = mnem(dir.path(), &["export", car.to_str().unwrap()])
.assert()
.success();
let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
assert!(
stdout.contains("exported") && stdout.contains("blocks"),
"expected export confirmation, got: {stdout}"
);
}
#[test]
fn export_then_import_round_trip() {
let src = TempDir::new().unwrap();
mnem(src.path(), &["init", src.path().to_str().unwrap()])
.assert()
.success();
mnem(
src.path(),
&[
"add",
"node",
"--summary",
"roundtrip payload",
"--prop",
"kind=doc",
],
)
.assert()
.success();
let car = src.path().join("snapshot.car");
let export_out = mnem(
src.path(),
&["export", car.to_str().unwrap(), "--from", "HEAD"],
)
.assert()
.success();
let export_stdout = String::from_utf8_lossy(&export_out.get_output().stdout).to_string();
assert!(
export_stdout.starts_with("exported "),
"export stdout: {export_stdout}"
);
assert!(car.exists(), "CAR file must be produced on disk");
let car_size = std::fs::metadata(&car).unwrap().len();
assert!(car_size > 0, "CAR must be non-empty");
let exported_n = export_stdout
.split_whitespace()
.nth(1)
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or_else(|| panic!("could not parse block count from: {export_stdout}"));
assert!(
exported_n >= 4,
"a committed repo has commit+view+op+trees, got {exported_n}"
);
let dst = TempDir::new().unwrap();
mnem(dst.path(), &["init", dst.path().to_str().unwrap()])
.assert()
.success();
let import_out = mnem(dst.path(), &["import", car.to_str().unwrap()])
.assert()
.success();
let import_stdout = String::from_utf8_lossy(&import_out.get_output().stdout).to_string();
assert!(
import_stdout.starts_with("imported "),
"import stdout: {import_stdout}"
);
let imported_n = import_stdout
.split_whitespace()
.nth(1)
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or_else(|| panic!("could not parse block count from: {import_stdout}"));
assert_eq!(
exported_n, imported_n,
"block counts must match across export / import"
);
}