//! The ring's bound and the replay's offsets, proved over kilobytes so the //! test runs in milliseconds and the arithmetic is readable. use super::*; fn scratch() -> tempfile::TempDir { tempfile::tempdir().expect("a scratch directory") } #[test] fn what_goes_in_comes_back_out_in_order() { let dir = scratch(); let mut log = Scrollback::open(dir.path().join("s").join("scrollback.log")).expect("opens"); log.append(b"hello ").expect("appends"); log.append(b"world").expect("appends"); let replay = log.replay(None, REPLAY_TAIL_BYTES).expect("replays"); assert_eq!(replay.bytes, b"hello world"); assert_eq!(replay.offset, 0); assert_eq!(log.offset(), 11); } #[test] fn the_file_never_ends_an_append_above_the_cap() { let dir = scratch(); let cap = 4096; let mut log = Scrollback::with_cap(dir.path().join("scrollback.log"), cap).expect("opens"); let chunk = vec![b'x'; 512]; for _ in 0..64 { log.append(&chunk).expect("appends"); assert!( log.len() <= cap, "the ring is {} bytes, past its {cap}-byte cap", log.len() ); } assert_eq!(log.offset(), 64 * 512, "the offset counts everything ever written"); let on_disk = std::fs::metadata(log.path()).expect("the file is there").len(); assert_eq!(on_disk, log.len(), "the file IS the ring — nothing else holds it"); } #[test] fn a_compaction_drops_the_oldest_bytes_and_keeps_the_newest() { let dir = scratch(); let cap = 2048; let mut log = Scrollback::with_cap(dir.path().join("scrollback.log"), cap).expect("opens"); log.append(&vec![b'a'; 2000]).expect("appends"); log.append(&vec![b'z'; 2000]).expect("appends"); let replay = log.replay(None, cap).expect("replays"); assert!( replay.bytes.iter().all(|byte| *byte == b'z'), "the oldest bytes went first" ); assert_eq!(replay.offset, 4000 - replay.bytes.len() as u64); } #[test] fn a_replay_is_capped_at_the_tail_size_even_when_the_ring_holds_more() { let dir = scratch(); let mut log = Scrollback::with_cap(dir.path().join("scrollback.log"), 8192).expect("opens"); log.append(&vec![b'q'; 4000]).expect("appends"); let replay = log.replay(None, 100).expect("replays"); assert_eq!(replay.bytes.len(), 100); assert_eq!(replay.offset, 3900); } #[test] fn a_viewer_that_says_where_it_left_off_gets_exactly_what_it_missed() { let dir = scratch(); let mut log = Scrollback::with_cap(dir.path().join("scrollback.log"), 8192).expect("opens"); log.append(b"0123456789").expect("appends"); let replay = log.replay(Some(4), REPLAY_TAIL_BYTES).expect("replays"); assert_eq!(replay.bytes, b"456789"); assert_eq!(replay.offset, 4); } #[test] fn a_viewer_whose_offset_the_ring_has_dropped_is_told_the_real_one() { let dir = scratch(); let cap = 1024; let mut log = Scrollback::with_cap(dir.path().join("scrollback.log"), cap).expect("opens"); log.append(&vec![b'a'; 1000]).expect("appends"); log.append(&vec![b'b'; 1000]).expect("appends"); // Offset 0 was compacted away. The replay starts where the ring really // starts, and SAYS so — a viewer that trusted its own 0 would render the // gap as continuity. let replay = log.replay(Some(0), cap).expect("replays"); assert!(replay.offset > 0, "the replay names the offset it really starts at"); assert_eq!(replay.offset + replay.bytes.len() as u64, log.offset()); } #[test] fn a_viewer_already_at_the_end_is_handed_nothing_rather_than_the_screen_again() { let dir = scratch(); let mut log = Scrollback::with_cap(dir.path().join("scrollback.log"), 8192).expect("opens"); log.append(b"already seen").expect("appends"); let replay = log.replay(Some(log.offset()), REPLAY_TAIL_BYTES).expect("replays"); assert!(replay.bytes.is_empty()); assert_eq!(replay.offset, log.offset()); } #[test] fn an_empty_append_writes_nothing_and_moves_no_offset() { let dir = scratch(); let mut log = Scrollback::with_cap(dir.path().join("scrollback.log"), 64).expect("opens"); log.append(b"").expect("appends"); assert!(log.is_empty()); assert_eq!(log.offset(), 0); } #[test] fn reopening_a_session_s_file_counts_from_what_is_in_it() { let dir = scratch(); let path = dir.path().join("scrollback.log"); { let mut log = Scrollback::with_cap(&path, 8192).expect("opens"); log.append(b"before the restart").expect("appends"); } let log = Scrollback::with_cap(&path, 8192).expect("reopens"); assert_eq!(log.offset(), 18); assert_eq!( log.replay(None, REPLAY_TAIL_BYTES).expect("replays").bytes, b"before the restart" ); }