|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck shell=bash |
| 3 | +# |
| 4 | +# Quitting during a turn, in Classic and in Next (#408). A local fake of the |
| 5 | +# Messages API drives real turns through the real client and query loop. |
| 6 | +# Quitting mid-turn with Ctrl+D twice must exit, give the terminal back, stop |
| 7 | +# the turn and save its prompt and the output it delivered, and must not wait |
| 8 | +# long while an approved command runs. Only Next reports on the turn after it |
| 9 | +# exits. (A unit test covers a turn that ignores the stop: the exit waits 2 s |
| 10 | +# at most.) Classic runs with COVEN_CODE_UI unset; |
| 11 | +# Next, which case 18 also covers, with COVEN_CODE_UI=next. Time limits are |
| 12 | +# generous because this also runs on debug builds and loaded hosts. |
| 13 | + |
| 14 | +register_case tc_classic_quit_save |
| 15 | + |
| 16 | +# The saved session file that names `text`, if any. |
| 17 | +_cqs_saved_with() { # home, text |
| 18 | + find "$1" -type f -path '*/sessions/*.json' -exec grep -lF -- "$2" {} + 2>/dev/null | head -n 1 |
| 19 | +} |
| 20 | + |
| 21 | +# Start the TUI for one renderer against the fake API, with its own home. |
| 22 | +_cqs_launch() { # renderer, home, port, project |
| 23 | + local var ui="" |
| 24 | + TUI_LAUNCH_PREFIX="cd $(printf '%q' "$4") && env" |
| 25 | + for var in OPENAI_API_KEY COVEN_CODE_PROVIDER COVEN_CODE_API_BASE COVEN_CODE_HOME \ |
| 26 | + COVEN_CODE_TEST_HOME COVEN_CODE_CLAUDE_BIN COVEN_PARENT COVEN_DAEMON_SOCKET COVEN_CODE_UI; do |
| 27 | + TUI_LAUNCH_PREFIX+=" -u $var" |
| 28 | + done |
| 29 | + if [ "$1" = next ]; then ui="COVEN_CODE_UI=next"; TUI_BOOT_STRING="preview" |
| 30 | + else TUI_BOOT_STRING="Coven v"; fi |
| 31 | + # shellcheck disable=SC2086 # an empty selector adds nothing |
| 32 | + TUI_LAUNCH_PREFIX+=" $(printf '%q ' "HOME=$2" "COVEN_HOME=$2/.coven" $ui \ |
| 33 | + ANTHROPIC_API_KEY=fake-key "ANTHROPIC_BASE_URL=http://127.0.0.1:$3")" |
| 34 | + tui_start |
| 35 | +} |
| 36 | + |
| 37 | +tc_classic_quit_save() { |
| 38 | + describe "Quit during a turn in each renderer" |
| 39 | + if ! have_tmux; then _skip "tmux not installed"; return 0; fi |
| 40 | + if ! command -v python3 >/dev/null 2>&1; then _skip "python3 not installed"; return 0; fi |
| 41 | + |
| 42 | + local scratch; scratch="$(mktemp -d)" |
| 43 | + mkdir -p "$scratch/project" "$scratch/markers" |
| 44 | + python3 -B "${BASH_SOURCE[0]%/*}/../fake_anthropic.py" \ |
| 45 | + "$scratch/port" "$scratch/requests.log" "$scratch/markers" 2>/dev/null & |
| 46 | + local fake_pid=$! waited=0 r home port |
| 47 | + while [ "$waited" -lt 50 ] && [ ! -s "$scratch/port" ]; do |
| 48 | + sleep 0.1; waited=$((waited + 1)) |
| 49 | + done |
| 50 | + local saved_prefix="$TUI_LAUNCH_PREFIX" saved_boot="$TUI_BOOT_STRING" |
| 51 | + |
| 52 | + if [ -s "$scratch/port" ]; then |
| 53 | + port="$(cat "$scratch/port")" |
| 54 | + for r in classic next; do |
| 55 | + home="$scratch/home-$r" |
| 56 | + mkdir -p "$home/.coven-code" |
| 57 | + printf '%s\n' '{"hasCompletedOnboarding": true}' >"$home/.coven-code/settings.json" |
| 58 | + _cqs_held "$r" "$home" "$port" "$scratch/project" |
| 59 | + tui_stop |
| 60 | + _cqs_command "$r" "$home" "$port" "$scratch/project" |
| 61 | + tui_stop |
| 62 | + done |
| 63 | + else |
| 64 | + _fail "the fake API starts" |
| 65 | + fi |
| 66 | + kill "$fake_pid" 2>/dev/null |
| 67 | + wait "$fake_pid" 2>/dev/null |
| 68 | + TUI_LAUNCH_PREFIX="$saved_prefix" TUI_BOOT_STRING="$saved_boot" |
| 69 | + rm -rf "$scratch" |
| 70 | +} |
| 71 | + |
| 72 | +# Quitting while text is still arriving keeps the prompt and the answer so far. |
| 73 | +# The fake holds the stream open after its burst, so the turn is still running |
| 74 | +# when the keys land. |
| 75 | +_cqs_held() { # renderer, home, port, project |
| 76 | + local r="$1" home="$2" saved pane |
| 77 | + _cqs_launch "$@" || return 0 |
| 78 | + tui_type "flood: 2000 hold"; sleep 0.3; tui_keys Enter |
| 79 | + if ! wait_for "held line" 30; then |
| 80 | + _fail "$r: the held turn starts" "$(tui_capture)"; return 0 |
| 81 | + fi |
| 82 | + tui_quit_and_check "$r: Ctrl+D twice during a turn" C-d C-d |
| 83 | + # The pane now shows the shell, with whatever the TUI printed as it exited: |
| 84 | + # Next's report on the turn and the resume command; nothing from Classic. |
| 85 | + pane="$(tui_capture)" |
| 86 | + if [ "$r" = next ]; then |
| 87 | + assert_contains "$pane" "Quit during a turn" "$r: the exit report follows the exit" |
| 88 | + assert_contains "$pane" "Pick it up with" "$r: the resume command follows the exit" |
| 89 | + else |
| 90 | + assert_absent "$pane" "Quit during a turn" "$r: no exit report follows the exit" |
| 91 | + assert_absent "$pane" "Pick it up with" "$r: no resume command follows the exit" |
| 92 | + fi |
| 93 | + saved="$(_cqs_saved_with "$home" "flood: 2000 hold")" |
| 94 | + if [ -n "$saved" ]; then _pass "$r: quitting during a turn keeps the prompt" |
| 95 | + else _fail "$r: quitting during a turn keeps the prompt" "no saved session names it"; fi |
| 96 | + if [ -n "$saved" ] && grep -q 'flood line' "$saved"; then |
| 97 | + _pass "$r: quitting during a turn keeps the answer so far" |
| 98 | + else |
| 99 | + _fail "$r: quitting during a turn keeps the answer so far" "${saved:-no saved session}" |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +# Quitting while an approved command runs exits promptly; 8 s leaves room for a |
| 104 | +# loaded host. The command stops with the turn here, so this does not reach the |
| 105 | +# 2 s bound. The prompt must be saved. |
| 106 | +_cqs_command() { # renderer, home, port, project |
| 107 | + local r="$1" home="$2" saved |
| 108 | + _cqs_launch "$@" || return 0 |
| 109 | + tui_type "run: sleep 20"; sleep 0.3; tui_keys Enter |
| 110 | + if ! wait_for "allow once" 15; then |
| 111 | + _fail "$r: a Bash call asks for approval" "$(tui_capture)"; return 0 |
| 112 | + fi |
| 113 | + sleep 0.7; tui_keys y; sleep 1.5 |
| 114 | + tui_quit_and_check "$r: Ctrl+D twice while a command runs" C-d C-d |
| 115 | + if [ "$TUI_QUIT_TENTHS" -lt 80 ]; then |
| 116 | + _pass "$r: quitting exits within 8 s while a command runs" |
| 117 | + else |
| 118 | + _fail "$r: quitting exits within 8 s while a command runs" "took about $((TUI_QUIT_TENTHS / 10))s" |
| 119 | + fi |
| 120 | + saved="$(_cqs_saved_with "$home" "run: sleep 20")" |
| 121 | + if [ -n "$saved" ]; then _pass "$r: quitting while a command runs keeps the prompt" |
| 122 | + else _fail "$r: quitting while a command runs keeps the prompt" "no saved session names it"; fi |
| 123 | +} |
0 commit comments