name: Validate 485Tech

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

permissions:
  contents: read

concurrency:
  group: validate-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  static-and-worker:
    name: Static, contract and Worker checks
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Install dependencies
        run: npm ci

      - name: Build generated assets
        run: npm run build

      - name: Check generated tree is committed
        run: git diff --exit-code

      - name: Validate tools and capability contract
        run: npm run validate:tools

      - name: Validate project documentation handoff
        run: npm run validate:docs

      - name: Validate syntax
        run: node scripts/validate_syntax.mjs

      - name: Unit app checks
        run: node scripts/unit-app-test.mjs

      - name: Background remover integration
        run: node scripts/bg-remover-test.mjs

      - name: Worker security and route smoke
        run: node scripts/worker-smoke.mjs

      - name: Check whitespace
        run: git diff --check

  browser-regression:
    name: Browser and all-tool regression
    runs-on: ubuntu-latest
    timeout-minutes: 35
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Install dependencies
        run: npm ci

      - name: Install Chromium for Playwright
        run: npx playwright install --with-deps chromium

      - name: Build generated assets
        run: npm run build

      - name: Start local preview server
        run: |
          npm run preview > /tmp/485tech-preview.log 2>&1 &
          for attempt in {1..30}; do
            if curl --fail --silent http://127.0.0.1:8788/tools >/dev/null; then
              exit 0
            fi
            sleep 1
          done
          cat /tmp/485tech-preview.log
          exit 1

      - name: Smoke every live tool
        run: node scripts/smoke_all_tools.mjs

      - name: Representative functional audit
        run: node scripts/functional_extra.mjs

      - name: Functional readiness matrix
        run: node scripts/functional-matrix.mjs

      - name: Deep interaction sweep
        run: node scripts/deep-tool-sweep.mjs

      - name: UI regression
        run: node scripts/ui-regression.mjs

      - name: Named snapshot viewport QA
        run: node scripts/qa-json-snapshots.mjs

      - name: Capability details QA
        run: node scripts/qa-capability-details.mjs
