minerva 0.2.0

Causal ordering for distributed systems
use crate::metis::{Anchor, Dotted, Locus};

use super::super::{Seq, clock, delete, insert, woven};

#[test]
fn test_anchor_for_visual_insert_returns_the_base_on_an_empty_bucket() {
    // The Fugue placement rule (S127): on an EMPTY base bucket the seam returns
    // the base directly (`Origin` at the document start, `After(a)` when the
    // caret sits after a childless `a`), so a top rank there lands right after
    // the caret.
    let clk = clock(1);
    let mut r: Seq = Dotted::new();
    assert_eq!(r.store().anchor_for_visual_insert(None), Anchor::Origin);
    // A lone element 'a' at the origin: after 'a' its After bucket is empty.
    let dot_a = insert(&mut r, &clk, 1, None);
    assert_eq!(
        r.store().anchor_for_visual_insert(Some(dot_a.into())),
        Anchor::After(dot_a.into())
    );
}

#[test]
fn test_anchor_for_visual_insert_returns_before_the_successor_on_a_nonempty_bucket() {
    // On a NONEMPTY base bucket the caret's in-order successor exists in the
    // subtree, so the seam returns `Before(successor)` (S127). Build "cat" as a
    // chain; the caret after 'a' has successor 't', so the seam hangs Before 't'.
    let clk = clock(1);
    let mut r: Seq = Dotted::new();
    let dot_c = insert(&mut r, &clk, 1, None);
    let dot_a = insert(&mut r, &clk, 1, Some(dot_c));
    let dot_t = insert(&mut r, &clk, 1, Some(dot_a));
    assert_eq!(
        r.store().anchor_for_visual_insert(Some(dot_a.into())),
        Anchor::Before(dot_t.into())
    );
    // At the document start the successor is the head 'c'.
    assert_eq!(
        r.store().anchor_for_visual_insert(None),
        Anchor::Before(dot_c.into())
    );
}

#[test]
fn test_anchor_for_visual_insert_descends_a_before_chain_through_a_tombstone() {
    // The descent reads the SKELETON, not visibility, so the successor it finds
    // may be an invisible tombstone (S127). Build 'c' then 'a' after 'c', then a
    // Before-child of 'a' that we delete; the caret at the document start still
    // descends past the head into the deepest Before element (the tombstone).
    let clk = clock(1);
    let mut r: Seq = Dotted::new();
    let dot_c = insert(&mut r, &clk, 1, None);
    let _dot_a = insert(&mut r, &clk, 1, Some(dot_c));
    // Weave a Before-child of 'c' with a top rank so it becomes the head's
    // deepest before-region element, then delete it (a tombstone the descent
    // must still read through).
    let before_rank = clk.now(0u16);
    let dot_b = r.next_dot(1);
    r = r.merge(&woven(
        dot_b,
        Locus {
            anchor: Anchor::Before(dot_c.into()),
            rank: before_rank,
        },
    ));
    delete(&mut r, dot_b);
    // The document-start caret's successor is the deepest Before element down
    // from the head 'c', which is the tombstone `dot_b`.
    assert_eq!(
        r.store().anchor_for_visual_insert(None),
        Anchor::Before(dot_b.into())
    );
}

#[test]
fn test_visual_insert_top_rank_lands_immediately_after_the_caret() {
    // THE "catX" cure at the store level. Build "cat" as a chain, then weave 'X'
    // at `anchor_for_visual_insert(Some(a))` (= `Before(t)`) with a rank ABOVE
    // `children_of(that anchor)`: it lands immediately after 'a', reading "caXt".
    let clk = clock(1);
    let mut r: Seq = Dotted::new();
    let dot_c = insert(&mut r, &clk, 1, None);
    let dot_a = insert(&mut r, &clk, 1, Some(dot_c));
    let dot_t = insert(&mut r, &clk, 1, Some(dot_a));
    assert_eq!(r.store().order(), [dot_c, dot_a, dot_t]);

    // The seam returns `Before(t)` (t is the successor of the caret after 'a').
    let anchor = r.store().anchor_for_visual_insert(Some(dot_a.into()));
    assert_eq!(anchor, Anchor::Before(dot_t.into()));
    // The Before(t) bucket is empty, so any rank places X adjacent (right before
    // t, i.e. right after 'a'); mint a fresh rank off the clock.
    let dot_x = r.next_dot(1);
    let rank = clk.now(0u16);
    r = r.merge(&woven(dot_x, Locus { anchor, rank }));

    // 'X' reads immediately after 'a' and before 't': "caXt", not "catX".
    assert_eq!(r.store().order(), [dot_c, dot_a, dot_x, dot_t]);
}