name: E2E Tests

on:
  pull_request:
    branches: ['main']
  push:
    branches: ['main']
  schedule:
    - cron: '0 0 * * *' # Daily at midnight UTC

permissions:
  contents: read

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  e2e-tests:
    timeout-minutes: 60
    runs-on: ubuntu-latest

    permissions:
      contents: read

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
        with:
          egress-policy: audit

      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Setup Node.js
        uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
        with:
          node-version: '25'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

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

      - name: Generate test content
        run: |
          npm run generate-news -- --types=week-ahead --languages=en,de,fr,es
          npm run generate-news-indexes
          npm run generate-sitemap

      - name: Run E2E tests
        run: npm run test:e2e

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: playwright-report
          path: builds/playwright-report/
          retention-days: 30

      - name: Upload test results (JUnit)
        if: always()
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: test-results
          path: builds/test-results/
          retention-days: 7

      - name: Upload screenshots
        if: failure()
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: playwright-screenshots
          path: builds/test-results/**/*.png
          retention-days: 7

      - name: Upload videos
        if: failure()
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: playwright-videos
          path: builds/test-results/**/*.webm
          retention-days: 7
