use crate::model::common::{Bounds, ExecutionMode};
use crate::model::muxbox::MuxBox;
use crate::pty_manager::PtyManager;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pty_box_dimensions_match_terminal() {
let mut muxbox = MuxBox {
id: "pty_box_1".to_string(),
execution_mode: ExecutionMode::Pty,
..Default::default()
};
let bounds = Bounds::new(0, 0, 79, 23);
let (expected_cols, expected_rows) = muxbox.calculate_terminal_dimensions(&bounds);
assert_eq!(expected_cols, 80);
assert_eq!(expected_rows, 24);
}
#[test]
fn test_pty_resize_on_bounds_change() {
let mut pty_manager = PtyManager::new().unwrap();
let mut muxbox = MuxBox {
id: "resizable_pty".to_string(),
execution_mode: ExecutionMode::Pty,
..Default::default()
};
let initial_bounds = Bounds::new(0, 0, 79, 23);
let (initial_cols, initial_rows) = (80u16, 24u16);
let new_bounds = Bounds::new(0, 0, 119, 39);
let (new_cols, new_rows) = (120u16, 40u16);
let result = muxbox.update_bounds_with_pty_resize(&new_bounds, &mut pty_manager);
assert!(result.is_ok());
let resize_result = pty_manager.resize_pty(&muxbox.id, new_rows, new_cols);
assert!(resize_result.is_ok());
}
#[test]
fn test_non_pty_box_ignores_resize() {
let mut pty_manager = PtyManager::new().unwrap();
let mut muxbox = MuxBox {
id: "regular_box".to_string(),
execution_mode: ExecutionMode::Thread, ..Default::default()
};
let bounds = Bounds::new(0, 0, 99, 29);
let result = muxbox.update_bounds_with_pty_resize(&bounds, &mut pty_manager);
assert!(result.is_ok()); }
#[test]
fn test_pty_title_propagation() {
let mut muxbox = MuxBox {
id: "titled_pty".to_string(),
execution_mode: ExecutionMode::Pty,
title: Some("Original Title".to_string()),
..Default::default()
};
let terminal_title = "vim - editing file.txt";
muxbox.update_title_from_pty(Some(terminal_title.to_string()));
assert_eq!(muxbox.title, Some(terminal_title.to_string()));
}
#[test]
fn test_calculate_terminal_dimensions_various_sizes() {
let muxbox = MuxBox {
id: "test_box".to_string(),
execution_mode: ExecutionMode::Pty,
..Default::default()
};
let test_cases = vec![
(Bounds::new(0, 0, 79, 23), (80, 24)), (Bounds::new(0, 0, 119, 39), (120, 40)), (Bounds::new(0, 0, 39, 11), (40, 12)), (Bounds::new(0, 0, 199, 59), (200, 60)), (Bounds::new(0, 0, 24, 24), (40, 25)), ];
for (bounds, (expected_cols, expected_rows)) in test_cases {
let (cols, rows) = muxbox.calculate_terminal_dimensions(&bounds);
assert_eq!(cols, expected_cols, "Cols mismatch for bounds {:?}", bounds);
assert_eq!(rows, expected_rows, "Rows mismatch for bounds {:?}", bounds);
}
}
#[test]
fn test_pty_ansi_processor_resize_sync() {
use crate::ansi_processor::AnsiProcessor;
let mut processor = AnsiProcessor::new();
assert_eq!(processor.terminal_state.screen_width, 80);
assert_eq!(processor.terminal_state.screen_height, 24);
processor.resize_screen(120, 40);
assert_eq!(processor.terminal_state.screen_width, 120);
assert_eq!(processor.terminal_state.screen_height, 40);
assert_eq!(processor.terminal_state.tab_stops.len(), 120);
assert!(processor.terminal_state.tab_stops[8]); assert!(processor.terminal_state.tab_stops[16]); assert!(processor.terminal_state.tab_stops[112]); }
#[test]
fn test_bounds_to_character_dimensions() {
let bounds = Bounds::new(10, 5, 89, 28);
let width = bounds.width();
let height = bounds.height();
assert_eq!(width, 80); assert_eq!(height, 24); }
#[test]
fn test_sigwinch_simulation() {
let mut pty_manager = PtyManager::new().unwrap();
let muxbox_id = "sigwinch_test";
let new_rows = 50u16;
let new_cols = 132u16;
let result = pty_manager.resize_pty(muxbox_id, new_rows, new_cols);
assert!(result.is_ok());
}
#[test]
fn test_content_preservation_during_resize() {
use crate::ansi_processor::AnsiProcessor;
let mut processor = AnsiProcessor::new();
processor.process_bytes("Hello\nWorld\nTest\n".as_bytes());
let original_content = processor.get_processed_text();
processor.resize_screen(120, 40);
let resized_content = processor.get_processed_text();
assert!(resized_content.contains("Hello"));
assert!(resized_content.contains("World"));
assert!(resized_content.contains("Test"));
}
#[test]
fn test_cursor_position_adjustment() {
use crate::ansi_processor::AnsiProcessor;
let mut processor = AnsiProcessor::new();
processor.process_bytes("\x1b[21;71H".as_bytes());
let original_x = processor.get_cursor_x();
let original_y = processor.get_cursor_y();
assert_eq!(original_x, 70);
assert_eq!(original_y, 20);
processor.resize_screen(40, 12);
let new_x = processor.get_cursor_x();
let new_y = processor.get_cursor_y();
assert!(new_x < 40);
assert!(new_y < 12);
}
#[test]
fn test_line_rewrapping_on_resize() {
use crate::ansi_processor::AnsiProcessor;
let mut processor = AnsiProcessor::new();
let long_line = "This is a very long line that will definitely exceed the terminal width and should be wrapped when the terminal is resized to a smaller width";
processor.process_bytes(long_line.as_bytes());
processor.resize_screen(40, 24);
let content = processor.get_processed_text();
assert!(content.contains("This is a very long"));
assert!(content.contains("line that will"));
}
}