name: Fuzz

on:
  push:
    branches: [main]
  schedule:
    - cron: '0 3 * * 1'  # every Monday at 03:00 UTC
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  fuzz:
    name: Fuzz (${{ matrix.target }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target:
          - fuzz_iff
          - fuzz_bzz
          - fuzz_jb2
          - fuzz_iw44
          - fuzz_full

    steps:
      - uses: actions/checkout@v4

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

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2

      - name: Install cargo-fuzz
        run: cargo install cargo-fuzz --locked

      - name: Run ${{ matrix.target }} for 60 s
        run: cargo fuzz run ${{ matrix.target }} -- -max_total_time=60 -timeout=10

      - name: Upload crash artifacts
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: fuzz-crash-${{ matrix.target }}
          path: fuzz/artifacts/${{ matrix.target }}/
          retention-days: 30
