name: 🧪 test

on:
  push:
    paths-ignore:
    - '**.md'
    branches:
    - long_lived/**
    - main
    - release/**
    tags:
    - '**'
  pull_request:
    paths-ignore:
    - '**.md'
    branches:
    - '**'
  workflow_dispatch: null

concurrency:
  group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}--${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/long_lived/')) && github.sha || '' }}
  cancel-in-progress: true

jobs:
  configure:
    name: Configure matrix
    runs-on: ubuntu-latest

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

      - name: Setup Python environment
        uses: Chia-Network/actions/setup-python@main
        with:
          python-version: '3.9'

      - name: Generate matrix configuration
        id: configure
        run: |
          python tests/build-job-matrix.py --per directory --verbose > matrix.json
          cat matrix.json
          echo configuration=$(cat matrix.json) >> $GITHUB_OUTPUT
          echo matrix_mode=${{ ( github.event_name == 'workflow_dispatch' ) && 'all' || ( github.repository_owner == 'Chia-Network' && github.repository != 'Chia-Network/chia-blockchain' ) && 'limited' || ( github.repository_owner == 'Chia-Network' && github.repository == 'Chia-Network/chia-blockchain' && github.ref == 'refs/heads/main' ) && 'main' || ( github.repository_owner == 'Chia-Network' && github.repository == 'Chia-Network/chia-blockchain' && startsWith(github.ref, 'refs/heads/release/') ) && 'all' || 'main' }} >> $GITHUB_OUTPUT

    outputs:
      configuration: ${{ steps.configure.outputs.configuration }}
      matrix_mode: ${{ steps.configure.outputs.matrix_mode }}

  macos:
    uses: ./.github/workflows/test-single.yml
    needs: configure
    with:
      emoji: 🍎
      matrix: macos
      name: macOS
      file_name: macos
      concurrency_name: macos
      configuration: ${{ needs.configure.outputs.configuration }}
      matrix_mode: ${{ needs.configure.outputs.matrix_mode }}
      runs-on: macos-latest
  ubuntu:
    uses: ./.github/workflows/test-single.yml
    needs: configure
    with:
      emoji: 🐧
      matrix: ubuntu
      name: Ubuntu
      file_name: ubuntu
      concurrency_name: ubuntu
      configuration: ${{ needs.configure.outputs.configuration }}
      matrix_mode: ${{ needs.configure.outputs.matrix_mode }}
      runs-on: ubuntu-latest
  windows:
    uses: ./.github/workflows/test-single.yml
    needs: configure
    with:
      emoji: 🪟
      matrix: windows
      name: Windows
      file_name: windows
      concurrency_name: windows
      configuration: ${{ needs.configure.outputs.configuration }}
      matrix_mode: ${{ needs.configure.outputs.matrix_mode }}
      runs-on: windows-latest

  coverage:
    name: ${{ matrix.os.emoji }} Coverage - ${{ matrix.python.name }}
    runs-on: ${{ matrix.os.runs-on }}
    needs:
      - macos
      - ubuntu
      - windows
    strategy:
      fail-fast: false
      matrix:
        os:
          - emoji: 🐧
            matrix: ubuntu
            name: Ubuntu
            runs-on: ubuntu-latest
        python:
          - name: '3.9'
            action: '3.9'
            apt: '3.9'
            install_sh: '3.9'
            matrix: '3.9'

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Download Coverage
        uses: actions/download-artifact@v3
        with:
          name: coverage-data
          path: coverage-data

      - name: Set up ${{ matrix.python.name }}
        uses: Chia-Network/actions/setup-python@main
        with:
          python-version: ${{ matrix.python.action }}

      - uses: ./.github/actions/install
        with:
          python-version: ${{ matrix.python.action }}
          development: true

      - uses: chia-network/actions/activate-venv@main

      - name: Coverage Processing
        run: |
          coverage combine --rcfile=.coveragerc --data-file coverage-reports/.coverage coverage-data/
          coverage xml --rcfile=.coveragerc --data-file coverage-reports/.coverage -o coverage-reports/coverage.xml
          coverage html --rcfile=.coveragerc --data-file coverage-reports/.coverage --directory coverage-reports/html/

      - name: Coverage report (chia/)
        run: |
          coverage report --rcfile=.coveragerc --data-file coverage-reports/.coverage --include='chia/*' --show-missing | tee coverage-reports/coverage-chia-stdout

      - name: Coverage report (tests/)
        run: |
          coverage report --rcfile=.coveragerc --data-file coverage-reports/.coverage --include='tests/*' --show-missing | tee coverage-reports/coverage-tests-stdout

      - name: Coverage report (diff)
        env:
          compare-branch: ${{ github.base_ref == '' && github.event.before || format('origin/{0}', github.base_ref) }}
        run: |
          diff-cover --compare-branch=${{ env.compare-branch }} --html-report coverage-reports/diff-cover.html --markdown-report coverage-reports/diff-cover.md coverage-reports/coverage.xml | tee coverage-reports/diff-cover-stdout
          cat coverage-reports/diff-cover.md >> $GITHUB_STEP_SUMMARY

      - name: Publish coverage reports
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: coverage-reports
          path: coverage-reports/*
          if-no-files-found: error
