name: Ontime test

on:
  pull_request:
    branches: '*'
  workflow_dispatch:

jobs:
  unit-test:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    env:
      CI: ''

    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Setup pnpm
        uses: pnpm/action-setup@v3
        with:
          version: 9

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

      # Run code quality per package
      - name: React - Run linter +  TypeScript checks
        if: always()
        run: pnpm lint && tsc --noEmit
        working-directory: ./apps/client

      - name: Server - Run linter +  TypeScript checks
        if: always()
        run: pnpm lint && tsc --noEmit
        working-directory: ./apps/server

      - name: Utils - Run linter +  TypeScript checks
        if: always()
        run: pnpm lint && tsc --noEmit
        working-directory: ./packages/utils

      - name: Types - Run linter
        if: always()
        run: pnpm lint
        working-directory: ./packages/types

      # We choose to run tests separately
      - name: React - Run unit tests
        if: always()
        run: pnpm test:pipeline
        working-directory: ./apps/client

      - name: Server - Run unit tests
        if: always()
        run: pnpm test:pipeline
        working-directory: ./apps/server

      - name: Utils - Run unit tests
        if: always()
        run: pnpm test:pipeline
        working-directory: ./packages/utils

  e2e-test:
    runs-on: ubuntu-latest
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Setup pnpm
        uses: pnpm/action-setup@v3
        with:
          version: 9

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

      - name: Build client
        run: pnpm build:local

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

      - name: Run Playwright tests
        run: pnpm e2e

      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 30

      - uses: actions/upload-artifact@v4
        with:
          name: automated-screenshots
          path: automated-screenshots/
          retention-days: 14
