Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

test(switch): assert alt-l/alt-h no-op via before/after equality (#3245)

Maximilian Roos94b989c8f36f
diff --git a/tests/integration_tests/switch_picker.rs b/tests/integration_tests/switch_picker.rs
index 384077639..b966aca69 100644
--- a/tests/integration_tests/switch_picker.rs
+++ b/tests/integration_tests/switch_picker.rs
@@ -104,13 +104,7 @@ impl PtyResult {
/// Avoids the │ border character that causes cross-platform rendering issues.
fn panels(&self) -> (String, String) {
let screen = self.parser.screen();
- let list = screen
- .rows(0, LIST_WIDTH)
- .map(|row| row.trim_end().to_string())
- .collect::<Vec<_>>()
- .join("\n")
- .trim_end()
- .to_string();
+ let list = list_pane_text(screen);
let preview = screen
.rows(PREVIEW_START_COL, TERM_COLS - PREVIEW_START_COL)
.map(|row| row.trim_end().to_string())
@@ -122,6 +116,19 @@ impl PtyResult {
}
}
+/// The list pane: screen columns left of the skim border, trailing whitespace
+/// trimmed (vt100 pads rows to the full width; that padding is buffer fill, not
+/// content, and varies across platforms).
+fn list_pane_text(screen: &vt100::Screen) -> String {
+ screen
+ .rows(0, LIST_WIDTH)
+ .map(|row| row.trim_end().to_string())
+ .collect::<Vec<_>>()
+ .join("\n")
+ .trim_end()
+ .to_string()
+}
+
/// Assert that exit code is valid for skim abort (0, 1, or 130)
fn assert_valid_abort_exit_code(exit_code: i32) {
// Skim exits with:
@@ -141,35 +148,32 @@ fn is_skim_ready(screen_content: &str) -> bool {
screen_content.starts_with("> ") || screen_content.contains("\n> ")
}
-/// Execute a command in a PTY and return the parsed terminal state.
+/// Live handles for a booted picker PTY session.
///
-/// Uses polling with stabilization detection instead of fixed delays.
-fn exec_in_pty_with_input(
- command: &str,
- args: &[&str],
- working_dir: &Path,
- env_vars: &[(String, String)],
- input: &str,
-) -> PtyResult {
- exec_in_pty_with_input_expectations(command, args, working_dir, env_vars, &[(input, None)])
+/// `_master` is held only to keep the pseudo-terminal open for the session's
+/// lifetime; it is never read. Dropping it tears down the Windows ConPTY, after
+/// which every write to `writer` fails with `BrokenPipe`. On Unix `take_writer()`
+/// hands back an independent fd, so the master's lifetime is irrelevant — which
+/// is exactly why dropping it early passes locally and on Linux/macOS CI yet
+/// wipes out every picker test on Windows.
+struct PickerSession {
+ child: Box<dyn portable_pty::Child + Send + Sync>,
+ _master: Box<dyn portable_pty::MasterPty + Send>,
+ writer: crate::common::pty::SharedPtyWriter,
+ rx: mpsc::Receiver<Vec<u8>>,
+ parser: vt100::Parser,
}
-/// Execute a command in a PTY with a sequence of inputs and optional content expectations.
-///
-/// Each input is `(input_bytes, expected_content)`:
-/// - `expected_content`: a substring that must appear on screen before the input is considered
-/// processed. Required for async preview content that lands later than the prompt update.
-///
-/// Example: `[("\x1b[B", None), ("\x1b3", Some("diff --git"))]`
-/// - After Down (move cursor to the next worktree): just wait for the screen to settle.
-/// - After Alt-3 (switch to the main…± diff panel): wait until "diff --git" appears.
-fn exec_in_pty_with_input_expectations(
+/// Spawn `command` in an isolated PTY, wait until skim is ready and the initial
+/// render has stabilized, and return the live session handles. Every picker PTY
+/// helper shares this boot sequence; they differ only in how they drive the
+/// session and capture its frames.
+fn boot_picker_pty(
command: &str,
args: &[&str],
working_dir: &Path,
env_vars: &[(String, String)],
- inputs: &[(&str, Option<&str>)],
-) -> PtyResult {
+) -> PickerSession {
let pair = crate::common::open_pty_with_size(TERM_ROWS, TERM_COLS);
let mut cmd = CommandBuilder::new(command);
@@ -178,17 +182,17 @@ fn exec_in_pty_with_input_expectations(
}
cmd.cwd(working_dir);
- // Set up isolated environment with coverage passthrough
+ // Isolated environment with coverage passthrough
crate::common::configure_pty_command(&mut cmd);
cmd.env("CLICOLOR_FORCE", "1");
cmd.env("TERM", "xterm-256color");
- // Add test-specific environment variables
+ // Test-specific environment variables
for (key, value) in env_vars {
cmd.env(key, value);
}
- let mut child = pair.slave.spawn_command(cmd).unwrap();
+ let child = pair.slave.spawn_command(cmd).unwrap();
drop(pair.slave);
let reader = pair.master.try_clone_reader().unwrap();
@@ -201,17 +205,12 @@ fn exec_in_pty_with_input_expectations(
let mut parser = vt100::Parser::new(TERM_ROWS, TERM_COLS, 0);
- // Helper to drain available output from the channel (non-blocking)
- let drain_output = |rx: &mpsc::Receiver<Vec<u8>>, parser: &mut vt100::Parser| {
- while let Ok(chunk) = rx.try_recv() {
- parser.process(&chunk);
- }
- };
-
// Wait for skim to be ready (show "> " prompt)
let start = Instant::now();
loop {
- drain_output(&rx, &mut parser);
+ while let Ok(chunk) = rx.try_recv() {
+ parser.process(&chunk);
+ }
let screen_content = parser.screen().contents();
if is_skim_ready(&screen_content) {
@@ -232,6 +231,91 @@ fn exec_in_pty_with_input_expectations(
// Wait for initial render to stabilize
wait_for_stable(&rx, &mut parser);
+ PickerSession {
+ child,
+ _master: pair.master,
+ writer,
+ rx,
+ parser,
+ }
+}
+
+/// Send Escape to abort the picker, then drain and discard remaining output —
+/// the caller has already captured the frame it wants, so teardown bytes must
+/// not reach its parser. Consumes the handles and returns the exit code.
+fn abort_and_exit_code(
+ mut child: Box<dyn portable_pty::Child + Send + Sync>,
+ writer: crate::common::pty::SharedPtyWriter,
+ rx: mpsc::Receiver<Vec<u8>>,
+) -> i32 {
+ {
+ let mut w = writer.lock().unwrap();
+ w.write_all(b"\x1b").unwrap();
+ w.flush().unwrap();
+ }
+ drop(writer);
+
+ let start = Instant::now();
+ let timeout = Duration::from_secs(5);
+ loop {
+ while rx.try_recv().is_ok() {} // discard chunks
+ if child.try_wait().unwrap().is_some() {
+ break;
+ }
+ if start.elapsed() >= timeout {
+ let _ = child.kill();
+ break;
+ }
+ std::thread::sleep(Duration::from_millis(10));
+ }
+
+ child.wait().unwrap().exit_code() as i32
+}
+
+/// Execute a command in a PTY and return the parsed terminal state.
+///
+/// Uses polling with stabilization detection instead of fixed delays.
+fn exec_in_pty_with_input(
+ command: &str,
+ args: &[&str],
+ working_dir: &Path,
+ env_vars: &[(String, String)],
+ input: &str,
+) -> PtyResult {
+ exec_in_pty_with_input_expectations(command, args, working_dir, env_vars, &[(input, None)])
+}
+
+/// Execute a command in a PTY with a sequence of inputs and optional content expectations.
+///
+/// Each input is `(input_bytes, expected_content)`:
+/// - `expected_content`: a substring that must appear on screen before the input is considered
+/// processed. Required for async preview content that lands later than the prompt update.
+///
+/// Example: `[("\x1b[B", None), ("\x1b3", Some("diff --git"))]`
+/// - After Down (move cursor to the next worktree): just wait for the screen to settle.
+/// - After Alt-3 (switch to the main…± diff panel): wait until "diff --git" appears.
+fn exec_in_pty_with_input_expectations(
+ command: &str,
+ args: &[&str],
+ working_dir: &Path,
+ env_vars: &[(String, String)],
+ inputs: &[(&str, Option<&str>)],
+) -> PtyResult {
+ let PickerSession {
+ mut child,
+ _master,
+ writer,
+ rx,
+ mut parser,
+ } = boot_picker_pty(command, args, working_dir, env_vars);
+
+ // Helper to drain available output from the channel (non-blocking)
+ let drain_output = |rx: &mpsc::Receiver<Vec<u8>>, parser: &mut vt100::Parser| {
+ while let Ok(chunk) = rx.try_recv() {
+ parser.process(&chunk);
+ }
+ };
+
// Send each input and wait for screen to stabilize after each
for (input, expected_content) in inputs {
send_input_awaiting_content(&writer, &rx, &mut parser, input, *expected_content);
@@ -280,62 +364,13 @@ fn exec_in_pty_capture_before_abort(
env_vars: &[(String, String)],
pre_abort_inputs: &[(&str, Option<&str>)],
) -> PtyResult {
- let pair = crate::common::open_pty_with_size(TERM_ROWS, TERM_COLS);
-
- let mut cmd = CommandBuilder::new(command);
- for arg in args {
- cmd.arg(arg);
- }
- cmd.cwd(working_dir);
-
- crate::common::configure_pty_command(&mut cmd);
- cmd.env("CLICOLOR_FORCE", "1");
- cmd.env("TERM", "xterm-256color");
-
- for (key, value) in env_vars {
- cmd.env(key, value);
- }
-
- let mut child = pair.slave.spawn_command(cmd).unwrap();
- drop(pair.slave);
-
- let reader = pair.master.try_clone_reader().unwrap();
- let writer: crate::common::pty::SharedPtyWriter =
- Arc::new(Mutex::new(pair.master.take_writer().unwrap()));
-
- let rx = crate::common::pty::spawn_pty_reader_answering_queries(reader, Arc::clone(&writer));
-
- let mut parser = vt100::Parser::new(TERM_ROWS, TERM_COLS, 0);
-
- let drain_output = |rx: &mpsc::Receiver<Vec<u8>>, parser: &mut vt100::Parser| {
- while let Ok(chunk) = rx.try_recv() {
- parser.process(&chunk);
- }
- };
-
- // Wait for skim to be ready
- let start = Instant::now();
- loop {
- drain_output(&rx, &mut parser);
-
- let screen_content = parser.screen().contents();
- if is_skim_ready(&screen_content) {
- break;
- }
-
- if start.elapsed() > READY_TIMEOUT {
- eprintln!(
- "Warning: Timed out waiting for skim ready state. Screen content:\n{}",
- screen_content
- );
- break;
- }
-
- std::thread::sleep(POLL_INTERVAL);
- }
-
- // Wait for initial render to stabilize
- wait_for_stable(&rx, &mut parser);
+ let PickerSession {
+ child,
+ _master,
+ writer,
+ rx,
+ mut parser,
+ } = boot_picker_pty(command, args, working_dir, env_vars);
// Send pre-abort inputs (filter text, panel switches, etc.)
for (input, expected_content) in pre_abort_inputs {
@@ -344,34 +379,52 @@ fn exec_in_pty_capture_before_abort(
// === CAPTURE: screen state is now stable — snapshot BEFORE aborting ===
// The parser retains this state because we stop feeding output to it.
+ let exit_code = abort_and_exit_code(child, writer, rx);
- // Send Escape to abort
- {
- let mut w = writer.lock().unwrap();
- w.write_all(b"\x1b").unwrap();
- w.flush().unwrap();
- }
- drop(writer);
+ PtyResult { parser, exit_code }
+}
- // Drain remaining output WITHOUT feeding to parser — preserves pre-abort screen
- let start = Instant::now();
- let timeout = Duration::from_secs(5);
- loop {
- while rx.try_recv().is_ok() {} // discard chunks
- if child.try_wait().unwrap().is_some() {
- break;
- }
- if start.elapsed() >= timeout {
- let _ = child.kill();
- break;
- }
- std::thread::sleep(Duration::from_millis(10));
+/// Drive the picker to a settled baseline, capture the list pane, send a
+/// sequence of keys, capture the list pane again, then abort. Returns
+/// `(baseline, after, exit_code)` so the caller can assert the keys left the
+/// list byte-for-byte unchanged.
+///
+/// This is the invariant form of a "this key is a visual no-op" test. It
+/// commits no frame, so picker column-layout changes never touch it, and there
+/// is no frozen baseline that can capture a different async-render frame than a
+/// sibling snapshot test — the failure mode the old committed snapshot hit.
+/// Both captures bracket only the probe keys and are each taken once the screen
+/// has settled, so a genuine no-op yields byte-identical frames.
+fn exec_in_pty_capture_noop_probe(
+ command: &str,
+ args: &[&str],
+ working_dir: &Path,
+ env_vars: &[(String, String)],
+ baseline_inputs: &[(&str, Option<&str>)],
+ probe_inputs: &[(&str, Option<&str>)],
+) -> (String, String, i32) {
+ let PickerSession {
+ child,
+ _master,
+ writer,
+ rx,
+ mut parser,
+ } = boot_picker_pty(command, args, working_dir, env_vars);
+
+ // Settle to the baseline, then capture it.
+ for (input, expected_content) in baseline_inputs {
+ send_input_awaiting_content(&writer, &rx, &mut parser, input, *expected_content);
}
+ let baseline = list_pane_text(parser.screen());
- let exit_status = child.wait().unwrap();
- let exit_code = exit_status.exit_code() as i32;
+ // Send the probe keys, then capture again.
+ for (input, expected_content) in probe_inputs {
+ send_input_awaiting_content(&writer, &rx, &mut parser, input, *expected_content);
+ }
+ let after = list_pane_text(parser.screen());
- PtyResult { parser, exit_code }
+ let exit_code = abort_and_exit_code(child, writer, rx);
+ (baseline, after, exit_code)
}
/// Wait for screen content to stabilize (no changes for STABLE_DURATION)
@@ -704,37 +757,39 @@ fn test_switch_picker_with_multiple_worktrees(mut repo: TestRepo) {
/// owns its layout with a leading worktree-status sigil; an unbound alt-l slides
/// every row left, clipping that sigil gutter (`no_hscroll(true)` only gates the
/// automatic match-following shift, not the manual offset these keys push).
-/// Pressing alt-l here must leave the list byte-for-byte unscrolled — the same
-/// frame `test_switch_picker_with_multiple_worktrees` snapshots.
+///
+/// The belief is narrow — "alt-l/alt-h change nothing" — so the test asserts it
+/// directly: capture the settled list, press the keys, capture again, require
+/// the two byte-for-byte equal. No committed frame, so picker column-layout
+/// changes never touch this test and the equality can't drift on async-column
+/// render timing the way a frozen snapshot can.
#[rstest]
fn test_switch_picker_alt_l_does_not_hscroll(mut repo: TestRepo) {
repo.remove_fixture_worktrees();
- // Remove origin so snapshots don't show origin/main
+ // Remove origin so the list doesn't show origin/main
repo.run_git(&["remote", "remove", "origin"]);
repo.add_worktree("feature-one");
repo.add_worktree("feature-two");
let env_vars = repo.test_env_vars();
- let result = exec_in_pty_capture_before_abort(
+ let (baseline, after, exit_code) = exec_in_pty_capture_noop_probe(
wt_bin().to_str().unwrap(),
&["switch"],
repo.root_path(),
&env_vars,
+ &[("", Some("feature-two"))], // settle: wait for items to render
&[
- ("", Some("feature-two")), // wait for items to render
- ("\x1bl", None), // Alt-l: ignored, must not scroll
- ("\x1bl", None), // a second press, still ignored
- ("\x1bh", None), // Alt-h: ignored too
+ ("\x1bl", None), // Alt-l: ignored, must not scroll
+ ("\x1bl", None), // a second press, still ignored
+ ("\x1bh", None), // Alt-h: ignored too
],
);
- assert_valid_abort_exit_code(result.exit_code);
-
- let (list, _preview) = result.panels();
- let settings = switch_picker_settings(&repo);
- settings.bind(|| {
- assert_snapshot!("switch_picker_alt_l_ignored_list", list);
- });
+ assert_valid_abort_exit_code(exit_code);
+ assert_eq!(
+ baseline, after,
+ "alt-l/alt-h must leave the list unscrolled (left = before keys, right = after)"
+ );
}
#[rstest]
diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_alt_l_ignored_list.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_alt_l_ignored_list.snap
deleted file mode 100644
index d9c73a430..000000000
--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_alt_l_ignored_list.snap
+++ /dev/null
@@ -1,9 +0,0 @@
----
-source: tests/integration_tests/switch_picker.rs
-expression: list
----
->
- Branch Status HEAD± main↕ main…± R
-> @ main ^
- + feature-one _
- + feature-two _