# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # Everything but the tests
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v4

      - name: Setup Node.js environment
        uses: actions/setup-node@v4.0.2
        with:
          node-version: 20.x
          cache: yarn
          cache-dependency-path: yarn.lock

      - name: Install dependencies
        run: yarn

      - name: Log Versions
        run: yarn tsc --version && yarn jest --version

      - name: eslint
        run: yarn lint

      - name: Prettier
        run: yarn format:check

      - name: Type Check
        run: yarn tsc

      # knip runs after tsc so that files can reference the `dist` dir.
      - name: Knip
        run: yarn knip

      - name: Integration Test
        run: yarn check:asciidoc

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        shard: [1, 2, 3]
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js environment
        uses: actions/setup-node@v4.0.2
        with:
          node-version: 20.x
          cache: yarn
          cache-dependency-path: yarn.lock

      - name: Install dependencies
        run: yarn

      - name: Test

        run: yarn test --shard=${{ matrix.shard }}/${{ strategy.job-total }} --coverage

      - run: mv coverage/coverage-final.json coverage/${{matrix.shard}}.json

      # https://github.com/actions/download-artifact?tab=readme-ov-file#download-multiple-filtered-artifacts-to-the-same-directory
      - uses: actions/upload-artifact@v4
        with:
          name: coverage-artifacts-${{ matrix.shard }}
          path: coverage/

  report-coverage:
    runs-on: ubuntu-latest
    needs: [test]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          path: coverage
          pattern: coverage-artifacts-*
          merge-multiple: true
      - run: ls -l coverage
      - name: Merge Code Coverage
        run: npx nyc merge coverage/ merged-output/merged-coverage.json
      - uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
