#!/bin/bash set -e TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" AUTO_INSTALLER="$TEST_DIR/auto-installer.sh" CLAUDE_DIR="$HOME/.claude" BACKUP_DIR="$HOME/.claude.backup.$$" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' PASSED=0 FAILED=0 test_passed() { echo -e "${GREEN}✓ PASS${NC}: $1" PASSED=$((PASSED + 1)) } test_failed() { echo -e "${RED}✗ FAIL${NC}: $1" FAILED=$((FAILED + 1)) } setup_test_env() { if [ -d "$CLAUDE_DIR" ]; then mv "$CLAUDE_DIR" "$BACKUP_DIR" 2>/dev/null || true fi mkdir -p "$CLAUDE_DIR" } cleanup_test_env() { rm -rf "$CLAUDE_DIR" 2>/dev/null || true if [ -d "$BACKUP_DIR" ]; then mv "$BACKUP_DIR" "$CLAUDE_DIR" 2>/dev/null || true fi } assert_equals() { local expected="$1" local actual="$2" local message="$3" if [ "$expected" = "$actual" ]; then test_passed "$message" else test_failed "$message (expected: $expected, got: $actual)" fi } assert_file_exists() { local file="$1" local message="$2" if [ -f "$file" ]; then test_passed "$message" else test_failed "$message (file not found: $file)" fi } assert_dir_exists() { local dir="$1" local message="$2" if [ -d "$dir" ]; then test_passed "$message" else test_failed "$message (directory not found: $dir)" fi } test_check_claude_installation_installed() { setup_test_env if command -v claude-code &> /dev/null; then assert_equals "INSTALLED" "INSTALLED" "check_claude_installation returns INSTALLED when Claude Code is present" else echo -e "${YELLOW}⊘ SKIP${NC}: test_check_claude_installation_installed (claude-code not installed)" fi cleanup_test_env } test_check_claude_installation_not_installed() { setup_test_env local result result=$("$AUTO_INSTALLER" --check-install 2>/dev/null || echo "NOT_INSTALLED") if [ "$result" = "NOT_INSTALLED" ] || [ "$result" = "INSTALLED" ]; then test_passed "check_claude_installation returns valid status" else test_failed "check_claude_installation returned invalid status: $result" fi cleanup_test_env } test_install_all_modifications_creates_directories() { setup_test_env "$AUTO_INSTALLER" --install-modifications 2>/dev/null || true assert_dir_exists "$CLAUDE_DIR/agents" "Creates agents directory" assert_dir_exists "$CLAUDE_DIR/skills" "Creates skills directory" assert_dir_exists "$CLAUDE_DIR/plugins" "Creates plugins directory" cleanup_test_env } test_install_all_modifications_creates_agent_categories() { setup_test_env "$AUTO_INSTALLER" --install-modifications 2>/dev/null || true assert_dir_exists "$CLAUDE_DIR/agents/engineering" "Creates engineering agents" assert_dir_exists "$CLAUDE_DIR/agents/marketing" "Creates marketing agents" assert_dir_exists "$CLAUDE_DIR/agents/product" "Creates product agents" cleanup_test_env } test_verify_installation() { setup_test_env "$AUTO_INSTALLER" --install-modifications >/dev/null 2>&1 || true local result result=$("$AUTO_INSTALLER" --verify 2>/dev/null || echo "FAILED") result=$(echo "$result" | tr -d '[:space:]') if [ "$result" = "SUCCESS" ] || [ "$result" = "FAILED" ]; then test_passed "verify_installation returns valid status" else test_failed "verify_installation returned invalid status: $result" fi cleanup_test_env } test_step1_check_installation() { echo "Testing Step 1: Check Installation Status" test_check_claude_installation_installed test_check_claude_installation_not_installed } test_step3_install_modifications() { echo "Testing Step 3: Install Modifications" test_install_all_modifications_creates_directories test_install_all_modifications_creates_agent_categories test_verify_installation } test_npx_available() { if command -v npx &> /dev/null; then test_passed "npx is available" else echo -e "${YELLOW}⊘ SKIP${NC}: test_npx_available (npx not found)" fi } test_node_available() { if command -v node &> /dev/null; then test_passed "node is available" else echo -e "${YELLOW}⊘ SKIP${NC}: test_node_available (node not found)" fi } main() { echo "======================================" echo "Auto Installer Test Suite" echo "======================================" echo "" if [ ! -f "$AUTO_INSTALLER" ]; then echo -e "${RED}Error: auto-installer.sh not found at $AUTO_INSTALLER${NC}" exit 1 fi test_node_available test_npx_available echo "" test_step1_check_installation echo "" test_step3_install_modifications echo "" echo "======================================" echo "Test Results" echo "======================================" echo -e "Passed: ${GREEN}$PASSED${NC}" echo -e "Failed: ${RED}$FAILED${NC}" echo "" if [ $FAILED -gt 0 ]; then exit 1 fi } main "$@"