#!/bin/bash set -euo pipefail # Brainy Vitest Verdict Check # Confirms a vitest run's own summary line is trustworthy before anything # downstream treats a green run as green. See scripts/gate/README.md for why # (the 2026-08-13 lost-day ledger). # # Usage: # vitest-verdict-check.sh # vitest-verdict-check.sh --count-tests # # The first form checks the "Test Files" summary line's total against an # exact expected count. The second checks the "Tests" summary line's total # against a minimum. Both also fail on any sign the worker pool died # mid-run, whether or not a summary line still made it into the log. # # Exit 0 and print one OK line per passing check when the log is clean. # Exit 1 and print one FATAL line per violation, quoting the exact line or # string that tripped it, when it is not. # # Known trap (shared with gate-preflight.sh): every helper below ends on an # explicit `return 0` as its own statement, never on a loop or test, so a # helper's last command can never hand its own exit status back as the # function's under `set -e`. The same applies to `var=$(cmd)` assignments # mid-helper: a bare assignment IS checked by `set -e`, so a `grep` that # legitimately finds nothing (exit 1) would otherwise kill the script # instead of just leaving the variable empty — every such assignment below # is paired with an explicit `|| true` inside the substitution. usage() { echo "Usage: $0 " echo " $0 --count-tests " exit 1 } MODE="files" if [ "${1:-}" = "--count-tests" ]; then MODE="tests" shift fi LOG_FILE="${1:-}" THRESHOLD="${2:-}" if [ -z "$LOG_FILE" ] || [ -z "$THRESHOLD" ]; then usage fi if [ ! -f "$LOG_FILE" ]; then echo "FATAL: log file '${LOG_FILE}' does not exist" exit 1 fi if ! [[ "$THRESHOLD" =~ ^[0-9]+$ ]]; then echo "FATAL: threshold '${THRESHOLD}' is not a non-negative integer" exit 1 fi VIOLATIONS=0 fatal() { echo "FATAL: $1" VIOLATIONS=$((VIOLATIONS + 1)) } ok() { echo "OK: $1" } # Vitest colorizes its summary with ANSI escapes; strip them before parsing # anything, or the color codes end up embedded in the fields we grep for. CLEAN_LOG="$(sed 's/\x1b\[[0-9;]*m//g' "$LOG_FILE")" # Worker-pool death: if either string appears, the run's own summary line — # even if present and even if its numbers look fine — cannot be trusted, # because the process died mid-suite and vitest's own accounting is what # died with it. check_worker_death() { if echo "$CLEAN_LOG" | grep -q "Unhandled Error"; then fatal "log contains 'Unhandled Error' — worker pool died mid-run" fi if echo "$CLEAN_LOG" | grep -q "Timeout calling"; then fatal "log contains 'Timeout calling' — worker pool died mid-run" fi return 0 } # Shared shape between the "Test Files" and "Tests" summary lines: #