#!/bin/bash set -euo pipefail # Test UniFi integration components set +e # Don't exit on errors, we handle them in test_step SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" cd "$PROJECT_ROOT" echo "UniFi Integration Test Suite" echo "============================" echo "" # Load environment variables if [ -f ~/.env ]; then source <(grep "^UNIFI_" ~/.env | sed 's/^/export /') fi # Test counters TESTS_PASSED=0 TESTS_FAILED=0 test_step() { local test_name="$1" local test_command="$2" echo -n "Testing: $test_name... " if eval "$test_command" >/dev/null 2>&1; then echo "✅ PASS" ((TESTS_PASSED++)) return 0 else echo "❌ FAIL" ((TESTS_FAILED++)) return 1 fi } echo "1. Package Build Verification" echo "----------------------------" test_step "unifi-api package built" "test -f unifi-api/dist/index.js" test_step "mcp-unifi package built" "test -f mcp-unifi/dist/index.js" test_step "CLI script exists" "test -f unifi-api/dist/cli/index.js" echo "" echo "2. Configuration Verification" echo "----------------------------" test_step "Environment file exists" "test -f ~/.env" test_step "UNIFI_UDM_URL configured" "[ -n \"\$UNIFI_UDM_URL\" ]" test_step "UNIFI_API_MODE configured" "[ -n \"\$UNIFI_API_MODE\" ]" if [ "$UNIFI_API_MODE" = "official" ]; then test_step "UNIFI_API_KEY configured" "[ -n \"\$UNIFI_API_KEY\" ]" else test_step "UNIFI_USERNAME configured" "[ -n \"\$UNIFI_USERNAME\" ]" test_step "UNIFI_PASSWORD configured" "[ -n \"\$UNIFI_PASSWORD\" ]" fi echo "" echo "3. Script Verification" echo "---------------------" test_step "list-sites.sh exists and executable" "test -x scripts/unifi/list-sites.sh" test_step "check-health.sh exists and executable" "test -x scripts/unifi/check-health.sh" test_step "list-devices.sh exists and executable" "test -x scripts/unifi/list-devices.sh" test_step "monitor-health.sh exists and executable" "test -x scripts/unifi/monitor-health.sh" echo "" echo "4. Documentation Verification" echo "---------------------------" test_step "UNIFI_API_SETUP.md exists" "test -f docs/04-configuration/UNIFI_API_SETUP.md" test_step "UNIFI_ENDPOINTS_REFERENCE.md exists" "test -f docs/04-configuration/UNIFI_ENDPOINTS_REFERENCE.md" test_step "Scripts README exists" "test -f scripts/unifi/README.md" echo "" echo "5. API Connection Test (if configured)" echo "-------------------------------------" if [ -n "$UNIFI_UDM_URL" ] && [ -n "$UNIFI_API_MODE" ]; then echo -n "Testing API connection... " if NODE_TLS_REJECT_UNAUTHORIZED=0 pnpm --filter unifi-api exec node dist/cli/index.js sites 2>&1 | grep -q "internalReference\|name" >/dev/null 2>&1; then echo "✅ PASS" ((TESTS_PASSED++)) else echo "⚠️ SKIP (connection test may fail if controller is unavailable)" echo " Run './scripts/unifi/check-health.sh' for detailed connection test" fi else echo "⚠️ SKIP (configuration not complete)" fi echo "" echo "Test Summary" echo "============" echo "✅ Passed: $TESTS_PASSED" echo "❌ Failed: $TESTS_FAILED" echo "" if [ $TESTS_FAILED -eq 0 ]; then echo "🎉 All tests passed!" exit 0 else echo "⚠️ Some tests failed. Please review the output above." exit 1 fi