diff --git a/scripts/check-phase18.sh b/scripts/check-phase18.sh new file mode 100755 index 0000000..4766b51 --- /dev/null +++ b/scripts/check-phase18.sh @@ -0,0 +1,570 @@ +#!/usr/bin/env bash +# Phase 18 fail-closed gate: payment gateway. The framework pieces in this +# repository (cabana money field and column, write-only settings secrets, +# richeditor and tabs on settings forms, danger record actions, the preview +# Edit rule, the admin SPA), the two plugin repositories (sm-paymentgateway- +# plugin, sm-pgstripe-plugin, whose proof host is a test inside pgstripe) and +# the Nuxt layer vue-paymentgateway-layer. +# +# Stages: +# go go vet ./... here; the Docker-free framework suites by required +# name (-short). +# plugins go vet ./... and go test -short ./... in both plugins; their +# Docker-free suites by required name and package (-short). +# admin the admin SPA typecheck and the vitest JSON report with required +# spec files and test names. +# layers pnpm test (node --test, TAP) in the layer with required test +# files and names. +# docs TestDocsTree, summer docs:build --check, check-admin-openapi.sh +# --check, check-admin-dist.sh, and check-phase10.sh --hygiene, so +# admin changes made here cannot silently break Phase 10's gate. +# postgres Docker host only: the testcontainers suites by required name in +# every Go repository. Without a reachable Docker daemon this +# stage fails. +# +# Every stage exits non-zero on a failing command; a go test -json run that +# fails, skips, matches zero tests, prints "no tests to run" or misses a named +# required test; a vitest JSON report with a failed, skipped or todo test, a +# failed file, no passing test or a missing required file or test; a TAP run +# with a "not ok", a fail, cancelled, skipped or todo count, zero passing tests +# or a missing required test; or a missing required layer test file. +# --self-test proves the detectors fail on planted output. +# +# --all runs self-test, go, plugins, admin, layers and docs, then postgres +# when a Docker daemon answers. When none does, --all says so loudly +# (NOT RUN postgres) and the Docker suites stay a manual step for a Docker +# host: +# bash scripts/check-phase18.sh postgres +# Set PHASE18_REQUIRE_POSTGRES=1 to make --all fail instead. +# +# Environment overrides (default: the sibling checkouts next to this +# repository): PHASE18_ROOT (this repository), PG (sm-paymentgateway-plugin), +# PGSTRIPE (sm-pgstripe-plugin), PG_LAYER (vue-paymentgateway-layer). +set -euo pipefail + +unset FORCE_COLOR +export NO_COLOR=1 + +ROOT="${PHASE18_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" +PG="${PG:-$ROOT/../sm-paymentgateway-plugin}" +PGSTRIPE="${PGSTRIPE:-$ROOT/../sm-pgstripe-plugin}" +PG_LAYER="${PG_LAYER:-$ROOT/../vue-paymentgateway-layer}" + +# --------------------------------------------------------------------------- +# Required test names. Each list is run with -run '^(a|b|...)$' and every name +# must pass; a name that skips or does not run fails the stage. + +# go stage: Docker-free framework suites (-short). +CABANA_SHORT_TESTS=(TestMoneyFieldCompile TestMoneyListCompile TestSettingsSecretCompile TestSettingsDropdownMethod) + +# plugins stage: Docker-free suites (-short), per repository and package. +PG_MONEY_TESTS=(TestMoneyVectorsSmoke TestAmount4ScanValue) +PG_ROOT_TESTS=(TestNotificationRoute) +PGSTRIPE_ROOT_TESTS=(TestStripeWebhook TestFixtureAbsentFromProduction) + +# postgres stage: testcontainers suites. +PG_CABANA_TESTS=(TestMoneyFieldSmoke) +PG_PG_TESTS=(TestPaymentGatewayTracer) +PG_PGSTRIPE_TESTS=(TestHostStripeFlow) + +# admin stage: vitest spec files and full test names (describe + it). +ADMIN_FILES=( + tests/form/MoneyField.test.ts +) +ADMIN_REQUIRE=( + "money field component (UI-SPEC F1) submits an untouched 85.4193 byte-identical: no emit on load or blur" + "money field component (UI-SPEC F1) emits the canonical dot string for \"1 234,56\" and null for empty" + "money field component (UI-SPEC F1) refuses 12.34567 at scale 4 on blur, neither rounded nor truncated" + "money helpers formats through Intl with the string itself, keeping four decimals" +) + +# layers stage: test files (must exist and match the test script's glob) and +# TAP test names. +LAYER_FILES=(tests/checkout.test.ts tests/orderPage.test.ts) +LAYER_REQUIRE=( + "a redirect answer calls location.assign once with the URL and the button stays busy" + "an AwaitingPayment order is polled every 3 s and polling stops at Paid" +) + +usage() { + cat >&2 <<'EOF' +usage: + check-phase18.sh --self-test + check-phase18.sh go | plugins | admin | layers | docs | postgres + check-phase18.sh --go | --plugins | --admin | --layers | --docs | --postgres + check-phase18.sh --all +EOF + exit 2 +} + +# detect_go reads go test -json. Exit 1 fail/build, 2 skip, 3 zero/no-tests, +# 4 non-JSON, 5 missing required name. +detect_go() { + python3 - "$1" <<'PY' +import json, os, sys +path = sys.argv[1] +require = [n for n in os.environ.get("REQUIRE_TESTS", "").split() if n] +passed = set() +failed = [] +with open(path, encoding="utf-8", errors="replace") as fh: + for raw in fh: + line = raw.strip() + if not line.startswith("{"): + continue + try: + ev = json.loads(line) + except json.JSONDecodeError: + print("refuse: non-json test output", file=sys.stderr) + sys.exit(4) + action = ev.get("Action") + test = ev.get("Test") or "" + pkg = ev.get("Package") or ev.get("ImportPath") or "" + if action == "build-fail" or (action == "fail" and ev.get("FailedBuild")): + print(f"refuse: build failed {pkg}", file=sys.stderr) + sys.exit(1) + text = ev.get("Output") or "" + if action == "output" and "no tests to run" in text: + print(f"refuse: no tests to run in {pkg}", file=sys.stderr) + sys.exit(3) + if action == "skip" and test: + print(f"refuse: skipped {pkg} {test}", file=sys.stderr) + sys.exit(2) + if action == "fail": + failed.append(f"{pkg} {test}".strip()) + if action == "pass" and test: + passed.add(test) +if failed: + print("refuse: failed " + ", ".join(failed), file=sys.stderr) + sys.exit(1) +if not passed: + print("refuse: zero tests", file=sys.stderr) + sys.exit(3) +missing = [n for n in require if n not in passed] +if missing: + print("refuse: required tests did not pass: " + ", ".join(missing), file=sys.stderr) + sys.exit(5) +PY +} + +# detect_vitest reads a vitest --reporter=json report. Exit 1 a failed test +# or file or an unsuccessful run, 2 a skipped or todo test, 3 no passing +# test, 4 an unreadable report, 5 a missing required file or test. +# REQUIRE_FILES and REQUIRE_NAMES hold one entry per line. +detect_vitest() { + python3 - "$1" <<'PY' +import json, os, sys +path = sys.argv[1] +files = [n for n in os.environ.get("REQUIRE_FILES", "").split("\n") if n.strip()] +names = [n for n in os.environ.get("REQUIRE_NAMES", "").split("\n") if n.strip()] +try: + with open(path, encoding="utf-8") as fh: + report = json.load(fh) +except (OSError, ValueError) as err: + print(f"refuse: unreadable vitest report: {err}", file=sys.stderr) + sys.exit(4) +results = report.get("testResults") or [] +bad = [r.get("name", "?") for r in results if r.get("status") != "passed"] +if bad or report.get("numFailedTests", 0) or report.get("numFailedTestSuites", 0) or report.get("success") is not True: + print("refuse: failed " + ", ".join(bad or ["run"]), file=sys.stderr) + sys.exit(1) +if report.get("numPendingTests", 0) or report.get("numTodoTests", 0): + print(f"refuse: {report.get('numPendingTests', 0)} skipped, {report.get('numTodoTests', 0)} todo", file=sys.stderr) + sys.exit(2) +passed = set() +by_file = {} +for r in results: + for a in r.get("assertionResults") or []: + status = a.get("status") + if status != "passed": + print(f"refuse: {status} {a.get('fullName')}", file=sys.stderr) + sys.exit(2 if status in ("skipped", "pending", "todo") else 1) + passed.add(a.get("fullName", "")) + by_file[r.get("name", "")] = by_file.get(r.get("name", ""), 0) + 1 +if report.get("numPassedTests", 0) < 1 or not passed: + print("refuse: no passing tests", file=sys.stderr) + sys.exit(3) +missing = [f for f in files if not any(name.endswith("/" + f) and count > 0 for name, count in by_file.items())] +missing += [n for n in names if n not in passed] +if missing: + print("refuse: required files or tests did not pass: " + "; ".join(missing), file=sys.stderr) + sys.exit(5) +print(f"vitest: {report.get('numPassedTests')} passed in {len(results)} files") +PY +} + +# detect_tap reads node --test --test-reporter=tap output. Exit 1 "not ok" or +# a fail/cancelled count, 2 a skip/todo directive or count, 3 no passing test, +# 5 a missing required name. REQUIRE_NAMES holds one test name per line. +detect_tap() { + python3 - "$1" <<'PY' +import os, re, sys +path = sys.argv[1] +require = [n for n in os.environ.get("REQUIRE_NAMES", "").split("\n") if n.strip()] +passed = set() +counts = {} +with open(path, encoding="utf-8", errors="replace") as fh: + for raw in fh: + line = raw.rstrip("\n") + body = line.lstrip() + if body.startswith("not ok"): + print(f"refuse: {body}", file=sys.stderr) + sys.exit(1) + m = re.match(r"ok \d+ - (.*?)(\s+#\s*(SKIP|TODO)\b.*)?$", body, re.I) + if m: + if m.group(2): + print(f"refuse: {body}", file=sys.stderr) + sys.exit(2) + passed.add(m.group(1).replace("\\#", "#")) + continue + m = re.match(r"# (tests|suites|pass|fail|cancelled|skipped|todo) (\d+)$", line) + if m: + counts[m.group(1)] = int(m.group(2)) +for key in ("fail", "cancelled"): + if counts.get(key, 0) > 0: + print(f"refuse: {counts[key]} {key}", file=sys.stderr) + sys.exit(1) +for key in ("skipped", "todo"): + if counts.get(key, 0) > 0: + print(f"refuse: {counts[key]} {key}", file=sys.stderr) + sys.exit(2) +if counts.get("pass", 0) < 1 or not passed: + print("refuse: no passing tests", file=sys.stderr) + sys.exit(3) +missing = [n for n in require if n not in passed] +if missing: + print("refuse: required tests did not pass: " + "; ".join(missing), file=sys.stderr) + sys.exit(5) +PY +} + +# detect_files fails (exit 5) unless every REQUIRE_FILES entry (one per line, +# relative to the directory given) is a non-empty regular file whose name +# ends in .test.ts, the glob of the layer's test script. +detect_files() { + python3 - "$1" <<'PY' +import os, sys +base = sys.argv[1] +files = [n for n in os.environ.get("REQUIRE_FILES", "").split("\n") if n.strip()] +missing = [f for f in files if not (f.startswith("tests/") and f.endswith(".test.ts") and os.path.isfile(os.path.join(base, f)) and os.path.getsize(os.path.join(base, f)) > 0)] +if missing: + print("refuse: required test files missing in " + base + ": " + ", ".join(missing), file=sys.stderr) + sys.exit(5) +PY +} + +go_json() { + local dir="$1" + shift + local log err rc=0 dc=0 + log="$(mktemp)" + err="$(mktemp)" + (cd "$dir" && go test -json "$@") >"$log" 2>"$err" || rc=$? + detect_go "$log" || dc=$? + if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then + cat "$err" >&2 || true + grep -v '^{' "$log" | tail -n 40 >&2 || true + python3 -c 'import json,sys +for raw in open(sys.argv[1], errors="replace"): + try: ev = json.loads(raw) + except ValueError: continue + if ev.get("Action") == "output" and ("FAIL" in ev.get("Output", "") or "_test.go:" in ev.get("Output", "")): + sys.stderr.write(ev["Output"])' "$log" 2>&1 | tail -n 40 >&2 || true + rm -f "$log" "$err" + echo "refuse: go test $* in $dir (test=$rc detect=$dc)" >&2 + return 1 + fi + echo "go: $(grep -c '"Action":"pass","Package":"[^"]*","Test":"[^"/]*","Elapsed"' "$log") top-level tests passed in $dir ($*)" + rm -f "$log" "$err" +} + +# go_named DIR PACKAGES FLAGS NAME... runs exactly the named top-level tests +# and requires every one of them to pass. PACKAGES and FLAGS are word-split. +go_named() { + local dir="$1" pkgs="$2" flags="$3" + shift 3 + local names=("$@") regex + regex="^($(IFS='|'; echo "${names[*]}"))\$" + # shellcheck disable=SC2086 + REQUIRE_TESTS="${names[*]}" go_json "$dir" $flags -count=1 -run "$regex" $pkgs +} + +# vitest_json DIR [VITEST ARGS...]: extra arguments (such as --config) go to vitest. +vitest_json() { + local dir="$1" report out rc=0 dc=0 + shift + report="$(mktemp)" + out="$(mktemp)" + (cd "$dir" && ./node_modules/.bin/vitest run "$@" --reporter=json --outputFile="$report") >"$out" 2>&1 || rc=$? + detect_vitest "$report" || dc=$? + if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then + grep -E 'FAIL|Error|×' "$out" | tail -n 40 >&2 || true + rm -f "$report" "$out" + echo "refuse: vitest in $dir (test=$rc detect=$dc)" >&2 + return 1 + fi + rm -f "$report" "$out" +} + +# pnpm_tap runs `pnpm -C