#!/usr/bin/env bash set -u -o pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" CLIENT_DIR="$ROOT_DIR/client" SERVER_DIR="$ROOT_DIR/server" PUBLIC_BASE_URL="${LIVE_SMOKE_BASE_URL:-https://oa.hyinda.com}" REPORT_DIR="${P2_SAFE_REPORT_DIR:-$ROOT_DIR/artifacts/qa-flow/p2-safe-regression-$(date +%Y%m%d-%H%M%S)}" SUMMARY_TXT="$REPORT_DIR/summary.txt" STATUS=0 mkdir -p "$REPORT_DIR" : >"$SUMMARY_TXT" load_default_env() { local env_file="${P2_SAFE_ENV_FILE:-$HOME/.hyb-oa/live-smoke.env}" if [[ "${P2_SAFE_SKIP_ENV_FILE:-0}" == "1" || ! -f "$env_file" ]]; then return 0 fi set -a # shellcheck disable=SC1090 source "$env_file" set +a PUBLIC_BASE_URL="${LIVE_SMOKE_BASE_URL:-$PUBLIC_BASE_URL}" echo "[p2-safe] loaded masked env file" | tee -a "$SUMMARY_TXT" } run_step() { local name="$1" shift local log_file="$REPORT_DIR/${name}.log" echo ">>> [p2-safe] $name" | tee -a "$SUMMARY_TXT" if "$@" > >(tee "$log_file") 2>&1; then echo "[p2-safe][PASS] $name" | tee -a "$SUMMARY_TXT" else local code="$?" echo "[p2-safe][FAIL] $name exit=$code" | tee -a "$SUMMARY_TXT" STATUS=1 fi } run_shell_step() { local name="$1" local command="$2" run_step "$name" bash -lc "$command" } write_summary_json() { local qa_status="PASS" if [[ "$STATUS" -ne 0 ]]; then qa_status="FAIL" fi QA_REPORT_DIR="$REPORT_DIR" \ QA_ISSUE_ID="${QA_ISSUE_ID:-P2AF}" \ QA_BASE_URL="$PUBLIC_BASE_URL" \ QA_MODE="p2-safe-regression" \ QA_STATUS="$qa_status" \ QA_SUMMARY_NOTES="P2AF 安全自动回归:不部署、不启用公网 payment_batch_v1、不开放图片 OCR;图片 OCR 默认 FREEZE_BY_DEFAULT。" \ node "$ROOT_DIR/scripts/qa/write-qa-summary.mjs" >/dev/null 2>&1 || true } finish() { write_summary_json echo "[p2-safe] report=$REPORT_DIR" | tee -a "$SUMMARY_TXT" exit "$STATUS" } trap finish EXIT load_default_env run_shell_step "backend-prisma" "cd '$SERVER_DIR' && npm run prisma:validate" run_shell_step "backend-node-check" "cd '$SERVER_DIR' && node --check src/routes/invoicePool.js src/jobs/ocrJob.js src/services/ocr/tencent.js src/routes/paymentBatches.js src/routes/exports.js src/routes/workflow.js src/routes/wecomAuth.js" run_shell_step "backend-focused-tests" "cd '$SERVER_DIR' && npm test -- tests/unit/services/ocr.tencent.test.js tests/unit/jobs/ocrJob.test.js tests/unit/routes/invoicePool.upload.test.js tests/unit/scripts/ocr-provider-samples.test.js tests/unit/routes/paymentBatches.reimbursement.test.js tests/unit/routes/exports.apOperations.test.js tests/unit/routes/wecomAuth.test.js tests/unit/routes/notifications.test.js" if [[ "${P2_SAFE_SKIP_BUILD:-0}" == "1" ]]; then echo "[p2-safe][SKIP] frontend-build P2_SAFE_SKIP_BUILD=1" | tee -a "$SUMMARY_TXT" else run_shell_step "frontend-typecheck" "cd '$CLIENT_DIR' && npm run typecheck" run_shell_step "frontend-build" "cd '$CLIENT_DIR' && npm run build" fi run_shell_step "business-flow-local" "BUSINESS_FLOW_GROUPS=auth,reimbursement,approval,leave,procurement,governance BUSINESS_FLOW_EXCLUDE_SPECS=tests/e2e/reimbursement-payment-batch-flow.spec.ts bash '$ROOT_DIR/scripts/qa/run-business-flow-gates.sh' local" if [[ "${P2_SAFE_SKIP_LIVE:-0}" == "1" ]]; then echo "[p2-safe][SKIP] live-readiness P2_SAFE_SKIP_LIVE=1" | tee -a "$SUMMARY_TXT" echo "[p2-safe][SKIP] payment-batch-flag-off P2_SAFE_SKIP_LIVE=1" | tee -a "$SUMMARY_TXT" echo "[p2-safe][SKIP] public-mobile-matrix P2_SAFE_SKIP_LIVE=1" | tee -a "$SUMMARY_TXT" else run_shell_step "live-readiness" "QA_ISSUE_ID=P2AF LIVE_SMOKE_BASE_URL='$PUBLIC_BASE_URL' bash '$ROOT_DIR/scripts/qa/run-live-smoke-readiness.sh'" run_shell_step "payment-batch-flag-off" "LIVE_SMOKE_BASE_URL='$PUBLIC_BASE_URL' node '$ROOT_DIR/scripts/qa/check-payment-batch-flag-off.mjs' --json" run_shell_step "public-mobile-matrix" "cd '$CLIENT_DIR' && E2E_BASE_URL='$PUBLIC_BASE_URL' npx playwright test tests/e2e/dg7-mobile-information-dedupe.spec.ts tests/e2e/r124-mobile-entry-matrix.spec.ts --workers=1 --reporter=list" fi run_shell_step "wecom-rendering-matrix" "cd '$CLIENT_DIR' && E2E_BASE_URL='$PUBLIC_BASE_URL' npx playwright test tests/e2e/r6-wecom-mobile-rendering-hardening.spec.ts --workers=1 --reporter=list" run_shell_step "ocr-live-runner-static" "node --check '$ROOT_DIR/scripts/qa/check-ocr-provider-samples.mjs'"