name: playwright

on:
  push:
    branches:
      - main
      - hotfix
    paths-ignore:
      - "apps/website/**"
  pull_request:

permissions:
  actions: read
  contents: read

jobs:
  e2e-server:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: linux-x64
            os: ubuntu-22.04
            arch: x64
          - name: linux-arm64
            os: ubuntu-24.04-arm
            arch: arm64
    runs-on: ${{ matrix.os }}
    name: E2E tests on ${{ matrix.name }}
    env:
      TRILIUM_DOCKER: 1
      TRILIUM_PORT: 8082
      TRILIUM_DATA_DIR: "${{ github.workspace }}/apps/server/spec/db"
      TRILIUM_INTEGRATION_TEST: memory
    steps:
      - uses: actions/checkout@v6
        with:
          filter: tree:0
          fetch-depth: 0

      - uses: pnpm/action-setup@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 24
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Install Playwright browsers
        run: pnpm exec playwright install --with-deps

      - name: Build the server
        uses: ./.github/actions/build-server
        with:
          os: linux
          arch: ${{ matrix.arch }}

      - name: Unpack and start the server
        run: |
          version=$(node --eval "console.log(require('./package.json').version)")
          file=$(find ./upload -name '*.tar.xz' -print -quit)
          name=$(basename "$file" .tar.xz)
          mkdir -p ./server-dist
          tar -xvf "$file" -C ./server-dist
          server_dir="./server-dist/TriliumNotes-Server-$version-linux-${{ matrix.arch }}"
          if [ ! -d "$server_dir" ]; then
            echo Missing dir.
            exit 1
          fi
          cd "$server_dir"
          "./trilium.sh" &
          sleep 10

      - name: Server end-to-end tests
        run: pnpm --filter server e2e

      - name: Upload test report
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: e2e report ${{ matrix.arch }}
          path: apps/server/test-output

      - name: Kill the server
        if: always()
        run: pkill -f trilium || true

  e2e-standalone:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: linux-x64
            os: ubuntu-22.04
          - name: linux-arm64
            os: ubuntu-24.04-arm
    runs-on: ${{ matrix.os }}
    name: Standalone E2E tests on ${{ matrix.name }}
    env:
      TRILIUM_DOCKER: 1
      TRILIUM_PORT: 8082
    steps:
      - uses: actions/checkout@v6
        with:
          filter: tree:0
          fetch-depth: 0

      - uses: pnpm/action-setup@v5
      - uses: actions/setup-node@v6
        with:
          node-version: 24
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Install Playwright browsers
        run: pnpm exec playwright install --with-deps

      - name: Build standalone
        run: TRILIUM_INTEGRATION_TEST=memory pnpm --filter client-standalone build

      - name: Start standalone preview server
        run: |
          cd apps/client-standalone
          pnpm vite preview --port $TRILIUM_PORT --host 127.0.0.1 &
          sleep 5

      - name: Standalone end-to-end tests
        run: pnpm --filter client-standalone e2e

      - name: Upload test report
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: standalone e2e report ${{ matrix.name }}
          path: apps/client-standalone/test-output
