name: CI
on:
  push:
    branches: [master]
    paths:
      - "packages/editor/**"
      - "apps/dev/**"
  workflow_dispatch:
    inputs:
      run_e2e:
        description: "Run Playwright E2E tests"
        type: boolean
        default: false

jobs:
  test:
    runs-on: codeberg-small
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v5
        with:
          node-version: 22
      - run: corepack enable && corepack prepare pnpm@latest --activate
      - run: pnpm install
      - name: Unit tests
        run: pnpm test
  deploy:
    needs: test
    runs-on: codeberg-small
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v5
        with:
          node-version: 22
      - run: corepack enable && corepack prepare pnpm@latest --activate
      - run: pnpm install
      - run: BASE_URL=/editor/ pnpm build
      - if: ${{ (forge.event_name == 'push' && forge.event.ref == 'refs/heads/master') || forge.event_name == 'workflow_dispatch' }}
        uses: actions/git-pages@v2
        with:
          site: https://${{ forge.event.repository.owner.username }}.codeberg.page/${{ forge.event.repository.name }}/
          token: ${{ forge.token }}
          source: apps/dev/dist/
  e2e:
    if: ${{ forge.event_name == 'workflow_dispatch' && forge.event.inputs.run_e2e == 'true' }}
    runs-on: codeberg-small
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v5
        with:
          node-version: 22
      - run: corepack enable && corepack prepare pnpm@latest --activate
      - run: pnpm install
      - run: pnpm --filter @enesis/editor exec playwright install chromium
      - name: E2E tests
        run: pnpm test:e2e
