name: Tests

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  unit-tests-linux:
    name: Unit Tests (Linux)
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - name: Free disk space
        run: |
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /opt/ghc
          sudo rm -rf /usr/local/share/boost
          df -h

      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-unit-${{ hashFiles('**/Cargo.lock') }}

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y pkg-config libssl-dev build-essential protobuf-compiler

      - name: Install cargo-nextest
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-nextest

      # Note: Using specific features instead of --all-features to avoid sp1-sdk
      # build issues. The zkvm-prover feature requires SP1 toolchain and causes
      # linker OOM on Linux and C++20 errors on Windows.
      - name: Run unit tests
        run: cargo nextest run --lib --profile default

      - name: Run integration tests
        run: cargo nextest run --test '*' --profile default

      - name: Run doctests
        run: cargo test --doc

  unit-tests-macos:
    name: Unit Tests (macOS)
    runs-on: macos-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-unit-${{ hashFiles('**/Cargo.lock') }}

      - name: Install system dependencies
        run: brew install protobuf

      - name: Install cargo-nextest
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-nextest

      # Note: Using specific features instead of --all-features to avoid sp1-sdk
      # build issues. The zkvm-prover feature requires SP1 toolchain.
      - name: Run unit tests
        run: cargo nextest run --lib --profile default

      - name: Run integration tests
        run: cargo nextest run --test '*' --profile default

  unit-tests-windows:
    name: Unit Tests (Windows)
    runs-on: windows-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-unit-${{ hashFiles('**/Cargo.lock') }}

      - name: Install system dependencies
        run: choco install protoc

      - name: Install cargo-nextest
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-nextest

      # Note: Using specific features instead of --all-features to avoid sp1-sdk
      # build issues. The zkvm-prover feature requires C++20 on Windows.
      - name: Run unit tests
        run: cargo nextest run --lib --profile default

      - name: Run integration tests
        run: cargo nextest run --test '*' --profile default
