# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

name: Fuzz Tests (daily)

on:
  schedule:
    - cron: '0 16 * * *' # Runs daily at 00:00 GMT+8
  workflow_dispatch:

jobs:
  fuzz:
    if: (github.event_name == 'schedule' && github.repository == 'apache/fesod') || (github.event_name != 'schedule')
    runs-on: ubuntu-latest
    timeout-minutes: 360
    env:
      # Enable Jazzer JUnit mode for fuzz testing, avoiding complex binary target command line configuration
      JAZZER_FUZZ: '1'
      MAVEN_OPTS: '-Xmx2g -Xms2g -Dfile.encoding=UTF-8'
    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Set up JDK
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: '25'
          cache: 'maven'

      - name: Run fuzz tests (fesod module)
        shell: bash
        env:
          # runner context is available at step-level, so this resolves correctly
          JAZZER_ARGS: "-artifact_prefix=${{ runner.temp }}/jazzer-artifacts/ -max_total_time=300  -rss_limit_mb=2048"
        run: |
          mkdir -p "${{ runner.temp }}/jazzer-artifacts/"
          # Execute only tests under fuzz package and force tests not to be skipped
          mvn -B -pl fesod-sheet -am -D"maven.test.skip=false" -DtrimStackTrace=false -D"test=org.apache.fesod.sheet.fuzz.*Test"  -D"surefire.failIfNoSpecifiedTests=false"  package

      - name: Upload Jazzer artifacts (crashes, repros)
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: jazzer-artifacts
          path: ${{ runner.temp }}/jazzer-artifacts/
          if-no-files-found: ignore
