name: Nightly

on:
  schedule:
    - cron: "0 3 * * *" # Every day at 03:00 UTC

jobs:
  extended-fuzz:
    name: Extended Fuzzing (10 min)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
            fuzz/target/
            fuzz/corpus/
          key: fuzz-${{ hashFiles('**/Cargo.toml', 'fuzz/**') }}
          restore-keys: fuzz-

      - name: Install Rust
        run: |
          rustup component add rustfmt
          rustup component add clippy

      - name: Install tools
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-fuzz@0.12.0,cargo-deny@0.19.0

      - name: Fuzz redb core (5 minutes)
        run: cargo fuzz run --sanitizer=none fuzz_redb -- -max_len=10000 -max_total_time=300

      - name: Fuzz vector ops (90s)
        run: cargo fuzz run --sanitizer=none fuzz_vector_ops -- -max_len=10000 -max_total_time=90

      - name: Fuzz quantization (90s)
        run: cargo fuzz run --sanitizer=none fuzz_quantization -- -max_len=10000 -max_total_time=90

      - name: Fuzz blob types (60s)
        run: cargo fuzz run --sanitizer=none fuzz_blob_types -- -max_len=10000 -max_total_time=60

      - name: Fuzz fractal types (60s)
        run: cargo fuzz run --sanitizer=none fuzz_fractal_types -- -max_len=10000 -max_total_time=60

      - name: Fuzz IVF-PQ kmeans (90s)
        run: cargo fuzz run --sanitizer=none fuzz_ivfpq_kmeans -- -max_len=10000 -max_total_time=90

      - name: Upload corpus on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: fuzz-artifacts-${{ github.run_id }}
          path: |
            fuzz/artifacts/
            fuzz/corpus/
          retention-days: 30
