#!/usr/bin/env bash # check-phase37: fail-closed verification gate for Phase 37 (Media Library: modules/album). # # Stages: # go go vet (default + summercms_mcp), go test -short ./..., and required # Phase 37 unit tests across album, cabana, pier (-tags summercms_mcp), and surf. # docker Docker host only: required Postgres integration tests in album and cabana. # admin npm run test (requiring Phase 37 Vitest files), typecheck, # scripts/check-admin-openapi.sh --check, and scripts/check-admin-dist.sh. # docs TestDocsTree and summer docs:build --check. # consumers Read-only local proofs against ../sm-journal-plugin (scratch modfile # pointing to this checkout + go vet + Go media/enrichment tests + Vitest) # and ../vue-journal-layer (Vitest), verifying clean sibling git status # before and after. # marker No framework lines added since PHASE37_BASE_TAG under modules/, cmd/, # internal/, or docs/ name a consuming application. # # Usage: # scripts/check-phase37.sh [--go] [--docker] [--admin] [--docs] [--consumers] [--marker] # scripts/check-phase37.sh --all set -euo pipefail unset FORCE_COLOR export NO_COLOR=1 ROOT="${PHASE37_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" SIBLINGS="${PHASE37_SIBLINGS:-$(dirname "$ROOT")}" JOURNAL_PLUGIN="${JOURNAL_PLUGIN:-$SIBLINGS/sm-journal-plugin}" VUE_JOURNAL_LAYER="${VUE_JOURNAL_LAYER:-$SIBLINGS/vue-journal-layer}" PHASE37_BASE_TAG="${PHASE37_BASE_TAG:-v0.6.9}" GO_TESTS=( ./modules/album:TestSlugAndShortID ./modules/album:TestWebPVariants ./modules/album:TestStaticHandlerSecurity ./modules/album:TestDecompressionBombAndPolyglotGuards ./modules/album:TestConfigAndInstallHelpers ./modules/cabana:TestMediaFinderFieldSchema ./modules/cabana:TestMediaMCPPluginSurfaceAndScopes ./modules/cabana:TestTokenRouteGates ./modules/cabana:TestPhase10ConformanceInventory ./modules/surf:TestMediaMount ) GO_MCP_TESTS=( ./modules/pier:TestMediaMCPTools ) DOCKER_TESTS=( ./modules/album:TestMigrationsUpDown ./modules/album:TestFolderCRUDAndNonEmptyDelete ./modules/album:TestUploadAndNoDedup ./modules/album:TestConsumeUploadTokenSingleUse ./modules/album:TestDeleteItemInTxAfterCommit ./modules/album:TestGetItemsByShortIDsAndListFilters ./modules/album:TestBackfillMailImages ./modules/cabana:TestMediaPermissionsMatrix ./modules/cabana:TestMediaTokenScopes ./modules/cabana:TestMediaUploadLifecycleAndAfterCommitDelete ./modules/cabana:TestMailImagesUnifiedOverAlbum ./modules/cabana:TestPhase10OpenAPIConformance ) ADMIN_FILES=( tests/media/MediaBrowser.test.ts tests/media/MediaPickerModal.test.ts tests/form/MediaFinderField.test.ts tests/form/MailImagePicker.test.ts ) APP_NAMES=("grzy""by" "fono""teka" "p[lł]yta""rium" "summercms""io" "sm-demo""-app" "sm-bm""-app") usage() { cat >&2 <<'EOF' usage: check-phase37.sh [--go] [--docker] [--admin] [--docs] [--consumers] [--marker] check-phase37.sh --all EOF exit 2 } 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() { python3 - "$1" <<'PY' import os, re, sys text = open(sys.argv[1], encoding="utf-8", errors="replace").read() text = re.sub(r"\x1b\[[0-9;]*m", "", text) need = os.environ.get("REQUIRE_FILES", "").split() summary = re.findall(r"^\s*(Test Files|Tests)\s+(.*)$", text, re.M) if any(re.search(r"\b(failed|skipped|todo)\b", rest) for _, rest in summary) or re.search(r"^\s*(FAIL|×)\s", text, re.M): print("refuse: vitest reports failed, skipped or todo tests", file=sys.stderr) sys.exit(1) m = re.search(r"^\s*Tests\s+(\d+) passed \((\d+)\)\s*$", text, re.M) if not m or m.group(1) != m.group(2) or m.group(1) == "0": print("refuse: no all-passing vitest summary", file=sys.stderr) sys.exit(3) missing = [f for f in need if not re.search(r"^\s*✓ (?:\|\w+\| )?" + re.escape(f) + r" \(\d+ tests?\)", text, re.M)] if missing: print("refuse: did not pass: " + ", ".join(missing), file=sys.stderr) sys.exit(5) PY } detect_tap() { python3 - "$1" <<'PY' import re, sys text = open(sys.argv[1], encoding="utf-8", errors="replace").read() counts = dict((k, int(v)) for k, v in re.findall(r"^# (pass|fail|skipped|todo|cancelled) (\d+)\s*$", text, re.M)) if re.search(r"^\s*not ok\b", text, re.M) or any(counts.get(k, 0) for k in ("fail", "skipped", "todo", "cancelled")): print("refuse: TAP reports failed, skipped, todo or cancelled tests", file=sys.stderr) sys.exit(1) if counts.get("pass", 0) == 0: print("refuse: TAP reports no passing test", file=sys.stderr) sys.exit(3) 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 python3 -c 'import json,sys for raw in open(sys.argv[1], errors="replace"): try: ev = json.loads(raw) except ValueError: continue out = ev.get("Output", "") if ev.get("Action") == "output" and ("FAIL" in out or "SKIP" in out or "_test.go:" in out): sys.stderr.write(out)' "$log" 2>&1 | tail -n 40 >&2 || true rm -f "$log" "$err" echo "refuse: FAIL 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() { local dir="$1" pkg="$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" "$pkg" } go_pkg_named() { local dir="$1" flags="$2" shift 2 local entry pkg name pkgs=() declare -A by=() for entry in "$@"; do pkg="${entry%%:*}" name="${entry#*:}" if [[ -z "${by[$pkg]+x}" ]]; then pkgs+=("$pkg") by[$pkg]="$name" else by[$pkg]="${by[$pkg]} $name" fi done for pkg in "${pkgs[@]}"; do # shellcheck disable=SC2086 go_named "$dir" "$pkg" "$flags" ${by[$pkg]} done } require_dir() { [[ -d "$1" ]] || { echo "refuse: missing $1 ($2)" >&2 return 1 } } sibling_clean() { local dir="$1" when="$2" status if ! status="$(git -c safe.directory='*' -C "$dir" status --porcelain 2>&1)"; then printf '%s\n' "$status" >&2 echo "refuse: git status failed in $dir ($when)" >&2 return 1 fi if [[ -n "$status" ]]; then printf '%s\n' "$status" | head -n 20 >&2 echo "refuse: $dir is not clean $when the consumers stage" >&2 return 1 fi echo "clean: $dir ($when)" } SCRATCH="" scratch_cleanup() { if [[ -n "$SCRATCH" && -d "$SCRATCH" ]]; then chmod -R u+w -- "${SCRATCH:?}" 2>/dev/null || true rm -rf -- "${SCRATCH:?}" fi } scratch_init() { SCRATCH="$(mktemp -d)" trap scratch_cleanup EXIT } consumer_modfile() { local dir="$1" name="$2" out out="$SCRATCH/modfile/$name" mkdir -p "$out" cp -- "$dir/go.mod" "$out/go.mod" if ! grep -q "replace git.golem15.com/golem15/summercms =>" "$out/go.mod"; then printf '\nreplace git.golem15.com/golem15/summercms => %s\n' "$ROOT" >>"$out/go.mod" else python3 - "$out/go.mod" "$ROOT" <<'PY' import re, sys p, root = sys.argv[1], sys.argv[2] text = open(p, encoding="utf-8").read() text = re.sub(r"replace\s+git\.golem15\.com/golem15/summercms\s+=>\s+\S+", f"replace git.golem15.com/golem15/summercms => {root}", text) open(p, "w", encoding="utf-8").write(text) PY fi { [[ -f "$dir/go.sum" ]] && cat -- "$dir/go.sum" [[ -f "$ROOT/go.sum" ]] && cat -- "$ROOT/go.sum" } | LC_ALL=C sort -u >"$out/go.sum" printf '%s\n' "$out/go.mod" } workspace_check() { local dir="$1" flags="${2:-}" got rgot rwant # shellcheck disable=SC2086 got="$( (cd "$dir" && go list $flags -m -f '{{if .Replace}}{{.Replace.Dir}}{{else}}{{.Dir}}{{end}}' git.golem15.com/golem15/summercms 2>/dev/null) || true)" rgot="$(realpath -e -- "$got" 2>/dev/null || true)" rwant="$(realpath -e -- "$ROOT" 2>/dev/null || true)" if [[ -z "$got" || -z "$rgot" || "$rgot" != "$rwant" ]]; then echo "refuse: $dir resolves git.golem15.com/golem15/summercms to ${got:-}, not $ROOT" >&2 return 1 fi echo "workspace: $dir resolves framework to $rwant" } docker_up() { command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1 } run_go() { (cd "$ROOT" && go vet ./...) echo "vet: $ROOT (./...)" (cd "$ROOT" && go vet -tags summercms_mcp ./...) echo "vet: $ROOT (-tags summercms_mcp ./...)" (cd "$ROOT" && go test -short -count=1 ./...) echo "short: $ROOT (./...)" go_pkg_named "$ROOT" "-short" "${GO_TESTS[@]}" go_pkg_named "$ROOT" "-short -tags summercms_mcp" "${GO_MCP_TESTS[@]}" echo "phase37 go passed" } run_docker() { if ! docker_up; then echo "refuse: the docker stage needs a reachable Docker daemon (testcontainers)" >&2 return 1 fi go_pkg_named "$ROOT" "-timeout 15m" "${DOCKER_TESTS[@]}" echo "phase37 docker passed" } docker_gate() { if docker_up; then if bash "${BASH_SOURCE[0]}" --docker; then echo "PASS docker" return 0 fi echo "FAIL docker" return 1 fi echo "NOT RUN docker: no reachable Docker daemon on this host" >&2 if [[ "${PHASE37_ALLOW_NO_DOCKER:-0}" != "1" ]]; then echo "FAIL docker: set PHASE37_ALLOW_NO_DOCKER=1 or run --docker on a Docker host" >&2 return 1 fi } run_admin() { require_dir "$ROOT/admin" "admin" local log rc=0 dc=0 log="$(mktemp)" npm --prefix "$ROOT/admin" run test >"$log" 2>&1 || rc=$? REQUIRE_FILES="${ADMIN_FILES[*]}" detect_vitest "$log" || dc=$? if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then grep -E '(FAIL|×|Error|expected)' "$log" | head -n 40 >&2 || true tail -n 20 "$log" >&2 rm -f "$log" echo "refuse: FAIL npm run test in admin (test=$rc detect=$dc)" >&2 return 1 fi echo "vitest (admin): $(grep -E '^\s*Tests\s' "$log" | tr -s ' ')" rm -f "$log" npm --prefix "$ROOT/admin" run typecheck >/dev/null echo "typecheck: admin" (cd "$ROOT" && bash scripts/check-admin-openapi.sh --check) (cd "$ROOT" && bash scripts/check-admin-dist.sh) echo "phase37 admin passed" } run_docs() { go_named "$ROOT" ./cmd/summer "" TestDocsTree (cd "$ROOT" && go run ./cmd/summer docs:build --check) echo "phase37 docs passed" } run_consumers() { require_dir "$JOURNAL_PLUGIN" "sm-journal-plugin" require_dir "$VUE_JOURNAL_LAYER" "vue-journal-layer" scratch_init sibling_clean "$JOURNAL_PLUGIN" before sibling_clean "$VUE_JOURNAL_LAYER" before export GOWORK=off GOPROXY=off local jmod mflags jmod="$(consumer_modfile "$JOURNAL_PLUGIN" sm-journal-plugin)" mflags="-mod=mod -modfile=$jmod" workspace_check "$JOURNAL_PLUGIN" "$mflags" # shellcheck disable=SC2086 (cd "$JOURNAL_PLUGIN" && go vet $mflags ./...) echo "vet: $JOURNAL_PLUGIN" # shellcheck disable=SC2086 (cd "$JOURNAL_PLUGIN" && go test $mflags -short -count=1 ./...) echo "short: $JOURNAL_PLUGIN" local log rc=0 dc=0 log="$(mktemp)" npm --prefix "$JOURNAL_PLUGIN" test >"$log" 2>&1 || rc=$? detect_vitest "$log" || dc=$? if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then tail -n 30 "$log" >&2 rm -f "$log" echo "refuse: FAIL npm test in $JOURNAL_PLUGIN" >&2 return 1 fi echo "vitest (sm-journal-plugin): $(grep -E '^\s*Tests\s' "$log" | tr -s ' ')" rm -f "$log" log="$(mktemp)" rc=0 dc=0 npm --prefix "$VUE_JOURNAL_LAYER" test >"$log" 2>&1 || rc=$? detect_tap "$log" || dc=$? if [[ "$rc" -ne 0 || "$dc" -ne 0 ]]; then tail -n 30 "$log" >&2 rm -f "$log" echo "refuse: FAIL npm test in $VUE_JOURNAL_LAYER" >&2 return 1 fi echo "tap (vue-journal-layer): $(grep -E '^# pass ' "$log" | tail -n 1)" rm -f "$log" sibling_clean "$JOURNAL_PLUGIN" after sibling_clean "$VUE_JOURNAL_LAYER" after echo "phase37 consumers passed" } run_marker() { if ! git -C "$ROOT" rev-parse --verify -q "$PHASE37_BASE_TAG^{commit}" >/dev/null; then echo "refuse: missing tag $PHASE37_BASE_TAG in $ROOT" >&2 return 1 fi local pattern hits pattern="$(IFS='|'; echo "${APP_NAMES[*]}")" hits="$(git -C "$ROOT" diff -U0 "$PHASE37_BASE_TAG" HEAD -- \ 'modules/*.go' 'modules/**/*.go' 'cmd/**/*.go' 'internal/**/*.go' \ 'modules/**/README.md' 'cmd/**/README.md' 'internal/**/README.md' 'docs' \ ':(exclude)*_test.go' ':(exclude)**/*_test.go' ':(exclude)**/testdata/**' | PATTERN="$pattern" python3 -c ' import os, re, sys pat = re.compile(os.environ["PATTERN"], re.I) path, line = "", 0 for raw in sys.stdin: if raw.startswith("+++ "): path = raw[6:].rstrip("\n") if raw.startswith("+++ b/") else "" continue m = re.match(r"^@@ -\S+ \+(\d+)", raw) if m: line = int(m.group(1)) continue if raw.startswith("+") and not raw.startswith("+++"): if path and pat.search(raw[1:]): print(f"{path}:{line}: {raw[1:].strip()[:120]}") line += 1 ')" if [[ -n "$hits" ]]; then printf '%s\n' "$hits" | head -n 40 >&2 echo "refuse: framework lines added since $PHASE37_BASE_TAG name a consuming application" >&2 return 1 fi echo "marker: no consuming-app names added since $PHASE37_BASE_TAG" echo "phase37 marker passed" } run_all() { local stage for stage in go admin docs consumers marker; do if bash "${BASH_SOURCE[0]}" "--$stage"; then echo "PASS $stage" else echo "FAIL $stage" exit 1 fi done docker_gate || exit 1 echo "Phase 37 gate passed" } run_stage() { case "$1" in --go | go) run_go ;; --docker | docker) run_docker ;; --docker-gate) docker_gate ;; --admin | admin) run_admin ;; --docs | docs) run_docs ;; --consumers | consumers) run_consumers ;; --marker | marker) run_marker ;; --all | all) run_all ;; *) usage ;; esac } if [[ "$#" -eq 0 ]]; then usage fi if [[ "$#" -eq 1 ]]; then run_stage "$1" exit 0 fi for arg in "$@"; do if bash "${BASH_SOURCE[0]}" "$arg"; then echo "PASS ${arg#--}" else echo "FAIL ${arg#--}" exit 1 fi done