# Tests a broad set of Quarto functionality that users are likely to encounter.
# A failure indicates some signficant portion of functionality is likely to be broken.
name: Parallel Smokes Tests
on:
  workflow_dispatch:
    inputs:
      nBuckets:
        description: "Number of buckets to split tests into"
        required: true
        default: 10
        type: number
      extra-r-packages:
        description: "extra R package to install for the tests runs (like a dev version of one of the deps) - comma separated, passed to renv::install"
        required: false
        type: string
        default: ""
  pull_request:
    branches:
      - main
      - "v[1-9].[0-9]+" # run also on released version branch (for patch releases)
    paths-ignore:
      # Don't run on PRs that only change these files that
      # are not relevant to the tested features
      - "src/resources/language/**"
      - "dev-docs/**"
      # don't run on PR working on other workflows
      - ".github/workflows/create-release.yml"
      - ".github/workflows/performance-check.yml"
      - ".github/workflows/stale-needs-repro.yml"
      - ".github/workflows/test-bundle.yml"
      - ".github/workflows/test-ff-matrix.yml"
      - ".github/workflows/test-install.yml"
      - ".github/workflows/test-quarto-latexmk.yml"
      - ".github/workflows/update-test-timing.yml"
  push:
    # only trigger on branches, not on tags
    branches:
      - main
      - "v[1-9].[0-9]+" # run also on released version branch (for patch releases)
    paths-ignore:
      - "news/**"

concurrency:
  # Use github.run_id on main branch
  # Use github.event.pull_request.number on pull requests, so it's unique per pull request
  # Use github.ref on other branches, so it's unique per branch
  group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_id || github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  jobs-matrix:
    runs-on: ubuntu-latest
    outputs:
      BUCKETS: ${{ steps.tests-buckets.outputs.BUCKETS }}
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v6

      - name: Set up Quarto
        uses: quarto-dev/quarto-actions/setup@v2
        with:
          version: ${{ github.ref == 'refs/heads/main' && 'pre-release' || 'release' }}

      - uses: ./.github/actions/merge-extension-tests

      - name: Create Job for tests
        id: tests-buckets
        run: |
          BUCKETS=$(./run-parallel-tests.sh -n=${{ inputs.nBuckets || 20 }} --json-for-ci --timing-file=timing-for-ci.txt | jq -rc 'def lpad(n): tostring | if (n > length) then ((n - length) * "0") + . else . end; to_entries | map(.key |= tostring)| map({ num: .key| tonumber | (. + 1) | lpad(2), files: .value | tojson }) | {buckets: .}')
          if [ -z $BUCKETS ]; then
            echo "::error::Failed to create buckets. Something possibly wrong in run-parallel-tests.sh."
            exit 1
          fi
          echo "BUCKETS=$BUCKETS" >> "$GITHUB_OUTPUT"
        working-directory: tests
      - name: Read buckets
        run: |
          echo ${{ steps.tests-buckets.outputs.BUCKETS }}

  check-matrix:
    runs-on: ubuntu-latest
    needs: jobs-matrix
    steps:
      - name: Install json2yaml
        run: |
          sudo npm install -g json2yaml

      - name: Check matrix definition
        run: |
          matrix='${{ needs.jobs-matrix.outputs.BUCKETS }}'
          echo $matrix
          echo $matrix | jq .
          echo $matrix | json2yaml

  run-smoke-tests:
    needs: jobs-matrix
    name: Running Tests buckets ${{ matrix.buckets.num }}
    strategy:
      fail-fast: false
      matrix: ${{ fromJSON(needs.jobs-matrix.outputs.BUCKETS) }}
    uses: ./.github/workflows/test-smokes.yml
    with:
      buckets: ${{ matrix.buckets.files }}
      extra-r-packages: ${{ inputs.extra-r-packages }}
