use agent_frame::{
Check, Life, Op, Packet, RawPacket, RawUnit, RootEvent, RootKind, SealError, Span, Unit, UnitId,
};
use content_addressable::{ContentAddressable, ContentId};
const SRC: &[u8] = b"the quick brown fox jumps over the lazy dog";
fn root() -> ContentId {
RootEvent::new(RootKind::OperatorPrompt, b"summarise the log", 0)
.id()
.unwrap()
}
fn sealed() -> Unit {
Unit::seal(Op::Elide, SRC, Span::new(4, 9), root()).expect("elide is mintable in v0")
}
#[test]
fn generation_is_the_only_riser() {
for d in [0_u32, 1, 7, 4096] {
assert_eq!(
Op::Concise.depth_after(d),
d,
"concise must not raise depth"
);
assert_eq!(Op::Elide.depth_after(d), d, "elide must not raise depth");
assert_eq!(
Op::Generate.depth_after(d),
d + 1,
"generation is the only riser"
);
}
}
#[test]
fn the_depth_arithmetic_saturates_rather_than_wrapping() {
assert_eq!(
Op::Generate.depth_after(u32::MAX),
u32::MAX,
"saturating, NOT wrapping: a wrap to 0 would satisfy `depth <= 1` by overflow"
);
assert_eq!(Op::Elide.depth_after(u32::MAX), u32::MAX);
let mut raw = sealed().to_raw();
raw.depth = u32::MAX;
assert!(
Unit::try_from(raw).is_err(),
"a saturated depth is still not the depth elision implies"
);
}
#[test]
fn only_elision_asserts_nothing() {
assert!(!Op::Elide.asserts());
assert!(Op::Concise.asserts());
assert!(Op::Generate.asserts());
}
#[test]
fn the_verification_classes_are_the_reproducibility_boundary() {
assert_eq!(Op::Elide.check(), Check::Rederive);
assert_eq!(Op::Concise.check(), Check::Ground);
assert_eq!(Op::Generate.check(), Check::GroundAttest);
for op in [Op::Concise, Op::Elide, Op::Generate] {
assert_eq!(
op.check() == Check::GroundAttest,
op == Op::Generate,
"only generation is non-reproducible, so only generation needs an attestation"
);
}
}
#[test]
fn a_sealed_unit_is_shallow_and_addressed() {
let u = sealed();
assert_eq!(u.depth(), 0);
assert!(u.is_well_formed(), "depth <= 1");
assert_eq!(u.life(), Life::Live);
let d = u.derivation();
assert_eq!(d.span, Span::new(4, 9));
assert_eq!(d.root, root());
assert!(
agent_frame::verify_unit(&u, SRC).is_ok(),
"the address must resolve — that is what 'addressed' now means"
);
}
#[test]
fn v0_refuses_to_mint_anything_it_cannot_check() {
for op in [Op::Concise, Op::Generate] {
let err = Unit::seal(op, SRC, Span::new(4, 9), root()).expect_err("must refuse");
assert!(
matches!(err, SealError::NotMintableInV0 { op: got, .. } if got == op),
"refusal must name the operation, got {err:?}"
);
assert!(
err.to_string().contains("verification class"),
"the refusal must say WHY: {err}"
);
}
}
#[test]
fn the_depth_bound_holds_on_every_construction_path() {
let minted = sealed();
assert!(minted.is_well_formed() && minted.depth() <= 1);
let admitted = Unit::try_from(minted.to_raw()).expect("its own bytes admit");
assert!(admitted.is_well_formed() && admitted.depth() <= 1);
assert_eq!(minted, admitted);
}
#[test]
fn every_unit_names_the_event_that_caused_it() {
let u = sealed();
assert_eq!(u.root(), root());
let kinds = [
RootKind::OperatorPrompt,
RootKind::UserAction,
RootKind::HarnessEvent,
];
let mut ids: Vec<ContentId> = kinds
.iter()
.map(|k| RootEvent::new(*k, b"same words", 0).id().unwrap())
.collect();
ids.sort_unstable();
ids.dedup();
assert_eq!(ids.len(), kinds.len(), "each kind is a distinct event");
}
#[test]
fn eviction_follows_the_root_not_the_operation() {
let live = sealed();
assert!(!live.evictable(), "a live root is not freely evictable");
for dead in [Life::Superseded, Life::Withdrawn] {
assert!(
live.with_life(dead).evictable(),
"{dead:?} makes the unit freely evictable"
);
}
}
#[test]
fn a_packets_identity_binds_the_chain_it_belongs_to() {
let uid = sealed().id().unwrap();
let genesis = Packet::genesis(vec![uid]);
let gid = genesis.id().unwrap();
let child = Packet::following(gid, vec![uid]);
let reparented = Packet::following(child.id().unwrap(), vec![uid]);
assert_ne!(
gid,
child.id().unwrap(),
"same units, different parent, must differ"
);
assert_ne!(
child.id().unwrap(),
reparented.id().unwrap(),
"re-parenting must change the id"
);
assert_eq!(child.prior(), Some(gid));
assert!(genesis.is_genesis() && genesis.prior().is_none());
assert!(!child.is_genesis());
}
#[test]
fn genesis_is_exactly_the_absence_of_a_parent() {
let empty: Vec<UnitId> = vec![];
let g = Packet::genesis(empty.clone());
assert!(g.is_genesis() && g.parents().is_empty());
let child = Packet::following(g.id().unwrap(), empty);
assert!(!child.is_genesis());
assert_eq!(child.parents().len(), 1);
}
#[test]
fn canonical_bytes_round_trip_and_the_id_is_stable() {
let u = sealed();
let bytes = u.canonical_form().expect("a unit encodes");
let id = u.id().expect("a unit addresses");
assert_eq!(
id.as_content_id(),
&u.derivation().id().unwrap(),
"the unit's id is its derivation's id"
);
assert_eq!(
content_addressable::ContentId::from_canonical_bytes(&bytes),
u.derivation().id().unwrap()
);
let wire = serde_json::to_vec(&u).expect("a unit serialises");
let raw: RawUnit = serde_json::from_slice(&wire).expect("the wire form is a RawUnit");
let back = Unit::try_from(raw).expect("its own bytes admit");
assert_eq!(back, u);
assert_eq!(back.id().unwrap(), id);
let p = Packet::following(Packet::genesis(vec![id]).id().unwrap(), vec![id]);
let pw = serde_json::to_vec(&p).unwrap();
let praw: RawPacket = serde_json::from_slice(&pw).unwrap();
let pb = Packet::try_from(praw).expect("its own bytes admit");
assert_eq!(pb, p);
assert_eq!(pb.id().unwrap(), p.id().unwrap());
}