name: Test backend

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  test-backend-real:
    runs-on: ubuntu-22.04

    strategy:
      matrix:
        node: ['20', '22']
      fail-fast: false

    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4

      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          cache: 'pnpm'

      - run: sudo add-apt-repository -y ppa:qbittorrent-team/qbittorrent-stable
      - run: sudo add-apt-repository -y ppa:transmissionbt/ppa
      - run: sudo apt-get install -y qbittorrent-nox transmission-daemon

      - run: |
          wget https://github.com/jesec/rtorrent/releases/latest/download/rtorrent-linux-amd64.deb
          sudo dpkg -i rtorrent-linux-amd64.deb

      - run: pnpm install --frozen-lockfile
      - run: npm test || npm test || npm test

      - uses: codecov/codecov-action@v3
        if: matrix.node == 20

      - run: |
          sudo apt-get remove --purge -y rtorrent
          sudo apt-get install -y rtorrent

      - run: npm test -- --selectProjects rtorrent || npm test -- --selectProjects rtorrent || npm test -- --selectProjects rtorrent
        if: matrix['node'] == '20'

  test-backend:
    # so we do not need to update GitHub repo config
    runs-on: ubuntu-latest
    needs:
      - test-backend-real
    strategy:
      matrix:
        node: ['18', '20']
      fail-fast: false

    steps:
      - run: echo pass
