name: Test Workflows

# This workflow is for testing our new workflow changes safely
on:
  push:
    branches: [workflow-testing, test-*]
  pull_request:
    branches: [workflow-testing]
  workflow_dispatch:
    inputs:
      test_type:
        description: 'Type of test to run'
        required: true
        default: 'all'
        type: choice
        options:
        - all
        - ci-only
        - security-only
        - quality-only

jobs:
  workflow-test-info:
    name: Workflow Test Information
    runs-on: ubuntu-latest
    steps:
      - name: Display test information
        run: |
          echo "🧪 Testing new workflow configurations"
          echo "Branch: ${{ github.ref_name }}"
          echo "Event: ${{ github.event_name }}"
          echo "Test type: ${{ github.event.inputs.test_type || 'all' }}"
          echo ""
          echo "This is a safe test environment - no changes will affect production workflows"

  test-basic-setup:
    name: Test Basic Setup
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Test setup-and-build action
        uses: ./.github/actions/setup-and-build

      - name: Verify Node.js version
        run: |
          echo "Node.js version: $(node --version)"
          if [[ "$(node --version)" == *"20.18.0"* ]]; then
            echo "✅ Correct Node.js version"
          else
            echo "❌ Wrong Node.js version"
            exit 1
          fi

      - name: Verify pnpm version
        run: |
          echo "pnpm version: $(pnpm --version)"
          if [[ "$(pnpm --version)" == *"9.14.4"* ]]; then
            echo "✅ Correct pnpm version"
          else
            echo "❌ Wrong pnpm version"
            exit 1
          fi

      - name: Test build process
        run: |
          echo "✅ Build completed successfully"

  test-linting:
    name: Test Linting
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup and Build
        uses: ./.github/actions/setup-and-build

      - name: Test ESLint
        run: |
          echo "Testing ESLint configuration..."
          pnpm run lint --max-warnings 0 || echo "ESLint found issues (expected for testing)"

      - name: Test TypeScript
        run: |
          echo "Testing TypeScript compilation..."
          pnpm run typecheck

  test-caching:
    name: Test Caching Strategy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup and Build
        uses: ./.github/actions/setup-and-build

      - name: Test TypeScript cache
        uses: actions/cache@v4
        with:
          path: |
            .tsbuildinfo
            node_modules/.cache
          key: test-${{ runner.os }}-typescript-${{ hashFiles('**/tsconfig.json', 'app/**/*.ts', 'app/**/*.tsx') }}
          restore-keys: |
            test-${{ runner.os }}-typescript-

      - name: Test ESLint cache
        uses: actions/cache@v4
        with:
          path: node_modules/.cache/eslint
          key: test-${{ runner.os }}-eslint-${{ hashFiles('.eslintrc*', 'app/**/*.ts', 'app/**/*.tsx') }}
          restore-keys: |
            test-${{ runner.os }}-eslint-

      - name: Verify caching works
        run: |
          echo "✅ Caching configuration tested"

  test-security-tools:
    name: Test Security Tools
    runs-on: ubuntu-latest
    if: github.event.inputs.test_type == 'all' || github.event.inputs.test_type == 'security-only'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20.18.0'

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: '9.14.4'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Test dependency audit (non-blocking)
        run: |
          echo "Testing pnpm audit..."
          pnpm audit --audit-level moderate || echo "Audit found issues (this is for testing)"

      - name: Test Trivy installation
        run: |
          echo "Testing Trivy secrets scanner..."
          docker run --rm -v ${{ github.workspace }}:/workspace aquasecurity/trivy:latest fs /workspace --exit-code 0 --no-progress --format table --scanners secret || echo "Trivy test completed"

  test-quality-checks:
    name: Test Quality Checks
    runs-on: ubuntu-latest
    if: github.event.inputs.test_type == 'all' || github.event.inputs.test_type == 'quality-only'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup and Build
        uses: ./.github/actions/setup-and-build

      - name: Test bundle size analysis
        run: |
          echo "Testing bundle size analysis..."
          ls -la build/client/ || echo "Build directory structure checked"

      - name: Test dependency checks
        run: |
          echo "Testing depcheck..."
          npx depcheck --config .depcheckrc.json || echo "Depcheck completed"

      - name: Test package.json formatting
        run: |
          echo "Testing package.json sorting..."
          npx sort-package-json package.json --check || echo "Package.json check completed"

  validate-docker-config:
    name: Validate Docker Configuration
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Test Docker build (without push)
        run: |
          echo "Testing Docker build configuration..."
          docker build --target bolt-ai-production . --no-cache --progress=plain
          echo "✅ Docker build test completed"

  test-results-summary:
    name: Test Results Summary
    runs-on: ubuntu-latest
    needs: [workflow-test-info, test-basic-setup, test-linting, test-caching, test-security-tools, test-quality-checks, validate-docker-config]
    if: always()
    steps:
      - name: Check all test results
        run: |
          echo "🧪 Workflow Testing Results Summary"
          echo "=================================="
          
          if [[ "${{ needs.test-basic-setup.result }}" == "success" ]]; then
            echo "✅ Basic Setup: PASSED"
          else
            echo "❌ Basic Setup: FAILED"
          fi
          
          if [[ "${{ needs.test-linting.result }}" == "success" ]]; then
            echo "✅ Linting Tests: PASSED"
          else
            echo "❌ Linting Tests: FAILED"
          fi
          
          if [[ "${{ needs.test-caching.result }}" == "success" ]]; then
            echo "✅ Caching Tests: PASSED"
          else
            echo "❌ Caching Tests: FAILED"
          fi
          
          if [[ "${{ needs.test-security-tools.result }}" == "success" ]]; then
            echo "✅ Security Tools: PASSED"
          else
            echo "❌ Security Tools: FAILED"
          fi
          
          if [[ "${{ needs.test-quality-checks.result }}" == "success" ]]; then
            echo "✅ Quality Checks: PASSED"
          else
            echo "❌ Quality Checks: FAILED"
          fi
          
          if [[ "${{ needs.validate-docker-config.result }}" == "success" ]]; then
            echo "✅ Docker Config: PASSED"
          else
            echo "❌ Docker Config: FAILED"
          fi
          
          echo ""
          echo "Next steps:"
          echo "1. Review any failures above"
          echo "2. Fix issues in workflow configurations"
          echo "3. Re-test until all checks pass"
          echo "4. Create PR to merge workflow improvements"