name: CI

on:
  workflow_dispatch:
  push:
    branches: [main, next]
  pull_request:
    branches: [main, next]

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    name: test (${{ matrix.os }})

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "22.5"

      - uses: oven-sh/setup-bun@v2
        if: runner.os == 'Linux'
        with:
          bun-version: "1.3.14"

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - uses: actions/setup-go@v5
        with:
          go-version: "stable"
          cache: false

      - uses: erlef/setup-beam@v1
        if: runner.os != 'Windows'
        with:
          elixir-version: "1.17"
          otp-version: "27"

      - name: Install Elixir (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: choco install elixir --yes --no-progress

      - name: Install dependencies
        run: npm install

      - name: Typecheck
        run: npx tsc -b --noEmit

      - name: Build
        run: npm run build

      - name: Bundle
        run: npm run bundle

      - name: Assert bundle invariants (G3 — Issue #511 guardrail)
        run: npm run assert-bundle

      - name: Run all tests
        shell: bash
        run: |
          # Run tests, capture output + exit code.
          # Vitest fork workers can crash during cleanup on CI (native addon teardown race)
          # even when all tests pass. We check the actual test results instead of relying
          # solely on the exit code.
          set +e
          npx vitest run 2>&1 | tee /tmp/vitest-output.txt
          VITEST_EXIT=$?
          set -e

          # Extract results from vitest output
          if grep -q "Tests.*failed" /tmp/vitest-output.txt; then
            echo "❌ Test failures detected"
            exit 1
          elif grep -q "Test Files.*failed" /tmp/vitest-output.txt; then
            echo "❌ Test file failures detected"
            exit 1
          elif grep -q "Tests.*passed" /tmp/vitest-output.txt; then
            echo "✅ All tests passed (exit code $VITEST_EXIT may reflect worker cleanup noise)"
            exit 0
          else
            echo "❌ Could not parse test results"
            exit $VITEST_EXIT
          fi

      - name: Doctor (verify server starts)
        run: npx tsx src/cli.ts doctor
        continue-on-error: true
