spawn/test_oauth_fallback.sh
Sprite 640af6260c Refactor spawn scripts with shared library and OAuth fallback
Major refactoring to improve code maintainability and reliability:

## Shared Library Architecture
- Created sprite/lib/common.sh with reusable bash functions
- Reduced openclaw.sh from 258 to 93 lines (-64%)
- Reduced claude.sh from 272 to 101 lines (-63%)
- Eliminated ~330 lines of duplicate code

## OAuth Fallback Mechanism
- Added automatic fallback to manual API key entry
- Handles missing netcat (nc) gracefully
- Handles port conflicts and timeouts
- Validates API key format with override option
- Works in headless and minimal environments

## Dual Execution Support
- Local: bash sprite/openclaw.sh
- Remote: curl URL | bash
- Auto-detects context and sources library appropriately

## New Shared Functions
- Logging: log_info(), log_warn(), log_error()
- Sprite setup: ensure_sprite_installed/authenticated/exists()
- Environment: setup_shell_environment()
- OAuth: get_openrouter_api_key_oauth() with fallback
- Utilities: run_sprite(), verify_sprite_connectivity()

## Documentation
- REFACTORING.md - Architecture and benefits
- OAUTH_FALLBACK.md - Fallback mechanism guide
- CURL_BASH_SOLUTION.md - Execution mode details
- EXAMPLES.md - Usage scenarios
- CHANGELOG.md - Complete change history

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-07 04:06:11 +00:00

40 lines
1.1 KiB
Bash

#!/bin/bash
# Test script to demonstrate OAuth fallback mechanism
# Source the library
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/sprite/lib/common.sh"
echo "Testing OAuth Fallback Mechanism"
echo "=================================="
echo ""
echo "This script will test the fallback behavior:"
echo "1. Try OAuth flow (will likely fail/timeout quickly for demo)"
echo "2. Fallback to manual API key entry"
echo ""
# Test with a very short timeout by simulating failure
log_info "Scenario 1: nc command not available"
echo "Simulating missing netcat..."
# Temporarily hide nc command
original_path="$PATH"
export PATH="/tmp/empty:$PATH"
API_KEY=$(get_openrouter_api_key_oauth 5180 2>&1)
status=$?
# Restore PATH
export PATH="$original_path"
if [[ $status -eq 0 ]]; then
echo ""
log_info "Test completed successfully!"
log_info "Got API key: ${API_KEY:0:10}..."
else
log_error "Test failed - authentication cancelled"
fi
echo ""
echo "=================================="
echo "To test OAuth success scenario, run one of the actual setup scripts."