# Regression gates for the design system — one named job per quality
# dimension so the Actions tab reads as a scorecard: an engineer
# evaluating HaTchi-MaXchi sees exactly what is guaranteed and where.
#
# Lives inside the package prefix: inert in the Dazzle monorepo (GitHub
# only reads the repo-root .github/), live in the standalone repo.
#
# Fast-fail route (2026-07-28):
#   - concurrency cancel-in-progress: a new tip supersedes an abandoned run
#     (Dazzle "Wait for hatchi-maxchi CI" no longer hangs on a doomed tip).
#   - console tier first (contract + validity): browser gates never start if
#     the cheap gates are red.
#   - browser matrix fail-fast: true: when Visual (or Behaviour) goes red,
#     sibling browser jobs cancel immediately instead of running ~15m more.
name: CI

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

# New push / re-run cancels the previous in-flight CI on this ref.
concurrency:
  group: hm-ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  # ── Console tier (fast) ─────────────────────────────────────────────
  # These finish in ~1 min. Browser gates need both so a contract/validity
  # red never burns playwright install time.
  contract:
    name: Contract & boundary
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install pytest fastapi pydantic
      - run: |
          python build.py
          python build.py --out /tmp/hm-ax --prefix ax-
      - run: python -m pytest tests/test_contract.py tests/test_boundary.py tests/test_contracts.py tests/test_hyperpart_cohesion.py tests/test_css_parse_integrity.py tests/test_icon_contract.py tests/test_pretty.py -q

  # Markup validity: the gallery (which embeds every component's live
  # canonical markup) must be conforming HTML — validated by the Nu
  # (W3C) checker. Documented exclusions, all "vnu lags the platform"
  # or intentional-htmx (vnu.jar pinned by html5validator is 20.6.30):
  # CSS grammar (logical properties, @layer, oklch relative color,
  # light-dark()); `hx-*` attributes (htmx's deliberate validity
  # trade-off; it also accepts data-hx-*); `closedby` (the standard
  # <dialog> light-dismiss attribute); `method="dialog"` on <form> (the
  # native no-JS dialog-close mechanism, in the spec since 2014 but not
  # in this vnu's method datatype); and `name` on <details> (the 2024
  # single-open exclusivity attribute). The last three are all standard
  # HTML too new for the pinned vnu — each pattern is narrow so real
  # structural errors still gate. Everything structural still gates.
  validity:
    name: Markup validity (Nu/W3C)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: "21"
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install html5validator
      - run: >
          html5validator --root site --match "*.html"
          --ignore-re 'CSS: ' 'Attribute "hx-[a-z-]+" not allowed'
          'Attribute "closedby" not allowed'
          'Bad value "dialog" for attribute "method"'
          'Attribute "name" not allowed on element "details"'

  # ── Browser tier (slow) ─────────────────────────────────────────────
  # Matrix + fail-fast: any red cell cancels the others (Visual fail no
  # longer leaves Behaviour running for another 10+ minutes).
  browser:
    name: ${{ matrix.name }}
    needs: [contract, validity]
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        include:
          # Interactive contracts in headless Chromium AND WebKit (Safari's
          # engine): command palette keys (⌘K / Esc / arrows) + pointer dismiss
          # (backdrop tap, close button — the mobile paths), designed hx-confirm
          # interception, theme toggle + persistence, copy feedback. WebKit
          # catches Safari/iPadOS regressions the others miss (e.g. a flex/abspos
          # layout that collapses only in WebKit).
          - name: Behaviour (Chromium + WebKit)
            id: behaviour
            browsers: chromium webkit
            pytest: tests/test_behaviour.py
            upload_diffs: "false"
          # WCAG 2.2 AA conformance via axe-core, light + dark + opened-overlay
          # states. The allowlist may only shrink — and it is currently EMPTY.
          - name: Accessibility (WCAG 2.2 AA, axe-core)
            id: accessibility
            browsers: chromium
            pytest: tests/test_wcag.py
            upload_diffs: "false"
          # Screenshot regression vs committed per-platform baselines (linux set
          # generated by update-baselines.yml in this same environment).
          - name: Visual regression
            id: visual
            browsers: chromium
            pytest: tests/test_visual.py
            upload_diffs: "true"
            pillow: "true"
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Install test deps
        run: |
          pkgs="pytest playwright"
          if [ "${{ matrix.pillow }}" = "true" ]; then
            pkgs="$pkgs pillow"
          fi
          pip install $pkgs
          playwright install --with-deps ${{ matrix.browsers }}
      - name: Run gate
        run: python -m pytest ${{ matrix.pytest }} -q
      - name: Upload visual diffs on failure
        if: failure() && matrix.upload_diffs == 'true'
        uses: actions/upload-artifact@v4
        with:
          name: visual-diffs
          path: tests/.diff/
          if-no-files-found: ignore
