From e27bbaaa434e01c47f9bf330280a633dba2a3fa8 Mon Sep 17 00:00:00 2001 From: Flo Date: Tue, 9 Jun 2026 23:54:55 +0200 Subject: [PATCH] test(interactive): replace blind y-loops with accept_until helper Add an accept_until helper that only sends "y" when a new [Y/n] prompt appears in the tmux pane, replacing the fixed-count blind send loops that could race ahead of prompts or send stray input. Resilient to changes in the number of apply-phase prompts. Relates to #51 Co-Authored-By: Claude Fable 5 --- test/interactive/helpers.sh | 27 ++++++++++++++++++++ test/interactive/test-full-accept.sh | 17 ++----------- test/interactive/test-identity-guard.sh | 30 +++-------------------- test/interactive/test-signing-generate.sh | 15 ++---------- test/interactive/test-signing-skip.sh | 15 ++---------- 5 files changed, 37 insertions(+), 67 deletions(-) diff --git a/test/interactive/helpers.sh b/test/interactive/helpers.sh index b0e791b..a3a493f 100755 --- a/test/interactive/helpers.sh +++ b/test/interactive/helpers.sh @@ -100,6 +100,33 @@ assert_contains() { return 1 } +# Accept all [Y/n] prompts until a stop pattern appears in the pane. +# Usage: accept_until "Signing key options" [max_iterations] +accept_until() { + local stop_pattern="$1" + local max_iters="${2:-80}" + local pane_content last_content="" + local i + for ((i = 0; i < max_iters; i++)); do + pane_content="$(tmux capture-pane -t "$TMUX_SESSION" -p 2>/dev/null || true)" + if printf '%s' "$pane_content" | grep -qF "$stop_pattern"; then + return 0 + fi + if printf '%s' "$pane_content" | grep -qF "Hardening complete"; then + return 0 + fi + # Only send "y" when a new prompt has appeared (pane changed and shows [Y/n]) + if [ "$pane_content" != "$last_content" ] \ + && printf '%s' "$pane_content" | grep -qE '\[Y/n\]|\[y/N\]'; then + send "y" Enter + last_content="$pane_content" + sleep 0.3 + else + sleep 0.2 + fi + done +} + pass() { printf '%b PASS:%b %s\n' "$C_GREEN" "$C_RESET" "$1" >&2 } diff --git a/test/interactive/test-full-accept.sh b/test/interactive/test-full-accept.sh index 3e3b3eb..c345496 100755 --- a/test/interactive/test-full-accept.sh +++ b/test/interactive/test-full-accept.sh @@ -26,21 +26,8 @@ main() { wait_for "Proceed with hardening" send "y" Enter - # Accept each setting prompt by sending "y" + Enter repeatedly. - # v0.2.0 adds more prompts (pre-commit hook, gitignore, core.symlinks), - # so we need enough iterations to get through all of them. - local pane_content - for _ in $(seq 1 50); do - sleep 0.3 - pane_content="$(tmux capture-pane -t "$TMUX_SESSION" -p 2>/dev/null || true)" - if printf '%s' "$pane_content" | grep -qF "Signing key options"; then - break - fi - if printf '%s' "$pane_content" | grep -qF "Hardening complete"; then - break - fi - send "y" Enter - done + # Accept all [Y/n] prompts until signing wizard + accept_until "Signing key options" # Signing wizard — skip wait_for "Signing key options" 20 diff --git a/test/interactive/test-identity-guard.sh b/test/interactive/test-identity-guard.sh index d54dcbe..5b062f4 100755 --- a/test/interactive/test-identity-guard.sh +++ b/test/interactive/test-identity-guard.sh @@ -36,20 +36,8 @@ main() { wait_for "Proceed with hardening" send "y" Enter - # Accept settings until identity guard prompt appears - local pane_content - for _ in $(seq 1 50); do - sleep 0.3 - pane_content="$(tmux capture-pane -t "$TMUX_SESSION" -p 2>/dev/null || true)" - if printf '%s' "$pane_content" | grep -qF "Enter your name"; then - break - fi - if printf '%s' "$pane_content" | grep -qF "Hardening complete"; then - fail "Identity guard did not trigger — reached completion" - exit 1 - fi - send "y" Enter - done + # Accept all [Y/n] prompts until identity guard + accept_until "Enter your name" # Identity guard: enter name wait_for "Enter your name" 15 @@ -59,18 +47,8 @@ main() { wait_for "Enter your email" 10 send "test@example.com" Enter - # Continue accepting remaining prompts - for _ in $(seq 1 50); do - sleep 0.3 - pane_content="$(tmux capture-pane -t "$TMUX_SESSION" -p 2>/dev/null || true)" - if printf '%s' "$pane_content" | grep -qF "Signing key options"; then - break - fi - if printf '%s' "$pane_content" | grep -qF "Hardening complete"; then - break - fi - send "y" Enter - done + # Accept remaining [Y/n] prompts until signing wizard + accept_until "Signing key options" # Skip signing if tmux capture-pane -t "$TMUX_SESSION" -p | grep -qF "Signing key options"; then diff --git a/test/interactive/test-signing-generate.sh b/test/interactive/test-signing-generate.sh index aa4c710..9c4b37a 100755 --- a/test/interactive/test-signing-generate.sh +++ b/test/interactive/test-signing-generate.sh @@ -34,19 +34,8 @@ main() { wait_for "Proceed with hardening" send "y" Enter - # Accept settings until signing wizard (v0.2.0 adds more prompts) - local pane_content - for _ in $(seq 1 50); do - sleep 0.3 - pane_content="$(tmux capture-pane -t "$TMUX_SESSION" -p 2>/dev/null || true)" - if printf '%s' "$pane_content" | grep -qF "Signing key options"; then - break - fi - if printf '%s' "$pane_content" | grep -qF "Hardening complete"; then - break - fi - send "y" Enter - done + # Accept all [Y/n] prompts until signing wizard + accept_until "Signing key options" # Signing wizard — option 1: generate ed25519 wait_for "Signing key options" 20 diff --git a/test/interactive/test-signing-skip.sh b/test/interactive/test-signing-skip.sh index a1e855e..19da548 100755 --- a/test/interactive/test-signing-skip.sh +++ b/test/interactive/test-signing-skip.sh @@ -35,19 +35,8 @@ main() { wait_for "Proceed with hardening" send "y" Enter - # Accept settings until signing wizard (v0.2.0 adds more prompts) - local pane_content - for _ in $(seq 1 50); do - sleep 0.3 - pane_content="$(tmux capture-pane -t "$TMUX_SESSION" -p 2>/dev/null || true)" - if printf '%s' "$pane_content" | grep -qF "Signing key options"; then - break - fi - if printf '%s' "$pane_content" | grep -qF "Hardening complete"; then - break - fi - send "y" Enter - done + # Accept all [Y/n] prompts until signing wizard + accept_until "Signing key options" # Signing wizard — skip wait_for "Signing key options" 20