name: Test @notesnook/desktop

on:
  workflow_dispatch:
  push:
    branches:
      - "master"
    paths:
      - "apps/desktop/**"
      # re-run workflow if workflow file changes
      - ".github/workflows/desktop.tests.yml"
  pull_request:
    branches:
      - "master"
    paths:
      - "apps/desktop/**"
      # re-run workflow if workflow file changes
      - ".github/workflows/desktop.tests.yml"

jobs:
  build:
    name: Build
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Install packages
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=web

      - name: Generate desktop build
        run: npm run tx @notesnook/web:build:desktop

      - name: Archive build artifact
        uses: actions/upload-artifact@v4
        with:
          name: build
          path: apps/web/build/**/*

  test-macos-x64:
    name: Test macOS x64
    needs: build
    runs-on: macos-15-intel

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v4

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Download build
        uses: actions/download-artifact@v4
        with:
          name: build
          path: ./apps/web/build

      - name: Install packages
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=desktop

      - name: Build Electron wrapper
        run: npm run tx @notesnook/desktop:release

      - name: Build app
        run: |
          yarn electron-builder --config=electron-builder.config.js --mac --dir --x64
        working-directory: ./apps/desktop

      - name: Run tests x64
        run: npm run test
        working-directory: ./apps/desktop

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-results-macos-x64
          path: apps/desktop/test-results
          retention-days: 5

  test-macos:
    name: Test macOS
    needs: build
    runs-on: macos-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v4

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Download build
        uses: actions/download-artifact@v4
        with:
          name: build
          path: ./apps/web/build

      - name: Install packages
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=desktop

      - name: Build Electron wrapper
        run: npm run tx @notesnook/desktop:release

      - name: Build app
        run: |
          yarn electron-builder --config=electron-builder.config.js --mac --dir --arm64
        working-directory: ./apps/desktop

      - name: Run tests arm64
        run: npm run test
        working-directory: ./apps/desktop

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-results-macos
          path: apps/desktop/test-results
          retention-days: 5

  test-linux:
    name: Test for Linux
    needs: build
    runs-on: ubuntu-22.04

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v4

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Download build
        uses: actions/download-artifact@v4
        with:
          name: build
          path: ./apps/web/build

      - name: Install packages
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=desktop

      - name: Install sqlite-better-trigram for all arch
        run: |
          npm i --cpu arm64 sqlite-better-trigram
          npm i --cpu x64 sqlite-better-trigram
        working-directory: ./apps/desktop

      - name: Build Electron wrapper
        run: npm run tx @notesnook/desktop:release

      - name: Build app
        run: |
          yarn electron-builder --config=electron-builder.config.js --linux --dir --arm64 --x64
        working-directory: ./apps/desktop

      - name: Run tests
        run: xvfb-run --auto-servernum --server-args="-screen 0 1920x1080x24" -- npm run test
        working-directory: ./apps/desktop

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-results-linux
          path: apps/desktop/test-results
          retention-days: 5

  test-windows:
    name: Test for Windows
    needs: build
    runs-on: windows-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v4

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Download build
        uses: actions/download-artifact@v4
        with:
          name: build
          path: ./apps/web/build

      - name: Install packages
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=desktop

      - name: Install sqlite-better-trigram for all arch
        run: |
          npm i --cpu arm64 sqlite-better-trigram
          npm i --cpu x64 sqlite-better-trigram

          npm i --cpu arm64 sqlite3-fts5-html
          npm i --cpu x64 sqlite3-fts5-html
        working-directory: ./apps/desktop

      - name: Build Electron wrapper
        run: node scripts/execute.mjs @notesnook/desktop:release

      - name: Build app
        run: |
          npx cross-env NOTESNOOK_STAGING=true yarn electron-builder --config=electron-builder.config.js --win --dir --arm64 --x64
        working-directory: ./apps/desktop

      - name: Run tests
        run: npm run test
        working-directory: ./apps/desktop

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-results-win
          path: apps/desktop/test-results
          retention-days: 5
