name: Tests
on: [push, pull_request_target]

jobs:
  tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    if: >
      github.event_name == 'push' ||
      github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository

    steps:
      - uses: actions/checkout@v4
        with:
          # Disabling shallow clone is recommended for improving relevancy of reporting for sonarcloud
          fetch-depth: 0

      - uses: actions/setup-node@v4

      - run: npm update

      - run: npm install --no-audit

      - run: npm run audit

      - run: npm run test

      - name: Fix code coverage paths
        run: sed -i 's/\/home\/runner\/work\/yoti-node-sdk\/yoti-node-sdk\//\/github\/workspace\//g' ./coverage/lcov.info

      - uses: sonarsource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

  examples:
    name: Lint Examples
    runs-on: ubuntu-latest
    if: >
      github.event_name == 'push' ||
      github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '14'

      - run: npm install

      - run: npm update

      - name: print versions
        run: echo npm $(npm -v), node $(node -v)

      - run: pushd ./examples/aml-check && npm install && npm update && npm run lint && popd

      - run: pushd ./examples/idv && npm install && npm update && npm run lint && popd

      - run: pushd ./examples/idv-identity-checks && npm install && npm update && npm run lint && popd

      - run: pushd ./examples/profile && npm install && npm update && npm run lint && popd

      - run: pushd ./examples/profile-identity-checks && npm install && npm update && npm run lint && popd

  node-compatibility:
    name: Node ${{ matrix.node-version }}
    runs-on: ubuntu-latest
    # always run on push events
    # only run on pull_request_target event when pull request pulls from fork repository
    if: >
      github.event_name == 'push' ||
      github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository
    strategy:
      fail-fast: false
      matrix:
        node-version: [14, 16, 18, 20]

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

      - run: npm install

      - run: npm update

      - run: npm run test

  os-compatibility:
    name: Running in ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    # always run on push events
    # only run on pull_request_target event when pull request pulls from fork repository
    if: >
      github.event_name == 'push' ||
      github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4

      - run: npm install

      - run: npm update

      - run: npm run test
