name: Prototype and iframe contracts

on:
  workflow_dispatch:
    inputs:
      suite:
        description: Prototype family to exercise
        required: true
        default: all
        type: choice
        options:
          - all
          - core
          - iframe
          - narrative
      confirm_run:
        description: Acquire a standard runner for this selected suite
        required: true
        default: false
        type: boolean
      include_browser:
        description: Exercise the selected rendered-page contracts with Chromium
        required: true
        default: false
        type: boolean
  pull_request:
    branches:
      - main
    types:
      - opened
      - synchronize
      - reopened
      - ready_for_review
    paths:
      - ".github/workflows/prototype-unit-tests.yml"
      - "package.json"
      - "package-lock.json"
      - "AUTHORS"
      - "CITATION.cff"
      - "GOVERNANCE.md"
      - "LICENSE"
      - "attribution.yml"
      - "tests/attribution/**"
      - "tests/core-architectures/**"
      - "tests/context-canvas/iframe-carriers.test.mjs"
      - "tests/iframe-routes/**"
      - "tests/narrative-architectures/**"
      - "tools/core-architecture-browser-helper.mjs"
      - "tools/core-decision-canvas-contract.mjs"
      - "tools/core-iframe-carrier-contract.mjs"
      - "tools/core-planning-instrument-contract.mjs"
      - "tools/core-reference-instrument-contract.mjs"
      - "tools/canvas-iframe-route-browser-contract.mjs"
      - "tools/iframe-route-package-engine.mjs"
      - "tools/materialize-public-references.mjs"
      - "tools/package-canvas-iframe-routes.mjs"
      - "tools/package-iframe-routes.mjs"
      - "tools/package-v1-iframe-routes.mjs"
      - "tools/verify-live-iframe-routes.mjs"
      - "tools/attribution-contract.mjs"
      - "tools/cross-prototype-exchange-contract.mjs"
      - "tools/narrative-architecture-contract.mjs"
      - "core-architectures-chimeric-prototype-current-pack-v0.1/**"
      - "iframe-route-families/**"
      - "participatory-design-kit-chimeric-prototype-set-v0.1/**"
      - "collaboration/**"
      - "context-canvas-project-surface/**/*.html"
      - "context-canvas-project-surface/**/*.js"
      - "context-canvas-project-surface/**/*.mjs"
      - "visualization-fidelity-atlas-bundle-v0.1/**"

permissions:
  contents: read

concurrency:
  group: prototype-units-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  prototype-units:
    name: Selected prototype or iframe contract
    if: >-
      (github.event_name == 'workflow_dispatch' && inputs.confirm_run) ||
      (github.event_name == 'pull_request' &&
       github.event.pull_request.draft == false &&
       vars.PROTOTYPE_UNIT_TESTS_AUTOMATIC == 'true')
    runs-on: ubuntu-24.04
    timeout-minutes: 10

    steps:
      - name: Check out the candidate source
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
        with:
          persist-credentials: false

      - name: Use the reviewed Node runtime
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
        with:
          node-version: "22"

      - name: Install the locked root toolchain
        run: npm ci --ignore-scripts

      - name: Install Chromium for the selected rendered-page contracts
        if: ${{ github.event_name == 'pull_request' || inputs.include_browser }}
        run: npm exec -- playwright install --with-deps chromium

      - name: Exercise the selected source contracts
        env:
          SELECTED_SUITE: ${{ inputs.suite || 'all' }}
          INCLUDE_BROWSER: ${{ github.event_name == 'pull_request' || inputs.include_browser }}
        shell: bash
        run: |
          set -euo pipefail

          node --test tests/context-canvas/iframe-carriers.test.mjs

          case "$SELECTED_SUITE" in
            core)
              npm run test:attribution
              npm run test:iframe-routes
              npm run check:iframe-routes
              npm run test:core-architectures
              if [[ "$INCLUDE_BROWSER" == "true" ]]; then npm run test:core-browser; fi
              ;;
            iframe)
              npm run test:attribution
              npm run test:iframe-routes
              npm run check:iframe-routes
              if [[ "$INCLUDE_BROWSER" == "true" ]]; then npm run test:iframe-browser; fi
              ;;
            narrative)
              npm run test:narrative-architectures
              if [[ "$INCLUDE_BROWSER" == "true" ]]; then npm run test:narrative-browser; fi
              ;;
            all)
              npm run test:attribution
              npm run test:iframe-routes
              npm run check:iframe-routes
              npm run test:prototype-units
              if [[ "$INCLUDE_BROWSER" == "true" ]]; then
                npm run test:core-browser
                npm run test:canvas-iframe-routes
                npm run test:narrative-browser
                npm run test:prototype-exchanges
              fi
              ;;
            *)
              echo "Choose core, iframe, narrative, or all."
              exit 2
              ;;
          esac
