name: Tests

on:
  workflow_call:

jobs:
  tests:
    name: ubuntu / coverage
    runs-on: ubuntu-latest
    timeout-minutes: 25
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - uses: ./.github/actions/setup-gitnexus
        with:
          build: 'true'

      - name: Run all tests with coverage
        run: >-
          npx vitest run
          --reporter=default
          --reporter=json
          --outputFile=test-results.json
          --coverage
          --coverage.reporter=json-summary
          --coverage.reporter=json
          --coverage.reporter=text
          --coverage.thresholdAutoUpdate=false
          --coverage.reportOnFailure=true
        working-directory: gitnexus

      # gitnexus-shared already built by setup-gitnexus action above
      - name: Install gitnexus-web dependencies
        run: npm ci
        working-directory: gitnexus-web

      - name: Run gitnexus-web unit tests
        run: >-
          npx vitest run
          --reporter=default
          --reporter=json
          --outputFile=web-test-results.json
        working-directory: gitnexus-web

      - name: Run docker-server integration tests
        run: node --test docker-server.test.mjs

      - name: Upload test reports
        if: always()
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: test-reports
          path: |
            gitnexus/coverage/coverage-summary.json
            gitnexus/coverage/coverage-final.json
            gitnexus/test-results.json
            gitnexus-web/web-test-results.json
          retention-days: 5

  cross-platform:
    name: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        # Ubuntu already covered by the coverage job above
        os: [windows-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    timeout-minutes: 25
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - uses: ./.github/actions/setup-gitnexus
        with:
          build: 'true'
      - run: npx vitest run
        working-directory: gitnexus
