name: ui-regression

on:
  push:
    branches:
      - main
      - master
    paths:
      - admin/**
      - frontend/**
      - playwright-smoke/**
      - .gitea/workflows/ui-regression.yml
  pull_request:
    paths:
      - admin/**
      - frontend/**
      - playwright-smoke/**
      - .gitea/workflows/ui-regression.yml
  workflow_dispatch:

jobs:
  playwright-regression:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Install frontend deps
        working-directory: frontend
        run: pnpm install --frozen-lockfile

      - name: Install admin deps
        working-directory: admin
        run: pnpm install --frozen-lockfile

      - name: Install Playwright deps
        working-directory: playwright-smoke
        run: pnpm install --frozen-lockfile

      - name: Install Playwright browsers
        working-directory: playwright-smoke
        run: pnpm exec playwright install --with-deps chromium

      - name: Typecheck Playwright suite
        working-directory: playwright-smoke
        run: pnpm exec tsc -p tsconfig.json --noEmit

      - name: Build frontend package
        working-directory: frontend
        env:
          PUBLIC_API_BASE_URL: http://127.0.0.1:5159/api
        run: pnpm build

      - name: Build admin package
        working-directory: admin
        env:
          VITE_API_BASE: http://127.0.0.1:5159
          VITE_FRONTEND_BASE_URL: http://127.0.0.1:4321
        run: pnpm build

      - name: Prepare Playwright artifact folders
        run: |
          rm -rf playwright-smoke/.artifacts
          mkdir -p playwright-smoke/.artifacts/frontend
          mkdir -p playwright-smoke/.artifacts/admin

      - name: Run frontend UI regression suite
        id: ui_frontend
        working-directory: playwright-smoke
        continue-on-error: true
        env:
          PLAYWRIGHT_USE_BUILT_APP: '1'
        run: pnpm test:frontend

      - name: Collect frontend Playwright artifacts
        if: always()
        run: |
          if [ -d playwright-smoke/playwright-report ]; then
            cp -R playwright-smoke/playwright-report playwright-smoke/.artifacts/frontend/playwright-report
          fi
          if [ -d playwright-smoke/test-results ]; then
            cp -R playwright-smoke/test-results playwright-smoke/.artifacts/frontend/test-results
          fi
          rm -rf playwright-smoke/playwright-report playwright-smoke/test-results

      - name: Run admin UI regression suite
        id: ui_admin
        working-directory: playwright-smoke
        continue-on-error: true
        env:
          PLAYWRIGHT_USE_BUILT_APP: '1'
        run: pnpm test:admin

      - name: Collect admin Playwright artifacts
        if: always()
        run: |
          if [ -d playwright-smoke/playwright-report ]; then
            cp -R playwright-smoke/playwright-report playwright-smoke/.artifacts/admin/playwright-report
          fi
          if [ -d playwright-smoke/test-results ]; then
            cp -R playwright-smoke/test-results playwright-smoke/.artifacts/admin/test-results
          fi

      - name: Summarize Playwright artifact paths
        if: always()
        shell: bash
        run: |
          set -euo pipefail

          echo "Gitea Actions 当前不支持 actions/upload-artifact@v4，改为直接输出产物目录："

          for path in \
            "playwright-smoke/.artifacts/frontend/playwright-report" \
            "playwright-smoke/.artifacts/frontend/test-results" \
            "playwright-smoke/.artifacts/admin/playwright-report" \
            "playwright-smoke/.artifacts/admin/test-results"
          do
            if [ -d "${path}" ]; then
              echo "- ${path}"
              find "${path}" -maxdepth 2 -type f | sort | head -n 20
            else
              echo "- ${path} (missing)"
            fi
          done

      - name: Mark workflow failed when any suite failed
        if: steps.ui_frontend.outcome != 'success' || steps.ui_admin.outcome != 'success'
        run: exit 1
