#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")/../.."

record_validation_event() {
  local event_status="$1"
  node ai/runtime/dist/cli.js validation full-event \
    --status "$event_status" \
    --command ai/commands/validate.sh \
    --runner validate.sh \
    --json >/dev/null 2>&1 || true
}

run() {
  echo
  echo "==> $*"
  "$@"
}

record_validation_event started
validation_exit=0
trap 'validation_exit=$?; if [[ "$validation_exit" -ne 0 ]]; then record_validation_event failed; fi; exit "$validation_exit"' EXIT

run yarn codegen
run yarn lint
run yarn format:check
run yarn build:packages
run yarn build:backend
run yarn build:frontend
run yarn test:backend
run yarn workspace backend test:e2e

# Frontend tests currently run non-interactively through Angular/Vitest.
# TODO: If `yarn test:frontend` is changed to watch mode or browser-interactive
# behavior, remove it from this script and document the replacement in
# ai/roles/qa.md.
run yarn test:frontend

run node ai/commands/runtime-bounded-maintenance.mjs --json

record_validation_event completed
trap - EXIT
