Files
codex/codex-rs/tui/src/clipboard_paste/text_tests.rs
T
Eric Traut 7a8a9456e9 Reuse and prewarm the WSL clipboard text reader (#52967)
## Why

WSL clipboard reads previously launched PowerShell for every paste. Process creation can hang under WSL and stall the shared clipboard worker.

## What changed

- Reuse a persistent PowerShell reader on a dedicated thread, keeping clipboard requests bounded by their deadlines and restarting the reader after errors.
- Prewarm the reader at startup when right-click paste is allowed on WSL, without accessing clipboard contents.
- Use length-framed UTF-8 responses to preserve whitespace and distinguish consecutive pastes while enforcing message size limits.

## Testing

Update the command-reader test to cover consecutive responses from one process, large clipboard text, UTF-8 and whitespace preservation, and read timeouts.

GitOrigin-RevId: 36af338097d6b178ab4887faa5918240c47e1515
2026-10-10 19:22:11 +00:00

57 lines
1.8 KiB
Rust

use super::*;
use pretty_assertions::assert_eq;
use std::time::Duration;
#[test]
fn text_preserves_whitespace_and_rejects_late_or_oversized_reads() {
let deadline = Instant::now() + Duration::from_secs(/*secs*/ 5);
assert_eq!(
validate(" café\r\n\t".into(), deadline),
Ok(" café\r\n\t".into())
);
assert!(validate("late".into(), Instant::now()).is_err());
assert!(validate("x".repeat(MAX_USER_INPUT_TEXT_CHARS + 1), deadline).is_err());
}
#[cfg(unix)]
#[tokio::test]
async fn command_drains_large_output_and_bounds_waiting() {
use std::process::Stdio;
let mut child = tokio::process::Command::new("sh")
.args([
"-c",
// Two distinct framed clipboard results, only in response to separate requests.
r#"read -r request; printf '\001\000\002\000'; head -c 131072 /dev/zero | tr '\000' x; printf '\n'; read -r request; printf '\010\000\000\000 café\r\n'"#,
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.kill_on_drop(/*kill_on_drop*/ true)
.spawn()
.unwrap();
let deadline = Instant::now() + Duration::from_secs(/*secs*/ 5);
assert_eq!(
read_command(&mut child, deadline).await,
Ok(format!("{}\n", "x".repeat(/*n*/ 131072)))
);
assert_eq!(
read_command(&mut child, deadline).await,
Ok(" café\r\n".into())
);
let mut child = tokio::process::Command::new("sh")
.args(["-c", "exec sleep 10"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.kill_on_drop(/*kill_on_drop*/ true)
.spawn()
.unwrap();
assert_eq!(
read_command(
&mut child,
Instant::now() + Duration::from_millis(/*millis*/ 20)
)
.await,
Err("clipboard read timed out".into())
);
}