claude-print/tests/pty_integration.rs
jedarden 17c35f40a7 Add mock-claude fixture, test_pty_spawns_tty integration test, and hook module export
- test-fixtures/mock-claude: implement mock binary that writes 'stop' to FIFO then checks isatty(stdin), exiting 0 on TTY
- tests/pty_integration.rs: test_pty_spawns_tty uses HookInstaller + PtySpawner to verify controlling TTY is established
- src/lib.rs: expose hook module publicly
- Cargo.lock: add libc dependency for mock-claude

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-08 08:56:36 -04:00

47 lines
1.7 KiB
Rust

use claude_print::hook::HookInstaller;
use claude_print::pty::PtySpawner;
use std::ffi::CString;
use std::io::Read;
/// Locate the mock-claude binary compiled alongside the test binary.
/// Test binaries live at `target/<profile>/deps/`; other bins at `target/<profile>/`.
fn mock_claude_bin() -> std::path::PathBuf {
let exe = std::env::current_exe().expect("current_exe");
let profile_dir = exe
.parent() // deps/
.and_then(|p| p.parent()) // target/<profile>/
.expect("unexpected test binary path");
profile_dir.join("mock-claude")
}
#[test]
fn test_pty_spawns_tty() {
let installer = HookInstaller::new().expect("HookInstaller::new");
let fifo_path = installer.fifo_path.clone();
// Read from the FIFO in a background thread so mock-claude's write doesn't block.
let reader = std::thread::spawn(move || {
let mut file = std::fs::File::open(&fifo_path).expect("open stop.fifo");
let mut buf = String::new();
file.read_to_string(&mut buf).ok();
buf
});
let bin = mock_claude_bin();
let cmd = CString::new(bin.to_str().expect("binary path is utf-8")).expect("CString");
let fifo_arg =
CString::new(installer.fifo_path.to_str().expect("fifo path is utf-8")).expect("CString");
let spawner = PtySpawner::spawn(&cmd, &[fifo_arg]).expect("PtySpawner::spawn");
let exit_code = spawner.relay().expect("relay");
let fifo_content = reader.join().expect("reader thread");
assert!(
fifo_content.contains("stop"),
"expected 'stop' in FIFO content, got: {fifo_content:?}",
);
assert_eq!(
exit_code, 0,
"mock-claude should detect a controlling TTY (isatty(0) returned false)",
);
}