use escriba_buffer::BufferSet;
fn fixture(name: &str, body: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join("escriba-no-dup-buffers");
std::fs::create_dir_all(&dir).expect("scratch dir");
let p = dir.join(name);
std::fs::write(&p, body).expect("write fixture");
p
}
#[test]
fn opening_the_same_path_twice_returns_the_same_buffer() {
let path = fixture("same.txt", "hello\n");
let mut set = BufferSet::new();
let a = set.open(&path).expect("first open");
let b = set.open(&path).expect("second open");
assert_eq!(a, b, "the same file must not become two buffers");
assert_eq!(set.ids().len(), 1, "and must not grow the set");
}
#[test]
fn edits_cannot_be_split_across_two_views_of_one_file() {
let path = fixture("split.txt", "original\n");
let mut set = BufferSet::new();
let first = set.open(&path).expect("open");
set.get_mut(first).expect("buffer").modified = true;
let second = set.open(&path).expect("re-open");
assert!(
set.get(second).expect("buffer").modified,
"re-opening must return the DIRTY buffer, not a clean second copy \
that would silently discard the edit on save",
);
}
#[test]
fn different_spellings_of_one_path_are_one_file() {
let path = fixture("spelling.txt", "x\n");
let dir = path.parent().expect("parent");
let dotted = dir.join(".").join("spelling.txt");
let mut set = BufferSet::new();
let a = set.open(&path).expect("plain");
let b = set.open(&dotted).expect("dotted");
assert_eq!(a, b, "./x and x are one file: {path:?} vs {dotted:?}");
}
#[test]
fn distinct_files_still_get_distinct_buffers() {
let one = fixture("one.txt", "1\n");
let two = fixture("two.txt", "2\n");
let mut set = BufferSet::new();
let a = set.open(&one).expect("one");
let b = set.open(&two).expect("two");
assert_ne!(a, b, "two files must be two buffers");
assert_eq!(set.ids().len(), 2);
}
#[test]
fn scratch_buffers_are_never_deduplicated() {
let mut set = BufferSet::new();
let a = set.scratch("a");
let b = set.scratch("b");
assert_ne!(a, b);
assert_eq!(set.ids().len(), 2);
}
#[test]
fn find_by_path_reports_honestly() {
let path = fixture("find.txt", "y\n");
let mut set = BufferSet::new();
assert_eq!(set.find_by_path(&path), None, "nothing open yet");
let id = set.open(&path).expect("open");
assert_eq!(set.find_by_path(&path), Some(id));
assert_eq!(
set.find_by_path(path.parent().expect("parent").join("absent.txt")),
None,
"a path nobody opened must not match",
);
}