name: 'Build and Test AuroraView'
description: 'Builds AuroraView extension and runs tests'

inputs:
  test-type:
    description: 'Type of tests to run (basic, full, rust, python, benchmark)'
    required: true
    default: 'basic'
  generate-stubs:
    description: 'Whether to generate type stubs'
    required: false
    default: 'true'
  upload-artifacts:
    description: 'Whether to upload test artifacts'
    required: false
    default: 'false'
  artifact-name:
    description: 'Name for uploaded artifacts'
    required: false
    default: 'test-results'

outputs:
  build-status:
    description: 'Build status (success/failure)'
    value: ${{ steps.build.outcome }}
  test-status:
    description: 'Test status (success/failure)'
    value: ${{ steps.test.outcome }}

runs:
  using: 'composite'
  steps:
    - name: Cache build artifacts
      uses: actions/cache@v4
      with:
        path: |
          target/release/
          target/debug/
          build/
        key: ${{ runner.os }}-build-${{ hashFiles('Cargo.lock') }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-build-${{ hashFiles('Cargo.lock') }}-
          ${{ runner.os }}-build-

    - name: Setup sccache
      uses: mozilla-actions/sccache-action@v0.0.9

    - name: Configure sccache
      shell: bash
      run: |
        echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV
        echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV

    - name: Prefetch Cargo crates (retry)
      shell: bash
      run: |
        set -e
        n=0
        until [ "$n" -ge 3 ]; do
          echo "[TRY $((n+1))/3] cargo fetch...";
          CARGO_HTTP_TIMEOUT=600 CARGO_NET_RETRY=10 cargo fetch --locked --verbose && break;
          n=$((n+1));
          echo "[WARN] cargo fetch failed, retrying in 5s..."; sleep 5;
        done

    - name: Build extension
      id: build
      shell: bash
      run: |
        # Use just command for consistent builds across local and CI
        just ci-build
      env:
        RUST_BACKTRACE: 1
        CARGO_HTTP_TIMEOUT: '600'
        CARGO_NET_RETRY: '10'
        CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse

    - name: Install cargo-llvm-cov
      if: inputs.test-type == 'rust' || inputs.test-type == 'full'
      shell: bash
      run: |
        if ! command -v cargo-llvm-cov &> /dev/null; then
          echo "Installing cargo-llvm-cov..."
          cargo install cargo-llvm-cov --locked
        fi

    - name: Install xvfb for headless UI tests (Linux)
      if: runner.os == 'Linux'
      shell: bash
      run: |
        sudo apt-get update
        sudo apt-get install -y xvfb

    - name: Install Playwright browsers for cross-platform testing
      shell: bash
      run: |
        # Install playwright and browsers for cross-platform UI testing
        # This allows PlaywrightBrowser tests to run on all platforms
        if uv pip show playwright > /dev/null 2>&1; then
          echo "Installing Playwright Chromium browser..."
          uv run playwright install chromium --with-deps
        else
          echo "Playwright not installed, skipping browser installation"
        fi

    - name: Run tests
      id: test
      shell: bash
      run: |
        # Use xvfb-run on Linux for headless UI tests
        if [ "$RUNNER_OS" = "Linux" ]; then
          XVFB_PREFIX="xvfb-run -a --server-args='-screen 0 1920x1080x24'"
        else
          XVFB_PREFIX=""
        fi

        case "${{ inputs.test-type }}" in
          "basic")
            echo "Running basic import tests..."
            just ci-test-basic
            ;;
          "rust")
            echo "Running Rust tests with coverage..."
            echo "  -> Rust integration tests with coverage..."
            # Exclude ipc_batch_integration which requires Python bindings
            cargo llvm-cov --features "test-helpers" --lcov --output-path rust-coverage.lcov \
              --test config_integration \
              --test file_protocol_integration \
              --test http_discovery_integration \
              --test ipc_json_integration \
              --test ipc_message_queue_integration \
              --test lifecycle_integration \
              --test mdns_integration \
              --test port_allocator_integration \
              --test protocol_handlers_integration \
              --test protocol_integration \
              --test standalone_integration \
              --test timer_integration
            echo "  -> Rust doc tests..."
            cargo test --doc
            ;;
          "python")
            echo "Running Python unit tests..."
            eval "$XVFB_PREFIX just ci-test-python"
            ;;
          "full")
            echo "Running comprehensive test suite..."
            echo "  -> Rust integration tests with coverage..."
            # Exclude ipc_batch_integration which requires Python bindings
            cargo llvm-cov --features "test-helpers" --lcov --output-path rust-coverage.lcov \
              --test config_integration \
              --test file_protocol_integration \
              --test http_discovery_integration \
              --test ipc_json_integration \
              --test ipc_message_queue_integration \
              --test lifecycle_integration \
              --test mdns_integration \
              --test port_allocator_integration \
              --test protocol_handlers_integration \
              --test protocol_integration \
              --test standalone_integration \
              --test timer_integration
            echo "  -> Rust doc tests..."
            cargo test --doc
            echo "  -> Python unit tests..."
            eval "$XVFB_PREFIX just ci-test-python"
            echo "  -> Basic functionality tests..."
            just ci-test-basic
            echo "[OK] All tests completed"
            ;;
          *)
            echo "[ERROR] Unknown test type: ${{ inputs.test-type }}"
            echo "Available types: basic, rust, python, full"
            exit 1
            ;;
        esac
      env:
        PYTHONUNBUFFERED: 1
        DISPLAY: ':99'
        # Disable auto-warmup in CI to avoid delays during import
        AURORAVIEW_DISABLE_WARMUP: '1'

    - name: Upload test artifacts
      if: inputs.upload-artifacts == 'true' && always()
      uses: actions/upload-artifact@v5
      with:
        name: ${{ inputs.artifact-name }}-${{ runner.os }}-${{ github.run_id }}
        path: |
          .pytest_cache/
          htmlcov/
          coverage.xml
          rust-coverage.lcov
        retention-days: 7

