use std::fs;
use std::process::Command;
fn get_scriptty_bin() -> std::path::PathBuf {
env!("CARGO_BIN_EXE_scriptty").into()
}
#[test]
fn test_basic_script_execution() {
let script = r#"
wait 100ms
type "echo test"
wait 100ms
"#;
let script_path = "/tmp/test_basic.script";
fs::write(script_path, script).expect("Failed to write test script");
let output = Command::new(get_scriptty_bin())
.arg("--script")
.arg(script_path)
.arg("--command")
.arg("sh")
.output()
.expect("Failed to execute scriptty");
assert!(
output.status.success(),
"scriptty failed with stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("echo test"),
"Output should contain typed command"
);
let _ = fs::remove_file(script_path);
}
#[test]
fn test_expect_command() {
let script = r#"
expect "$"
type "echo 'Hello World'"
expect "Hello World"
wait 200ms
type "exit"
"#;
let script_path = "/tmp/test_expect.script";
fs::write(script_path, script).expect("Failed to write test script");
let output = Command::new(get_scriptty_bin())
.arg("--script")
.arg(script_path)
.arg("--command")
.arg("sh")
.output()
.expect("Failed to execute scriptty");
assert!(
output.status.success(),
"scriptty failed with stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("Hello World"),
"Output should contain expected text"
);
let _ = fs::remove_file(script_path);
}
#[test]
fn test_send_command() {
let script = r#"
expect "$"
send "echo instant"
wait 200ms
type "exit"
"#;
let script_path = "/tmp/test_send.script";
fs::write(script_path, script).expect("Failed to write test script");
let output = Command::new(get_scriptty_bin())
.arg("--script")
.arg(script_path)
.arg("--command")
.arg("sh")
.output()
.expect("Failed to execute scriptty");
assert!(
output.status.success(),
"scriptty failed with stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let _ = fs::remove_file(script_path);
}
#[test]
fn test_invalid_script() {
let script = r#"
invalid_command "test"
"#;
let script_path = "/tmp/test_invalid.script";
fs::write(script_path, script).expect("Failed to write test script");
let output = Command::new(get_scriptty_bin())
.arg("--script")
.arg(script_path)
.arg("--command")
.arg("sh")
.output()
.expect("Failed to execute scriptty");
assert!(
!output.status.success(),
"scriptty should fail with invalid command"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("Unknown command"),
"Error should mention unknown command"
);
let _ = fs::remove_file(script_path);
}
#[test]
fn test_expect_timeout() {
let script = r#"
expect "$"
type "echo test"
expect "this_will_never_appear" 500ms
"#;
let script_path = "/tmp/test_timeout.script";
fs::write(script_path, script).expect("Failed to write test script");
let output = Command::new(get_scriptty_bin())
.arg("--script")
.arg(script_path)
.arg("--command")
.arg("sh")
.output()
.expect("Failed to execute scriptty");
assert!(
!output.status.success(),
"scriptty should fail with timeout"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("Timeout"), "Error should mention timeout");
let _ = fs::remove_file(script_path);
}
#[test]
fn test_comment_handling() {
let script = r#"
# This is a comment
expect "$" # Wait for prompt
# Another comment
type "echo test"
wait 200ms
type "exit"
"#;
let script_path = "/tmp/test_comments.script";
fs::write(script_path, script).expect("Failed to write test script");
let output = Command::new(get_scriptty_bin())
.arg("--script")
.arg(script_path)
.arg("--command")
.arg("sh")
.output()
.expect("Failed to execute scriptty");
assert!(
output.status.success(),
"scriptty failed with stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let _ = fs::remove_file(script_path);
}