name: PR Test (Rust)

on:
  push:
    branches: [ main ]
    paths:
      - "sgl-router/**"
  pull_request:
    branches: [ main ]
    paths:
      - "sgl-router/**"
  workflow_dispatch:

concurrency:
  group: pr-test-rust-${{ github.ref }}
  cancel-in-progress: true

jobs:
  unit-test-rust:
    if: github.repository == 'sgl-project/sglang' || github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          bash scripts/ci/ci_install_rust.sh

      - name: Rust cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: sgl-router

      - name: Run lint
        run: |
          source "$HOME/.cargo/env"
          cd sgl-router/
          cargo clippy --all-targets --all-features -- -D warnings

      - name: Run fmt
        run: |
          source "$HOME/.cargo/env"
          cd sgl-router/
          cargo fmt -- --check

      - name: Run Rust tests
        timeout-minutes: 20
        run: |
          source "$HOME/.cargo/env"
          cd sgl-router/
          cargo test

      - name: Check benchmark compilation
        run: |
          source "$HOME/.cargo/env"
          cd sgl-router/
          cargo check --benches

      - name: Quick benchmark sanity check
        timeout-minutes: 15
        run: |
          source "$HOME/.cargo/env"
          cd sgl-router/
          # Run quick benchmarks to ensure they work using Python script
          python3 scripts/run_benchmarks.py --quick

  e2e-python:
    if: github.repository == 'sgl-project/sglang' || github.event_name == 'pull_request'
    runs-on: BM.A10.4
    timeout-minutes: 35
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install rust dependencies
        run: |
          bash scripts/ci/ci_install_rust.sh

      - name: Install SGLang dependencies
        run: |
          sudo bash scripts/ci/ci_install_dependency.sh

      - name: Build python binding
        run: |
          source "$HOME/.cargo/env"
          cd sgl-router
          pip install setuptools-rust wheel build
          python3 -m build
          pip install --force-reinstall dist/*.whl


      - name: Run Python unit tests
        run: |
          cd sgl-router
          source "$HOME/.cargo/env"
          pip install pytest pytest-cov pytest-xdist
          pytest -q py_test/unit --cov=sglang_router --cov-report=term-missing --cov-fail-under=80

      - name: Run Python integration tests
        run: |
          cd sgl-router
          source "$HOME/.cargo/env"
          # Integration tests use FastAPI/uvicorn for mock workers
          pip install fastapi uvicorn orjson
          pytest -q -m integration

      - name: Run Python E2E tests
        run: |
          bash scripts/killall_sglang.sh "nuk_gpus"
          cd sgl-router
          pytest -m e2e -s  -vv -o log_cli=true --log-cli-level=INFO

  finish:
    needs: [unit-test-rust, e2e-python]
    runs-on: ubuntu-latest
    steps:
      - name: Finish
        run: echo "This is an empty step to ensure that all jobs are completed."
