name: Nightly Fuzzing

on:
  schedule:
    # Runs at 02:00 UTC every day
    - cron: '0 2 * * *'
  # Allows you to click "Run workflow" manually in GitHub Actions UI
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  PROPTEST_CASES: 1000000 # 1 Million cases

jobs:
  deep_fuzz:
    name: Fuzz (Release)
    runs-on: ubuntu-latest
    timeout-minutes: 60 # Safety net to prevent hanging jobs costing $$
    
    permissions:
      contents: write       # Required to push the new branch with seeds
      pull-requests: write  # Required to create the Pull Request

    steps:
    - uses: actions/checkout@v6

    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        # Distinct key for RELEASE artifacts so we don't mix with Debug
        key: ${{ runner.os }}-cargo-release-${{ hashFiles('**/Cargo.lock') }}

    - name: Build (Release)
      # Compiling in release takes longer, but makes the 1M tests run 10x faster
      run: cargo build --verbose --release

    - name: Run Deep Tests
      # If this fails, check the logs for "seed: <number>" to reproduce locally
      run: cargo test --verbose --release

    - name: Create Regression PR
      # This runs ONLY if the previous "cargo test" step failed
      if: failure() 
      uses: peter-evans/create-pull-request@v8
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        commit-message: "test: add fuzzing regression seeds"
        title: "🐛 Fuzzing Failure: New Regression Cases Found"
        body: |
          ## 💥 Fuzzing Failure Detected
          The nightly fuzzing suite detected a crash or logic error. 
          
          The cryptographic seeds for these failures have been automatically appended to the regression file. Merging this PR will ensure these specific edge cases are permanently added to the test suite and re-run on every build to prevent regression.
        branch: fuzzing-failures
        delete-branch: true
