use tempfile::TempDir;
use tmuxrs::session::SessionManager;
use tmuxrs::tmux::TmuxCommand;
#[test]
fn test_stop_existing_session() {
if std::env::var("CI").is_ok() {
return;
}
let session_name = "stop-test-existing";
let temp_dir = TempDir::new().unwrap();
let _ = TmuxCommand::kill_session(session_name);
TmuxCommand::new_session(session_name, temp_dir.path()).unwrap();
assert!(TmuxCommand::session_exists(session_name).unwrap());
let session_manager = SessionManager::new();
let result = session_manager.stop_session(session_name);
assert!(result.is_ok(), "Failed to stop session: {result:?}");
assert_eq!(result.unwrap(), format!("Stopped session '{session_name}'"));
assert!(!TmuxCommand::session_exists(session_name).unwrap());
}
#[test]
fn test_stop_nonexistent_session() {
if std::env::var("CI").is_ok() {
return;
}
let session_name = "stop-test-nonexistent";
let _ = TmuxCommand::kill_session(session_name);
let session_manager = SessionManager::new();
let result = session_manager.stop_session(session_name);
assert!(
result.is_err(),
"Should fail when stopping non-existent session"
);
assert!(result
.unwrap_err()
.to_string()
.contains("Session 'stop-test-nonexistent' does not exist"));
}
#[test]
fn test_start_and_stop_workflow() {
if std::env::var("CI").is_ok() {
return;
}
let temp_dir = TempDir::new().unwrap();
let config_dir = temp_dir.path().join(".config").join("tmuxrs");
std::fs::create_dir_all(&config_dir).unwrap();
let config_file = config_dir.join("workflow-test.yml");
let yaml_content = r#"
name: workflow-test
root: /tmp
windows:
- editor: vim
- server: rails server
"#;
std::fs::write(&config_file, yaml_content).unwrap();
let session_manager = SessionManager::new();
let start_result = session_manager.start_session_with_options(
Some("workflow-test"),
Some(&config_dir),
false, false, );
assert!(
start_result.is_ok(),
"Failed to start session: {start_result:?}"
);
assert!(TmuxCommand::session_exists("workflow-test").unwrap());
let stop_result = session_manager.stop_session("workflow-test");
assert!(
stop_result.is_ok(),
"Failed to stop session: {stop_result:?}"
);
assert!(!TmuxCommand::session_exists("workflow-test").unwrap());
}
#[test]
fn test_stop_session_with_complex_windows() {
if std::env::var("CI").is_ok() {
return;
}
let temp_dir = TempDir::new().unwrap();
let config_dir = temp_dir.path().join(".config").join("tmuxrs");
std::fs::create_dir_all(&config_dir).unwrap();
let config_file = config_dir.join("complex-stop-test.yml");
let yaml_content = r#"
name: complex-stop-test
root: /tmp
windows:
- editor: vim
- server:
layout: main-vertical
panes:
- rails server
- tail -f log/development.log
- monitoring:
layout: tiled
panes:
- htop
- iostat
- netstat
"#;
std::fs::write(&config_file, yaml_content).unwrap();
let session_manager = SessionManager::new();
let start_result = session_manager.start_session_with_options(
Some("complex-stop-test"),
Some(&config_dir),
false, false, );
match start_result {
Ok(_) => {
}
Err(e) if e.to_string().contains("can't find window") => {
eprintln!("Warning: tmux race condition in test: {e}");
let _ = TmuxCommand::kill_session("complex-stop-test");
return; }
Err(e) => {
panic!("Unexpected error starting session: {e:?}");
}
}
assert!(TmuxCommand::session_exists("complex-stop-test").unwrap());
let stop_result = session_manager.stop_session("complex-stop-test");
assert!(
stop_result.is_ok(),
"Failed to stop complex session: {stop_result:?}"
);
assert!(!TmuxCommand::session_exists("complex-stop-test").unwrap());
}