#!/usr/bin/env bash

set -euo pipefail

REPO_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"

run_module_test() {
  local module=$1
  shift
  (
    cd "$REPO_ROOT/$module"
    GOWORK=off go test "$@"
  )
}

# The ordinary module suites already run in go-ci. This gate names the current
# in-process M2d-B closure explicitly so removing a stage test cannot leave the
# tracking claim green by accident.
run_module_test nexuscrm-records . -run '^TestReadSelectedRecord' -count=1
run_module_test nexuscrm-analytics . -run '^TestReadSelectedRootProjection' -count=1
run_module_test nexuscrm-persistence . -run '^TestQueryRepositorySelectedRoot' -count=1

# Race the same executable seams, including concurrent cancellation and the
# real sql.DB-backed repository owner. No future wire, replica, or delegation
# implementation is simulated here.
run_module_test nexuscrm-records -race . -run '^TestReadSelectedRecord' -count=1
run_module_test nexuscrm-analytics -race . -run '^TestReadSelectedRootProjection' -count=1
run_module_test nexuscrm-persistence -race . -run '^TestQueryRepositorySelectedRoot' -count=1

# Keep short deterministic fuzz budgets in ordinary CI. Seed-corpus execution
# remains part of every normal records test; these invocations additionally
# mutate identifier/selection and typed-value shapes on every main build.
run_module_test nexuscrm-records . -run '^$' -fuzz '^FuzzNormalizeSelectedRootFieldsDeterministic$' -fuzztime=5s
run_module_test nexuscrm-records . -run '^$' -fuzz '^FuzzSelectedRootScalarProjectionDeterministic$' -fuzztime=5s
run_module_test nexuscrm-records . -run '^$' -fuzz '^FuzzReadSelectedRecordIdentifierAndSelectionOutcomeStable$' -fuzztime=5s

echo "selected-root M2d-B current-seam fault, fuzz, race, and resource tests passed"
