#!/usr/bin/env bash # 隔离库跑 document_duty 相关 PostgreSQL 集成测试。 # 不启停/重建 compose,不连 agent_system_dev / 共享对话库。 # # 用法 A(已有隔离库): # TEST_DATABASE_URL='postgres://app:pass@127.0.0.1:55432/agent_duty_sync_itest?sslmode=disable' \ # ./scripts/run-isolated-duty-sync-pg-tests.sh # # 用法 B(Codex/本机用管理连接建临时库,测完删除): # PG_ADMIN_URL='postgres://postgres:pass@127.0.0.1:55432/postgres?sslmode=disable' \ # ./scripts/run-isolated-duty-sync-pg-tests.sh # # 禁止:把 TEST_DATABASE_URL 指到 agent_system_dev、5433 默认开发库。 set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "${ROOT_DIR}" refuse_shared_dsn() { local dsn="$1" if [[ "${dsn}" == *"agent_system_dev"* || "${dsn}" == *"/agent_system_test"* || "${dsn}" == *"/agent_system_main"* ]]; then echo "[错误] 拒绝共享环境库名(agent_system_dev/test/main)。请 CREATE DATABASE 临时库。" >&2 exit 2 fi } rewrite_db_name() { local src="$1" local dbname="$2" local prefix query if [[ "${src}" == *"?"* ]]; then prefix="${src%%\?*}" query="${src#*\?}" echo "${prefix%/*}/${dbname}?${query}" else echo "${src%/*}/${dbname}" fi } CREATED_DB="" ADMIN_URL="${PG_ADMIN_URL:-}" TEST_URL="${TEST_DATABASE_URL:-}" if [[ -n "${ADMIN_URL}" ]]; then refuse_shared_dsn "${ADMIN_URL}" if ! command -v psql >/dev/null 2>&1; then echo "[错误] 需要 psql 才能用 PG_ADMIN_URL 建临时库" >&2 exit 1 fi CREATED_DB="agent_duty_sync_itest_$$" echo "[info] creating isolated database ${CREATED_DB}" psql "${ADMIN_URL}" -v ON_ERROR_STOP=1 -c "CREATE DATABASE ${CREATED_DB};" cleanup() { if [[ -n "${CREATED_DB}" ]]; then echo "[info] dropping isolated database ${CREATED_DB}" psql "${ADMIN_URL}" -v ON_ERROR_STOP=1 -c "DROP DATABASE IF EXISTS ${CREATED_DB};" || true fi } trap cleanup EXIT TEST_URL="$(rewrite_db_name "${ADMIN_URL}" "${CREATED_DB}")" elif [[ -n "${TEST_URL}" ]]; then refuse_shared_dsn "${TEST_URL}" else echo "[错误] 请设置 TEST_DATABASE_URL(隔离库)或 PG_ADMIN_URL(自动建临时库)" >&2 echo "示例:" >&2 echo " createdb agent_duty_sync_itest" >&2 echo " TEST_DATABASE_URL='postgres://app:pass@127.0.0.1:55432/agent_duty_sync_itest?sslmode=disable' \\" >&2 echo " go test ./internal/docduty ./internal/authz ./internal/server -count=1 -run 'Duty|ReviewerDuty|DocumentDuty|ConcurrentLastWriter|OwnerAuthor'" >&2 exit 1 fi export TEST_DATABASE_URL="${TEST_URL}" echo "[info] running isolated duty-sync PostgreSQL tests" # 包串行,避免并行 goose 抢同一临时库。 go test ./internal/docduty -count=1 -run 'Duty|ReviewerDuty|DocumentDuty|OwnerAuthor|HealBinding|ApplySlotRevocations|HealProjectDocuments|TransferDoesNotResurrect' go test ./internal/authz -count=1 -run 'ConcurrentLastWriter|DocumentDuty|AtomicDutyRevoke|UpsertProjectMember' go test ./internal/server -count=1 -run 'AuthorizeDocRevision|ResolveUpdateProjectRole|WriteDutySyncError'