#!/bin/bash
# test.sh — Verify Claude Style installation
#
# Usage: ./scripts/test.sh
#
# Checks that all components are properly installed.

set -e

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'

SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
EXTENSION_DIR="$HOME/.qwen/extensions/claude-style"
PASS=0
FAIL=0

echo -e "${BLUE}╔══════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║     Claude Style — Test Suite            ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════╝${NC}"
echo ""

# Test functions — direct bash tests, no eval
check_file() {
    local name="$1"
    local path="$2"
    if [ -f "$path" ]; then
        echo -e "${GREEN}✓ $name${NC}"
        PASS=$((PASS + 1))
    else
        echo -e "${RED}✗ $name${NC}"
        FAIL=$((FAIL + 1))
    fi
}

check_dir() {
    local name="$1"
    local path="$2"
    if [ -d "$path" ]; then
        echo -e "${GREEN}✓ $name${NC}"
        PASS=$((PASS + 1))
    else
        echo -e "${RED}✗ $name${NC}"
        FAIL=$((FAIL + 1))
    fi
}

check_executable() {
    local name="$1"
    local path="$2"
    if [ -f "$path" ] && [ -x "$path" ]; then
        echo -e "${GREEN}✓ $name${NC}"
        PASS=$((PASS + 1))
    else
        echo -e "${RED}✗ $name${NC}"
        FAIL=$((FAIL + 1))
    fi
}

# Core checks
echo -e "${YELLOW}Extension files:${NC}"
check_dir "Extension directory exists" "$EXTENSION_DIR"
check_file "qwen-extension.json exists" "$EXTENSION_DIR/qwen-extension.json"
check_file "QWEN.md exists" "$EXTENSION_DIR/QWEN.md"

echo ""
echo -e "${YELLOW}Commands:${NC}"
for cmd in load-core-prompts claude-review claude-refactor claude-debug claude-plan claude-explore claude-security claude-commit; do
    check_file "/$cmd command" "$EXTENSION_DIR/commands/$cmd.md"
done

echo ""
echo -e "${YELLOW}Skills:${NC}"
for skill in claude-code-review claude-refactor claude-debugging claude-planning claude-explore; do
    check_file "$skill skill" "$EXTENSION_DIR/skills/$skill/SKILL.md"
done

echo ""
echo -e "${YELLOW}Agents:${NC}"
for agent in claude-reviewer claude-planner claude-verifier; do
    check_file "$agent agent" "$EXTENSION_DIR/agents/$agent.yaml"
done

echo ""
echo -e "${YELLOW}Prompts:${NC}"
PROMPT_COUNT=$(find "$PROJECT_DIR/prompts" -name "*.md" 2>/dev/null | wc -l)
if [ "$PROMPT_COUNT" -gt 0 ]; then
    echo -e "${GREEN}✓ $PROMPT_COUNT prompt files found${NC}"
    PASS=$((PASS + 1))
else
    echo -e "${RED}✗ No prompt files found${NC}"
    FAIL=$((FAIL + 1))
fi

echo ""
echo -e "${YELLOW}Hooks:${NC}"
check_executable "session-start.sh" "$PROJECT_DIR/hooks/session-start.sh"
check_executable "tool-guard.sh" "$PROJECT_DIR/hooks/tool-guard.sh"
check_executable "agent-context.sh" "$PROJECT_DIR/hooks/agent-context.sh"

echo ""
echo -e "${YELLOW}Project files:${NC}"
check_file "package.json" "$PROJECT_DIR/package.json"
check_file "README.md" "$PROJECT_DIR/README.md"
check_file "LICENSE" "$PROJECT_DIR/LICENSE"
check_file "CONTRIBUTING.md" "$PROJECT_DIR/CONTRIBUTING.md"
check_file "CHANGELOG.md" "$PROJECT_DIR/CHANGELOG.md"

# Summary
echo ""
echo -e "${BLUE}────────────────────────────────────────────${NC}"
echo -e "Results: ${GREEN}$PASS passed${NC}, ${RED}$FAIL failed${NC}"

if [ $FAIL -eq 0 ]; then
    echo -e "${GREEN}✓ All tests passed!${NC}"
    echo ""
    echo -e "${YELLOW}Note: This tests file presence only.${NC}"
    echo "  For behavioral testing, load the extension in Qwen Code:"
    echo "  ${BLUE}qwen${NC}"
    echo "  ${BLUE}> /load-core-prompts${NC}"
    exit 0
else
    echo -e "${RED}✗ Some tests failed. Run npx claude-style to fix.${NC}"
    exit 1
fi
