#![cfg(feature = "plugins")]
use crate::common::harness::EditorTestHarness;
use fresh_core::BufferId;
use std::fs;
fn snapshot_splits_for_buffer(
harness: &EditorTestHarness,
buffer_id: BufferId,
) -> Option<Vec<usize>> {
let snapshot_handle = harness.editor().plugin_manager().state_snapshot_handle()?;
let snapshot = snapshot_handle.read().ok()?;
snapshot
.buffers
.get(&buffer_id)
.map(|b| b.splits.iter().map(|s| s.0).collect())
}
fn snapshot_active_split(harness: &EditorTestHarness) -> Option<usize> {
let snapshot_handle = harness.editor().plugin_manager().state_snapshot_handle()?;
let snapshot = snapshot_handle.read().ok()?;
Some(snapshot.active_split_id)
}
#[test]
fn buffer_info_splits_reports_single_split() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("hello.txt");
fs::write(&path, "hi\n").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
let buffer_id = harness.editor_mut().open_file(&path).unwrap();
harness.tick_and_render().unwrap();
let splits = snapshot_splits_for_buffer(&harness, buffer_id)
.expect("snapshot should know the buffer we just opened");
assert_eq!(
splits.len(),
1,
"Buffer open in one split must report exactly one split id. splits={:?}",
splits
);
}
#[test]
fn buffer_info_splits_reports_both_splits_after_split_horizontal() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("hello.txt");
fs::write(&path, "hi\n").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
let buffer_id = harness.editor_mut().open_file(&path).unwrap();
harness.tick_and_render().unwrap();
harness
.editor_mut()
.dispatch_action_for_tests(fresh::input::keybindings::Action::SplitHorizontal);
harness.tick_and_render().unwrap();
let splits = snapshot_splits_for_buffer(&harness, buffer_id)
.expect("snapshot should still know the buffer after the split");
assert_eq!(
splits.len(),
2,
"After split_horizontal, the buffer is in two splits. splits={:?}",
splits
);
assert_ne!(
splits[0], splits[1],
"The two splits showing the buffer must have distinct ids. splits={:?}",
splits
);
}
#[test]
fn buffer_info_splits_drives_refocus_pattern() {
let temp = tempfile::tempdir().unwrap();
let hello = temp.path().join("hello.txt");
let world = temp.path().join("world.txt");
fs::write(&hello, "hello\n").unwrap();
fs::write(&world, "world\n").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
let hello_id = harness.editor_mut().open_file(&hello).unwrap();
harness.tick_and_render().unwrap();
harness
.editor_mut()
.dispatch_action_for_tests(fresh::input::keybindings::Action::SplitHorizontal);
harness.editor_mut().open_file(&world).unwrap();
harness.tick_and_render().unwrap();
let hello_splits = snapshot_splits_for_buffer(&harness, hello_id)
.expect("hello.txt must still be in the snapshot");
assert_eq!(hello_splits.len(), 1, "hello.txt is in exactly one split");
let hello_split = hello_splits[0];
let active = snapshot_active_split(&harness).unwrap();
assert_ne!(
active, hello_split,
"Sanity: active split should be the world.txt split, not hello's"
);
harness
.editor_mut()
.handle_plugin_command(fresh::services::plugins::api::PluginCommand::FocusSplit {
split_id: fresh_core::SplitId(hello_split),
})
.unwrap();
harness.tick_and_render().unwrap();
let active_after = snapshot_active_split(&harness).unwrap();
assert_eq!(
active_after, hello_split,
"focusSplit(hello_split) must make that split active. was {} now {}",
active, active_after
);
}