Files
ai-setup/tests/test_fixes.sh

112 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# tests/test_fixes.sh — unit tests for code-review fixes in claude_setup.sh
# Run: bash tests/test_fixes.sh
# Requires: bash 4+, curl (can be mocked via PATH)
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)); }
# ── helpers ──────────────────────────────────────────────────────────────────
# Source only the heredoc functions, not the setup-script body.
# The heredoc begins after "cat >> \"$BASHRC\" << 'BASHEOF'" and contains
# all the launcher functions; we extract and source that block directly.
_source_functions() {
local tmp
tmp=$(mktemp)
awk '/^# === CLAUDE LAUNCHER ===/,/^# === END CLAUDE LAUNCHER ===/' "$SCRIPT" > "$tmp"
# shellcheck disable=SC1090
source "$tmp"
rm -f "$tmp"
}
# ── Fix 1: ANTHROPIC_API_KEY exported in manual-key path ────────────────────
test_fix1_export_api_key() {
# Extract the [Kk] branch from the script and confirm `export` keyword exists
local kk_block
kk_block=$(awk '/\[Kk\]/,/\[Ll\]/' "$SCRIPT" | grep 'ANTHROPIC_API_KEY')
if echo "$kk_block" | grep -q 'export ANTHROPIC_API_KEY'; then
ok "Fix1: [K] branch uses 'export ANTHROPIC_API_KEY'"
else
fail "Fix1: [K] branch missing 'export' for ANTHROPIC_API_KEY"
fi
}
# ── Fix 2: trap RETURN kills proxy on early exit ─────────────────────────────
test_fix2_trap_return() {
if grep -q "trap '.*kill.*proxy_pid.*' RETURN" "$SCRIPT"; then
ok "Fix2: trap RETURN for proxy cleanup present in claude_gpt"
else
fail "Fix2: trap RETURN for proxy cleanup missing in claude_gpt"
fi
}
# ── Fix 3: readiness loop replaces bare sleep 1 ──────────────────────────────
test_fix3_readiness_loop() {
# The old code had just "sleep 1" after starting proxy; now there's a while loop
local gpt_section
gpt_section=$(awk '/^claude_gpt\(\)/,/^}/' "$SCRIPT")
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
# Confirm bare "sleep 1" is gone from the proxy-start section (the loop contains sleep 1 but in context)
# The old pattern was: proxy_pid=$!\n sleep 1\n 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() {
# Verify the comment and condition are as expected
if grep -q 'exit 7 = connection refused' "$SCRIPT"; then
ok "Fix3b: exit-7 comment present (connection refused check documented)"
else
fail "Fix3b: exit-7 comment missing"
fi
if grep -q '_ce.*-ne 7' "$SCRIPT"; then
ok "Fix3b: [ \$_ce -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() {
local gpt_section
gpt_section=$(awk '/^claude_gpt\(\)/,/^}/' "$SCRIPT")
if echo "$gpt_section" | grep -q 'Проверяю авторизацию после входа'; then
ok "Fix4: re-validate after codex auth login present in claude_gpt"
else
fail "Fix4: re-validate after codex auth login missing in claude_gpt"
fi
}
# ── Fix 5: re-validate after claude_gemini reauth (both 401 and 429) ─────────
test_fix5_gemini_revalidate() {
local gemini_section
gemini_section=$(awk '/^claude_gemini\(\)/,/^}/' "$SCRIPT")
local count
count=$(echo "$gemini_section" | grep -c 'Проверяю авторизацию Gemini' || true)
if [ "$count" -ge 2 ]; then
ok "Fix5: re-validate after gemini reauth present in both 401/403 and 429 branches ($count occurrences)"
else
fail "Fix5: re-validate after gemini reauth missing or only in one branch (found $count)"
fi
}