name: CI + E2E

on:
  push:
    branches: [main]
  pull_request:
  workflow_dispatch:

jobs:
  e2e:
    runs-on: self-hosted
    timeout-minutes: 10
    steps:
      - name: Run E2E tests
        id: e2e
        run: |
          cd /home/clems/mascarade/web
          BASE_URL=http://localhost:3100 npx playwright test --reporter=line 2>&1 | tee /tmp/e2e-output.txt
          PASSED=$(grep -oP '\d+ passed' /tmp/e2e-output.txt | grep -oP '\d+' || echo 0)
          FAILED=$(grep -oP '\d+ failed' /tmp/e2e-output.txt | grep -oP '\d+' || echo 0)
          echo "passed=$PASSED" >> $GITHUB_OUTPUT
          echo "failed=$FAILED" >> $GITHUB_OUTPUT
          if [ "${FAILED:-0}" != "0" ] && [ "${FAILED:-0}" != "" ]; then exit 1; fi

      - name: Notify ntfy on failure
        if: failure()
        run: |
          curl -s -d "E2E FAILED: ${{ steps.e2e.outputs.failed }} tests failed on $(date '+%H:%M')" \
            -H "Title: Ops ER E2E Failed" \
            -H "Priority: high" \
            -H "Tags: rotating_light" \
            https://ntfy.saillant.cc/ops-alerts 2>/dev/null || true

      - name: Notify ntfy on success
        if: success()
        run: |
          curl -s -d "E2E OK: ${{ steps.e2e.outputs.passed }} passed" \
            -H "Title: Ops ER E2E Pass" \
            -H "Priority: low" \
            -H "Tags: white_check_mark" \
            https://ntfy.saillant.cc/ops-alerts 2>/dev/null || true
