use crate::common::harness::EditorTestHarness;
use crossterm::event::{KeyCode, KeyModifiers};
use tempfile::TempDir;
#[test]
fn test_initial_split_has_buffer_in_tabs() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("test.txt");
std::fs::write(&file_path, "Hello").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
harness.open_file(&file_path).unwrap();
harness.render().unwrap();
harness.assert_screen_contains("test.txt");
}
#[test]
fn test_initial_scratch_buffer_in_tabs() {
let mut harness = EditorTestHarness::new(80, 24).unwrap();
harness.render().unwrap();
let screen = harness.screen_to_string();
eprintln!("Initial editor screen:\n{}", screen);
}
#[test]
fn test_open_file_adds_to_split_tabs() {
let temp_dir = TempDir::new().unwrap();
let file1 = temp_dir.path().join("file1.txt");
let file2 = temp_dir.path().join("file2.txt");
std::fs::write(&file1, "Content 1").unwrap();
std::fs::write(&file2, "Content 2").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
harness.open_file(&file1).unwrap();
harness.render().unwrap();
harness.assert_screen_contains("file1.txt");
harness.open_file(&file2).unwrap();
harness.render().unwrap();
harness.assert_screen_contains("file1.txt");
harness.assert_screen_contains("file2.txt");
}
#[test]
fn test_new_split_has_buffer_in_tabs() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("test.txt");
std::fs::write(&file_path, "Hello").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
harness.open_file(&file_path).unwrap();
harness.render().unwrap();
harness.assert_screen_contains("test.txt");
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("split horiz").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.render().unwrap();
let screen = harness.screen_to_string();
let count = screen.matches("test.txt").count();
assert!(
count >= 2,
"Expected at least 2 occurrences of 'test.txt' in split tabs, found {}. Screen:\n{}",
count,
screen
);
}
#[test]
fn test_splits_have_independent_tabs() {
let temp_dir = TempDir::new().unwrap();
let file1 = temp_dir.path().join("file1.txt");
let file2 = temp_dir.path().join("file2.txt");
std::fs::write(&file1, "Content 1").unwrap();
std::fs::write(&file2, "Content 2").unwrap();
let mut harness = EditorTestHarness::new(100, 30).unwrap();
harness.open_file(&file1).unwrap();
harness.render().unwrap();
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("split vert").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.render().unwrap();
harness.open_file(&file2).unwrap();
harness.render().unwrap();
harness.assert_screen_contains("file1.txt");
harness.assert_screen_contains("file2.txt");
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("next split").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.render().unwrap();
let screen = harness.screen_to_string();
eprintln!("Screen after switching to first split:\n{}", screen);
}
#[test]
fn test_buffer_cycling_within_split() {
let temp_dir = TempDir::new().unwrap();
let file1 = temp_dir.path().join("file1.txt");
let file2 = temp_dir.path().join("file2.txt");
let file3 = temp_dir.path().join("file3.txt");
std::fs::write(&file1, "Content 1").unwrap();
std::fs::write(&file2, "Content 2").unwrap();
std::fs::write(&file3, "Content 3").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
harness.open_file(&file1).unwrap();
harness.open_file(&file2).unwrap();
harness.open_file(&file3).unwrap();
harness.render().unwrap();
harness.assert_buffer_content("Content 3");
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("next buffer").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.render().unwrap();
harness.assert_buffer_content("Content 1");
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("next buffer").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.render().unwrap();
harness.assert_buffer_content("Content 2");
}
#[test]
fn test_tab_bar_in_split_area() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("test.txt");
std::fs::write(&file_path, "Hello world").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
harness.open_file(&file_path).unwrap();
harness.render().unwrap();
let screen = harness.screen_to_string();
eprintln!("Screen content:\n{}", screen);
harness.assert_screen_contains("test.txt");
}
#[test]
fn test_close_buffer_removes_from_tabs() {
let temp_dir = TempDir::new().unwrap();
let file1 = temp_dir.path().join("file1.txt");
let file2 = temp_dir.path().join("file2.txt");
std::fs::write(&file1, "Content 1").unwrap();
std::fs::write(&file2, "Content 2").unwrap();
let mut harness = EditorTestHarness::new(80, 24).unwrap();
harness.open_file(&file1).unwrap();
harness.open_file(&file2).unwrap();
harness.render().unwrap();
harness.assert_screen_contains("file1.txt");
harness.assert_screen_contains("file2.txt");
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("close buffer").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.render().unwrap();
harness.assert_screen_contains("file1.txt");
harness.assert_screen_not_contains("file2.txt");
}
#[test]
fn test_git_log_split_tabs() {
let temp_dir = TempDir::new().unwrap();
let repo_path = temp_dir.path();
std::process::Command::new("git")
.args(["init"])
.current_dir(repo_path)
.output()
.expect("Failed to init git repo");
std::process::Command::new("git")
.args(["config", "user.email", "test@test.com"])
.current_dir(repo_path)
.output()
.expect("Failed to set git email");
std::process::Command::new("git")
.args(["config", "user.name", "Test User"])
.current_dir(repo_path)
.output()
.expect("Failed to set git name");
let file_path = repo_path.join("test.txt");
std::fs::write(&file_path, "Hello world").unwrap();
std::process::Command::new("git")
.args(["add", "test.txt"])
.current_dir(repo_path)
.output()
.expect("Failed to git add");
std::process::Command::new("git")
.args(["commit", "-m", "Initial commit"])
.current_dir(repo_path)
.output()
.expect("Failed to git commit");
let mut harness = EditorTestHarness::new(100, 30).unwrap();
harness.open_file(&file_path).unwrap();
harness.render().unwrap();
eprintln!("\n=== Before git log ===");
eprintln!("{}", harness.screen_to_string());
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("git log").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
harness.render().unwrap();
eprintln!("\n=== After git log ===");
let screen = harness.screen_to_string();
eprintln!("{}", screen);
}
#[test]
fn test_debug_split_tabs_rendering() {
let temp_dir = TempDir::new().unwrap();
let file1 = temp_dir.path().join("alpha.txt");
let file2 = temp_dir.path().join("beta.txt");
std::fs::write(&file1, "Alpha content").unwrap();
std::fs::write(&file2, "Beta content").unwrap();
let mut harness = EditorTestHarness::new(100, 30).unwrap();
harness.open_file(&file1).unwrap();
harness.render().unwrap();
eprintln!("\n=== After opening alpha.txt ===");
eprintln!("{}", harness.screen_to_string());
harness.open_file(&file2).unwrap();
harness.render().unwrap();
eprintln!("\n=== After opening beta.txt ===");
eprintln!("{}", harness.screen_to_string());
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.render().unwrap();
harness.type_text("split vert").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.render().unwrap();
eprintln!("\n=== After vertical split ===");
eprintln!("{}", harness.screen_to_string());
let screen = harness.screen_to_string();
assert!(
screen.contains("alpha.txt") || screen.contains("beta.txt"),
"Expected to see file tabs in screen. Screen:\n{}",
screen
);
}