use std::io;
use tokio::task::JoinHandle;
use crate::agent::cli::stdin::{
await_optional_stdin_write, is_broken_pipe_error, spawn_optional_stdin_write,
};
#[derive(Debug, PartialEq)]
struct TestError(String);
fn make_test_error(message: String) -> TestError {
TestError(message)
}
#[test]
fn test_spawn_returns_none_when_no_payload() {
let result =
spawn_optional_stdin_write::<TestError>(None, None, "stdin unavailable", make_test_error);
assert!(result.is_none());
}
#[tokio::test]
async fn test_spawn_returns_error_when_stdin_unavailable() {
let payload = Some(b"prompt".to_vec());
let handle = spawn_optional_stdin_write::<TestError>(
None,
payload,
"stdin unavailable",
make_test_error,
);
let join_result = handle
.expect("expected a task handle")
.await
.expect("task should not panic");
assert_eq!(join_result, Err(TestError("stdin unavailable".to_string())));
}
#[tokio::test]
async fn test_spawn_writes_payload_to_child_stdin() {
let mut child_command = tokio::process::Command::new("cat");
child_command.stdin(std::process::Stdio::piped());
child_command.stdout(std::process::Stdio::piped());
let mut child = child_command.spawn().expect("failed to spawn cat");
let child_stdin = child.stdin.take();
let payload = Some(b"hello world".to_vec());
let handle = spawn_optional_stdin_write::<TestError>(
child_stdin,
payload,
"stdin unavailable",
make_test_error,
);
let write_result = handle
.expect("expected a task handle")
.await
.expect("task should not panic");
assert!(write_result.is_ok());
let output = child
.wait_with_output()
.await
.expect("failed to read cat output");
assert_eq!(output.stdout, b"hello world");
}
#[tokio::test]
async fn test_await_returns_ok_when_no_task() {
let result =
await_optional_stdin_write::<TestError>(None, "join failed", make_test_error).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_await_propagates_inner_error() {
let handle = tokio::spawn(async { Err(TestError("write failed".to_string())) });
let result = await_optional_stdin_write(Some(handle), "join failed", make_test_error).await;
assert_eq!(result, Err(TestError("write failed".to_string())));
}
#[tokio::test]
async fn test_await_returns_join_error_when_task_is_aborted() {
let handle: JoinHandle<Result<(), TestError>> = tokio::spawn(async {
tokio::time::sleep(std::time::Duration::from_mins(1)).await;
Ok(())
});
handle.abort();
let result = await_optional_stdin_write(Some(handle), "join failed", make_test_error).await;
let error = result.expect_err("expected a join error");
assert!(error.0.starts_with("join failed:"));
}
#[test]
fn test_broken_pipe_returns_true() {
let error = io::Error::new(io::ErrorKind::BrokenPipe, "broken pipe");
assert!(is_broken_pipe_error(&error));
}
#[test]
fn test_non_broken_pipe_returns_false() {
let error = io::Error::new(io::ErrorKind::PermissionDenied, "denied");
assert!(!is_broken_pipe_error(&error));
}