Files
wecom_it_smart_desk/scripts/test_quick_rules_api.sh
T
Simon 7864ab404b feat(scripts): 收纳运维/部署/修复/调试脚本到 scripts/ 目录
**目录结构**:
- scripts/                 顶层运维/工具脚本(20 个)
- scripts/deploy/          一键部署脚本(deploy_*.py / upload_*.py / redeploy.py 等 8 个)
- scripts/fix/             一次性修复脚本(fix_*.py / fix_*.cypher / fix_*.sql 等 9 个)
- scripts/debug/           调试脚本集合(check_*.py/sql、test_*.py、debug_*.py 等 42 个)

**ops-tools/jms_ops.py**:jumpserver-ops skill 默认路径修复
- 从 'jumpserver-automation-shareable' 改为 'jumpserver-ops'
- 同步更新注释(优先 JP_SKILL_DIR 环境变量)

**典型脚本用途**:
- scripts/deploy-v1.2.ps1 / -staging.ps1:一键部署 v1.2 到生产 / staging
- scripts/init_quick_rules.sql:quick_rules 表初始数据
- scripts/gen_admin_token.py:生成 admin JWT token(调试用)
- scripts/build-and-verify.sh:v1.1 实施验证脚本
- scripts/verify_*.sh / .py:API/quick_rules 验证
- scripts/fix/*.py:环境修复(compose、env_key、itsm_bridge 等)

合计 81 文件 + 3317 行 / - 1 行
2026-08-03 18:48:02 +08:00

234 lines
9.1 KiB
Bash
Raw 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.
#!/bin/bash
# =============================================================================
# 企微IT智能服务台 — 快速规则 API 联调测试脚本
# =============================================================================
# 用途:独立测试所有 12 个快速规则 API
# 日期:2026-07-27
# =============================================================================
# 默认配置
BASE_URL="${API_BASE_URL:-http://localhost:8000}"
TOKEN="${API_TOKEN:-}"
# 颜色
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 计数器
PASS=0
FAIL=0
# --------------------------------------------------------------------------
# 工具函数
# --------------------------------------------------------------------------
check_response() {
local response="$1"
local expected_code="$2"
local test_name="$3"
if echo "$response" | grep -q "\"code\":$expected_code"; then
echo -e "${GREEN}✓ PASS${NC}: $test_name"
((PASS++))
else
echo -e "${RED}✗ FAIL${NC}: $test_name"
echo "Response: $response"
((FAIL++))
fi
}
# --------------------------------------------------------------------------
# 0. 健康检查
# --------------------------------------------------------------------------
echo -e "${YELLOW}=== 0. 健康检查 ===${NC}"
RESPONSE=$(curl -s -X GET "$BASE_URL/health")
echo "Health: $RESPONSE"
# --------------------------------------------------------------------------
# 1. 测试登录(获取 token
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 1. 登录 ===${NC}"
echo "请先用浏览器登录获取 token,或使用下面的方式:"
# 直接测试(需要 token
if [ -z "$TOKEN" ]; then
echo -e "${RED}错误:请先设置 API_TOKEN 环境变量${NC}"
echo "使用方式:API_TOKEN=your_token bash $0"
exit 1
fi
# --------------------------------------------------------------------------
# 2. 规则列表
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 2. 规则列表 ===${NC}"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/admin/quick-rules?page=1&size=10" \
-H "Authorization: Bearer $TOKEN")
check_response "$RESPONSE" "0" "GET /quick-rules"
# --------------------------------------------------------------------------
# 3. 创建规则
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 3. 创建规则 ===${NC}"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/quick-rules" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"rule_type": "greeting",
"keyword": "test_你好",
"priority": 10,
"is_active": true
}')
check_response "$RESPONSE" "0" "POST /quick-rules"
RULE_ID=$(echo "$RESPONSE" | grep -oP '"id":\K\d+' | head -1)
echo "Created rule ID: $RULE_ID"
# --------------------------------------------------------------------------
# 4. 更新规则
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 4. 更新规则 ===${NC}"
RESPONSE=$(curl -s -X PUT "$BASE_URL/api/admin/quick-rules/$RULE_ID" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"priority": 20,
"is_active": false
}')
check_response "$RESPONSE" "0" "PUT /quick-rules/$RULE_ID"
# --------------------------------------------------------------------------
# 5. 批量导入
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 5. 批量导入 ===${NC}"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/quick-rules/import" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"rules": [
{"rule_type": "greeting", "keyword": "test_hey", "priority": 5},
{"rule_type": "greeting", "keyword": "test_hello", "priority": 5}
],
"mode": "skip_duplicates"
}')
check_response "$RESPONSE" "0" "POST /quick-rules/import"
# --------------------------------------------------------------------------
# 6. 批量导出
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 6. 批量导出 ===${NC}"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/admin/quick-rules/export?format=json" \
-H "Authorization: Bearer $TOKEN")
check_response "$RESPONSE" "0" "GET /quick-rules/export"
# --------------------------------------------------------------------------
# 7. 智能体更新(高置信度)
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 7. 智能体更新(高置信度) ===${NC}"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/quick-rules/agent-update" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"rule_type": "routing_prefilter",
"category": "IT服务",
"keyword": "test_投影仪",
"priority": 5,
"confidence": 0.92,
"reason": "测试用例",
"agent_id": "test_agent"
}')
check_response "$RESPONSE" "0" "POST /quick-rules/agent-update (confidence=0.92)"
# --------------------------------------------------------------------------
# 8. 智能体更新(中等置信度)
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 8. 智能体更新(中等置信度) ===${NC}"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/quick-rules/agent-update" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"rule_type": "routing_prefilter",
"category": "IT服务",
"keyword": "test_音响",
"priority": 5,
"confidence": 0.7,
"reason": "测试用例",
"agent_id": "test_agent"
}')
check_response "$RESPONSE" "0" "POST /quick-rules/agent-update (confidence=0.7)"
# --------------------------------------------------------------------------
# 9. 智能体更新(低置信度)
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 9. 智能体更新(低置信度) ===${NC}"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/quick-rules/agent-update" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"rule_type": "routing_prefilter",
"category": "IT服务",
"keyword": "test_键盘",
"priority": 5,
"confidence": 0.3,
"reason": "测试用例",
"agent_id": "test_agent"
}')
check_response "$RESPONSE" "0" "POST /quick-rules/agent-update (confidence=0.3)"
# --------------------------------------------------------------------------
# 10. 审计日志
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 10. 审计日志 ===${NC}"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/admin/quick-rules/audit-log?page=1&size=10" \
-H "Authorization: Bearer $TOKEN")
check_response "$RESPONSE" "0" "GET /quick-rules/audit-log"
# --------------------------------------------------------------------------
# 11. 统计详情
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 11. 统计详情 ===${NC}"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/admin/quick-rules/stats/detail" \
-H "Authorization: Bearer $TOKEN")
check_response "$RESPONSE" "0" "GET /quick-rules/stats/detail"
# --------------------------------------------------------------------------
# 12. 刷新缓存
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 12. 刷新缓存 ===${NC}"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/quick-rules/refresh" \
-H "Authorization: Bearer $TOKEN")
check_response "$RESPONSE" "0" "POST /quick-rules/refresh"
# --------------------------------------------------------------------------
# 13. 批量删除
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 13. 批量删除 ===${NC}"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/quick-rules/batch-delete" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"ids\": [$RULE_ID]}")
check_response "$RESPONSE" "0" "POST /quick-rules/batch-delete"
# --------------------------------------------------------------------------
# 14. 简单统计
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 14. 简单统计 ===${NC}"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/admin/quick-rules/stats" \
-H "Authorization: Bearer $TOKEN")
check_response "$RESPONSE" "0" "GET /quick-rules/stats"
# --------------------------------------------------------------------------
# 汇总
# --------------------------------------------------------------------------
echo -e "\n${YELLOW}=== 测试汇总 ===${NC}"
echo -e "${GREEN}通过: $PASS${NC}"
echo -e "${RED}失败: $FAIL${NC}"
if [ $FAIL -eq 0 ]; then
echo -e "${GREEN}✓ 所有测试通过!${NC}"
exit 0
else
echo -e "${RED}✗ 有 $FAIL 个测试失败${NC}"
exit 1
fi