name: ESM Tests

on:
  pull_request:
    branches:
      - "**"
  workflow_dispatch:
  merge_group:
    types: [checks_requested]

permissions:
  contents: read
  pull-requests: write

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  test-precommit:
    runs-on: ubuntu-24.04

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Setup Environment
      uses: prefix-dev/setup-pixi@v0.8.1
      with:
        pixi-version: v0.47.0
        cache: false
        cache-write: ${{ github.event_name == 'push' && github.ref_name == 'main' }}

    - name: Check formatting and typing
      run: pixi run lint-all


  test-esm:
    runs-on: ubuntu-24.04

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Setup Environment
      uses: prefix-dev/setup-pixi@v0.8.1
      with:
        pixi-version: v0.47.0
        cache: false
        cache-write: ${{ github.event_name == 'push' && github.ref_name == 'main' }}

    - name: Run tests
      run: pixi run cov-test

    - name: Run Docker tests
      env:
        DOCKER_TAG: ${{ github.sha }}
        FORGE_URL: https://forge.evolutionaryscale.ai/
        ESM3_FORGE_TOKEN: ${{ secrets.ESM3_FORGE_TOKEN }}
      run: |
        set -e
        cd tests
        make build-oss-ci
        make start-docker-oss URL=${{ env.FORGE_URL }} DOCKER_TAG=${{ env.DOCKER_TAG }} ESM3_FORGE_TOKEN=${{ env.ESM3_FORGE_TOKEN }}
      shell: pixi run bash -e {0}

    - name: cleanup docker containers if they're hanging
      if: ${{ always() }}
      run: |
          docker rm --force --volumes $(docker ps -q) || true

    # Next two steps adds a comment to any PR that reports tests + code coverage.
    - name: Pytest Coverage Comment
      if: ${{ github.event_name == 'pull_request' }}
      uses: MishaKav/pytest-coverage-comment@v1.1.47
      id: coverageComment
      with:
        title: Coverage Report
        pytest-coverage-path: pytest-coverage.txt
        junitxml-path: pytest.xml
        report-only-changed-files: true

    - name: Pytest coverage GitHub summary
      if: ${{ github.event_name == 'pull_request' }}
      run: |
        echo '${{ steps.coverageComment.outputs.coverageHtml }}' >> $GITHUB_STEP_SUMMARY
