name: test-realtime

on:
  workflow_dispatch:
    inputs:
      branch:
        description: "Branch or revision to test"
        required: false
        type: string
        default: "main"
  pull_request:
    paths:
      - "livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/**"
      - "livekit-plugins/livekit-plugins-xai/livekit/plugins/xai/realtime/**"
      - "tests/test_realtime/**"
      - ".github/workflows/test-realtime.yml"

jobs:
  test-realtime:
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false
    runs-on: ubuntu-latest
    timeout-minutes: 15
    permissions:
      contents: read

    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.branch != '' && github.event.inputs.branch || github.ref }}
          lfs: true

      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          version: "latest"

      - name: Set up Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
        with:
          python-version: "3.12"

      - name: Install dependencies
        run: uv sync --all-extras --dev

      - name: Run Realtime tests
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}
          AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}
          AZURE_OPENAI_DEPLOYMENT: ${{ secrets.AZURE_OPENAI_DEPLOYMENT }}
          AZURE_OPENAI_API_VERSION: ${{ secrets.AZURE_OPENAI_API_VERSION }}
          # XAI_API_KEY not set — xAI rate-limits GitHub Actions IPs (429 on ws handshake)
        run: |
          uv run pytest --realtime -v -s --tb=long -p no:xdist
