Refactor architecture to use standalone scripts in ~/.local/bin/ instead of ~/.bashrc

This commit is contained in:
2026-05-31 12:55:54 +07:00
parent 4f25596867
commit 61c21fe296
4 changed files with 435 additions and 857 deletions

View File

@@ -1,7 +1,6 @@
#!/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
@@ -11,56 +10,28 @@ PASS=0; FAIL=0
ok() { echo "[PASS] $1"; PASS=$((PASS+1)); }
fail() { echo "[FAIL] $1"; FAIL=$((FAIL+1)); }
# ── helpers ──────────────────────────────────────────────────────────────────
# Extract sections
GPT_SECTION=$(awk '/^cat > "\$BIN_DIR\/claude_gpt"/,/^GPTEOF/' "$SCRIPT")
GEMINI_SECTION=$(awk '/^cat > "\$BIN_DIR\/claude_gemini"/,/^GEMINIEOF/' "$SCRIPT")
# 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'"
# ── 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 "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"
fail "Fix2: trap EXIT 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
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
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"
@@ -69,15 +40,14 @@ test_fix3_readiness_loop() {
# ── 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
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 grep -q '_ce.*-ne 7' "$SCRIPT"; then
ok "Fix3b: [ \$_ce -ne 7 ] break condition present"
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
@@ -85,27 +55,51 @@ test_fix3b_exit7_logic() {
# ── 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"
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: re-validate after codex auth login missing in claude_gpt"
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 gemini_section
gemini_section=$(awk '/^claude_gemini\(\)/,/^}/' "$SCRIPT")
local count
count=$(echo "$gemini_section" | grep -c 'Проверяю авторизацию Gemini' || true)
count=$(echo "$GEMINI_SECTION" | grep -c '_claude_test_api' || true)
if [ "$count" -ge 2 ]; then
ok "Fix5: re-validate after gemini reauth present in both 401/403 and 429 branches ($count occurrences)"
ok "Fix5: _claude_test_api present in gemini reauth flow ($count occurrences)"
else
fail "Fix5: re-validate after gemini reauth missing or only in one branch (found $count)"
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