name: Auto Test

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-server
  cancel-in-progress: true

on:
  push:
    branches: [master, 1.23.X, 3.0.X]
  pull_request:
permissions: {}

jobs:
  auto-test:
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read

    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, ubuntu-22.04, windows-latest, ubuntu-22.04-arm]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
        node: [24, 25]

    steps:
      - run: git config --global core.autocrlf false # Mainly for Windows
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with: { persist-credentials: false }

      - name: Cache/Restore node_modules
        uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
        id: node-modules-cache
        with:
          path: node_modules
          key: node-modules-${{ runner.os }}-node${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}

      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
        with:
          node-version: ${{ matrix.node }}
      - run: npm clean-install --no-fund

      - name: Rebuild native modules for ARM64
        if: matrix.os == 'ubuntu-22.04-arm'
        run: npm rebuild @louislam/sqlite3

      - run: npm run build
      - run: npm run test-backend
        env:
          HEADLESS_TEST: 1
          JUST_FOR_TEST: ${{ secrets.JUST_FOR_TEST }}

  check-linters:
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - run: git config --global core.autocrlf false # Mainly for Windows
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with: { persist-credentials: false }

      - name: Cache/Restore node_modules
        uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
        id: node-modules-cache
        with:
          path: node_modules
          key: node-modules-${{ runner.os }}-node${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}

      - name: Use Node.js 24
        uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
        with:
          node-version: 24
      - run: npm clean-install --no-fund
      - run: npm run lint:prod

  e2e-test:
    runs-on: ubuntu-22.04-arm
    permissions:
      contents: read
    env:
      PLAYWRIGHT_VERSION: ~1.56.0
    steps:
      - run: git config --global core.autocrlf false # Mainly for Windows
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with: { persist-credentials: false }

      - name: Cache/Restore node_modules
        uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
        id: node-modules-cache
        with:
          path: node_modules
          key: node-modules-${{ runner.os }}-node${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}

      - name: Setup Node.js
        uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
        with:
          node-version: 24
      - run: npm clean-install --no-fund

      - name: Rebuild native modules for ARM64
        run: npm rebuild @louislam/sqlite3

      - name: Install Playwright ${{ env.PLAYWRIGHT_VERSION }}
        run: npx playwright@${{ env.PLAYWRIGHT_VERSION }} install

      - run: npm run build
      - run: npm run test-e2e
