name: Optimizer - Tests

on:
  workflow_call:
  workflow_dispatch:
    secrets:
      CONCRETE_CI_SSH_PRIVATE:
        required: true
      CONCRETE_ACTIONS_TOKEN:
        required: true

concurrency:
  group: optimizer-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
  CARGO_TERM_COLOR: always
jobs:
  tests:
    strategy:
      matrix:
        os: [ubuntu-20.04, macos-11]
    runs-on: ${{ matrix.os }}
    env:
      RUSTFLAGS: -D warnings
    steps:
      - uses: actions/checkout@v3

      - name: "Setup"
        uses: ./.github/workflows/optimizer_setup
        with:
          ssh_private_key: ${{ secrets.CONCRETE_CI_SSH_PRIVATE }}

      - name: Formatting
        run: |
          cd compilers/concrete-optimizer
          cargo fmt --check

      - name: Build
        run: |
          cd compilers/concrete-optimizer
          cargo build --release --all-targets

      - name: Lint
        run: |
          cd compilers/concrete-optimizer
          cargo clippy --release --all-targets

      - name: Tests
        if: matrix.os == 'ubuntu-20.04'
        run: |
          cd compilers/concrete-optimizer
          cargo test --release --no-fail-fast --all-targets
          make -C concrete-optimizer-cpp test-ci

  benchmarks:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3

      - name: "Setup"
        uses: ./.github/workflows/optimizer_setup
        with:
          ssh_private_key: ${{ secrets.CONCRETE_CI_SSH_PRIVATE }}

      - name: Run benchmark
        run: |
          cd compilers/concrete-optimizer
          cargo bench -p v0-parameters -- --output-format bencher | tee bench_result.txt

      - name: Download PR base benchmark data
        if: ${{ github.event_name == 'pull_request' }}
        # for artifacts restrictions see https://github.com/actions/download-artifact/issues/3
        # for cache restrictions see https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
        # and  https://github.com/actions/cache/issues/692
        uses: dawidd6/action-download-artifact@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          workflow_conclusion: ""
          search_artifacts: true
          name: ${{ runner.os }}-benchmark
          if_no_artifact_found: warn
          path: ./benchmark

      - name: Save benchmark result to file
        uses: benchmark-action/github-action-benchmark@v1
        with:
          tool: 'cargo'
          output-file-path: ./compilers/concrete-optimizer/bench_result.txt
          external-data-json-path: ./benchmark/benchmark-data.json
          # GitHub API token to make a commit comment
          github-token: ${{ secrets.GITHUB_TOKEN }}
          # Enable alert commit comment
          comment-always: true
          # Enable Job Summary for PRs
          summary-always: true

      - name: Upload benchmark data
        uses: actions/upload-artifact@v4
        with:
          path: ./benchmark
          name: ${{ runner.os }}-benchmark
