From 71f2f02ee57a4a2b1c069067c6b99254c55ed8d8 Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Tue, 1 Dec 2026 16:11:41 +0200 Subject: [PATCH] crosscode: the Church App (app.sh, add, demo) and the App's changelog scripts/products/app.sh and app.env.template.json; app in SUPPORTED_PRODUCTS; crosscode add app (Accounts first); crosscode demo app --profile , and the App's turn, last, in a whole demo scenario. app.bats and demo.bats; app.sh linted. The full bats suite wasn't run (stopped for low memory); app.bats and demo.bats pass. Co-Authored-By: Claude Opus 5.5 --- apps/app/CHANGELOG.md | 24 +++++ apps/app/README.md | 13 ++- package.json | 2 +- scripts/crosscode | 5 +- scripts/lib/common.sh | 6 +- scripts/lib/install.sh | 18 ++-- scripts/lib/manage.sh | 47 ++++++++- scripts/products/app.env.template.json | 24 +++++ scripts/products/app.sh | 140 +++++++++++++++++++++++++ scripts/tests/app.bats | 93 ++++++++++++++++ scripts/tests/demo.bats | 60 ++++++++++- 11 files changed, 407 insertions(+), 25 deletions(-) create mode 100644 scripts/products/app.env.template.json create mode 100644 scripts/products/app.sh create mode 100644 scripts/tests/app.bats diff --git a/apps/app/CHANGELOG.md b/apps/app/CHANGELOG.md index ae1f3cb..414f864 100644 --- a/apps/app/CHANGELOG.md +++ b/apps/app/CHANGELOG.md @@ -155,6 +155,30 @@ Ships `@crosscode/seo` 1.1.0 (`articleJsonLd`, workspace). `crosscode.church-export/1`: members with their roles, the wall, commitments as rules, "I prayed", campaigns, testimonies, prayer requests with their moderation, the emergency numbers, announcements, the church's own devotionals and events, and the uploads; no internal ids. +- **The demo scenarios** (plan §12, `src/demo/`, `scripts/demo.ts`): `npm run demo` loads Acacia + Hill; `--profile ` (with `--scenario`, as the agent sends it) any of the eight demo + churches, around `@crosscode/demo`'s people with the same account ids as Accounts, so their + sign-ins find them. Each is in its scenario's active-member band (0–100 to 2 001–5 000), with its + leads' roles; three months of prayer commitments with the scenario's coverage (a quiet parish wall, + 30-minute slots in the Karoo, every hour twice at Northpoint, students on the 02:00 slots), a few + skips and renewals due, "I prayed" on most of the last three weeks; a campaign (Lent from Ash + Wednesday to Easter Eve for St Columba's), testimonies, prayer requests waiting for approval, + announcements (per campus at Riverside, in Afrikaans in the Karoo), and Harbour City's setup + checklist half done. Devotionals: Cross Code's fetched from the Hub, or the church's own for St + Columba's (KJV), Harbour City and Karoo Gemeenskapskerk (Afrikaans, quoting the BSB). Events come + from Cross Events (else the Website) through the feed when linked, else the App's own. Demo members + of an earlier scenario go; real members and their roles stay; an App whose real members have + used it needs `--force`. +- The demo devotionals' passages are copied **word for word** from the public-domain KJV and BSB by + `scripts/demo-passages.ts` into `src/demo/passages.ts` (no Bible text typed or generated). +- **Nothing is sent to a demo person** (an address at `example.com`): the sender marks their + messages done without sending (`isDemoPerson`). +- **The `crosscode` script** (ships with this release): `scripts/products/app.sh` (the Website's + hooks for the App: ports 5400 / 3400, `contracts`, `ui`, `auth-client`, `seo` and `demo`, backups + as `app-*.sql.gz`, the export and uploads), `scripts/products/app.env.template.json`, `app` in + `SUPPORTED_PRODUCTS`; `crosscode add app` (on `app.
`, Accounts first); + `crosscode demo app --profile ` and the App's turn, last, in a whole scenario + (`crosscode demo --scenario `); bats tests (`app.bats`, `demo.bats`). - Home shows today's devotional, the latest announcements (pinned first) and what's on. A sitemap of the public pages (`/sitemap.xml`, never prayer, members-only items or imported events), named in robots.txt. `GET /api/v1/app` gives the App's own address (`url`). diff --git a/apps/app/README.md b/apps/app/README.md index c69f9dc..265d92e 100644 --- a/apps/app/README.md +++ b/apps/app/README.md @@ -8,7 +8,8 @@ and notification settings need a sign-in through the church's Accounts. The admi **State:** being built (from 2026-12-01) on the branch `feat/app-scaffold`, which is merged into main only when `app@1.0.0` is done (main keeps a placeholder until then). Not released; -`app@1.0.0` is tagged when the plan is built. Done so far (the foundation, plan §1, §2, §10, §12, the House of Prayer, §3, prayer requests, §4, and announcements, devotionals and events, §5–§7): +`app@1.0.0` is tagged when the plan is built. Done so far (plan §1–§12: the foundation, the House of Prayer, prayer requests, announcements, +devotionals and events, email and WhatsApp, the admin, and the demo scenarios): - **Backend:** Express + Prisma + pg-boss (ES modules, as the Website). Registers with the Hub (`CCP-` licence key, product `app`) and sends the heartbeat with the active-member band the App @@ -52,8 +53,14 @@ main only when `app@1.0.0` is done (main keeps a placeholder until then). Not re (`backend/src/media/`); backups of the database and the images (`backend/src/backups/`); the church export's `app` part (`backend/scripts/export-church.ts`). -**Still to come** (plan §12–§15): the demo church and scenarios, `scripts/products/app.sh`, e2e, -axe and Lighthouse. +- **Demo scenarios** (§12, `backend/src/demo/`): `npm run demo` loads Acacia Hill; `npm run demo -- + --profile ` any of the eight demo churches (`--list`), around the same people as + Accounts. On a server: `crosscode demo app --profile `, or the whole scenario with + `crosscode demo --scenario ` (`scripts/products/app.sh`; `crosscode add app`). + The demo devotionals' Bible text comes only from `backend/scripts/demo-passages.ts`, which copies + it from the public-domain KJV and BSB files (not in the repo): never type or edit it. + +**Still to come** (plan §14–§15): e2e, axe and Lighthouse. - `backend/`: `npm run dev`, `npm test` (Node's test runner through tsx), `npm run build`. The schema is in `prisma/schema.prisma`. diff --git a/package.json b/package.json index 43f242e..aefeb17 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "test:scripts": "bats scripts/tests", "typecheck": "npm run typecheck --workspaces --if-present", "lint": "eslint .", - "lint:scripts": "shellcheck -x scripts/crosscode scripts/products/events.sh scripts/products/accounts.sh scripts/products/website.sh scripts/lib/report.sh scripts/lib/env.sh scripts/lib/frpc.sh scripts/lib/manage.sh apps/agent/deploy/install.sh apps/agent/deploy/revert.sh", + "lint:scripts": "shellcheck -x scripts/crosscode scripts/products/events.sh scripts/products/accounts.sh scripts/products/website.sh scripts/products/app.sh scripts/lib/report.sh scripts/lib/env.sh scripts/lib/frpc.sh scripts/lib/manage.sh apps/agent/deploy/install.sh apps/agent/deploy/revert.sh", "check": "npm run lint && npm run lint:scripts && npm run typecheck && npm test && npm run test:scripts && npm run build" }, "engines": { diff --git a/scripts/crosscode b/scripts/crosscode index 7e734b1..f97b708 100755 --- a/scripts/crosscode +++ b/scripts/crosscode @@ -92,11 +92,12 @@ crosscode: Cross Code installs on this server ($(git -C "${CROSSCODE_FROM:-.}" r crosscode deploy … --check only the checks; changes nothing crosscode status [] versions, latest releases, health crosscode install --products events --domain
a new church - crosscode add accounts|website [--domain …] add a product to a church here + crosscode add accounts|website|app [--domain …] add a product to a church here crosscode adopt --events-dir move an existing Cross Events install here crosscode adopt --undo go back to the old folder (within 14 days) crosscode restart [events] reload the PM2 processes - crosscode demo [website] --profile

replace Events' (or the Website's) data with a demo + crosscode demo [website|app] --profile

replace Events' (or another product's) data with a demo + crosscode demo --scenario a demo scenario on every product the church has crosscode env [--set NAME=value] show or change a product's .env values crosscode export [product …] --out the church's data and uploads crosscode uninstall [product …] --confirm diff --git a/scripts/lib/common.sh b/scripts/lib/common.sh index 76184a6..7ec0c54 100644 --- a/scripts/lib/common.sh +++ b/scripts/lib/common.sh @@ -8,7 +8,7 @@ CROSSCODE_REPO="${CROSSCODE_REPO:-https://c-git.crosscode.co.za/joshua/crosscode # through it. PRODUCT_ORDER=(accounts events website app) # Products this version of the script can deploy (a products/.sh exists for each). -SUPPORTED_PRODUCTS=(accounts events website) +SUPPORTED_PRODUCTS=(accounts events website app) # Releases older than this get a warning; older than CROSSCODE_NODE_MIN the deploy is refused. CROSSCODE_NODE_MIN=20 CROSSCODE_NODE_WARN=22 @@ -358,8 +358,8 @@ node_major() { node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo # 1.0–1.1 has no .gitignore for its ecosystem.config.cjs). Nor do the files in the folders this # script writes into (Accounts below 1.3.0 doesn't ignore the database dumps made before each # deploy, so the dump would stop the very deploy that brings the .gitignore). -CROSSCODE_OWN_FILES=(apps/events/ecosystem.config.js apps/accounts/ecosystem.config.cjs apps/website/ecosystem.config.cjs) -CROSSCODE_OWN_DIRS=(apps/accounts/backend/backups/ apps/website/backend/backups/) +CROSSCODE_OWN_FILES=(apps/events/ecosystem.config.js apps/accounts/ecosystem.config.cjs apps/website/ecosystem.config.cjs apps/app/ecosystem.config.cjs) +CROSSCODE_OWN_DIRS=(apps/accounts/backend/backups/ apps/website/backend/backups/ apps/app/backend/backups/) local_changes() { git_in "$1" status --porcelain --untracked-files=all | awk -v own="${CROSSCODE_OWN_FILES[*]}" -v dirs="${CROSSCODE_OWN_DIRS[*]}" ' diff --git a/scripts/lib/install.sh b/scripts/lib/install.sh index 9d70c1e..151dcc3 100644 --- a/scripts/lib/install.sh +++ b/scripts/lib/install.sh @@ -214,6 +214,7 @@ install_product() { events) install_events "$@" ;; accounts) install_accounts "$@" ;; website) install_website "$@" ;; + app) install_app_product "$@" ;; *) die "This version of crosscode can't install $product." ;; esac } @@ -242,7 +243,7 @@ install_done() { info "DNS (if not set yet): $domain → this server (A record, or a CNAME)." fi case "$product" in - website) info "Open https://$domain/admin and sign in with the admin email the Hub was given: that account becomes the first Owner." ;; + website|app) info "Open https://$domain/admin and sign in with the admin email the Hub was given: that account becomes the first Owner." ;; *) info "Open https://$domain/setup to create the admin account." ;; esac if [ "$product" = accounts ]; then info "Updates: crosscode deploy $church (Accounts is always updated first)"; else info "Updates: crosscode deploy $church $product"; fi @@ -252,10 +253,10 @@ add_usage() { cat <<'EOF' crosscode add [options] -Adds a product to a church already installed here (Accounts next to Events, the Website next to -Accounts), in -its own folder, database, PM2 processes and site, run by the church's app user. If a step fails, -what this run made is removed again; the church's other products aren't touched. +Adds a product to a church already installed here (Accounts next to Events, the Website or the +App next to Accounts), in its own folder, database, PM2 processes and site, run by the church's +app user. If a step fails, what this run made is removed again; the church's other products +aren't touched. --domain where it's served (default: .; the Website: www.

, or the address itself on a Cross Code address) @@ -298,13 +299,15 @@ cmd_add() { [ -n "$church" ] && [ -n "$product" ] || { add_usage; die "Name the church and the product."; } valid_slug "$church" || die "\"$church\" isn't a church name." is_known_product "$product" || die "Unknown product \"$product\"." - case "$product" in events|accounts|website) ;; *) die "This version of crosscode can only add events, accounts and website." ;; esac + case "$product" in events|accounts|website|app) ;; *) die "This version of crosscode can only add events, accounts, website and app." ;; esac install_check_options "$product" "$ssl" "$email" "$version" "$main" "$frp" "$api" "$web" require_root load_church "$church" ! church_has_product "$product" || die "$church already has $product (crosscode status $church)." # The Website signs in through the church's Accounts and can't be reached without it (WEBSITE-1.0-PLAN §11). [ "$product" != website ] || church_has_product accounts || die "The Website needs the church's Accounts: add it first (crosscode add $church accounts)." + # The App too: everyone signs in through Accounts, and its licence needs the church's Accounts (APP-1.0-PLAN §12). + [ "$product" != app ] || church_has_product accounts || die "The App needs the church's Accounts: add it first (crosscode add $church accounts)." local address user="" existing address="$(cenv ADDRESS)" [ -n "$domain" ] || [ -n "$address" ] || die "--domain is needed: the church has no address in church.env." @@ -448,9 +451,10 @@ install_events() { install_accounts() { install_app accounts Accounts ACCOUNTS_SECRET "$@"; } install_website() { install_app website Website WEBSITE_SECRET "$@"; } +install_app_product() { install_app app App APP_SECRET "$@"; } # install_app <generated secret> <church> <user> <domain> <api> <web> <version> -# <main> <ssl> <email> <frp>: Accounts and the Website (and later the App), which share one shape: +# <main> <ssl> <email> <frp>: Accounts, the Website and the App, which share one shape: # a checkout of crosscode-platform, apps/<product>/{backend,frontend}, ecosystem.config.cjs, and # one secret generated here that never leaves the server. install_app() { diff --git a/scripts/lib/manage.sh b/scripts/lib/manage.sh index 25f778c..b5969a8 100644 --- a/scripts/lib/manage.sh +++ b/scripts/lib/manage.sh @@ -24,6 +24,11 @@ crosscode demo <church> accounts --profile <scenario> [--scenario <scenario>] people in Accounts (the eight scenarios, as the Website's profiles); a backup is taken first; real people and admins stay +crosscode demo <church> app --profile <scenario> [--scenario <scenario>] + a demo scenario's prayer wall, requests, + announcements, devotionals and events in the App + (App 1.0.0, the eight scenarios); a backup is taken + first; real members stay crosscode demo <church> --scenario <scenario> [--admin-email <email>] a whole demo scenario on every product the church has here, Accounts first (then Events, the Website, @@ -115,6 +120,7 @@ DEMO_SCENARIOS="$WEBSITE_DEMO_PROFILES" # sites. Afterwards Events is reloaded: the demo switches campuses on or off. # `crosscode demo <church> website --profile <p>`: the Website's content instead (cmd_demo_website). # `crosscode demo <church> accounts --profile <scenario>`: a scenario's church and people (cmd_demo_accounts). +# `crosscode demo <church> app --profile <scenario>`: the App's part of a scenario (cmd_demo_app). # `--scenario <s>` (agent 1.7.0): part of loading that scenario; the profile must be the scenario's. # `--scenario <s>` with no product and no profile (the Hub's seed-scenario job): the whole scenario # on every product the church has (cmd_demo_scenario). @@ -144,8 +150,12 @@ cmd_demo() { # Every product loads the profile named after the scenario (contracts DEMO_SCENARIOS). [ "$profile" = "$scenario" ] || die "--scenario $scenario loads the $scenario profile in $product, not $profile." fi - # Agent 1.7.0 sends the App's demo jobs; the App and its demo come with App 1.0.0. - [ "$product" != app ] || die "The Church App's demo data comes with App 1.0.0 (this crosscode has none yet)." + if [ "$product" = app ]; then + [[ " $DEMO_SCENARIOS " == *" $profile "* ]] || die "--profile: one of $DEMO_SCENARIOS." + [ -z "$email" ] || die "--admin-email: the App's demo keeps every real member." + cmd_demo_app "$church" "$profile" + return + fi if [ "$product" = accounts ]; then [[ " $DEMO_SCENARIOS " == *" $profile "* ]] || die "--profile: one of $DEMO_SCENARIOS." [ -z "$email" ] || die "--admin-email: Accounts' demo keeps every admin." @@ -221,8 +231,6 @@ cmd_demo_scenario() { step "Demo scenario $scenario: checking ${todo[*]}" [ "${#skipped[@]}" -eq 0 ] || info "Not installed, so skipped: ${skipped[*]}" for p in "${todo[@]}"; do - # The App and its demo come with App 1.0.0 (scripts/products/app.sh). - [ "$p" != app ] || die "The Church App's demo data comes with App 1.0.0 (this crosscode has none yet). Nothing was changed." # shellcheck source=/dev/null source "$CROSSCODE_HOME/products/$p.sh" v="$("${p}_expected_version" "$(pvar "$p" DIR)" 2>/dev/null || true)" @@ -239,6 +247,7 @@ cmd_demo_scenario() { accounts) cmd_demo_accounts "$church" "$scenario" ;; events) cmd_demo_events "$church" "$scenario" "$email" ;; website) cmd_demo_website "$church" "$scenario" "$scenario" ;; + app) cmd_demo_app "$church" "$scenario" ;; esac ); then seen=0 @@ -283,6 +292,36 @@ cmd_demo_accounts() { die "Accounts didn't answer after the reload (sudo -u $user pm2 logs)." } +# The App's demo (App 1.0.0+, backend/scripts/demo.ts, APP-1.0-PLAN §12): a scenario's prayer wall, +# requests, announcements, devotionals and events around the scenario's people (Accounts has them +# by now), replacing the App's content and its demo members; real members stay. Every App profile is +# a scenario, so it always loads as one. Backup first; then reloaded. +cmd_demo_app() { + local church="$1" profile="$2" dir user app + require_root + load_church "$church" + church_has_product app || die "$church has no Church App." + # shellcheck source=/dev/null + source "$CROSSCODE_HOME/products/app.sh" + dir="$(pvar app DIR)" + user="$(pvar app USER)" + app="$(app_app "$dir")" + [ -f "$app/backend/scripts/demo.ts" ] || die "This release of the App ($(checkout_ref "$dir" app)) has no demo (1.0.0 or later has)." + + step "App: backup first" + app_backup "$dir" "$user" || die "The backup failed, so the demo wasn't loaded." + + step "App: demo scenario ($profile)" + run_as "$user" "$app/backend" npm run --silent demo -- --profile "$profile" --scenario "$profile" --force || + die "The demo wasn't loaded (see above). If it got as far as changing anything, the backup from just now has the data from before." + + step "App: reloading PM2" + app_reload "$dir" "$user" + step "App: waiting for it to answer" + wait_for_version "$(app_health_url)" "$(app_expected_version "$dir")" 60 || + die "The App didn't answer after the reload (sudo -u $user pm2 logs)." +} + # The Website's demo (Website 1.3.0+, backend/src/demo/seeds.ts): its content replaced with one of # the eight demo churches, keeping the church's profile, the site settings and the admins. Backup # first; then reloaded, so nothing cached from before is shown. diff --git a/scripts/products/app.env.template.json b/scripts/products/app.env.template.json new file mode 100644 index 0000000..e8d0fc9 --- /dev/null +++ b/scripts/products/app.env.template.json @@ -0,0 +1,24 @@ +{ + "schema": "crosscode.env-template/1", + "product": "app", + "files": { + "backend/.env": [ + { "name": "NODE_ENV", "layer": "generated", "required": true, "description": "production" }, + { "name": "PORT", "layer": "install", "from": "{apiPort}", "required": true, "description": "The backend's port (from the Hub's port list)" }, + { "name": "DATABASE_URL", "layer": "generated", "required": true, "description": "Made on the server with a new password; never leaves it" }, + { "name": "APP_SECRET", "layer": "generated", "required": true, "description": "Signs cookies and links, encrypts stored secrets: never change it" }, + { "name": "APP_URL", "layer": "install", "from": "https://{domain}", "required": true, "description": "The public address (app. or the Cross Code address)" }, + { "name": "TRUST_PROXY", "layer": "server", "required": true, "description": "Behind frp: loopback" }, + { "name": "APP_TIMEZONE", "layer": "install", "default": "Africa/Johannesburg", "description": "Until the church profile says its own" }, + { "name": "HUB_URL", "layer": "defaults", "default": "https://hub.crosscode.co.za" }, + { "name": "HUB_PUBLIC_KEY", "layer": "defaults", "description": "Only for a Hub other than production" }, + { "name": "HUB_LICENCE_KEY", "layer": "install", "description": "A Hub install registers by itself with this (CCP-…)" }, + { "name": "HUB_ADMIN_NAME", "layer": "install" }, + { "name": "HUB_ADMIN_EMAIL", "layer": "install", "description": "The first person to sign in with this address (through Accounts, verified) becomes the Owner" }, + { "name": "JOBS_ENABLED", "layer": "defaults", "default": "true" } + ], + "frontend/.env.production": [ + { "name": "APP_API_URL", "layer": "install", "from": "http://127.0.0.1:{apiPort}", "required": true, "description": "Where the pages reach the backend on this server" } + ] + } +} diff --git a/scripts/products/app.sh b/scripts/products/app.sh new file mode 100644 index 0000000..9045069 --- /dev/null +++ b/scripts/products/app.sh @@ -0,0 +1,140 @@ +# shellcheck shell=bash +# The Church App's hooks for the crosscode script (docs/APP-1.0-PLAN.md §12), the same hooks as +# products/website.sh. Every hook takes the product's checkout (APP_DIR, a checkout of +# crosscode-platform) and the app user. Values come from church.env (CENV): APP_DIR, +# APP_DOMAIN, APP_PM2 ("<backend> <frontend>"), APP_PORTS ("<api> <web>"), +# APP_DB, APP_USER. + +app_app() { printf '%s/apps/app' "$1"; } + +app_pm2_names() { pvar app PM2; } + +app_api_port() { local p; read -r p _ <<< "$(pvar app PORTS)"; printf '%s' "${p:-5400}"; } +app_web_port() { local p; read -r _ p <<< "$(pvar app PORTS)"; printf '%s' "${p:-3400}"; } +app_health_url() { printf 'http://127.0.0.1:%s/api/health' "$(app_api_port)"; } + +app_expected_version() { + node -p 'require(process.argv[1]).version' "$(app_app "$1")/backend/package.json" +} + +# The shared packages the App is built with (contracts, auth-client for signing in, ui and seo for its +# pages, demo for the demo scenarios). +app_shared_packages() { + local p + for p in packages/contracts packages/ui packages/auth-client packages/seo packages/demo; do + [ -f "$1/$p/package.json" ] && printf '%s\n' "$p" + done +} + +# A database dump into backend/backups/ (the last 14 are kept), before every deploy and uninstall. +app_backup() { + local dir="$1" user="$2" backend + backend="$(app_app "$dir")/backend" + # shellcheck disable=SC2016 # the app user's shell expands these + run_as "$user" "$backend" bash -c ' + set -euo pipefail + url="$(grep -E "^DATABASE_URL=" .env | head -1 | cut -d= -f2- | sed -e "s/^\"//" -e "s/\"$//")" + mkdir -p backups + file="backups/app-$(date +%Y%m%d-%H%M%S).sql.gz" + pg_dump --no-owner --no-privileges "$url" | gzip > "$file" + chmod 600 "$file" + ls -1t backups/app-*.sql.gz | tail -n +15 | xargs -r rm -f + echo " $file"' +} + +app_install_deps() { + local -a ws=() + local p + while IFS= read -r p; do ws+=(-w "$p"); done < <(app_shared_packages "$1") + run_as "$2" "$1" npm ci --include=dev --no-audit --no-fund --include-workspace-root "${ws[@]}" -w apps/app/backend -w apps/app/frontend +} + +app_migrate() { + run_as "$2" "$(app_app "$1")/backend" npm run prisma:deploy +} + +app_build() { + local p + for p in packages/contracts packages/auth-client packages/seo packages/demo; do + [ ! -f "$1/$p/package.json" ] || run_as "$2" "$1/$p" npm run build || return 1 + done + run_as "$2" "$(app_app "$1")/backend" npm run build && + run_as "$2" "$(app_app "$1")/frontend" npm run build +} + +app_rollback_build() { + app_install_deps "$1" "$2" && app_build "$1" "$2" +} + +# The install's own PM2 config (not in git), made from the example the first time. +app_ensure_pm2_config() { + local dir="$1" user="$2" app conf backend frontend + app="$(app_app "$dir")" + conf="$app/ecosystem.config.cjs" + [ -f "$conf" ] && return 0 + read -r backend frontend <<< "$(app_pm2_names)" + sed -e "s/'app-backend'/'${backend}'/" \ + -e "s/'app-frontend'/'${frontend}'/" \ + -e "s/-p 3400/-p $(app_web_port)/" \ + "$app/ecosystem.config.example.cjs" > "$conf" + chown "$user:$user" "$conf" 2>/dev/null || true + info "Created $conf from the example." +} + +app_reload() { + app_ensure_pm2_config "$1" "$2" + run_as "$2" "$(app_app "$1")" pm2 reload ecosystem.config.cjs --update-env +} + +# export.json (crosscode.church-export/1 with its `app` part: members and roles, the prayer wall, +# requests, announcements, devotionals, events, uploads; no secrets) into <out dir>. The uploads are copied by app_copy_uploads. +app_export() { + local app + app="$(app_app "$1")" + run_as "$2" "$app/backend" npx tsx scripts/export-church.ts --out-dir "$3" +} + +# The church's uploads (announcement, campaign and event images) into <out dir>, as Events' copy_uploads. +app_copy_uploads() { + local uploads + uploads="$(app_app "$1")/backend/uploads" + mkdir -p "$2" + [ ! -d "$uploads" ] || rsync -a "$uploads/" "$2/" +} + +# What uninstall keeps for 30 days: the backups and the .env (its APP_SECRET decrypts what's stored). +app_keep() { + local app dest="$2" + app="$(app_app "$1")" + mkdir -p "$dest" + # -H: the uploads snapshots share unchanged files as hard links; keep them shared. + [ ! -d "$app/backend/backups" ] || rsync -aH "$app/backend/backups/" "$dest/backups/" + cp -p "$app/backend/.env" "$dest/backend.env" 2>/dev/null || true + chmod -R go-rwx "$dest" +} + +app_env_vars() { + printf '%s\n' --var "domain=$(pvar app DOMAIN)" --var "apiPort=$(app_api_port)" --var "webPort=$(app_web_port)" --var "church=$(cenv CHURCH)" +} + +# As events_resolve_failed_migrations: a migration that failed in this release and isn't in the +# previous one is marked as rolled back (the backup taken first has the database as it was). +app_resolve_failed_migrations() { + local dir="$1" user="$2" prev="$3" backend name + backend="$(app_app "$dir")/backend" + local -a failed + # shellcheck disable=SC2016 # SQL through psql, run as the app user + mapfile -t failed < <(run_as "$user" "$backend" bash -c ' + url="$(grep -E "^DATABASE_URL=" .env | head -1 | cut -d= -f2- | sed -e "s/^\"//" -e "s/\"$//")" + psql "$url" -At -c "select migration_name from _prisma_migrations where finished_at is null and rolled_back_at is null"') + for name in "${failed[@]}"; do + [ -n "$name" ] || continue + if git_in "$dir" cat-file -e "$prev:apps/app/backend/prisma/migrations/$name/migration.sql" 2>/dev/null; then + warn "app: $name failed but $prev has it too; left for a look (npx prisma migrate status)." + continue + fi + run_as "$user" "$backend" npx prisma migrate resolve --rolled-back "$name" >/dev/null + info "app: marked the failed migration $name as rolled back." + report_result "resolvedMigration" "$name" + done +} diff --git a/scripts/tests/app.bats b/scripts/tests/app.bats new file mode 100644 index 0000000..7123ad3 --- /dev/null +++ b/scripts/tests/app.bats @@ -0,0 +1,93 @@ +#!/usr/bin/env bats +# The Church App's hooks that don't need a server (APP-1.0-PLAN §12), and where `crosscode add` +# puts it on the church's address. Run with `npm run test:scripts`. + +setup() { + # shellcheck source=../lib/common.sh + source "$BATS_TEST_DIRNAME/../lib/common.sh" + # shellcheck source=../lib/install.sh + source "$BATS_TEST_DIRNAME/../lib/install.sh" + # shellcheck source=../products/app.sh + source "$BATS_TEST_DIRNAME/../products/app.sh" + TMP="$BATS_TEST_TMPDIR" + CALLS="$TMP/calls" + : > "$CALLS" + # Record what would run, as the app user, instead of running it. + run_as() { local user="$1" dir="$2"; shift 2; echo "$user|${dir#"$TMP"/}|$*" >> "$CALLS"; } + CENV=() +} + +checkout() { + local dir="$TMP/$1" p + for p in contracts ui auth-client seo demo; do + mkdir -p "$dir/packages/$p" + echo "{\"name\":\"@crosscode/$p\"}" > "$dir/packages/$p/package.json" + done + mkdir -p "$dir/apps/app/backend" "$dir/apps/app/frontend" + echo '{"version":"1.0.0"}' > "$dir/apps/app/backend/package.json" + cp "$BATS_TEST_DIRNAME/../../apps/app/ecosystem.config.example.cjs" "$dir/apps/app/" +} + +@test "the App goes on app.<address>" { + [ "$(default_domain app hopefamily.co.za)" = app.hopefamily.co.za ] + [ "$(default_domain app hope.crosscode.co.za)" = app.hope.crosscode.co.za ] +} + +@test "the App is a product this script deploys, last" { + is_supported_product app + [ "${PRODUCT_ORDER[*]}" = "accounts events website app" ] +} + +@test "contracts, ui, auth-client, seo and demo are installed; all but ui built before the App" { + checkout new + app_install_deps "$TMP/new" hope + grep -qx 'hope|new|npm ci --include=dev --no-audit --no-fund --include-workspace-root -w packages/contracts -w packages/ui -w packages/auth-client -w packages/seo -w packages/demo -w apps/app/backend -w apps/app/frontend' "$CALLS" + : > "$CALLS" + app_build "$TMP/new" hope + [ "$(cut -d'|' -f2 "$CALLS" | tr '\n' ' ')" = "new/packages/contracts new/packages/auth-client new/packages/seo new/packages/demo new/apps/app/backend new/apps/app/frontend " ] +} + +@test "ports default to 5400 / 3400, else church.env's" { + [ "$(app_api_port)" = 5400 ] + [ "$(app_web_port)" = 3400 ] + church_env_set APP_PORTS "5510 3510" + [ "$(app_health_url)" = http://127.0.0.1:5510/api/health ] + [ "$(app_web_port)" = 3510 ] +} + +@test "the PM2 config is made once, with the church's names and port" { + checkout new + church_env_set APP_PM2 "hope-app-backend hope-app-frontend" + church_env_set APP_PORTS "5510 3510" + info() { :; } + app_ensure_pm2_config "$TMP/new" "$(id -un)" + conf="$TMP/new/apps/app/ecosystem.config.cjs" + grep -q "'hope-app-backend'" "$conf" + grep -q "'hope-app-frontend'" "$conf" + grep -q -- "-p 3510" "$conf" + echo changed > "$conf" + app_ensure_pm2_config "$TMP/new" "$(id -un)" + [ "$(cat "$conf")" = changed ] +} + +@test "the export is the App's own script, into the folder given" { + checkout new + app_export "$TMP/new" hope "$TMP/out" + grep -qx "hope|new/apps/app/backend|npx tsx scripts/export-church.ts --out-dir $TMP/out" "$CALLS" +} + +@test "the uploads are copied into the export; none is fine" { + command -v rsync >/dev/null || skip "rsync isn't installed here" + checkout new + app_copy_uploads "$TMP/new" "$TMP/out1" + [ -d "$TMP/out1" ] + mkdir -p "$TMP/new/apps/app/backend/uploads/2026/12" + echo jpg > "$TMP/new/apps/app/backend/uploads/2026/12/campaign.jpg" + app_copy_uploads "$TMP/new" "$TMP/out2" + [ "$(cat "$TMP/out2/2026/12/campaign.jpg")" = jpg ] +} + +@test "the env template in the script is the App's own (the Hub reads apps/app's at the tag)" { + diff <(node -e 'console.log(JSON.stringify(require(process.argv[1])))' "$BATS_TEST_DIRNAME/../products/app.env.template.json") \ + <(node -e 'console.log(JSON.stringify(require(process.argv[1])))' "$BATS_TEST_DIRNAME/../../apps/app/env.template.json") +} diff --git a/scripts/tests/demo.bats b/scripts/tests/demo.bats index 5f4f80b..b2eab24 100644 --- a/scripts/tests/demo.bats +++ b/scripts/tests/demo.bats @@ -21,6 +21,15 @@ setup() { echo "scenarioTeam" > "$TMP/sites/hope/website/apps/website/backend/src/demo/seeds.ts" mkdir -p "$TMP/sites/hope/accounts/apps/accounts/backend/scripts" touch "$TMP/sites/hope/accounts/apps/accounts/backend/scripts/demo.ts" + mkdir -p "$TMP/sites/hope/app/apps/app/backend/scripts" + touch "$TMP/sites/hope/app/apps/app/backend/scripts/demo.ts" + cat > "$CROSSCODE_HOME/products/app.sh" <<EOF4 +app_app() { printf '%s/apps/app' "\$1"; } +app_backup() { echo "backup \$2" >> "$CALLS"; [ -z "\${FAIL_BACKUP:-}" ]; } +app_reload() { echo "reload \$2" >> "$CALLS"; } +app_health_url() { echo http://127.0.0.1:5400/api/health; } +app_expected_version() { echo \${APP_VERSION:-1.0.0}; } +EOF4 cat > "$CROSSCODE_HOME/products/accounts.sh" <<EOF3 accounts_app() { printf '%s/apps/accounts' "\$1"; } accounts_backup() { echo "backup \$2" >> "$CALLS"; [ -z "\${FAIL_BACKUP:-}" ]; } @@ -172,13 +181,36 @@ wait 1.9.0" ] [[ "$(cat "$CALLS")" == *"demo -- --profile bold-youth --force"* ]] } -@test "the App's demo (agent 1.7.0 sends it) is refused clearly until App 1.0.0" { - run cmd_demo hope app --profile community-family --scenario community-family +@test "app: backup, then the scenario as the App's user, then a reload" { + HAS="accounts app" run cmd_demo hope app --profile community-family --scenario community-family + [ "$status" -eq 0 ] + [ "$(cat "$CALLS")" = "backup cc-hope-app +run cc-hope-app sites/hope/app/apps/app/backend npm run --silent demo -- --profile community-family --scenario community-family --force +reload cc-hope-app +wait 1.0.0" ] +} + +@test "app: only a scenario, no admin email, an App installed, and a release that has the demo" { + HAS="accounts app" run cmd_demo hope app --profile small [ "$status" -eq 1 ] - [[ "$output" == *"comes with App 1.0.0"* ]] + HAS="accounts app" run cmd_demo hope app --profile bold-plant --admin-email jane@hope.test + [ "$status" -eq 1 ] + HAS="accounts" run cmd_demo hope app --profile bold-plant + [ "$status" -eq 1 ] + [[ "$output" == *"has no Church App"* ]] + rm "$TMP/sites/hope/app/apps/app/backend/scripts/demo.ts" + HAS="accounts app" run cmd_demo hope app --profile bold-plant + [ "$status" -eq 1 ] + [[ "$output" == *"has no demo (1.0.0 or later has)"* ]] [ ! -s "$CALLS" ] } +@test "app: a failed backup stops it before the data is touched" { + HAS="accounts app" FAIL_BACKUP=1 run cmd_demo hope app --profile bold-plant + [ "$status" -eq 1 ] + [[ "$(cat "$CALLS")" != *"run "* ]] +} + @test "a failed Accounts demo isn't followed by a reload" { FAIL_SEED=1 run cmd_demo hope accounts --profile clean-modern [ "$status" -eq 1 ] @@ -224,10 +256,28 @@ cc-hope-website" ] [[ "$(cat "$CALLS")" != *"cc-hope-website"* ]] } -@test "seed-scenario: an App installed is refused (its demo comes with App 1.0.0), nothing changed" { +@test "seed-scenario: the App loads last, after Accounts, Events and the Website" { + HAS="accounts events website app" EVENTS_VERSION=3.8.0 WEBSITE_VERSION=1.8.0 run cmd_demo hope --scenario clean-campus + [ "$status" -eq 0 ] + [ "$(grep '^run' "$CALLS" | cut -d' ' -f2)" = "cc-hope-accounts +cc-hope-events +cc-hope-website +cc-hope-app" ] + [[ "$(cat "$CALLS")" == *"run cc-hope-app sites/hope/app/apps/app/backend npm run --silent demo -- --profile clean-campus --scenario clean-campus --force"* ]] +} + +@test "seed-scenario: only Accounts and the App, the rest skipped" { HAS="accounts app" run cmd_demo hope --scenario bold-plant + [ "$status" -eq 0 ] + [[ "$output" == *"Not installed, so skipped: events website"* ]] + [ "$(grep '^run' "$CALLS" | cut -d' ' -f2)" = "cc-hope-accounts +cc-hope-app" ] +} + +@test "seed-scenario: an App before 1.0.0 stops it before anything changes" { + HAS="accounts app" APP_VERSION=0.9.0 run cmd_demo hope --scenario bold-plant [ "$status" -eq 1 ] - [[ "$output" == *"comes with App 1.0.0"* ]] + [[ "$output" == *"The App is 0.9.0 here: a demo scenario needs 1.0.0 or later"* ]] [ ! -s "$CALLS" ] }