fischer-agentkit/scripts/test-start.sh

193 lines
6.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# =============================================================================
# Fischer AgentKit — 一键测试环境脚本
# =============================================================================
#
# 启动完整测试环境:后端 API + E2E 测试(使用独立端口,不影响开发环境)
#
# 用法:
# bash scripts/test-start.sh # 启动测试环境并运行全部测试
# bash scripts/test-start.sh --unit # 仅单元测试
# bash scripts/test-start.sh --e2e # 仅 E2E 测试
# bash scripts/test-start.sh --bitable # 仅 bitable 模块测试
# bash scripts/test-start.sh --skip-server # 跳过服务启动(使用已有服务)
# bash scripts/test-start.sh --help # 帮助
# =============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"
: "${E2E_PORT:=18765}"
: "${E2E_API_KEY:=ak_live_e2e_test_key_000000000000000000000000000000000000000000000000}"
# ── 颜色 ────────────────────────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# ── 帮助 ────────────────────────────────────────────────────────────────────
show_help() {
cat <<-'EOF'
Fischer AgentKit — 测试环境
用法: bash scripts/test-start.sh [选项]
选项:
--unit 仅运行单元测试pytest -m "not integration"
--e2e 仅运行 E2E 测试
--bitable 仅运行 bitable 模块测试
--skip-server 跳过服务启动(使用已有服务)
--help 显示帮助
环境变量:
E2E_PORT 测试服务器端口(默认: 18765
E2E_API_KEY 测试 API Key默认: 内置测试 Key
EOF
}
# ── 参数解析 ────────────────────────────────────────────────────────────────
RUN_MODE="all"
SKIP_SERVER=0
while [[ $# -gt 0 ]]; do
case $1 in
--unit) RUN_MODE="unit"; shift ;;
--e2e) RUN_MODE="e2e"; shift ;;
--bitable) RUN_MODE="bitable"; shift ;;
--skip-server) SKIP_SERVER=1; shift ;;
--help|-h) show_help; exit 0 ;;
*) shift ;;
esac
done
# ── 日志函数 ────────────────────────────────────────────────────────────────
info() { echo -e "${BLUE}[INFO]${NC} $*"; }
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
fail() { echo -e "${RED}[FAIL]${NC} $*"; }
# ── 服务管理 ────────────────────────────────────────────────────────────────
start_test_server() {
if [[ $SKIP_SERVER -eq 1 ]]; then
info "SKIP_SERVER=1跳过服务启动"
return 0
fi
info "启动测试服务器(端口 $E2E_PORT..."
if lsof -i :$E2E_PORT 2>/dev/null | grep -q LISTEN; then
ok "测试服务器已在端口 $E2E_PORT 运行"
return 0
fi
export AGENTKIT_E2E_MODE=1
export AGENTKIT_WS_TIMEOUT=0
export AGENTKIT_API_KEY="$E2E_API_KEY"
source .venv/bin/activate 2>/dev/null || true
python3 -m agentkit.cli.main serve --host 127.0.0.1 --port "$E2E_PORT" &
SERVER_PID=$!
# 等待就绪
local attempt=0
while [[ $attempt -lt 60 ]]; do
if curl -sf "http://127.0.0.1:$E2E_PORT/api/v1/health" &>/dev/null; then
ok "测试服务器就绪 (PID $SERVER_PID,端口 $E2E_PORT)"
return 0
fi
sleep 0.5
((attempt++))
done
fail "测试服务器启动超时"
kill $SERVER_PID 2>/dev/null || true
exit 1
}
stop_test_server() {
if [[ $SKIP_SERVER -eq 1 ]]; then return 0; fi
PID=$(lsof -ti :$E2E_PORT 2>/dev/null || true)
if [[ -n "$PID" ]]; then
kill $PID 2>/dev/null && ok "测试服务器已停止" || true
fi
}
# ── 测试执行 ────────────────────────────────────────────────────────────────
run_unit_tests() {
info "运行单元测试..."
source .venv/bin/activate 2>/dev/null || true
pytest -m "not integration" -q --tb=short
ok "单元测试完成"
}
run_bitable_tests() {
info "运行 bitable 模块测试..."
source .venv/bin/activate 2>/dev/null || true
pytest tests/unit/bitable/ -v --tb=short
ok "bitable 测试完成"
}
run_e2e_tests() {
info "运行 E2E 测试..."
FE_DIR="$PROJECT_ROOT/src/agentkit/server/frontend"
cd "$FE_DIR"
if [[ ! -d node_modules ]]; then
info "安装前端依赖..."
npm install
fi
export AGENTKIT_SERVER_URL="http://127.0.0.1:$E2E_PORT"
export AGENTKIT_API_KEY="$E2E_API_KEY"
npm run test:e2e
ok "E2E 测试完成"
cd "$PROJECT_ROOT"
}
# ── 主流程 ─────────────────────────────────────────────────────────────────
echo ""
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo -e "${CYAN} Fischer AgentKit — 测试环境${NC}"
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo ""
trap stop_test_server EXIT INT TERM
case $RUN_MODE in
unit)
run_unit_tests
;;
e2e)
start_test_server
run_e2e_tests
;;
bitable)
run_bitable_tests
;;
all)
info "运行全部测试..."
run_unit_tests
echo ""
run_bitable_tests
echo ""
start_test_server
run_e2e_tests
;;
esac
echo ""
ok "测试完成!"