|
| 1 | +#!/usr/bin/env bats |
| 2 | +# setup.bats - `basecamp setup` refuses to prompt when nothing can answer it. |
| 3 | +# |
| 4 | +# The wizard is prompts end to end, and huh runs them as a bubbletea program. |
| 5 | +# Redirecting stdin does not make that program fail: bubbletea sees a |
| 6 | +# non-terminal stdin and opens /dev/tty instead, so the prompt waits on the real |
| 7 | +# terminal — `basecamp setup --json < /dev/null` hung forever. |
| 8 | +# |
| 9 | +# Every case runs under a timeout, and the timeout is the assertion: exit 124 is |
| 10 | +# the bug reproducing. A unit test with a fake reader cannot catch this, because |
| 11 | +# the hang lives in what the real os.Stdin makes bubbletea do — which is also |
| 12 | +# why the PTY case at the bottom exists. Under bats alone, stdout is captured, |
| 13 | +# so stdout is what fails the interactivity check and stdin is never the |
| 14 | +# deciding factor. |
| 15 | + |
| 16 | +load test_helper |
| 17 | + |
| 18 | +# timeout_bin names GNU timeout, which stock macOS does not ship. Tests that |
| 19 | +# need it skip rather than silently drop their only real assertion. |
| 20 | +timeout_bin() { |
| 21 | + if command -v timeout >/dev/null 2>&1; then |
| 22 | + echo timeout |
| 23 | + elif command -v gtimeout >/dev/null 2>&1; then |
| 24 | + echo gtimeout |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +# run_guarded runs a shell snippet under a timeout. |
| 29 | +# |
| 30 | +# The timeout wraps the whole shell, not the snippet's first word. Prefixing it |
| 31 | +# (`timeout 10 printf '' | basecamp setup`) times `printf` and leaves `basecamp` |
| 32 | +# to hang the suite forever — the exact failure these tests exist to catch. |
| 33 | +# pipefail so a refusal upstream of a pipe is not masked by the last stage. |
| 34 | +run_guarded() { |
| 35 | + local to |
| 36 | + to="$(timeout_bin)" |
| 37 | + if [[ -z "$to" ]]; then |
| 38 | + skip "no GNU timeout available (install coreutils)" |
| 39 | + fi |
| 40 | + run "$to" 10 bash -c "set -o pipefail; $1" |
| 41 | +} |
| 42 | + |
| 43 | +assert_not_timed_out() { |
| 44 | + if [[ "$status" -eq 124 ]]; then |
| 45 | + echo "Command hit the timeout (exit 124) — it hung instead of refusing" |
| 46 | + echo "Output: $output" |
| 47 | + return 1 |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +# assert_refused is the whole contract: exit 1, a usage envelope, and a hint |
| 52 | +# naming something that works without a terminal. "Non-zero and not 124" is too |
| 53 | +# weak — the pre-fix binary also exits non-zero where no controlling terminal |
| 54 | +# exists, on huh's own TTY error, several steps in. |
| 55 | +assert_refused() { |
| 56 | + assert_not_timed_out |
| 57 | + assert_exit_code 1 |
| 58 | + assert_json_value '.code' 'usage' |
| 59 | + assert_json_value '.hint | contains("basecamp setup agents")' 'true' |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +@test "setup --json with stdin closed refuses instead of hanging" { |
| 64 | + create_credentials |
| 65 | + create_global_config '{"account_id": 99999}' |
| 66 | + |
| 67 | + run_guarded "basecamp setup --json < /dev/null" |
| 68 | + assert_refused |
| 69 | +} |
| 70 | + |
| 71 | +@test "setup without --json and stdin closed refuses instead of hanging" { |
| 72 | + create_credentials |
| 73 | + create_global_config '{"account_id": 99999}' |
| 74 | + |
| 75 | + run_guarded "basecamp setup < /dev/null" |
| 76 | + assert_refused |
| 77 | +} |
| 78 | + |
| 79 | +@test "setup with piped stdin refuses instead of hanging" { |
| 80 | + create_credentials |
| 81 | + create_global_config '{"account_id": 99999}' |
| 82 | + |
| 83 | + run_guarded "printf '' | basecamp setup --json" |
| 84 | + assert_refused |
| 85 | +} |
| 86 | + |
| 87 | +@test "setup with piped stdout refuses instead of hanging" { |
| 88 | + create_credentials |
| 89 | + create_global_config '{"account_id": 99999}' |
| 90 | + |
| 91 | + run_guarded "basecamp setup --json < /dev/null | cat" |
| 92 | + assert_refused |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +# The reported bug: a real terminal on stdout, nothing on stdin. bats cannot |
| 97 | +# produce that on its own — it captures stdout — so borrow a pty from script(1). |
| 98 | +# The exit code travels in a sentinel rather than through script, whose status |
| 99 | +# propagation differs between the util-linux and BSD versions. |
| 100 | + |
| 101 | +# run_in_pty runs a shell snippet with stdout attached to a pseudo-terminal. |
| 102 | +run_in_pty() { |
| 103 | + local snippet="$1" |
| 104 | + if ! command -v script >/dev/null 2>&1; then |
| 105 | + skip "script(1) not available to allocate a pty" |
| 106 | + fi |
| 107 | + if script --version 2>&1 | grep -qi util-linux; then |
| 108 | + run bash -c "script -qec '$snippet' /dev/null | tr -d '\r'" |
| 109 | + else |
| 110 | + run bash -c "script -q /dev/null /bin/sh -c '$snippet' | tr -d '\r'" |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +@test "setup --json on a terminal with stdin closed refuses instead of hanging" { |
| 115 | + create_credentials |
| 116 | + create_global_config '{"account_id": 99999}' |
| 117 | + |
| 118 | + local to |
| 119 | + to="$(timeout_bin)" |
| 120 | + if [[ -z "$to" ]]; then |
| 121 | + skip "no GNU timeout available (install coreutils)" |
| 122 | + fi |
| 123 | + |
| 124 | + # stdout is a pty here, so /dev/null on stdin is the only disqualifier — the |
| 125 | + # case a character-device check called interactive and bubbletea did not. |
| 126 | + run_in_pty "$to 10 basecamp setup --json < /dev/null; echo EXIT:\$?" |
| 127 | + |
| 128 | + assert_output_not_contains "EXIT:124" |
| 129 | + assert_output_contains "EXIT:1" |
| 130 | + assert_output_contains "basecamp setup agents" |
| 131 | +} |
| 132 | + |
| 133 | +@test "setup on a terminal with stdin closed refuses instead of hanging" { |
| 134 | + create_credentials |
| 135 | + create_global_config '{"account_id": 99999}' |
| 136 | + |
| 137 | + local to |
| 138 | + to="$(timeout_bin)" |
| 139 | + if [[ -z "$to" ]]; then |
| 140 | + skip "no GNU timeout available (install coreutils)" |
| 141 | + fi |
| 142 | + |
| 143 | + run_in_pty "$to 10 basecamp setup < /dev/null; echo EXIT:\$?" |
| 144 | + |
| 145 | + assert_output_not_contains "EXIT:124" |
| 146 | + assert_output_contains "EXIT:1" |
| 147 | + assert_output_contains "basecamp setup agents" |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | +# The gate is on the parent's RunE only. These three are the supported |
| 152 | +# non-interactive paths and have to keep working — a persistent hook would have |
| 153 | +# taken all of them out, which is the easiest thing to get wrong here. |
| 154 | + |
| 155 | +# hide_agent_binaries drops the developer's real claude/codex from PATH. Without |
| 156 | +# it these tests shell out to whichever agent CLI happens to be installed, and |
| 157 | +# those have prompts of their own — a hang in somebody else's tool, unrelated to |
| 158 | +# the gate under test. What we are asserting is that the parent's gate does not |
| 159 | +# reach the subcommands, and that holds with or without an agent installed. |
| 160 | +hide_agent_binaries() { |
| 161 | + export PATH="$BASECAMP_ROOT/bin:/usr/bin:/bin" |
| 162 | +} |
| 163 | + |
| 164 | +@test "setup agents still runs without a terminal" { |
| 165 | + create_credentials |
| 166 | + create_global_config '{"account_id": 99999}' |
| 167 | + hide_agent_binaries |
| 168 | + export BASECAMP_SETUP_AGENT=none |
| 169 | + |
| 170 | + run_guarded "basecamp setup agents --json < /dev/null" |
| 171 | + assert_not_timed_out |
| 172 | + assert_success |
| 173 | +} |
| 174 | + |
| 175 | +@test "setup claude still runs without a terminal" { |
| 176 | + create_credentials |
| 177 | + create_global_config '{"account_id": 99999}' |
| 178 | + hide_agent_binaries |
| 179 | + |
| 180 | + run_guarded "basecamp setup claude --json < /dev/null" |
| 181 | + assert_not_timed_out |
| 182 | + assert_success |
| 183 | +} |
| 184 | + |
| 185 | +@test "setup codex still runs without a terminal" { |
| 186 | + create_credentials |
| 187 | + create_global_config '{"account_id": 99999}' |
| 188 | + hide_agent_binaries |
| 189 | + |
| 190 | + run_guarded "basecamp setup codex --json < /dev/null" |
| 191 | + assert_not_timed_out |
| 192 | + assert_success |
| 193 | +} |
0 commit comments