use super::*;
struct ConvergingAnvil {
j: Journal,
}
impl Anvil for ConvergingAnvil {
fn head(&self) -> io::Result<String> {
Ok("T0".into())
}
fn open(&self, _dir: &Path) -> io::Result<()> {
self.j.borrow_mut().push("open".into());
Ok(())
}
fn changed(&self, _dir: &Path) -> io::Result<Vec<String>> {
Ok(Vec::new()) }
fn seal(&self, _dir: &Path, _message: &str) -> io::Result<String> {
self.j.borrow_mut().push("seal".into());
Ok("T0".into())
}
fn unseal(&self, sha: &str) -> io::Result<()> {
self.j.borrow_mut().push(format!("unseal:{sha}"));
Ok(())
}
fn close(&self, _dir: &Path) -> io::Result<()> {
self.j.borrow_mut().push("close".into());
Ok(())
}
}
struct NarratedBase;
impl BaseChange for NarratedBase {
fn stage(&self, _dir: &Path) -> io::Result<()> {
Ok(())
}
fn finalize(&self, _dir: &Path) -> io::Result<String> {
Ok("MSG".into())
}
fn narrated(&self) -> bool {
true
}
}
#[test]
fn the_converging_fakes_unseal_only_journals() {
let jrn = journal();
let anvil = ConvergingAnvil { j: jrn.clone() };
anvil.unseal("X").unwrap();
assert_eq!(*jrn.borrow(), ["unseal:X"]);
}
#[test]
fn a_narrated_op_refuses_the_noop_seal_and_unwinds() {
let jrn = journal();
let anvil = ConvergingAnvil { j: jrn.clone() };
let plugins = FakePlugins::new(jrn.clone(), None);
let pre = [plugin("p")];
let post = [plugin("q")];
let err = Engine::new(&anvil, &plugins, &test_log())
.seal(&NarratedBase, Verb::Update, Path::new("/c"), &pre, &post)
.unwrap_err();
assert!(err.to_string().contains("-m note"), "names the dropped note: {err}");
assert!(format!("{err:?}").contains("Narration"));
assert_eq!(*jrn.borrow(), ["open", "run:p:pre", "seal", "rollback:p:pre", "close"]);
}
#[test]
fn an_unnarrated_noop_seal_still_converges_quietly() {
let jrn = journal();
let anvil = ConvergingAnvil { j: jrn.clone() };
let plugins = FakePlugins::new(jrn.clone(), None);
let base = FakeBase { j: jrn.clone(), fail_stage: false, fail_finalize: false };
let post = [plugin("q")];
let sha = Engine::new(&anvil, &plugins, &test_log())
.seal(&base, Verb::Update, Path::new("/c"), &[], &post)
.unwrap();
assert_eq!(sha, "T0");
assert_eq!(*plugins.seen.borrow(), [("q".into(), Some("T0".into()))]);
}