continuity-buffer 0.2.36

Rope-backed buffer, revisions, selections, and undo state for Continuity
Documentation
use continuity_text::{Position, Range};

use super::*;

#[test]
fn empty_buffer_has_revision_zero() {
    let b = Buffer::empty();
    assert_eq!(b.revision(), Revision::INITIAL);
    assert_eq!(b.rope().len_bytes(), 0);
    assert_eq!(b.selections(), &[Selection::caret_at(Position::ZERO)]);
}

#[test]
fn from_text_populates_rope() {
    let b = Buffer::from_text("hello");
    assert_eq!(b.rope().to_string(), "hello");
    assert_eq!(b.revision(), Revision::INITIAL);
}

#[test]
fn insert_advances_revision_and_changes_rope() {
    let mut b = Buffer::from_text("hello");
    let r = b
        .apply(&EditOp::insert(Position::new(0, 5), " world"))
        .unwrap();
    assert_eq!(r, Revision(1));
    assert_eq!(b.rope().to_string(), "hello world");
}

#[test]
fn delete_removes_range() {
    let mut b = Buffer::from_text("hello world");
    let range = Range::new(Position::new(0, 5), Position::new(0, 11));
    b.apply(&EditOp::delete(range)).unwrap();
    assert_eq!(b.rope().to_string(), "hello");
}

#[test]
fn replace_swaps_range() {
    let mut b = Buffer::from_text("hello world");
    let range = Range::new(Position::new(0, 6), Position::new(0, 11));
    b.apply(&EditOp::replace(range, "rust")).unwrap();
    assert_eq!(b.rope().to_string(), "hello rust");
}

#[test]
fn snapshot_carries_revision() {
    let mut b = Buffer::from_text("");
    b.apply(&EditOp::insert(Position::ZERO, "x")).unwrap();
    let s = b.snapshot();
    assert_eq!(s.revision(), Revision(1));
    assert_eq!(s.rope().to_string(), "x");
}

#[test]
fn snapshot_does_not_observe_later_edits() {
    let mut b = Buffer::from_text("a");
    let s = b.snapshot();
    b.apply(&EditOp::insert(Position::new(0, 1), "b")).unwrap();
    assert_eq!(s.rope().to_string(), "a");
    assert_eq!(b.rope().to_string(), "ab");
}

#[test]
fn from_parts_preserves_id_and_revision() {
    let id = BufferId::new();
    let b = Buffer::from_parts(id, "hello", Revision(7));
    assert_eq!(b.id(), id);
    assert_eq!(b.revision(), Revision(7));
    assert_eq!(b.rope().to_string(), "hello");
    assert_eq!(b.selections(), &[Selection::caret_at(Position::ZERO)]);
}

#[test]
fn set_selections_keeps_at_least_one_caret() {
    let mut b = Buffer::from_text("hello");
    b.set_selections(Vec::new());
    assert_eq!(b.selections(), &[Selection::caret_at(Position::ZERO)]);
}

#[test]
fn set_selections_clamps_positions_to_rope() {
    let mut b = Buffer::from_text("hello\nworld");
    b.set_selections(vec![Selection::caret_at(Position::new(99, 12))]);
    assert_eq!(b.selections(), &[Selection::caret_at(Position::new(1, 5))]);

    b.set_selections(vec![Selection::caret_at(Position::new(0, 99))]);
    assert_eq!(b.selections(), &[Selection::caret_at(Position::new(0, 5))]);
}

#[test]
fn from_parts_then_apply_advances_revision() {
    let id = BufferId::new();
    let mut b = Buffer::from_parts(id, "ab", Revision(3));
    let r = b.apply(&EditOp::insert(Position::new(0, 2), "c")).unwrap();
    assert_eq!(r, Revision(4));
    assert_eq!(b.rope().to_string(), "abc");
}

#[test]
fn title_picks_first_nonempty_line() {
    let b = Buffer::from_text("\n\n  hello world  \nsecond\n");
    assert_eq!(b.title(80).as_deref(), Some("hello world"));
}

#[test]
fn title_truncates_with_ellipsis() {
    let long = "a".repeat(50);
    let b = Buffer::from_text(&long);
    let title = b.title(10).unwrap();
    assert_eq!(title.chars().count(), 10);
    assert!(title.ends_with('…'));
}

#[test]
fn title_returns_none_for_empty() {
    let b = Buffer::empty();
    assert_eq!(b.title(80), None);
}

#[test]
fn title_returns_none_for_whitespace_only() {
    let b = Buffer::from_text("\n\n   \n\t\n");
    assert_eq!(b.title(80), None);
}

#[test]
fn out_of_bounds_edit_errors() {
    let mut b = Buffer::from_text("hi");
    let op = EditOp::insert(Position::new(99, 0), "x");
    assert!(b.apply(&op).is_err());
    assert_eq!(b.revision(), Revision::INITIAL);
}

#[test]
fn default_buffer_is_not_synthetic_or_read_only() {
    let b = Buffer::empty();
    assert!(!b.is_synthetic());
    assert!(!b.is_read_only());
    let b = Buffer::from_text("hi");
    assert!(!b.is_synthetic());
    assert!(!b.is_read_only());
    let b = Buffer::from_parts(BufferId::new(), "hi", Revision::INITIAL);
    assert!(!b.is_synthetic());
    assert!(!b.is_read_only());
}

#[test]
fn synthetic_read_only_carries_both_flags_and_text() {
    let b = Buffer::synthetic_read_only("# Tutorial\n\nbody");
    assert!(b.is_synthetic());
    assert!(b.is_read_only());
    assert_eq!(b.rope().to_string(), "# Tutorial\n\nbody");
}

#[test]
fn read_only_buffer_rejects_edits_without_touching_rope() {
    let mut b = Buffer::synthetic_read_only("hello");
    let r = b.apply(&EditOp::insert(Position::new(0, 5), " world"));
    assert!(matches!(r, Err(Error::ReadOnly)));
    assert_eq!(b.rope().to_string(), "hello");
    assert_eq!(b.revision(), Revision::INITIAL);
}

#[test]
fn running_checksum_initializes_from_initial_text() {
    let b = Buffer::from_text("hello world");
    assert_eq!(
        b.running_checksum(),
        crate::checksum::full_walk_rope(&Rope::from_str("hello world")),
    );
}

#[test]
fn running_checksum_tracks_apply_chain() {
    let mut b = Buffer::from_text("abc");
    b.apply(&EditOp::insert(Position::new(0, 3), "def"))
        .unwrap();
    b.apply(&EditOp::delete(Range::new(
        Position::new(0, 1),
        Position::new(0, 4),
    )))
    .unwrap();
    b.apply(&EditOp::replace(
        Range::new(Position::new(0, 1), Position::new(0, 2)),
        "ZZ",
    ))
    .unwrap();
    assert_eq!(
        b.running_checksum(),
        crate::checksum::full_walk_rope(b.rope()),
    );
}

#[test]
fn verify_running_checksum_reseats_and_clears_counter() {
    let mut b = Buffer::from_text("");
    for ch in ["a", "b", "c", "d"] {
        let len = b.rope().len_bytes() as u32;
        b.apply(&EditOp::insert(Position::new(0, len), ch)).unwrap();
    }
    assert_eq!(b.edits_since_verify(), 4);
    let (observed, computed) = b.verify_running_checksum();
    assert_eq!(observed, computed);
    assert_eq!(b.edits_since_verify(), 0);
}