name: Tests & Coverage

on: [workflow_call]


jobs:
  test-python:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest]
        python-version: ["3.11", "3.12", "3.13"]

    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install tox
      run: python -m pip install --upgrade pip tox==4.23.2 tox-uv~=1.16.0 tox-gh-actions~=3.0

    - name: Run tests
      run: tox

    - name: Upload coverage artifact
      if: matrix.os == 'ubuntu-latest'
      uses: actions/upload-artifact@v4
      with:
        name: coverage-${{ matrix.python-version }}
        path: reports/.coverage.*test
        include-hidden-files: true

  coverage-python:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    needs: test-python
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download all coverage artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true
          path: reports

      - uses: actions/setup-python@v5
        with:
          python-version: 3.11

      - name: Install tox
        run: python -m pip install --upgrade pip tox

      - name: Combine coverage results
        run: tox run -e combine-test-reports

      - name: Upload combined python coverage
        uses: actions/upload-artifact@v4
        with:
          name: python-coverage-report
          path: reports/coverage-python.xml

  test-ui-unit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-node@v4
        with:
          node-version: lts/*

      - name: Install Dependencies
        run: npm ci

      - name: Run UI unit tests
        run: npm run test:unit

      - name: Upload vitest coverage report
        uses: actions/upload-artifact@v4
        with:
          name: vitest-coverage-report
          path: reports/coverage-vitest.xml

  test-ui-e2e:
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - name: Install Dependencies
        run: npm ci

      - name: Install Playwright Browsers
        run: npx playwright install --with-deps

      - name: Run Playwright tests
        run: npm run test:e2e
        env:
          VDOC_PLUGINS_ORAMA_ENDPOINT: "${{ secrets.ORAMA_API_ENDPOINT }}"
          VDOC_PLUGINS_ORAMA_API_KEY: "${{ secrets.ORAMA_API_KEY }}"

      - uses: actions/upload-artifact@v4
        if: ${{ !cancelled() }}
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 30

  upload-coverage:
    timeout-minutes: 2
    runs-on: ubuntu-latest
    if: ${{ github.actor != 'dependabot[bot]' }}
    needs:
      - test-ui-unit
      - coverage-python
    steps:
      - uses: actions/checkout@v4

      - name: Download all coverage artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true
          path: reports

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          slug: vorausrobotik/vdoc
          disable_search: true
          disable_telem: true
          files: 'reports/coverage-python.xml,reports/coverage-vitest.xml'
          fail_ci_if_error: true
          verbose: true
