#!/usr/bin/env bats # crosscode demo (lib/manage.sh): argument checks, then backup → demo data as the site's user → # reload, in that order, and nothing after a step that fails. setup() { export CROSSCODE_NO_UPDATE=1 CC="$BATS_TEST_DIRNAME/../crosscode" TMP="$BATS_TEST_TMPDIR" CALLS="$TMP/calls" : > "$CALLS" # shellcheck source=../lib/common.sh source "$BATS_TEST_DIRNAME/../lib/common.sh" # shellcheck source=../lib/manage.sh source "$BATS_TEST_DIRNAME/../lib/manage.sh" # A church with Events, and Events' hooks recording what they'd do. export CROSSCODE_HOME="$TMP/home" mkdir -p "$CROSSCODE_HOME/products" "$TMP/sites/hope/events/apps/events/backend/scripts" touch "$TMP/sites/hope/events/apps/events/backend/scripts/set-demo.js" mkdir -p "$TMP/sites/hope/website/apps/website/backend/src/demo" 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" cat > "$CROSSCODE_HOME/products/accounts.sh" <> "$CALLS"; [ -z "\${FAIL_BACKUP:-}" ]; } accounts_reload() { echo "reload \$2" >> "$CALLS"; } accounts_health_url() { echo http://127.0.0.1:5200/api/health; } accounts_expected_version() { echo \${ACCOUNTS_VERSION:-1.9.0}; } EOF3 cat > "$CROSSCODE_HOME/products/website.sh" <> "$CALLS"; [ -z "\${FAIL_BACKUP:-}" ]; } website_reload() { echo "reload \$2" >> "$CALLS"; } website_health_url() { echo http://127.0.0.1:5300/api/health; } website_expected_version() { echo \${WEBSITE_VERSION:-1.3.0}; } EOF2 cat > "$CROSSCODE_HOME/products/events.sh" <> "$CALLS"; [ -z "\${FAIL_BACKUP:-}" ]; } events_reload() { echo "reload \$2" >> "$CALLS"; } events_health_url() { echo http://127.0.0.1:20001/health; } events_expected_version() { echo \${EVENTS_VERSION:-2.4.1}; } EOF require_root() { :; } load_church() { [ "$1" = hope ] || die "There's no church $1 here."; } HAS="${HAS:-events website accounts}" church_has_product() { [[ " $HAS " == *" $1 "* ]]; } pvar() { case "$2" in DIR) echo "$TMP/sites/hope/$1" ;; USER) echo "cc-hope-$1" ;; esac; } checkout_ref() { echo "$2@1.0.0"; } wait_for_version() { echo "wait $2" >> "$CALLS"; } run_as() { local user="$1" dir="$2"; shift 2; echo "run $user ${dir#"$TMP"/} $*" >> "$CALLS"; [ -z "${FAIL_SEED:-}" ] && [ "$user" != "${FAIL_USER:-}" ]; } } @test "the profile and admin email are checked before anything else" { CROSSCODE_HOME= run bash "$CC" demo hope [ "$status" -eq 1 ] [[ "$output" == *"--profile: one of small events ministry multicampus"* ]] CROSSCODE_HOME= run bash "$CC" demo hope --profile everything [ "$status" -eq 1 ] CROSSCODE_HOME= run bash "$CC" demo hope --profile small --admin-email 'x; rm -rf /' [ "$status" -eq 1 ] [[ "$output" == *"not an email address"* ]] CROSSCODE_HOME= run bash "$CC" help demo [[ "$output" == *"crosscode demo --profile"* ]] } @test "backup, then the demo as the site's user (no prompt), then a reload" { run cmd_demo hope --profile ministry [ "$status" -eq 0 ] [ "$(cat "$CALLS")" = "backup cc-hope-events run cc-hope-events sites/hope/events/apps/events/backend env node scripts/set-demo.js --profile=ministry --yes reload cc-hope-events wait 2.4.1" ] } @test "the admin to keep goes in DEMO_ADMIN_EMAIL (older releases only read that)" { run cmd_demo hope --profile small --admin-email jane@hope.test [ "$status" -eq 0 ] grep -qx 'run cc-hope-events sites/hope/events/apps/events/backend env DEMO_ADMIN_EMAIL=jane@hope.test node scripts/set-demo.js --profile=small --yes' "$CALLS" } @test "a failed backup stops it before the data is touched" { FAIL_BACKUP=1 run cmd_demo hope --profile small [ "$status" -eq 1 ] [[ "$output" == *"The backup failed"* ]] [ "$(cat "$CALLS")" = "backup cc-hope-events" ] } @test "a failed demo isn't followed by a reload" { FAIL_SEED=1 run cmd_demo hope --profile small [ "$status" -eq 1 ] ! grep -q reload "$CALLS" } @test "a release without the demo script is refused" { rm "$TMP/sites/hope/events/apps/events/backend/scripts/set-demo.js" run cmd_demo hope --profile small [ "$status" -eq 1 ] [[ "$output" == *"has no demo data"* ]] [ ! -s "$CALLS" ] } @test "website: backup, then the demo profile as the site's user, then a reload" { run cmd_demo hope website --profile bold-plant [ "$status" -eq 0 ] [ "$(cat "$CALLS")" = "backup cc-hope-website run cc-hope-website sites/hope/website/apps/website/backend npm run --silent demo -- --profile bold-plant --force reload cc-hope-website wait 1.3.0" ] } @test "website: only its own profiles, no admin email, and only a release that has them" { run cmd_demo hope website --profile small [ "$status" -eq 1 ] [[ "$output" == *"heritage-parish"* ]] run cmd_demo hope website --profile clean-campus --admin-email jane@hope.test [ "$status" -eq 1 ] run cmd_demo hope --profile clean-campus --admin-email not-an-email [ "$status" -eq 1 ] rm "$TMP/sites/hope/website/apps/website/backend/src/demo/seeds.ts" run cmd_demo hope website --profile clean-campus [ "$status" -eq 1 ] [[ "$output" == *"no demo profiles"* ]] [ ! -s "$CALLS" ] } @test "accounts: backup, then the scenario's church and people as the site's user, then a reload" { run cmd_demo hope accounts --profile community-family --scenario community-family [ "$status" -eq 0 ] [ "$(cat "$CALLS")" = "backup cc-hope-accounts run cc-hope-accounts sites/hope/accounts/apps/accounts/backend npm run --silent demo -- --profile community-family --force reload cc-hope-accounts wait 1.9.0" ] } @test "accounts: only a scenario, no admin email, and only a release that has scenarios" { run cmd_demo hope accounts --profile small [ "$status" -eq 1 ] [[ "$output" == *"clean-campus"* ]] run cmd_demo hope accounts --profile bold-youth --admin-email jane@hope.test [ "$status" -eq 1 ] rm "$TMP/sites/hope/accounts/apps/accounts/backend/scripts/demo.ts" run cmd_demo hope accounts --profile bold-youth [ "$status" -eq 1 ] [[ "$output" == *"no demo scenarios"* ]] [ ! -s "$CALLS" ] } @test "--scenario: a known one, and the profile must be the scenario's" { run cmd_demo hope website --profile bold-plant --scenario bold-youth [ "$status" -eq 1 ] [[ "$output" == *"loads the bold-youth profile"* ]] run cmd_demo hope accounts --profile bold-youth --scenario nowhere [ "$status" -eq 1 ] run cmd_demo hope website --profile bold-youth --scenario bold-youth [ "$status" -eq 0 ] } @test "website 1.8.0: --scenario is passed on, and only to a release that has scenarios" { run cmd_demo hope website --profile bold-youth --scenario bold-youth [ "$status" -eq 0 ] [[ "$(cat "$CALLS")" == *"npm run --silent demo -- --profile bold-youth --scenario bold-youth --force"* ]] : > "$CALLS" : > "$TMP/sites/hope/website/apps/website/backend/src/demo/seeds.ts" run cmd_demo hope website --profile bold-youth --scenario bold-youth [ "$status" -eq 1 ] [[ "$output" == *"no demo scenarios"* ]] [ ! -s "$CALLS" ] run cmd_demo hope website --profile bold-youth [ "$status" -eq 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 [ "$status" -eq 1 ] [[ "$output" == *"comes with App 1.0.0"* ]] [ ! -s "$CALLS" ] } @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 ] [[ "$(cat "$CALLS")" != *"reload"* ]] } @test "events 3.8.0: a demo scenario's church is an Events profile too" { run cmd_demo hope --profile clean-campus --scenario clean-campus [ "$status" -eq 0 ] [[ "$(cat "$CALLS")" == *"node scripts/set-demo.js --profile=clean-campus --yes"* ]] } @test "seed-scenario: only Accounts and the Website installed, both loaded in order, the rest skipped" { HAS="accounts website" WEBSITE_VERSION=1.8.0 run cmd_demo hope --scenario bold-youth [ "$status" -eq 0 ] [ "$(grep '^run' "$CALLS")" = "run cc-hope-accounts sites/hope/accounts/apps/accounts/backend npm run --silent demo -- --profile bold-youth --force run cc-hope-website sites/hope/website/apps/website/backend npm run --silent demo -- --profile bold-youth --scenario bold-youth --force" ] [[ "$output" == *"Not installed, so skipped: events app"* ]] [[ "$(cat "$CALLS")" != *"cc-hope-events"* ]] } @test "seed-scenario: every product the church has, Accounts, Events, then the Website" { EVENTS_VERSION=3.8.0 WEBSITE_VERSION=1.8.0 run cmd_demo hope --scenario clean-campus --admin-email jane@hope.test [ "$status" -eq 0 ] [ "$(grep '^run' "$CALLS" | cut -d' ' -f2)" = "cc-hope-accounts cc-hope-events cc-hope-website" ] [[ "$(cat "$CALLS")" == *"DEMO_ADMIN_EMAIL=jane@hope.test node scripts/set-demo.js --profile=clean-campus --yes"* ]] } @test "seed-scenario: a release too old for scenarios stops it before anything changes" { EVENTS_VERSION=3.7.2 WEBSITE_VERSION=1.8.0 run cmd_demo hope --scenario bold-plant [ "$status" -eq 1 ] [[ "$output" == *"Cross Events is 3.7.2 here: a demo scenario needs 3.8.0 or later"* ]] [ ! -s "$CALLS" ] } @test "seed-scenario: the first failure stops the rest and says what's out of step" { EVENTS_VERSION=3.8.0 WEBSITE_VERSION=1.8.0 FAIL_USER=cc-hope-events run cmd_demo hope --scenario bold-plant [ "$status" -eq 1 ] [[ "$output" == *"Cross Events's demo failed"* ]] [[ "$output" == *"Loaded: accounts. Out of step with bold-plant: events website"* ]] [[ "$(cat "$CALLS")" != *"cc-hope-website"* ]] } @test "seed-scenario: an App installed is refused (its demo comes with App 1.0.0), nothing changed" { HAS="accounts app" run cmd_demo hope --scenario bold-plant [ "$status" -eq 1 ] [[ "$output" == *"comes with App 1.0.0"* ]] [ ! -s "$CALLS" ] } @test "seed-scenario: a known scenario only" { run cmd_demo hope --scenario nowhere [ "$status" -eq 1 ] [ ! -s "$CALLS" ] }