#!/bin/bash
# Integration test script for scan-projects.ts
# Tests the script with a mock projects directory structure

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
TEST_DIR="/tmp/scan-projects-test-$$"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

cleanup() {
    echo -e "${YELLOW}Cleaning up test directory: $TEST_DIR${NC}"
    rm -rf "$TEST_DIR"
}

trap cleanup EXIT

echo -e "${GREEN}Setting up test environment...${NC}"

# Create test directory structure
mkdir -p "$TEST_DIR"

# Create test projects
mkdir -p "$TEST_DIR/git-repo"
cd "$TEST_DIR/git-repo"
git init --quiet
git remote add origin "https://github.com/testuser/git-repo.git" 2>/dev/null || true
echo "# Test Project" > README.md
git add README.md
git commit -m "Initial commit" --quiet 2>/dev/null || true

mkdir -p "$TEST_DIR/monorepo"
cd "$TEST_DIR/monorepo"
cat > package.json <<EOF
{
  "name": "monorepo",
  "version": "1.0.0",
  "workspaces": ["packages/*"]
}
EOF
mkdir -p packages/pkg1 packages/pkg2
echo '{"name": "pkg1"}' > packages/pkg1/package.json
echo '{"name": "pkg2"}' > packages/pkg2/package.json
echo 'packages:' > pnpm-workspace.yaml

mkdir -p "$TEST_DIR/npm-project"
cd "$TEST_DIR/npm-project"
cat > package.json <<EOF
{
  "name": "npm-project",
  "version": "1.0.0"
}
EOF
echo '{}' > package-lock.json

mkdir -p "$TEST_DIR/yarn-project"
cd "$TEST_DIR/yarn-project"
cat > package.json <<EOF
{
  "name": "yarn-project",
  "version": "1.0.0"
}
EOF
echo '{}' > yarn.lock

mkdir -p "$TEST_DIR/invalid-name project"
cd "$TEST_DIR/invalid-name project"
cat > package.json <<EOF
{
  "name": "invalid-name project",
  "version": "1.0.0"
}
EOF

echo -e "${GREEN}Test environment created at: $TEST_DIR${NC}"
echo ""
echo "Test projects:"
echo "  - git-repo (Git repository)"
echo "  - monorepo (Monorepo with workspaces)"
echo "  - npm-project (npm project)"
echo "  - yarn-project (yarn project)"
echo "  - invalid-name project (invalid name for testing)"
echo ""

# Check if API is available
if [ -z "${GRAPHQL_API_URL:-}" ]; then
    GRAPHQL_API_URL="http://localhost:4000/graphql"
fi

echo -e "${YELLOW}Testing with API: $GRAPHQL_API_URL${NC}"
echo ""

# Test 1: Dry run
echo -e "${GREEN}Test 1: Dry run${NC}"
cd "$PROJECT_ROOT"
if pnpm tsx scripts/scan-projects.ts \
    --dry-run \
    --projects-dir "$TEST_DIR" \
    --api-url "$GRAPHQL_API_URL" \
    --verbose 2>&1 | grep -q "DRY RUN"; then
    echo -e "${GREEN}✓ Dry run test passed${NC}"
else
    echo -e "${RED}✗ Dry run test failed${NC}"
    exit 1
fi

echo ""

# Test 2: Validation (should fail on invalid project name)
echo -e "${GREEN}Test 2: Validation${NC}"
if pnpm tsx scripts/scan-projects.ts \
    --dry-run \
    --projects-dir "$TEST_DIR" \
    --api-url "$GRAPHQL_API_URL" \
    --verbose 2>&1 | grep -q "invalid-name project"; then
    echo -e "${GREEN}✓ Validation test passed (invalid name detected)${NC}"
else
    echo -e "${YELLOW}⚠ Validation test: Invalid name may have been sanitized${NC}"
fi

echo ""

# Test 3: JSON output
echo -e "${GREEN}Test 3: JSON output${NC}"
if pnpm tsx scripts/scan-projects.ts \
    --dry-run \
    --projects-dir "$TEST_DIR" \
    --api-url "$GRAPHQL_API_URL" \
    --output json 2>&1 | grep -q '"created"'; then
    echo -e "${GREEN}✓ JSON output test passed${NC}"
else
    echo -e "${RED}✗ JSON output test failed${NC}"
    exit 1
fi

echo ""

# Test 4: Help
echo -e "${GREEN}Test 4: Help command${NC}"
if pnpm tsx scripts/scan-projects.ts --help 2>&1 | grep -q "Scan projects directory"; then
    echo -e "${GREEN}✓ Help command test passed${NC}"
else
    echo -e "${RED}✗ Help command test failed${NC}"
    exit 1
fi

echo ""
echo -e "${GREEN}All integration tests passed!${NC}"

