name: concrete-python tests macos
on:
  workflow_dispatch:
  pull_request:
    paths:
      - .github/workflows/concrete_python_test_macos.yml
  push:
    branches:
      - 'main'
      - 'release/*'

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

env:
  ACTION_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
  SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }}
  SLACK_USERNAME: ${{ secrets.BOT_USERNAME }}
  SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

jobs:
  concrete-python-test-pytest:
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.10"]
        machine: ["aws-mac1-metal", "aws-mac2-metal"]
    runs-on: ${{ matrix.machine }}
    outputs:
      slack_message: ${{ steps.prepare_slack_notif.outputs.slack_message }}
      slack_color: ${{ steps.prepare_slack_notif.outputs.slack_color }}
    env:
      python: python${{matrix.python-version}}
      concrete-python-dir: ${{ github.workspace }}/frontends/concrete-python
      concrete-compiler-dir: ${{ github.workspace }}/compilers/concrete-compiler/compiler
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          submodules: recursive
          fetch-depth: 0
      - name: Install build dependencies
        run: |
          brew install ninja ccache
      - name: Setup rust toolchain for concrete-cpu
        uses: ./.github/actions/setup_rust_toolchain_for_concrete_cpu
      - name: Prepare build environment
        run: |
          set -e
          cd ${{ env.concrete-python-dir }}

          # Setup pkg-config to find OpenBLAS (scipy need it)
          export PKG_CONFIG_PATH="/opt/homebrew/opt/openblas/lib/pkgconfig"

          rm -rf .venv
          ${{ env.python }} -m venv .venv

          . ${{ env.concrete-python-dir }}/.venv/bin/activate

          pip install -r requirements.dev.txt
          pip install -r requirements.txt
      - name: Build concrete-compiler python-bindings
        run: |
          . ${{ env.concrete-python-dir }}/.venv/bin/activate
          cd ${{ env.concrete-compiler-dir }}
          ccache -z
          make Python3_EXECUTABLE="$(which ${{ env.python }})" python-bindings
          ccache -s
      - name: Create wheels
        run: |
          . ${{ env.concrete-python-dir }}/.venv/bin/activate
          cd ${{ env.concrete-python-dir }}

          export COMPILER_BUILD_DIRECTORY=${{ env.concrete-compiler-dir }}/build
          rm -rf dist && mkdir -p dist
          pip wheel -v --no-deps -w dist .
          delocate-wheel -v dist/*macos*.whl

          deactivate
      - name: Prepare test environment
        run: |
          set -e
          TEST_TMP_DIR=$(mktemp -d)
          echo "TEST_TMP_DIR=$TEST_TMP_DIR" >> "${GITHUB_ENV}"
          cd "${TEST_TMP_DIR}"

          ${{ env.python }} -m venv .testenv
          . .testenv/bin/activate

          pip install ${{ env.concrete-python-dir }}/dist/*macos*.whl
          pip install -r ${{ env.concrete-python-dir }}/requirements.dev.txt

          ${{ github.workspace }}/.github/workflows/scripts/fix_multi_omp_bug_macos.sh

          cp -R ${{ env.concrete-python-dir }}/examples ./examples
          cp -R ${{ env.concrete-python-dir }}/tests ./tests
          cp -R ${{ env.concrete-python-dir }}/scripts ./scripts

          cp ${{ env.concrete-python-dir }}/Makefile .
      - name: Run pytest-macos
        run: |
          set -e
          cd "${TEST_TMP_DIR}" && . .testenv/bin/activate
          KEY_CACHE_DIRECTORY=./KeySetCache PYTEST_MARKERS="not dataflow and not graphviz" make pytest-macos
      - name: Run test-notebooks
        run: |
          set -e
          cd "${TEST_TMP_DIR}" && . .testenv/bin/activate
          make test-notebooks
      - name: Cleanup host
        if: success() || failure()
        run: |
          rm -rf "${TEST_TMP_DIR}"
      - name: Prepare Slack Notification
        id: prepare_slack_notif
        if: ${{ failure() }}
        continue-on-error: true
        run: |
          echo "slack_message=concrete-python-test-pytest (${{matrix.machine}}/${{ matrix.python-version }}) finished with status: ${{ job.status }}. (${{ env.ACTION_RUN_URL }})" >> "$GITHUB_OUTPUT"
          echo "slack_color=${{ job.status }}" >> "$GITHUB_OUTPUT"

  slack-notif-macos:
    needs: ["concrete-python-test-pytest"]
    runs-on: "ubuntu-latest"
    if: always()
    steps:
      - name: Slack Notification
        # we want to check that prepare_slack_notif was run
        if: ${{ needs.concrete-python-test-pytest.outputs.slack_color != '' }}
        uses: rtCamp/action-slack-notify@c33737706dea87cd7784c687dadc9adf1be59990
        env:
          SLACK_COLOR: ${{ needs.concrete-python-test-pytest.outputs.slack_color }}
          SLACK_MESSAGE: ${{ needs.concrete-python-test-pytest.outputs.slack_message }}
