Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: use vt100 rows() API and insta filters for switch picker snapshots (#975)
Maximilian Roos003bf9f829dd
diff --git a/tests/integration_tests/switch_picker.rs b/tests/integration_tests/switch_picker.rsindex eb024c621..eb0ffab7d 100644--- a/tests/integration_tests/switch_picker.rs+++ b/tests/integration_tests/switch_picker.rs@@ -5,8 +5,9 @@//! what the user actually sees on screen, enabling meaningful snapshot testing of//! the skim-based TUI interface.//!-//! The tests normalize timing-sensitive parts of the output (query line, count-//! indicators) to ensure stable snapshots despite TUI rendering variations.+//! Timing-sensitive output (query line, commit hashes, timestamps, count indicators)+//! is normalized via insta filters for stable snapshots. Preview tests split the+//! screen into list and preview panels to avoid the │ border character entirely.//!//! ## Timing Strategy//!@@ -50,6 +51,10 @@ const STABLE_DURATION: Duration = Duration::from_millis(500);/// Fast polling ensures tests complete quickly when ready.const POLL_INTERVAL: Duration = Duration::from_millis(10);+/// Column where skim renders the │ border between list and preview panels.+/// TERM_COLS=120, preview window spec `right:60` → list gets 60 cols, separator at 60.+const SEPARATOR_COL: u16 = 60;+/// 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:@@ -269,61 +274,62 @@ fn wait_for_stable_with_content();}-/// Render raw PTY output through vt100 terminal emulator to get clean screen text+/// Render raw PTY output through vt100 terminal emulator to get clean screen text.+///+/// Uses vt100's built-in `rows()` API which handles wide characters, trailing+/// whitespace, and content extraction correctly — replacing the previous manual+/// cell-by-cell iteration.fn render_terminal_screen(raw_output: &[u8]) -> String {let mut parser = vt100::Parser::new(TERM_ROWS, TERM_COLS, 0);parser.process(raw_output);+ parser+ .screen()+ .rows(0, TERM_COLS)+ .collect::<Vec<_>>()+ .join("\n")+}- let screen = parser.screen();- let mut lines = Vec::new();+/// Create insta settings with filters for switch picker snapshot stability.+///+/// Replaces the manual `normalize_output()` approach with declarative insta filters.+/// Since `rows()` returns plain text (no ANSI codes, no OSC 8 hyperlinks),+/// `add_pty_filters()` and `strip_osc8_hyperlinks()` are not needed.+fn switch_picker_settings(repo: &TestRepo) -> insta::Settings {+ let mut settings = crate::common::setup_snapshot_settings(repo);- for row in 0..TERM_ROWS {- let mut line = String::new();- for col in 0..TERM_COLS {- if let Some(cell) = screen.cell(row, col) {- line.push_str(cell.contents());- }- }- // Trim trailing whitespace from each line- lines.push(line.trim_end().to_string());- }+ // Query line has timing variations (shows typed chars at different rates).+ // \A anchors to absolute start of string, matching only the first line.+ settings.add_filter(r"\A> [^\n]*", "> [QUERY]");- // Join all lines - preserve blank lines for cross-platform consistency- // The preview panel intentionally shows blank lines even when empty- lines.join("\n")-}+ // Skim count indicators (matched/total) at end of lines+ settings.add_filter(r"(?m)\d+/\d+\s*$", "[N/M]");-/// Normalize output for snapshot stability-fn normalize_output(output: &str) -> String {- // Strip OSC 8 hyperlinks first (git on macOS generates these in diffs)- let output = worktrunk::styling::strip_osc8_hyperlinks(output);+ // Commit hashes (7-8 hex chars)+ settings.add_filter(r"\b[0-9a-f]{7,8}\b", "[HASH]");- let mut lines: Vec<&str> = output.lines().collect();+ // Truncated commit hashes (6+ hex chars followed by ..) in narrow columns+ settings.add_filter(r"\b[0-9a-f]{6,8}\.\.", "[HASH]..");- // Normalize line 1 (query line) - replace with fixed marker- // This line shows typed query which has timing variations- if !lines.is_empty() {- lines[0] =- "> [QUERY] │[PREVIEW_HEADER]";- }+ // Relative timestamps (1d, 16h, etc.)+ settings.add_filter(r"\b\d+[dhms]\b", "[TIME]");- let output = lines.join("\n");+ settings+}- // Replace temp paths like /var/folders/.../repo.XXX with _REPO_- let re = regex::Regex::new(r"/[^\s]+\.tmp[^\s/]*").unwrap();- let output = re.replace_all(&output, "_REPO_");+/// Extract list and preview panel content separately, avoiding the │ border character+/// that has been the source of cross-platform rendering failures.+fn extract_panels(raw_output: &[u8]) -> (String, String) {+ let mut parser = vt100::Parser::new(TERM_ROWS, TERM_COLS, 0);+ parser.process(raw_output);+ let screen = parser.screen();- // Replace count indicators like "1/4", "3/4" etc at end of lines- let count_re = regex::Regex::new(r"\d+/\d+$").unwrap();- let output = count_re.replace_all(&output, "[N/M]");+ let list = screen.rows(0, SEPARATOR_COL).collect::<Vec<_>>().join("\n");+ let preview = screen+ .rows(SEPARATOR_COL + 1, TERM_COLS - SEPARATOR_COL - 1)+ .collect::<Vec<_>>()+ .join("\n");- // Replace home directory paths- if let Some(home) = home::home_dir() {- let home_str = home.to_string_lossy();- output.replace(&*home_str, "~")- } else {- output.to_string()- }+ (list, preview)}#[rstest]@@ -344,8 +350,10 @@ fn test_switch_picker_abort_with_escape(mut repo: TestRepo) {assert_valid_abort_exit_code(exit_code);let screen = render_terminal_screen(&raw_output);- let normalized = normalize_output(&screen);- assert_snapshot!("switch_picker_abort_escape", normalized);+ let settings = switch_picker_settings(&repo);+ settings.bind(|| {+ assert_snapshot!("switch_picker_abort_escape", screen);+ });}#[rstest]@@ -369,8 +377,10 @@ fn test_switch_picker_with_multiple_worktrees(mut repo: TestRepo) {assert_valid_abort_exit_code(exit_code);let screen = render_terminal_screen(&raw_output);- let normalized = normalize_output(&screen);- assert_snapshot!("switch_picker_multiple_worktrees", normalized);+ let settings = switch_picker_settings(&repo);+ settings.bind(|| {+ assert_snapshot!("switch_picker_multiple_worktrees", screen);+ });}#[rstest]@@ -400,8 +410,10 @@ fn test_switch_picker_with_branches(mut repo: TestRepo) {assert_valid_abort_exit_code(exit_code);let screen = render_terminal_screen(&raw_output);- let normalized = normalize_output(&screen);- assert_snapshot!("switch_picker_with_branches", normalized);+ let settings = switch_picker_settings(&repo);+ settings.bind(|| {+ assert_snapshot!("switch_picker_with_branches", screen);+ });}#[rstest]@@ -457,9 +469,12 @@ fn test_switch_picker_preview_panel_uncommitted(mut repo: TestRepo) {assert_valid_abort_exit_code(exit_code);- let screen = render_terminal_screen(&raw_output);- let normalized = normalize_output(&screen);- assert_snapshot!("switch_picker_preview_uncommitted", normalized);+ let (list, preview) = extract_panels(&raw_output);+ let settings = switch_picker_settings(&repo);+ settings.bind(|| {+ assert_snapshot!("switch_picker_preview_uncommitted_list", list);+ assert_snapshot!("switch_picker_preview_uncommitted_preview", preview);+ });}#[rstest]@@ -514,9 +529,12 @@ fn test_switch_picker_preview_panel_log(mut repo: TestRepo) {assert_valid_abort_exit_code(exit_code);- let screen = render_terminal_screen(&raw_output);- let normalized = normalize_output(&screen);- assert_snapshot!("switch_picker_preview_log", normalized);+ let (list, preview) = extract_panels(&raw_output);+ let settings = switch_picker_settings(&repo);+ settings.bind(|| {+ assert_snapshot!("switch_picker_preview_log_list", list);+ assert_snapshot!("switch_picker_preview_log_preview", preview);+ });}#[rstest]@@ -604,9 +622,12 @@ fn test_new_feature() {assert_valid_abort_exit_code(exit_code);- let screen = render_terminal_screen(&raw_output);- let normalized = normalize_output(&screen);- assert_snapshot!("switch_picker_preview_main_diff", normalized);+ let (list, preview) = extract_panels(&raw_output);+ let settings = switch_picker_settings(&repo);+ settings.bind(|| {+ assert_snapshot!("switch_picker_preview_main_diff_list", list);+ assert_snapshot!("switch_picker_preview_main_diff_preview", preview);+ });}#[rstest]diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape.snapindex 6462c4ad3..fdc1808b5 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_abort_escape.snap@@ -1,31 +1,31 @@---source: tests/integration_tests/switch_picker.rs-expression: normalized+expression: screen----> [QUERY] │[PREVIEW_HEADER]+> [QUERY]Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr-> @ main ^ 05a4a45d │- │○ main has no uncommitted changes- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │+> @ main ^ [HASH] │+ │○ main has no uncommitted changes+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ ││diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees.snapindex 4f149b979..e9dd62127 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_multiple_worktrees.snap@@ -1,31 +1,31 @@---source: tests/integration_tests/switch_picker.rs-expression: normalized+expression: screen----> [QUERY] │[PREVIEW_HEADER]+> [QUERY]Branch Status HEAD± main↕ Remote⇅ Age │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr-> @ main ^ 16h │- + feature-one _ 16h │○ main has no uncommitted changes- + feature-two _ 16h │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │+> @ main ^ [TIME] │+ + feature-one _ [TIME] │○ main has no uncommitted changes+ + feature-two _ [TIME] │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ ││diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log.snapdeleted file mode 100644index 6b6038aef..000000000--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log.snap+++ /dev/null@@ -1,31 +0,0 @@-----source: tests/integration_tests/switch_picker.rs-expression: normalized-----> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr-> + feature ↑ ↑5 855962..│- │* 8559621 +1 1d (feature) Add file 5 with content- │* b8208db +1 1d Add file 4 with content- │* 046917d +1 1d Add file 3 with content- │* b0684ea +1 1d Add file 2 with content- │* 19be611 +1 1d Add file 1 with content- │* 05a4a45 +2 16h (HEAD -> main) Initial commit- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_list.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_list.snapnew file mode 100644index 000000000..c6d1f751b--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_list.snap@@ -0,0 +1,7 @@+---+source: tests/integration_tests/switch_picker.rs+expression: list+---+> [QUERY]+ Branch Status HEAD± main↕ Remote⇅ Commit+> + feature ↑ ↑5 [HASH]..diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_preview.snapnew file mode 100644index 000000000..582746488--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_log_preview.snap@@ -0,0 +1,13 @@+---+source: tests/integration_tests/switch_picker.rs+expression: preview+---+1: HEAD± | 2: log | 3: main…± | 4: remote⇅ [N/M]+Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr++* [HASH] +1 [TIME] (feature) Add file 5 with content+* [HASH] +1 [TIME] Add file 4 with content+* [HASH] +1 [TIME] Add file 3 with content+* [HASH] +1 [TIME] Add file 2 with content+* [HASH] +1 [TIME] Add file 1 with content+* [HASH] +2 [TIME] (HEAD -> main) Initial commitdiff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff.snapdeleted file mode 100644index 2e45727c3..000000000--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff.snap+++ /dev/null@@ -1,31 +0,0 @@-----source: tests/integration_tests/switch_picker.rs-expression: normalized-----> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr-> + feature ↑ ↑2 d1ceec..│- │ feature_code.rs | 6 ++++++- │ tests.rs | 4 ++++- │ 2 files changed, 10 insertions(+)- │diff --git a/feature_code.rs b/feature_code.rs- │new file mode 100644- │index 0000000..b8bd34c- │--- /dev/null- │+++ b/feature_code.rs- │@@ -0,0 +1,6 @@- │+fn new_feature() {- │+ println!("This is a new feature!");- │+ let x = 42;- │+ let y = x * 2;- │+ println!("Result: {}", y);- │+}- │diff --git a/tests.rs b/tests.rs- │new file mode 100644- │index 0000000..f71e4d6- │--- /dev/null- │+++ b/tests.rs- │@@ -0,0 +1,4 @@- │+#[test]- │+fn test_new_feature() {- │+ assert_eq!(42 * 2, 84);diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_list.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_list.snapnew file mode 100644index 000000000..e02ba5ef3--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_list.snap@@ -0,0 +1,7 @@+---+source: tests/integration_tests/switch_picker.rs+expression: list+---+> [QUERY]+ Branch Status HEAD± main↕ Remote⇅ Commit+> + feature ↑ ↑2 [HASH]..diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_preview.snapnew file mode 100644index 000000000..4c48ca09b--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_main_diff_preview.snap@@ -0,0 +1,31 @@+---+source: tests/integration_tests/switch_picker.rs+expression: preview+---+1: HEAD± | 2: log | 3: main…± | 4: remote⇅ [N/M]+Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr++ feature_code.rs | 6 +++++++ tests.rs | 4 +++++ 2 files changed, 10 insertions(+)+diff --git a/feature_code.rs b/feature_code.rs+new file mode 100644+index [HASH]..[HASH]+--- /dev/null++++ b/feature_code.rs+@@ -0,0 +1,6 @@++fn new_feature() {++ println!("This is a new feature!");++ let x = 42;++ let y = x * 2;++ println!("Result: {}", y);++}+diff --git a/tests.rs b/tests.rs+new file mode 100644+index [HASH]..[HASH]+--- /dev/null++++ b/tests.rs+@@ -0,0 +1,4 @@++#[test]++fn test_new_feature() {++ assert_eq!(42 * 2, 84);diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted.snapdeleted file mode 100644index 40ab0023e..000000000--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted.snap+++ /dev/null@@ -1,31 +0,0 @@-----source: tests/integration_tests/switch_picker.rs-expression: normalized-----> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr-> + feature ! ↑ +3 -1 ↑1 c605f9..│- │ tracked.txt | 4 +++-- │ 1 file changed, 3 insertions(+), 1 deletion(-)- │diff --git a/tracked.txt b/tracked.txt- │index 9415d49..1142e4d 100644- │--- a/tracked.txt- │+++ b/tracked.txt- │@@ -1 +1,3 @@- │-Original content- │+Modified content- │+New line added- │+Another line- │- │- │- │- │- │- │- │- │- │- │- │- │diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_list.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_list.snapnew file mode 100644index 000000000..7f2b25ae6--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_list.snap@@ -0,0 +1,7 @@+---+source: tests/integration_tests/switch_picker.rs+expression: list+---+> [QUERY]+ Branch Status HEAD± main↕ Remote⇅ Commit+> + feature ! ↑ +3 -1 ↑1 [HASH]..diff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_preview.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_preview.snapnew file mode 100644index 000000000..b11da9188--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_preview_uncommitted_preview.snap@@ -0,0 +1,18 @@+---+source: tests/integration_tests/switch_picker.rs+expression: preview+---+1: HEAD± | 2: log | 3: main…± | 4: remote⇅ [N/M]+Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr++ tracked.txt | 4 +++-+ 1 file changed, 3 insertions(+), 1 deletion(-)+diff --git a/tracked.txt b/tracked.txt+index [HASH]..[HASH] 100644+--- a/tracked.txt++++ b/tracked.txt+@@ -1 +1,3 @@+-Original content++Modified content++New line added++Another linediff --git a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches.snap b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches.snapindex 12445313b..cf260537a 100644--- a/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches.snap+++ b/tests/snapshots/integration__integration_tests__switch_picker__switch_picker_with_branches.snap@@ -1,31 +1,31 @@---source: tests/integration_tests/switch_picker.rs-expression: normalized+expression: screen----> [QUERY] │[PREVIEW_HEADER]+> [QUERY]Branch Status HEAD± main↕ Remote⇅ │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr-> @ main ^ │- + active-worktree _ │○ main has no uncommitted changes- orphan-branch /_ │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │- │+> @ main ^ │+ + active-worktree _ │○ main has no uncommitted changes+ orphan-branch /_ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ │+ ││