#![allow(
clippy::expect_used,
clippy::tests_outside_test_module,
reason = "integration test: `expect` is the idiomatic fail-fast, and test fns live at crate root by construction"
)]
use std::path::Path;
use std::process::Command;
const BIN: &str = env!("CARGO_BIN_EXE_doctrine");
fn doctrine(dir: &Path, args: &[&str]) -> String {
let out = Command::new(BIN)
.args(args)
.arg("-p")
.arg(dir)
.output()
.expect("spawn doctrine");
assert!(
out.status.success(),
"doctrine {args:?} failed: {}\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
String::from_utf8(out.stdout).expect("utf8 stdout")
}
fn normalised_toml(dir: &Path, uid: &str) -> String {
let path = dir
.join(".doctrine/memory/items")
.join(uid)
.join("memory.toml");
let raw = std::fs::read_to_string(&path).expect("read memory.toml");
raw.lines()
.filter(|l| {
let k = l.split('=').next().unwrap_or("").trim();
!matches!(
k,
"memory_uid" | "created" | "updated" | "commit" | "checkout_state_id"
)
})
.collect::<Vec<_>>()
.join("\n")
}
fn parse_uid(stdout: &str) -> String {
stdout
.split_whitespace()
.find(|tok| tok.starts_with("mem_"))
.expect("record line names a mem_ uid")
.trim_end_matches(':')
.to_owned()
}
#[test]
fn memory_new_is_an_alias_of_memory_record_and_creates_an_identical_entity() {
let tmp = tempfile::tempdir().expect("tempdir");
let dir = tmp.path();
let common = &[
"--type",
"pattern",
"--summary",
"one liner",
"--tag",
"cli",
];
let via_record = doctrine(
dir,
&[&["memory", "record", "Alias proof"], common.as_slice()].concat(),
);
let via_new = doctrine(
dir,
&[&["memory", "new", "Alias proof"], common.as_slice()].concat(),
);
let uid_record = parse_uid(&via_record);
let uid_new = parse_uid(&via_new);
assert_ne!(uid_record, uid_new, "each call mints a fresh uid");
let shown_record = doctrine(dir, &["memory", "show", &uid_record]);
let shown_new = doctrine(dir, &["memory", "show", &uid_new]);
assert!(shown_record.contains("Alias proof"));
assert!(shown_new.contains("Alias proof"));
assert_eq!(
normalised_toml(dir, &uid_record),
normalised_toml(dir, &uid_new),
"memory new and memory record produce the identical entity shape"
);
}