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
}
fn current_op_cid(repo: &Path) -> String {
let out = mnem(repo, &["log", "-n", "1"]).assert().success();
let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
for line in stdout.lines() {
if let Some(rest) = line.strip_prefix("op ") {
return rest.trim().to_string();
}
}
panic!("log -n 1 had no op line: {stdout}");
}
#[test]
fn cat_file_raw_bytes_look_like_cbor_map() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
mnem(dir.path(), &["add", "node", "--summary", "a", "--no-embed"])
.assert()
.success();
let op = current_op_cid(dir.path());
let out = mnem(dir.path(), &["cat-file", &op]).assert().success();
let stdout = &out.get_output().stdout;
assert!(!stdout.is_empty(), "raw cat-file should emit bytes");
let first = stdout[0];
assert!(
(0xa0..=0xbf).contains(&first),
"expected CBOR map first byte (0xa0..0xbf), got {first:#x}"
);
}
#[test]
fn cat_file_json_carries_kind_field() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
mnem(dir.path(), &["add", "node", "--summary", "b", "--no-embed"])
.assert()
.success();
let op = current_op_cid(dir.path());
let out = mnem(dir.path(), &["cat-file", &op, "--json"])
.assert()
.success();
let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
assert!(
stdout.contains("\"_kind\""),
"JSON must carry _kind discriminator, got: {stdout}"
);
assert!(
stdout.contains("\"operation\""),
"this op block must be of kind `operation`, got: {stdout}"
);
}
#[test]
fn cat_file_unknown_cid_errors() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
let missing = "bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc3pgi";
let out = mnem(dir.path(), &["cat-file", missing]).assert().failure();
let stderr = String::from_utf8_lossy(&out.get_output().stderr).to_string();
assert!(
stderr.contains("not present"),
"expected not-present error, got: {stderr}"
);
}