GuDingZiChan/scripts/tests/deploy-disposal-precheck-test.sh

96 lines
2.8 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
TARGET_SCRIPT="${REPO_ROOT}/deploy.sh"
DEPLOY_SH_FUNCTIONS_ONLY=1 source "${TARGET_SCRIPT}"
MOCK_MIGRATION_STATE="pending"
MOCK_ACTIVE_COUNT="0"
MOCK_CANONICAL_ROWS=""
get_container_status() {
printf 'running\n'
}
run_equipment_mysql() {
local sql review_count
sql="$(cat)"
case "${sql}" in
*information_schema.tables*) printf '1\n' ;;
*_prisma_migrations*) printf '%s\n' "${MOCK_MIGRATION_STATE}" ;;
*"START TRANSACTION WITH CONSISTENT SNAPSHOT"*)
review_count="$(printf '%b\n' "${MOCK_CANONICAL_ROWS}" | awk 'NF { count += 1 } END { print count + 0 }')"
printf 'MISSING_ROOT_COUNT\t0\n'
printf 'TREE_ANOMALY_COUNT\t0\n'
printf 'ACTIVE_DISPOSAL_COUNT\t%s\n' "${MOCK_ACTIVE_COUNT}"
printf 'REVIEW_ROW_COUNT\t%s\n' "${review_count}"
if [ "${review_count}" -gt 0 ]; then
printf '%b\n' "${MOCK_CANONICAL_ROWS}" | awk 'NF { print "REVIEW_ROW\t" $0 }'
fi
;;
*) printf '' ;;
esac
}
assert_equal() {
local expected="$1"
local actual="$2"
if [ "${expected}" != "${actual}" ]; then
echo "Expected '${expected}', got '${actual}'" >&2
return 1
fi
}
assert_not_equal() {
local left="$1"
local right="$2"
if [ "${left}" = "${right}" ]; then
echo "Expected values to differ: '${left}'" >&2
return 1
fi
}
MOCK_MIGRATION_STATE="applied"
assert_equal "applied" "$(get_disposal_migration_state)"
DISPOSAL_PRECHECK_SQL="${REPO_ROOT}/does-not-exist.sql"
DISPOSAL_PRECHECK_REQUIRED=0
run_disposal_migration_precheck >/dev/null
assert_equal "0" "${DISPOSAL_PRECHECK_REQUIRED}"
MOCK_MIGRATION_STATE="incomplete"
assert_equal "incomplete" "$(get_disposal_migration_state)"
if run_disposal_migration_precheck >/dev/null 2>&1; then
echo "Incomplete migration must block deployment" >&2
exit 1
fi
MOCK_MIGRATION_STATE="pending"
assert_equal "pending" "$(get_disposal_migration_state)"
DISPOSAL_PRECHECK_SQL="${REPO_ROOT}/backend/prisma/migrations/20260714_asset_permissions_disposal_dingtalk/precheck.sql"
MOCK_ACTIVE_COUNT="1"
MOCK_CANONICAL_ROWS="D|1|pending|10\nT|1|0|10|AVAILABLE|NULL|2026-07-16T10:00:00.000"
calculate_active_disposal_fingerprint
first_fingerprint="${CURRENT_DISPOSAL_FINGERPRINT}"
MOCK_CANONICAL_ROWS="D|1|pending|10\nT|1|0|10|BORROWED|NULL|2026-07-16T10:01:00.000"
calculate_active_disposal_fingerprint
second_fingerprint="${CURRENT_DISPOSAL_FINGERPRINT}"
assert_not_equal "${first_fingerprint}" "${second_fingerprint}"
for required_field in \
"'parent_id'" \
"'current_status'" \
"'department_id'" \
"'storage_location_id'" \
"'equipment_updated_at'"; do
if ! grep -q "${required_field}" "${DISPOSAL_PRECHECK_SQL}"; then
echo "Fingerprint is missing ${required_field}" >&2
exit 1
fi
done