# Run the complete test suite incl. many external command line dependencies (like Openbabel)
# as well as the pymatgen.ext package. Coverage used to be computed based on this workflow.
name: Tests

on:
  push:
    branches: [master]
    paths-ignore: ["**/*.md", docs/**]
  pull_request:
    branches: [master]
    paths-ignore: ["**/*.md", docs/**]
  workflow_dispatch:
  workflow_call: # make this workflow reusable by release.yml

permissions:
  contents: read

jobs:
  test:
    # Prevent this action from running on forks
    if: github.repository == 'materialsproject/pymatgen'
    defaults:
      run:
        shell: bash -l {0} # Enable conda/mamba env activation by reading bash profile
    strategy:
      fail-fast: false
      matrix:
        # Maximize CI coverage of different platforms and python versions while minimizing the
        # total number of jobs. We run all pytest splits with the oldest supported python
        # version (currently 3.10) on windows (seems most likely to surface errors) and with
        # newest version (currently 3.12) on ubuntu (to get complete coverage on unix).
        config:
          - os: windows-latest
            python: "3.10"
            resolution: highest
            extras: ci,optional
          - os: windows-latest
            python: "3.10"
            resolution: highest
            extras: ci,optional,numpy-v1 # Test NP1 on Windows (quite buggy ATM)
          - os: ubuntu-latest
            python: "3.12"
            resolution: lowest-direct
            extras: ci,optional
          - os: macos-latest
            python: "3.11"
            resolution: lowest-direct
            extras: ci # test with only required dependencies installed

        # pytest-split automatically distributes work load so parallel jobs finish in similar time
        # update durations file with `pytest --store-durations --durations-path tests/files/.pytest-split-durations`
        split: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    runs-on: ${{ matrix.config.os }}
    env:
      MPLBACKEND: Agg  # non-interactive backend for matplotlib
      PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
      PYTHONWARNDEFAULTENCODING: "true"  # PEP 597: Enable optional EncodingWarning
    steps:
      - name: Check out repo
        uses: actions/checkout@v4

      - name: Create mamba environment
        uses: mamba-org/setup-micromamba@main
        with:
          environment-name: pmg
          create-args: >-
            python=${{ matrix.config.python }}

      - name: Install ubuntu-only conda dependencies
        if: matrix.config.os == 'ubuntu-latest'
        run: |
          micromamba install -n pmg -c conda-forge bader enumlib \
            openff-toolkit packmol pygraphviz tblite --yes

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Install pymatgen and dependencies via uv
        run: |
          micromamba activate pmg
          # TODO1 (use uv over pip) uv install torch is flaky, track #3826
          # TODO2 (pin torch version): DGL library (matgl) doesn't support torch > 2.2.1,
          # see: https://discuss.dgl.ai/t/filenotfounderror-cannot-find-dgl-c-graphbolt-library/4302
          pip install torch==2.2.1

          # Install from wheels to test the content
          uv build --wheel --no-build-logs
          WHEEL_FILE=$(ls dist/pymatgen*.whl)
          uv pip install $WHEEL_FILE[${{matrix.config.extras}}] --resolution=${{matrix.config.resolution}}

      - name: Install optional Ubuntu dependencies
        if: matrix.config.os == 'ubuntu-latest'
        run: |
          # Install BoltzTraP
          wget -q -O BoltzTraP.tar.bz2 https://owncloud.tuwien.ac.at/index.php/s/s2d55LYlZnioa3s/download
          tar -jxf BoltzTraP.tar.bz2
          echo "$(realpath boltztrap-1.2.5/src/)" >> $GITHUB_PATH

          # Install Vampire 5.0
          wget -q https://vampire.york.ac.uk/resources/release-5/vampire-5.0-linux.tar.gz
          tar -zxf vampire-5.0-linux.tar.gz
          mv linux vampire-5.0
          echo "$(realpath vampire-5.0/)" >> $GITHUB_PATH

      - name: pytest split ${{ matrix.split }}
        env:
          MPLBACKEND: Agg  # non-interactive backend for matplotlib
          PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
          PMG_TEST_FILES_DIR: "${{ github.workspace }}/tests/files"
        run: |
          micromamba activate pmg
          pytest --splits 10 --group ${{ matrix.split }} --durations-path tests/files/.pytest-split-durations tests

  trigger_atomate2_ci:
    needs: test
    runs-on: ubuntu-latest
    # Only run if changes are pushed to master
    if: github.ref == 'refs/heads/master'
    steps:
      - name: Trigger Atomate2 CI
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          curl -L \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/materialsproject/atomate2/dispatches \
            -d '{"event_type": "pymatgen-ci-trigger", "client_payload": {"pymatgen_ref": "${{ github.sha }}"}}'
