use super::*;
#[test]
fn test_collation_follows_typing_without_replay() {
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 1, 1, 8);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: dots[5].into(),
to: locus_at(&clk, Anchor::After(dots[1].into())),
}
));
let mut view = text.recension(&moves);
let (replays, rebuilds) = (view.collation_replays(), view.collation_rebuilds());
for keystroke in 0..32u64 {
let _ = typed_chain(&mut text, &clk, 1, 9 + keystroke, 1);
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
}
assert_eq!(view.collation_replays(), replays, "typing never replays");
assert_eq!(view.collation_rebuilds(), rebuilds, "typing never rebuilds");
}
#[test]
fn test_an_out_of_order_move_unwinds_and_replays() {
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 1, 1, 6);
let low = Metathesis {
target: dots[4].into(),
to: locus_at(&clk, Anchor::After(dots[0].into())),
};
let high = Metathesis {
target: dots[4].into(),
to: locus_at(&clk, Anchor::After(dots[2].into())),
};
let mut only_high = Metatheses::new();
assert!(only_high.insert(d(1, 101), high));
let mut view = text.recension(&Metatheses::new());
view.collate(&text, &only_high);
view.check_collation(&text.recension(&only_high));
let mut both = only_high.clone();
assert!(both.insert(d(1, 100), low));
let replays = view.collation_replays();
view.collate(&text, &both);
view.check_collation(&text.recension(&both));
assert!(
view.collation_replays() > replays,
"a below-rank arrival replays the suffix"
);
assert_eq!(
view.locus(dots[4]),
Some(high.to),
"the highest-rank surviving testimony wins"
);
}
#[test]
fn test_a_late_birth_reverdicts_a_walked_verdict() {
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 1, 1, 3);
let unborn = d(2, 1);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: dots[1].into(),
to: locus_at(&clk, Anchor::After(unborn.into())),
}
));
let mut view = text.recension(&Metatheses::new());
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert!(
view.refused().is_empty(),
"the walk ends at the unborn link"
);
assert!(text.weave(
unborn,
Locus {
anchor: Anchor::After(dots[1].into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(
view.refused(),
&[d(1, 100)],
"the extended chain refuses the move"
);
}
#[test]
fn test_a_guard_skipped_verdict_reverdicts_on_the_births_arrival() {
let clk = clock(1);
let mut text = Rhapsody::new();
let target = d(3, 1);
assert!(text.weave(
target,
Locus {
anchor: Anchor::Origin,
rank: clk.now(0u16),
}
));
let unborn = d(1, 1);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: target.into(),
to: locus_at(&clk, Anchor::After(unborn.into())),
}
));
let mut view = text.recension(&Metatheses::new());
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert!(view.refused().is_empty(), "no walk ran and no cycle exists");
assert!(text.weave(
unborn,
Locus {
anchor: Anchor::After(target.into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(
view.refused(),
&[d(1, 100)],
"the guard-skipped verdict flips with the birth"
);
}
#[test]
fn test_a_dangling_weave_behind_the_anchor_reverdicts_on_its_birth() {
let clk = clock(1);
let mut text = Rhapsody::new();
let target = d(3, 1);
assert!(text.weave(
target,
Locus {
anchor: Anchor::Origin,
rank: clk.now(0u16),
}
));
let mut view = text.recension(&Metatheses::new());
let unborn = d(1, 1);
let behind = d(2, 1);
assert!(text.weave(
behind,
Locus {
anchor: Anchor::After(unborn.into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &Metatheses::new());
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: target.into(),
to: locus_at(&clk, Anchor::After(behind.into())),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert!(
view.refused().is_empty(),
"the chain ends at the unborn link"
);
assert!(text.weave(
unborn,
Locus {
anchor: Anchor::After(target.into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(
view.refused(),
&[d(1, 100)],
"the dangling weave's fold-time reference re-earns the verdict"
);
}
#[test]
fn test_a_view_built_over_an_unborn_anchor_reverdicts_on_its_birth() {
let clk = clock(1);
let mut text = Rhapsody::new();
let target = d(3, 1);
assert!(text.weave(
target,
Locus {
anchor: Anchor::Origin,
rank: clk.now(0u16),
}
));
let unborn = d(1, 1);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: target.into(),
to: locus_at(&clk, Anchor::After(unborn.into())),
}
));
let mut view = text.recension(&moves);
assert!(
view.refused().is_empty(),
"the chain ends at the unborn link"
);
assert!(text.weave(
unborn,
Locus {
anchor: Anchor::After(target.into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(
view.refused(),
&[d(1, 100)],
"the whole replay's play-application recording re-earns the verdict"
);
}
#[test]
fn test_a_view_built_over_a_dangling_weave_reverdicts_on_its_birth() {
let clk = clock(1);
let mut text = Rhapsody::new();
let target = d(3, 1);
assert!(text.weave(
target,
Locus {
anchor: Anchor::Origin,
rank: clk.now(0u16),
}
));
let unborn = d(1, 1);
let behind = d(2, 1);
assert!(text.weave(
behind,
Locus {
anchor: Anchor::After(unborn.into()),
rank: clk.now(0u16),
}
));
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: target.into(),
to: locus_at(&clk, Anchor::After(behind.into())),
}
));
let mut view = text.recension(&moves);
assert!(
view.refused().is_empty(),
"the chain ends at the unborn link"
);
assert!(text.weave(
unborn,
Locus {
anchor: Anchor::After(target.into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(
view.refused(),
&[d(1, 100)],
"the whole replay's birth-fold recording re-earns the verdict"
);
}
#[test]
fn test_an_undone_move_restores_through_the_collation() {
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 1, 1, 6);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: dots[4].into(),
to: locus_at(&clk, Anchor::After(dots[0].into())),
}
));
let mut view = text.recension(&moves);
let rearranged = view.order();
let undone = Metatheses::new();
view.collate(&text, &undone);
view.check_collation(&text.recension(&undone));
assert_eq!(view.order(), text.order(), "the birth order returns");
assert_ne!(view.order(), rearranged, "the move had moved something");
}
#[test]
fn test_a_pending_move_activates_through_the_collation() {
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 1, 1, 4);
let foreign = d(2, 1);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: foreign.into(),
to: locus_at(&clk, Anchor::After(dots[0].into())),
}
));
let mut view = text.recension(&Metatheses::new());
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(view.pending(), &[d(1, 100)], "the outrun move parks");
assert!(text.weave(
foreign,
Locus {
anchor: Anchor::After(dots[3].into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert!(view.pending().is_empty(), "the birth activates the move");
assert_eq!(
view.order_at(1),
Some(foreign),
"the activated move places its target"
);
}
#[test]
fn test_a_condense_round_rebuilds_the_collation() {
use crate::metis::DotStore;
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 1, 1, 6);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: dots[2].into(),
to: locus_at(&clk, Anchor::After(dots[4].into())),
}
));
let mut view = text.recension(&moves);
let rebuilds = view.collation_rebuilds();
let tail = dots[5];
let mut removal_context = DotSet::new();
let _ = removal_context.insert(tail);
text.causal_merge_from(&DotSet::new(), &Rhapsody::new(), &removal_context);
assert!(!text.is_visible(tail), "the tail is a tombstone");
let excised = text.condense(&Retired::trust(removal_context));
assert_eq!(excised, 1, "the sterile tail excises");
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(
view.collation_rebuilds(),
rebuilds + 1,
"a shrunken skeleton takes the whole-replay fallback"
);
}
#[test]
fn test_a_late_birth_under_a_twice_moved_target_unwinds_safely() {
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 3, 1, 4);
let target = dots[1];
let unborn = d(1, 1);
let mut moves = Metatheses::new();
assert!(moves.insert(
d(1, 100),
Metathesis {
target: target.into(),
to: locus_at(&clk, Anchor::After(unborn.into())),
}
));
assert!(moves.insert(
d(1, 101),
Metathesis {
target: target.into(),
to: locus_at(&clk, Anchor::Before(dots[0].into())),
}
));
let mut view = text.recension(&moves);
view.check_collation(&text.recension(&moves));
assert!(view.refused().is_empty(), "both chains end lawfully today");
assert!(text.weave(
unborn,
Locus {
anchor: Anchor::After(target.into()),
rank: clk.now(0u16),
}
));
view.collate(&text, &moves);
view.check_collation(&text.recension(&moves));
assert_eq!(
view.refused(),
&[d(1, 100)],
"the earlier move refuses under the extended chain"
);
}
#[test]
fn test_a_replaced_testimony_collates_as_a_replacement() {
let clk = clock(1);
let mut text = Rhapsody::new();
let dots = typed_chain(&mut text, &clk, 1, 1, 6);
let witness = d(1, 100);
let mut first = Metatheses::new();
assert!(first.insert(
witness,
Metathesis {
target: dots[4].into(),
to: locus_at(&clk, Anchor::After(dots[0].into())),
}
));
let mut view = text.recension(&first);
let first_order = view.order();
let mut second = Metatheses::new();
assert!(second.insert(
witness,
Metathesis {
target: dots[2].into(),
to: locus_at(&clk, Anchor::After(dots[5].into())),
}
));
view.collate(&text, &second);
view.check_collation(&text.recension(&second));
assert_ne!(view.order(), first_order, "the replacement re-placed");
}
#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "a collated text keeps the honest basis")]
fn test_an_unrelated_text_violates_the_basis_loudly() {
let clk = clock(1);
let mut original = Rhapsody::new();
let dot = d(1, 1);
assert!(original.weave(
dot,
Locus {
anchor: Anchor::Origin,
rank: clk.now(0u16),
}
));
let mut view = original.recension(&Metatheses::new());
let mut unrelated = Rhapsody::new();
let decoy = d(2, 1);
assert!(unrelated.weave(
decoy,
Locus {
anchor: Anchor::Origin,
rank: clk.now(0u16),
}
));
assert!(unrelated.weave(
dot,
Locus {
anchor: Anchor::After(decoy.into()),
rank: clk.now(0u16),
}
));
view.collate(&unrelated, &Metatheses::new());
}