#!/usr/bin/env bash # tests/test_fixes.sh — unit tests for code-review fixes in claude_setup.sh # Run: bash tests/test_fixes.sh set -euo pipefail SCRIPT="$(cd "$(dirname "$0")/.." && pwd)/claude_setup.sh" PASS=0; FAIL=0 ok() { echo "[PASS] $1"; PASS=$((PASS+1)); } fail() { echo "[FAIL] $1"; FAIL=$((FAIL+1)); } # Extract sections GPT_SECTION=$(awk '/^cat > "\$BIN_DIR\/claude_gpt"/,/^GPTEOF/' "$SCRIPT") GEMINI_SECTION=$(awk '/^cat > "\$BIN_DIR\/claude_gemini"/,/^GEMINIEOF/' "$SCRIPT") # ── Fix 2: trap EXIT kills proxy ────────────────────────────────────────────── test_fix2_trap_exit() { if echo "$GPT_SECTION" | grep -q "trap .* EXIT"; then ok "Fix2: trap EXIT for proxy cleanup present in claude_gpt" else fail "Fix2: trap EXIT for proxy cleanup missing in claude_gpt" fi } # ── Fix 3: readiness loop replaces bare sleep 1 ────────────────────────────── test_fix3_readiness_loop() { if echo "$GPT_SECTION" | grep -q 'while \[ \$_i -lt'; then ok "Fix3: readiness poll loop present in claude_gpt proxy start" else fail "Fix3: readiness poll loop missing in claude_gpt" fi if echo "$GPT_SECTION" | grep -qP 'proxy_pid=\$!\n\s+sleep 1\n\s+fi'; then fail "Fix3: bare 'sleep 1' still present right after proxy_pid=\$!" else ok "Fix3: bare 'sleep 1; fi' pattern removed" fi } # ── Fix 3b: curl exit-7 logic correct ──────────────────────────────────────── test_fix3b_exit7_logic() { if echo "$GPT_SECTION" | grep -q 'exit 7 = connection refused'; then ok "Fix3b: exit-7 comment present (connection refused check documented)" else fail "Fix3b: exit-7 comment missing" fi if echo "$GPT_SECTION" | grep -q '\[ "\$?" -ne 7 \]'; then ok "Fix3b: [ \$? -ne 7 ] break condition present" else fail "Fix3b: exit-7 break condition missing" fi } # ── Fix 4: re-validate after claude_gpt reauth ─────────────────────────────── test_fix4_gpt_revalidate() { if echo "$GPT_SECTION" | grep -q '_claude_test_api.*http://localhost:18765'; then ok "Fix4: _claude_test_api called in claude_gpt" else fail "Fix4: _claude_test_api missing in claude_gpt" fi } # ── Fix 5: re-validate after claude_gemini reauth (both 401 and 429) ───────── test_fix5_gemini_revalidate() { local count count=$(echo "$GEMINI_SECTION" | grep -c '_claude_test_api' || true) if [ "$count" -ge 2 ]; then ok "Fix5: _claude_test_api present in gemini reauth flow ($count occurrences)" else fail "Fix5: _claude_test_api missing or only in one branch (found $count)" fi } # ── Fix 7: trap quotes $TMP correctly ──────────────────────────────────────── test_fix7_trap_tmp() { if grep -q "trap 'rm -rf \"\$TMP\"' EXIT" "$SCRIPT"; then ok "Fix7: trap uses single quotes with quoted \"\$TMP\"" else fail "Fix7: trap still uses double quotes or \$TMP still unquoted at execution" fi } # ── bash syntax of the whole script ───────────────────────────────────────── test_script_syntax() { if bash -n "$SCRIPT" 2>&1; then ok "syntax: claude_setup.sh passes 'bash -n'" else fail "syntax: claude_setup.sh has syntax errors" fi } # ── run all tests ───────────────────────────────────────────────────────────── test_script_syntax test_fix2_trap_exit test_fix3_readiness_loop test_fix3b_exit7_logic test_fix4_gpt_revalidate test_fix5_gemini_revalidate test_fix7_trap_tmp echo "" echo "Results: $PASS passed, $FAIL failed" [ "$FAIL" -eq 0 ] && exit 0 || exit 1