Files
joshuaandClaude Opus 5.5 c442fe0d42 crosscode script: deploy, status, install, adopt (Events 2.2.0, plan §4-5)
One script for installing, updating and moving church installs, replacing
hope-events' scripts/deploy.sh and install.sh:

- deploy <church> [events] / --every-church, with --version, --main, --check
  and --allow-local-changes: checks every product first (files changed by
  hand, Node, release requirements from WHATS-NEW.md headings), then each
  product in order with backup, checkout, install, migrate, build, PM2 reload
  and a /health version check; a failure rolls that product back and stops.
- status [<church>]: checked-out and latest release, /health, and the old
  folder kept after adopt.
- install <church> --products events --domain <address>: a new church under
  /var/www/sites/<church>, writing church.env.
- adopt <church> --events-dir <old> builds the monorepo checkout next to the
  running install and switches PM2 over; adopt --undo goes back to it.

Product hooks live in scripts/products/events.sh. shellcheck (lint:scripts)
and 47 bats tests (test:scripts) are part of npm run check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:17:06 +02:00

262 lines
8.7 KiB
Bash

#!/usr/bin/env bats
# The parts of the crosscode script that don't need a server: versions, tags, WHATS-NEW.md
# requirements, church.env and .env reading. Run with `npm run test:scripts`.
setup() {
# shellcheck source=../lib/common.sh
source "$BATS_TEST_DIRNAME/../lib/common.sh"
TMP="$BATS_TEST_TMPDIR"
}
# ── version_ge ──────────────────────────────────────────────────────────────────────────────
@test "version_ge compares x.y.z numerically" {
version_ge 1.2.0 1.2.0
version_ge 1.10.0 1.9.9
version_ge 2.0.0 1.99.99
run version_ge 1.9.0 1.10.0
[ "$status" -eq 1 ]
run version_ge 2.1.1 2.2.0
[ "$status" -eq 1 ]
}
@test "version_ge puts a prerelease below its release" {
version_ge 2.2.0 2.2.0-test
run version_ge 2.2.0-test 2.2.0
[ "$status" -eq 1 ]
version_ge 2.2.0-rc2 2.2.0-rc1
version_ge 2.2.0-test 2.1.9
}
@test "version_ge refuses something that isn't a version" {
run version_ge abc 1.0.0
[ "$status" -eq 2 ]
}
# ── tags ────────────────────────────────────────────────────────────────────────────────────
@test "latest_tag_from takes the newest release of that product only" {
run latest_tag_from events <<'EOF'
events@2.2.0
events@2.10.0
events@2.9.3
events@3.0.0-test
website@9.0.0
contracts@1.1.0
eventsx@4.0.0
EOF
[ "$status" -eq 0 ]
[ "$output" = "events@2.10.0" ]
}
@test "latest_tag_from is empty without releases" {
run latest_tag_from events <<'EOF'
events@2.2.0-test
contracts@1.0.0
EOF
[ "$output" = "" ]
}
@test "latest_tag and checkout_ref read a real checkout" {
git init -q "$TMP/repo"
cd "$TMP/repo"
git -c user.name=t -c user.email=t@t commit -q --allow-empty -m one
git tag events@2.2.0
git tag contracts@1.1.0
git -c user.name=t -c user.email=t@t commit -q --allow-empty -m two
git tag events@2.2.1
git tag events@2.3.0-test
[ "$(latest_tag "$TMP/repo" events)" = "events@2.2.1" ]
[ "$(checkout_ref "$TMP/repo" events)" = "events@2.2.1" ]
git checkout -q --detach events@2.2.0
[ "$(checkout_ref "$TMP/repo" events)" = "events@2.2.0" ]
git -c user.name=t -c user.email=t@t commit -q --allow-empty -m three
[ "$(checkout_ref "$TMP/repo" events)" = "$(git rev-parse --short HEAD)" ]
[ "$(tag_version events@2.2.1)" = "2.2.1" ]
}
# ── WHATS-NEW.md requirements ───────────────────────────────────────────────────────────────
whatsnew() {
cat <<'EOF'
# What's new in Cross Events
<!--
How this file is used: … Each version is a "## 1.2.3" heading; add "— urgent" …
## 9.9.9 — requires accounts 9.9.9
-->
## 3.1.0 — urgent — requires accounts 1.3.0, website v1.1.0
- Something.
## 3.0.0 — requires accounts 1.2.0
- Sign-in moves.
## [2.4.0] - 2026-12-01 — Requires Website 1.0.0 — urgent
## 2.3.0 — urgent
## 2.2.0
- Nothing changes for you.
EOF
}
@test "whatsnew_requires reads one requirement" {
run whatsnew_requires 3.0.0 < <(whatsnew)
[ "$output" = "accounts 1.2.0" ]
}
@test "whatsnew_requires reads several, after urgent and with a v prefix" {
run whatsnew_requires 3.1.0 < <(whatsnew)
[ "${lines[0]}" = "accounts 1.3.0" ]
[ "${lines[1]}" = "website 1.1.0" ]
[ "${#lines[@]}" -eq 2 ]
}
@test "whatsnew_requires ignores case, brackets, dates and a trailing urgent" {
run whatsnew_requires 2.4.0 < <(whatsnew)
[ "$output" = "website 1.0.0" ]
}
@test "whatsnew_requires is empty without requirements, or for a missing version" {
run whatsnew_requires 2.3.0 < <(whatsnew)
[ "$output" = "" ]
run whatsnew_requires 2.2.0 < <(whatsnew)
[ "$output" = "" ]
run whatsnew_requires 1.0.0 < <(whatsnew)
[ "$output" = "" ]
}
@test "whatsnew_requires skips headings inside HTML comments" {
run whatsnew_requires 9.9.9 < <(whatsnew)
[ "$output" = "" ]
}
@test "whatsnew_requires handles Windows line endings" {
run whatsnew_requires 3.0.0 < <(whatsnew | sed 's/$/\r/')
[ "$output" = "accounts 1.2.0" ]
}
@test "whatsnew_requires reads the real Cross Events notes" {
run whatsnew_requires 2.1.1 < "$BATS_TEST_DIRNAME/../../apps/events/WHATS-NEW.md"
[ "$status" -eq 0 ]
[ "$output" = "" ]
}
# ── church.env ──────────────────────────────────────────────────────────────────────────────
@test "church_env_read takes known keys, quoted or bare, and skips the rest" {
cat > "$TMP/church.env" <<'EOF'
# a comment
CHURCH=hopefamily
PRODUCTS="events website"
ADDRESS=hopefamily.co.za # the address
EVENTS_DIR=/var/www/sites/hopefamily/events
EVENTS_PM2='hope-events-backend hope-events-frontend'
EVENTS_PORTS="5000 3000" # api web
PATH=/tmp/evil
SOMETHING_ELSE=1
lower=1
EOF
church_env_read "$TMP/church.env"
[ "$(cenv CHURCH)" = hopefamily ]
[ "$(cenv PRODUCTS)" = "events website" ]
[ "$(cenv ADDRESS)" = hopefamily.co.za ]
[ "$(pvar events PM2)" = "hope-events-backend hope-events-frontend" ]
[ "$(pvar events PORTS)" = "5000 3000" ]
[ "$(pvar events DIR)" = /var/www/sites/hopefamily/events ]
[ -z "${CENV[PATH]+x}" ]
[ -z "${CENV[SOMETHING_ELSE]+x}" ]
[ "$PATH" != /tmp/evil ]
[ "$(pvar website DIR /fallback)" = /fallback ]
}
@test "church_env_read handles Windows line endings and fails on a missing file" {
printf 'CHURCH=grace\r\nEVENTS_DB=grace_events\r\n' > "$TMP/church.env"
church_env_read "$TMP/church.env"
[ "$(cenv CHURCH)" = grace ]
[ "$(pvar events DB)" = grace_events ]
run church_env_read "$TMP/missing.env"
[ "$status" -eq 1 ]
}
@test "church_env_write writes a fixed order that reads back the same" {
CENV=()
church_env_set EVENTS_PORTS "5000 3000"
church_env_set ADDRESS hopefamily.co.za
church_env_set EVENTS_DIR /var/www/sites/hopefamily/events
church_env_set ACCOUNTS_DIR /var/www/sites/hopefamily/accounts
church_env_set CHURCH hopefamily
church_env_set PRODUCTS "accounts events"
church_env_write "$TMP/church.env"
run grep -v '^#' "$TMP/church.env"
[ "${lines[0]}" = "CHURCH=hopefamily" ]
[ "${lines[1]}" = 'PRODUCTS="accounts events"' ]
[ "${lines[2]}" = "ADDRESS=hopefamily.co.za" ]
[ "${lines[3]}" = "ACCOUNTS_DIR=/var/www/sites/hopefamily/accounts" ]
[ "${lines[4]}" = "EVENTS_DIR=/var/www/sites/hopefamily/events" ]
[ "${lines[5]}" = 'EVENTS_PORTS="5000 3000"' ]
CENV=()
church_env_read "$TMP/church.env"
[ "$(cenv PRODUCTS)" = "accounts events" ]
[ "$(pvar events PORTS)" = "5000 3000" ]
[ "${#CENV[@]}" -eq 6 ]
}
@test "church_env_set refuses unknown keys and unsafe values" {
run church_env_set PATH /tmp
[ "$status" -eq 1 ]
run church_env_set EVENTS_DIR '/var/www/$(reboot)'
[ "$status" -eq 1 ]
run church_env_set EVENTS_DIR 'a"b'
[ "$status" -eq 1 ]
}
@test "list_churches lists folders with a church.env" {
CROSSCODE_SITES="$TMP/sites"
mkdir -p "$TMP/sites/hopefamily" "$TMP/sites/grace" "$TMP/sites/.undone-old"
touch "$TMP/sites/hopefamily/church.env" "$TMP/sites/grace/church.env"
run list_churches
[ "${#lines[@]}" -eq 2 ]
[[ "$output" == *hopefamily* ]]
[[ "$output" == *grace* ]]
}
@test "in_product_order puts products in deploy order, once each" {
run in_product_order app events accounts events
[ "${lines[0]}" = accounts ]
[ "${lines[1]}" = events ]
[ "${lines[2]}" = app ]
[ "${#lines[@]}" -eq 3 ]
}
@test "valid_slug" {
valid_slug hopefamily
valid_slug grace-church2
run valid_slug Hope
[ "$status" -eq 1 ]
run valid_slug ../etc
[ "$status" -eq 1 ]
run valid_slug 1church
[ "$status" -eq 1 ]
}
# ── .env and URLs ───────────────────────────────────────────────────────────────────────────
@test "env_value, db_name_from_url and host_from_url" {
cat > "$TMP/.env" <<'EOF'
# DATABASE_URL=postgresql://old
DATABASE_URL="postgresql://hope_events:p%40ss@localhost:5432/hope_events?schema=public"
PORT=5001
FRONTEND_URL=https://events.hopefamily.co.za,https://www.hopefamily.co.za
EOF
[ "$(env_value "$TMP/.env" PORT)" = 5001 ]
[ "$(env_value "$TMP/.env" MISSING)" = "" ]
[ "$(db_name_from_url "$(env_value "$TMP/.env" DATABASE_URL)")" = hope_events ]
[ "$(host_from_url "$(env_value "$TMP/.env" FRONTEND_URL)")" = events.hopefamily.co.za ]
[ "$(host_from_url http://localhost:3000/x)" = localhost ]
}