use crate::common::git_test_helper::GitTestRepo;
use crate::common::harness::EditorTestHarness;
use crossterm::event::{KeyCode, KeyModifiers};
use fresh::config::Config;
use std::time::Duration;
fn advance_past_double_click(harness: &mut EditorTestHarness) {
let dc = Duration::from_millis(
harness
.config()
.editor
.double_click_time_ms
.saturating_mul(2),
);
harness.advance_time(dc);
}
fn col_of_text_in_row(harness: &EditorTestHarness, row: u16, needle: &str) -> u16 {
let row_text = harness.screen_row_text(row);
let needle: Vec<char> = needle.chars().collect();
let chars: Vec<char> = row_text.chars().collect();
chars
.windows(needle.len())
.position(|w| w == needle.as_slice())
.unwrap_or_else(|| panic!("{:?} not in row {row}: {row_text:?}", needle)) as u16
}
#[test]
fn clicking_group_tab_activates_group_in_the_clicked_split() {
let repo = GitTestRepo::new();
repo.setup_typical_project();
repo.setup_git_log_plugin();
let width = 120u16;
let height = 40u16;
let mut harness = EditorTestHarness::with_config_and_working_dir(
width,
height,
Config::default(),
repo.path.clone(),
)
.unwrap();
harness.open_file(&repo.path.join("src/main.rs")).unwrap();
harness.render().unwrap();
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.wait_for_prompt().unwrap();
harness.type_text("Git Log").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness
.wait_until(|h| {
let s = h.screen_to_string();
s.contains("switch pane") && s.contains("Initial commit")
})
.unwrap();
const TAB_BAR_ROW: u16 = 1;
let file_tab_col = col_of_text_in_row(&harness, TAB_BAR_ROW, "main.rs");
advance_past_double_click(&mut harness);
harness.mouse_click(file_tab_col, TAB_BAR_ROW).unwrap();
harness
.wait_until(|h| !h.screen_to_string().contains("switch pane"))
.unwrap();
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.wait_for_prompt().unwrap();
harness.type_text("split horiz").unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness
.wait_until(|h| h.screen_to_string().contains("Split pane horiz"))
.unwrap();
let git_log_tab_col = col_of_text_in_row(&harness, TAB_BAR_ROW, "Git Log");
advance_past_double_click(&mut harness);
harness.mouse_click(git_log_tab_col, TAB_BAR_ROW).unwrap();
harness
.wait_until(|h| h.screen_to_string().contains("switch pane"))
.unwrap();
let screen = harness.screen_to_string();
let toolbar_rows: Vec<usize> = screen
.lines()
.enumerate()
.filter(|(_, line)| line.contains("switch pane"))
.map(|(row, _)| row)
.collect();
let top_half_end = (height / 2) as usize;
assert!(
!toolbar_rows.is_empty(),
"git log toolbar not rendered at all; screen:\n{screen}"
);
assert!(
toolbar_rows.iter().all(|row| *row < top_half_end),
"git log activated in the wrong split: 'switch pane' toolbar \
appeared on rows {toolbar_rows:?}, but the clicked *Git Log* tab \
lives in the TOP pane (rows < {top_half_end}). Screen:\n{screen}"
);
}