name: Rust CI

on:
  push:
    branches: [main]
    paths-ignore:
      - '**.md'
      - 'doc/**'
  pull_request:
    branches: [main]
    paths-ignore:
      - '**.md'
      - 'doc/**'

env:
  CARGO_TERM_COLOR: always
  # Ensure consistent macOS deployment target across all compiled objects
  # (Rust, cc-compiled C code, and Zig-compiled zlob) to avoid linker warnings
  MACOSX_DEPLOYMENT_TARGET: "13"

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    # Guard against deadlocks in the shared-picker / watcher teardown
    # path: a stuck test would otherwise consume a full 6h CI slot.
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v5
      
      # Zig is required to compile zlob
      - name: Install Zig
        uses: goto-bus-stop/setup-zig@v2
        with:
          version: 0.16.0

      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1.15.4
        with:
          cache: true
          cache-on-failure: true
          cache-key: "v1-rust"
          components: rustfmt, clippy

      - name: Run tests
        run: cargo test --no-default-features --features zlob --workspace --exclude fff-nvim

  stress-test:
    name: Fuzz Tests
    runs-on: ${{ matrix.os }}
    strategy:
      # Keep going after one OS fails so we can see whether a bug
      # reproduces everywhere or is platform-specific.
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    # Long-running; don't let a stuck watcher thread burn a full CI
    # timeout. Two scenarios should finish well under this limit.
    timeout-minutes: 20
    env:
      FFF_STRESS_CASES: "5"
      FFF_STRESS_MIN_OPS: "30"
      FFF_STRESS_MAX_OPS: "60"
    steps:
      - uses: actions/checkout@v5

      - name: Install Zig
        uses: goto-bus-stop/setup-zig@v2
        with:
          version: 0.16.0

      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1.15.4
        with:
          cache: true
          cache-on-failure: true
          cache-key: "v1-rust-stress-${{ matrix.os }}"
          components: rustfmt, clippy

      - name: Stress test seeded
        shell: bash
        run: make test-stress-seeded

      - name: Stress test random
        shell: bash
        run: make test-stress-random

      - name: Stress test regressions
        shell: bash
        run: make test-stress-regressions

      - name: Upload proptest regressions on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: proptest-regressions-${{ matrix.os }}
          path: crates/fff-core/tests/fuzz_git_watcher_stress.proptest-regressions
          if-no-files-found: ignore

  build-i686:
    name: Build i686-unknown-linux-gnu
    runs-on: ubuntu-latest
    # Verifies that fff-search compiles on 32-bit x86, where std::arch::x86_64
    # is unavailable. SIMD paths are disabled on this target; only the scalar
    # fallback should build. See issue #656.
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v5

      - name: Install cross toolchain
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-multilib g++-multilib

      - name: Install Rust (i686 target)
        uses: actions-rust-lang/setup-rust-toolchain@v1.15.4
        with:
          target: i686-unknown-linux-gnu
          cache: true
          cache-on-failure: true
          cache-key: "v1-rust-i686"

      - name: Build fff-search for i686
        run: cargo build -p fff-search --target i686-unknown-linux-gnu

  fmt:
    name: cargo fmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Install Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          components: rustfmt
      - name: Check formatting
        run: cargo fmt -- --check

  clippy:
    name: cargo clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      
      # Zig is required to compile zlob
      - name: Install Zig
        uses: goto-bus-stop/setup-zig@v2
        with:
          version: 0.16.0

      - name: Install Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          components: clippy
          
      - name: Run clippy
        run: cargo clippy --no-default-features --features zlob -- -D warnings
