name: Fuzz

on:
  workflow_call:

jobs:
  fuzz:
    name: Fuzz Tests
    runs-on: ubuntu-latest
    env:
      MISE_PROFILE: cicd

    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          fetch-depth: 1

      - name: Use mise to install dependencies
        uses: jdx/mise-action@c1ecc8f748cd28cdeabf76dab3cccde4ce692fe4 # v4.0.0
        with:
          version: 2026.1.9
          experimental: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - id: go-cache-paths
        run: |
          echo "go-build=$(go env GOCACHE)" >> "$GITHUB_OUTPUT"
          echo "go-mod=$(go env GOMODCACHE)" >> "$GITHUB_OUTPUT"
        shell: bash

      - name: Go Build Cache
        uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
        with:
          path: ${{ steps.go-cache-paths.outputs.go-build }}
          key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}-linux-amd64
          restore-keys: |
            ${{ runner.os }}-go-build-

      - name: Go Mod Cache
        uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
        with:
          path: ${{ steps.go-cache-paths.outputs.go-mod }}
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}-linux-amd64
          restore-keys: |
            ${{ runner.os }}-go-mod-

      - name: Fuzz Corpus Cache
        uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
        with:
          path: ~/.cache/go-build/fuzz
          key: ${{ runner.os }}-fuzz-corpus-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-fuzz-corpus-

      - name: Run Fuzz Tests
        run: make fuzz

      - name: Archive Fuzz Failures
        if: failure()
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: fuzz-failures
          path: |
            **/testdata/fuzz/**
