name: CI
permissions:
  contents: read

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

jobs:
  ci:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.3.14

      - uses: actions/setup-node@v6
        with:
          node-version: 24

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

      - name: Format check
        run: bun run format:check

      - name: Lint
        run: bun run lint

      - name: Type check
        run: bun run typecheck

      - name: Build
        run: bun run build

      - name: Cache Playwright browsers
        uses: actions/cache@v4
        with:
          path: ~/.cache/ms-playwright
          key: playwright-${{ runner.os }}-${{ hashFiles('bun.lock') }}

      - name: Install Playwright browsers
        timeout-minutes: 10
        run: npx playwright install chromium-headless-shell

      - name: Test
        id: test
        run: |
          start=$(date +%s)
          bun run test
          echo "duration=$(( $(date +%s) - start ))" >> "$GITHUB_OUTPUT"

      - name: Record test suite duration
        if: always()
        run: |
          duration="${{ steps.test.outputs.duration }}"
          echo "## Test suite duration" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "| Suite | Duration |" >> "$GITHUB_STEP_SUMMARY"
          echo "| ----- | -------- |" >> "$GITHUB_STEP_SUMMARY"
          echo "| unit + e2e | ${duration}s |" >> "$GITHUB_STEP_SUMMARY"
          if [ "${duration:-0}" -gt 300 ]; then
            echo "" >> "$GITHUB_STEP_SUMMARY"
            echo "> [!WARNING]" >> "$GITHUB_STEP_SUMMARY"
            echo "> Suite exceeded 300 s — possible performance regression." >> "$GITHUB_STEP_SUMMARY"
          fi
