name: test

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  workflow_dispatch:
  push:
    branches:
      - 'main'
  pull_request:
    paths-ignore:
      - '.github/*-releases.json'

env:
  NODE_VERSION: "20"

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v4
      -
        name: Test
        uses: docker/bake-action@v4
        with:
          targets: test-coverage
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Check coverage
        run: |
          if [ -f ./coverage/clover.xml ] && [ ! -f ./coverage/allSkipped.txt ]; then
            echo "RUN_CODECOV=true" >> $GITHUB_ENV
          else
            echo "RUN_CODECOV=false" >> $GITHUB_ENV
          fi
        shell: bash
      -
        name: Upload coverage
        uses: codecov/codecov-action@v4
        if: env.RUN_CODECOV == 'true'
        with:
          file: ./coverage/clover.xml
          flags: unit
          token: ${{ secrets.CODECOV_TOKEN }}

  prepare-itg:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.tests.outputs.matrix }}
    steps:
      -
        name: Checkout
        uses: actions/checkout@v4
      -
        name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'yarn'
      -
        name: Install
        run: yarn install
      -
        name: Create matrix
        id: tests
        run: |
          declare -a tests
          for test in $(yarn run test:itg-list); do
            tests+=("${test#$(pwd)/__tests__/}")
          done
          echo "matrix=$(echo ${tests[@]} | jq -cR 'split(" ")')" >>${GITHUB_OUTPUT}
      -
        name: Show matrix
        run: |
          echo ${{ steps.tests.outputs.matrix }}

  test-itg:
    runs-on: ${{ matrix.os }}
    needs:
      - prepare-itg
    strategy:
      fail-fast: false
      matrix:
        test: ${{ fromJson(needs.prepare-itg.outputs.matrix) }}
        os:
          - ubuntu-latest
          - macos-13
          - macos-latest
          - windows-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v4
      -
        name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'yarn'
      -
        name: Install
        run: yarn install
      -
        name: Test
        run: |
          yarn test:itg-coverage --runTestsByPath __tests__/${{ matrix.test }} --coverageDirectory=./coverage
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Check coverage
        run: |
          if [ -f ./coverage/clover.xml ] && [ ! -f ./coverage/allSkipped.txt ]; then
            echo "RUN_CODECOV=true" >> $GITHUB_ENV
          else
            echo "RUN_CODECOV=false" >> $GITHUB_ENV
          fi
        shell: bash
      -
        name: Upload coverage
        uses: codecov/codecov-action@v4
        if: env.RUN_CODECOV == 'true'
        with:
          file: ./coverage/clover.xml
          flags: itg
          token: ${{ secrets.CODECOV_TOKEN }}
