name: Fuzz Testing
on:
  # Perform Fuzz testing on PRs and on a daily basis. Daily will continue to
  # look for problems. Doing this on PRs will look for issues introduced in
  # a change.
  pull_request:
  schedule:
    - cron: '33 23 * * *' # Run at 11:33 every day
jobs:
  test:
    runs-on: ubuntu-latest
    env:
      cache-key: fuzzing
    steps:
    - name: Install Go
      uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
      with:
        # There are no dependencies so there is no go.sum file. This is needed
        # for the cache key generation. So, caching doesn't happen and a
        # warning is presented on each run. Disabling the Go cache and caching
        # the go-build cache separately for fuzzing.
        cache: false
        go-version: "1.26"
    # The cache path may be different on different runners. GitHub may change
    # this in the future. So, we dynamically fetch it.
    - name: Get Go Cache Paths
      id: go-cache-paths
      run: echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT
    - name: Checkout code
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Restore Cache
      id: cache-restore
      uses: actions/cache/restore@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
      with:
        path: ${{ steps.go-cache-paths.outputs.go-build }}
        key: ${{ env.cache-key }}
    - name: Fuzz
      run: make fuzz
    # Cannot overwrite the existing cache (id's are immutable) so we delete it.
    - name: Delete Previous Cache
      if: ${{ steps.cache-restore.outputs.cache-hit }}
      continue-on-error: true
      run: |
        gh extension install actions/gh-actions-cache
        gh actions-cache delete "${{ env.cache-key }}" --confirm
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    # Saving the cache so that Fuzz testing can be additive to previous fuzz testing.
    - name: Save Cache
      uses: actions/cache/save@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
      with:
        path: ${{ steps.go-cache-paths.outputs.go-build }}
        key: ${{ env.cache-key }}
