name: Tests
on:
    pull_request: {}
    merge_group:
        types: [checks_requested]
    push:
        branches: [develop, master]
    repository_dispatch:
        types: [upstream-sdk-notify]
    workflow_call:
        inputs:
            disable_coverage:
                type: boolean
                required: false
                description: "Specify true to skip generating and uploading coverage for tests"
            matrix-js-sdk-sha:
                type: string
                required: false
                description: "The matrix-js-sdk SHA to use"
concurrency:
    group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
    cancel-in-progress: true

env:
    ENABLE_COVERAGE: ${{ github.event_name != 'merge_group' && inputs.disable_coverage != 'true' }}
    # fetchdep.sh needs to know our PR number
    PR_NUMBER: ${{ github.event.pull_request.number }}

jobs:
    jest:
        name: Jest
        runs-on: ubuntu-latest
        strategy:
            fail-fast: false
            matrix:
                # Run multiple instances in parallel to speed up the tests
                runner: [1, 2]
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
              with:
                  repository: ${{ inputs.matrix-js-sdk-sha && 'matrix-org/matrix-react-sdk' || github.repository }}

            - name: Yarn cache
              uses: actions/setup-node@v4
              with:
                  cache: "yarn"

            - name: Install Deps
              run: "./scripts/ci/install-deps.sh --ignore-scripts"
              env:
                  JS_SDK_GITHUB_BASE_REF: ${{ inputs.matrix-js-sdk-sha }}

            - name: Jest Cache
              uses: actions/cache@v4
              with:
                  path: /tmp/jest_cache
                  key: ${{ hashFiles('**/yarn.lock') }}

            - name: Get number of CPU cores
              id: cpu-cores
              uses: SimenB/github-actions-cpu-cores@97ba232459a8e02ff6121db9362b09661c875ab8 # v2

            - name: Run tests
              run: |
                  yarn test \
                      --coverage=${{ env.ENABLE_COVERAGE }} \
                      --ci \
                      --max-workers ${{ steps.cpu-cores.outputs.count }} \
                      --shard ${{ matrix.runner }}/${{ strategy.job-total }} \
                      --cacheDirectory /tmp/jest_cache
              env:
                  JEST_SONAR_UNIQUE_OUTPUT_NAME: true

                  # tell jest to use coloured output
                  FORCE_COLOR: true

            - name: Move coverage files into place
              if: env.ENABLE_COVERAGE == 'true'
              run: mv coverage/lcov.info coverage/${{ steps.setupNode.outputs.node-version }}-${{ matrix.runner }}.lcov.info

            - name: Upload Artifact
              if: env.ENABLE_COVERAGE == 'true'
              uses: actions/upload-artifact@v4
              with:
                  name: coverage-${{ matrix.runner }}
                  path: |
                      coverage
                      !coverage/lcov-report

    skip_sonar:
        name: Skip SonarCloud in merge queue
        if: github.event_name == 'merge_group' || inputs.disable_coverage == 'true'
        runs-on: ubuntu-latest
        needs: jest
        steps:
            - name: Skip SonarCloud
              uses: Sibz/github-status-action@071b5370da85afbb16637d6eed8524a06bc2053e # v1
              with:
                  authToken: ${{ secrets.GITHUB_TOKEN }}
                  state: success
                  description: SonarCloud skipped
                  context: SonarCloud Code Analysis
                  sha: ${{ github.sha }}
                  target_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}

    app-tests:
        name: Element Web Integration Tests
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
              with:
                  repository: ${{ inputs.matrix-js-sdk-sha && 'matrix-org/matrix-react-sdk' || github.repository }}

            - uses: actions/setup-node@v4
              with:
                  cache: "yarn"

            - name: Run tests
              run: "./scripts/ci/app-tests.sh"
              env:
                  JS_SDK_GITHUB_BASE_REF: ${{ inputs.matrix-js-sdk-sha }}