use tempfile::TempDir;
use tmuxrs::session::SessionManager;
use tmuxrs::tmux::TmuxCommand;
#[test]
fn test_attach_to_existing_session() {
if std::env::var("CI").is_ok() {
return;
}
let session_name = "attach-test-existing";
let temp_dir = TempDir::new().unwrap();
let _ = TmuxCommand::kill_session(session_name);
TmuxCommand::new_session(session_name, temp_dir.path()).unwrap();
let result = TmuxCommand::attach_session(session_name);
assert!(
result.is_err(),
"Attach should fail in test environment due to no TTY inheritance"
);
let _ = TmuxCommand::kill_session(session_name);
}
#[test]
fn test_attach_to_nonexistent_session() {
if std::env::var("CI").is_ok() {
return;
}
let session_name = "attach-test-nonexistent";
let _ = TmuxCommand::kill_session(session_name);
let result = TmuxCommand::attach_session(session_name);
assert!(
result.is_err(),
"Should fail when attaching to non-existent session"
);
}
#[test]
fn test_start_session_with_attach_flag() {
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("attach-flag-test.yml");
let yaml_content = r#"
name: attach-flag-test
root: /tmp
windows:
- editor: vim
"#;
std::fs::write(&config_file, yaml_content).unwrap();
let session_manager = SessionManager::new();
let result = session_manager.start_session_with_options(
Some("attach-flag-test"),
Some(&config_dir),
true, false, );
match result {
Err(error) => {
let error_msg = error.to_string();
assert!(
error_msg.contains("Failed to attach")
|| error_msg.contains("but failed to attach"),
"Expected attach failure error, got: {error_msg}"
);
}
Ok(msg) => {
panic!("Expected attach to fail in test environment, but got success: {msg}");
}
}
assert!(TmuxCommand::session_exists("attach-flag-test").unwrap());
let _ = TmuxCommand::kill_session("attach-flag-test");
}
#[test]
fn test_start_session_no_attach_flag() {
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("no-attach-test.yml");
let yaml_content = r#"
name: no-attach-test
root: /tmp
windows:
- editor: vim
"#;
std::fs::write(&config_file, yaml_content).unwrap();
let session_manager = SessionManager::new();
let result = session_manager.start_session_with_options(
Some("no-attach-test"),
Some(&config_dir),
false, false, );
assert!(result.is_ok(), "Should create detached session: {result:?}");
assert!(TmuxCommand::session_exists("no-attach-test").unwrap());
let _ = TmuxCommand::kill_session("no-attach-test");
}
#[test]
fn test_existing_session_with_attach() {
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("existing-attach-test.yml");
let yaml_content = r#"
name: existing-attach-test
root: /tmp
windows:
- editor: vim
"#;
std::fs::write(&config_file, yaml_content).unwrap();
let session_manager = SessionManager::new();
let _ = session_manager.start_session_with_options(
Some("existing-attach-test"),
Some(&config_dir),
false, false, );
let result = session_manager.start_session_with_options(
Some("existing-attach-test"),
Some(&config_dir),
true, false, );
assert!(
result.is_err(),
"Should fail to attach to existing session in test environment: {result:?}"
);
assert!(result.unwrap_err().to_string().contains("Failed to attach"));
let _ = TmuxCommand::kill_session("existing-attach-test");
}