use super::*;
fn entry(files: &[&str], fingerprint: &str) -> AdoptionEntry {
AdoptionEntry {
anchor: "root/control/button".to_owned(),
certifies: "call chain and implementation".to_owned(),
evidence: "converge: 6 live edges".to_owned(),
verifier: "maintainer".to_owned(),
at: "2026-09-29T14:00:00Z".to_owned(),
files: files.iter().map(|file| (*file).to_owned()).collect(),
fingerprint: fingerprint.to_owned(),
reason: "re-confirmed after the chain fix".to_owned(),
}
}
#[test]
fn a_ledger_line_round_trips_through_its_own_parser() {
let ledger = "# anchor|certifies|evidence|verifier|at|files|fingerprint|reason\nroot/control/\
button|chain+impl|converge: 6 live|maintainer|2026-09-29T14:00:00Z|a.rs,b.rs|\
deadbeef|first adoption\n";
let entries = parse_adoption(ledger).expect("the line parses");
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].anchor, "root/control/button");
assert_eq!(entries[0].files, vec!["a.rs".to_owned(), "b.rs".to_owned()]);
assert_eq!(entries[0].fingerprint, "deadbeef");
assert_eq!(entries[0].reason, "first adoption");
}
#[test]
fn a_line_missing_a_field_is_refused_with_its_line_number() {
let ledger = "root/control/button|chain|evidence|maintainer|2026-09-29|a.rs\n";
let error = parse_adoption(ledger).expect_err("six fields is not a line");
assert_eq!(error.line, 1);
assert!(error.message.contains("8 fields"), "{error:?}");
}
#[test]
fn an_entry_naming_no_file_is_refused() {
let ledger = "a|b|c|d|e| |f|g\n";
let error = parse_adoption(ledger).expect_err("an empty field is refused");
assert!(error.message.contains("empty field"), "{error:?}");
}
#[test]
fn the_fingerprint_ignores_the_order_files_were_read_in() {
let first = adoption_fingerprint(&[
("a.rs".to_owned(), "alpha".to_owned()),
("b.rs".to_owned(), "beta".to_owned()),
]);
let second = adoption_fingerprint(&[
("b.rs".to_owned(), "beta".to_owned()),
("a.rs".to_owned(), "alpha".to_owned()),
]);
assert_eq!(first, second);
let different = adoption_fingerprint(&[
("a.rs".to_owned(), "alpha".to_owned()),
("b.rs".to_owned(), "gamma".to_owned()),
]);
assert_ne!(first, different);
}
#[test]
fn an_unchanged_tree_keeps_the_lease_and_a_changed_byte_lapses_it() {
let files = vec![
("a.rs".to_owned(), "alpha".to_owned()),
("b.rs".to_owned(), "beta".to_owned()),
];
let held = entry(&["a.rs", "b.rs"], &adoption_fingerprint(&files));
assert_eq!(verdict_of(&held, &files), AdoptionVerdict::Provisional);
let moved = vec![
("a.rs".to_owned(), "alpha".to_owned()),
("b.rs".to_owned(), "beta ".to_owned()),
];
assert!(matches!(
verdict_of(&held, &moved),
AdoptionVerdict::Lapsed { .. }
));
}
#[test]
fn a_missing_file_lapses_the_lease_and_is_the_one_named() {
let files = vec![("a.rs".to_owned(), "alpha".to_owned())];
let held = entry(&["a.rs", "gone.rs"], "whatever");
assert_eq!(
verdict_of(&held, &files),
AdoptionVerdict::Lapsed {
file: "gone.rs".to_owned()
}
);
}
#[test]
fn the_state_carries_the_fingerprint_its_verdict_came_from() {
let files = vec![
("a.rs".to_owned(), "alpha".to_owned()),
("b.rs".to_owned(), "beta".to_owned()),
];
let recorded = adoption_fingerprint(&files);
let held = entry(&["a.rs", "b.rs"], &recorded);
let state = state_of(&held, &files);
assert_eq!(state.verdict, AdoptionVerdict::Provisional);
assert_eq!(
state.current, recorded,
"an unchanged tree gives back the fingerprint it was taken on"
);
assert_eq!(
state_of(&held, &files).verdict,
verdict_of(&held, &files),
"the thin reading agrees with the state it is read from"
);
let moved = vec![
("a.rs".to_owned(), "alpha".to_owned()),
("b.rs".to_owned(), "beta!".to_owned()),
];
let after = state_of(&held, &moved);
assert_ne!(after.current, recorded, "a moved byte changes the value");
assert!(matches!(after.verdict, AdoptionVerdict::Lapsed { .. }));
assert_eq!(after.verdict, verdict_of(&held, &moved));
}
#[test]
fn a_missing_file_still_yields_a_current_fingerprint() {
let files = vec![("a.rs".to_owned(), "alpha".to_owned())];
let held = entry(&["a.rs", "gone.rs"], "whatever");
let state = state_of(&held, &files);
assert_eq!(
state.verdict,
AdoptionVerdict::Lapsed {
file: "gone.rs".to_owned()
}
);
assert_eq!(
state.current.len(),
64,
"a fingerprint, not an empty string"
);
}
#[test]
fn the_per_file_prints_round_trip_and_leave_the_entries_untouched() {
let pairs = vec![
("src/a.rs".to_owned(), "aa11".to_owned()),
("src/b.rs".to_owned(), "bb22".to_owned()),
];
let entry = "root/button|chain|converge: 1 edge|maintainer|2026-10-02T10:00:00Z|src/a.rs,src/b.rs|setprint|why\n";
let ledger = format!(
"{entry}{}",
render_adoption_prints("root/button", "2026-10-02T10:00:00Z", &pairs)
);
let parsed = parse_adoption_prints(&ledger).expect("the prints parse");
assert_eq!(parsed.len(), 1, "{parsed:?}");
assert_eq!(parsed[0].anchor, "root/button");
assert_eq!(parsed[0].at, "2026-10-02T10:00:00Z");
assert_eq!(parsed[0].files, pairs);
let entries = parse_adoption(&ledger).expect("the entry still parses");
assert_eq!(entries.len(), 1, "the comment line is not an entry");
assert_eq!(entries[0].fingerprint, "setprint");
}
#[test]
fn a_malformed_prints_line_is_refused_rather_than_skipped() {
let bad = "# prints root/button | 2026-10-02T10:00:00Z | src/a.rs\n";
let error = parse_adoption_prints(bad).expect_err("a pair without `=` is not a print");
assert!(error.message.contains("`<path>=<print>`"), "{error:?}");
assert_eq!(error.line, 1);
}
#[test]
fn an_object_binding_is_additive_and_validated() {
use super::{parse_adoption_objects, render_adoption_object};
let fingerprint = "0".repeat(64);
let ledger = format!(
"policy|certifies|evidence|person|12:00|policy.txt|{fingerprint}|why\n\
# prints policy | 12:00 | policy.txt=abc\n\
# object policy | 12:00 | 0123456789abcdef0123456789abcdef\n"
);
let entries = super::parse_adoption(&ledger).expect("the entry line is unchanged");
assert_eq!(entries.len(), 1, "a comment line is not an entry");
assert_eq!(entries[0].anchor, "policy");
let objects = parse_adoption_objects(&ledger).expect("the binding parses");
assert_eq!(objects.len(), 1);
assert_eq!(objects[0].stable, "0123456789abcdef0123456789abcdef");
let none = format!("policy|certifies|evidence|person|12:00|policy.txt|{fingerprint}|why\n");
assert!(
parse_adoption_objects(&none)
.expect("no binding is lawful")
.is_empty(),
"an older ledger carries no binding and still parses"
);
assert_eq!(
render_adoption_object("policy", "12:00", "0123456789abcdef0123456789abcdef"),
"# object policy | 12:00 | 0123456789abcdef0123456789abcdef\n"
);
let short = "# object policy | 12:00 | deadbeef\n";
let error = parse_adoption_objects(short).expect_err("a short identity is refused");
assert!(error.message.contains("32-hex"), "{error:?}");
}