Files
blog/write-server/scripts/test-features.sh
T
2026-06-21 20:42:44 +08:00

277 lines
7.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
# Write Server 功能测试脚本
# 测试在线写作和 Telegram Bot 功能
set -e
echo "==================================="
echo " Write Server 功能测试"
echo "==================================="
echo ""
# 加载环境变量
if [ -f .env ]; then
source .env
fi
PORT=${PORT:-8016}
BASE_URL="http://localhost:$PORT"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 测试结果
TESTS_PASSED=0
TESTS_FAILED=0
# 测试函数
test_pass() {
echo -e "${GREEN}✓${NC} $1"
((TESTS_PASSED++))
}
test_fail() {
echo -e "${RED}✗${NC} $1"
((TESTS_FAILED++))
}
# 检查服务是否运行
echo "1. 检查服务状态"
if curl -s "$BASE_URL/api/stats" > /dev/null 2>&1; then
test_pass "服务正在运行"
else
test_fail "服务未运行"
echo " 请先启动服务: ./scripts/start.sh"
exit 1
fi
echo ""
# 测试文章列表 API
echo "2. 测试文章管理功能"
RESPONSE=$(curl -s "$BASE_URL/api/posts?page=1&perPage=5")
if echo "$RESPONSE" | grep -q '"posts"'; then
test_pass "文章列表 API 正常"
# 解析文章数量
TOTAL=$(echo "$RESPONSE" | grep -o '"total":[0-9]*' | cut -d: -f2)
echo " 📝 共 $TOTAL 篇文章"
else
test_fail "文章列表 API 异常"
fi
echo ""
# 测试单篇文章 API
echo "3. 测试文章详情功能"
# 获取第一篇文章的 slug
FIRST_SLUG=$(curl -s "$BASE_URL/api/posts?page=1&perPage=1" | grep -o '"slug":"[^"]*"' | head -n 1 | cut -d'"' -f4)
if [ -n "$FIRST_SLUG" ]; then
POST_RESPONSE=$(curl -s "$BASE_URL/api/posts/$FIRST_SLUG")
if echo "$POST_RESPONSE" | grep -q '"title"'; then
test_pass "文章详情 API 正常"
TITLE=$(echo "$POST_RESPONSE" | grep -o '"title":"[^"]*"' | head -n 1 | cut -d'"' -f4)
echo " 📄 测试文章: $TITLE"
else
test_fail "文章详情 API 异常"
fi
else
test_fail "无法获取文章列表"
fi
echo ""
# 测试统计 API
echo "4. 测试统计功能"
STATS_RESPONSE=$(curl -s "$BASE_URL/api/stats")
if echo "$STATS_RESPONSE" | grep -q '"total"'; then
test_pass "统计 API 正常"
else
test_fail "统计 API 异常"
fi
echo ""
# 测试 Hugo 集成
echo "5. 测试 Hugo 集成"
HUGO_RESPONSE=$(curl -s "$BASE_URL/api/hugo")
if echo "$HUGO_RESPONSE" | grep -q '"running"'; then
test_pass "Hugo API 正常"
# 检查 Hugo 是否运行
if echo "$HUGO_RESPONSE" | grep -q '"running":true'; then
echo " 🚀 Hugo 预览服务正在运行"
else
echo " ⚠️ Hugo 预览服务未运行(将按需启动)"
fi
else
test_fail "Hugo API 异常"
fi
echo ""
# 测试评论系统(如果配置了)
echo "6. 测试评论系统"
if [ -n "$ARTALK_SERVER" ]; then
COMMENTS_RESPONSE=$(curl -s "$BASE_URL/api/comments/login" -X POST -H "Content-Type: application/json" -d '{}")
if [ $? -eq 0 ]; then
test_pass "评论 API 可访问"
else
test_fail "评论 API 无法访问"
fi
else
echo -e "${YELLOW}⚠${NC} Artalk 未配置,跳过评论测试"
fi
echo ""
# 测试 Telegram Bot(如果配置了)
echo "7. 测试 Telegram Bot"
if [ -n "$TG_BOT_TOKEN" ]; then
BOT_RESPONSE=$(curl -s "$BASE_URL/api/bot/status")
if echo "$BOT_RESPONSE" | grep -q '"running"'; then
test_pass "Bot API 正常"
# 检查 Bot 状态
if echo "$BOT_RESPONSE" | grep -q '"running":true'; then
echo " 🤖 Telegram Bot 正在运行"
else
echo " ⚠️ Telegram Bot 未运行"
fi
else
test_fail "Bot API 异常"
fi
else
echo -e "${YELLOW}⚠${NC} Telegram Bot Token 未配置,跳过 Bot 测试"
fi
echo ""
# 测试在线写作功能(模拟)
echo "8. 测试在线写作功能"
# 测试创建文章 API
TEST_SLUG="test-$(date +%s)"
TEST_POST_DATA='{
"frontMatter": {
"title": "测试文章",
"slug": "'$TEST_SLUG'",
"date": "'$(date -Iseconds)'",
"draft": true,
"categories": ["测试"],
"tags": ["test"],
"author": "测试"
},
"content": "这是一篇测试文章的内容。"
}'
CREATE_RESPONSE=$(curl -s "$BASE_URL/api/posts" -X POST -H "Content-Type: application/json" -d "$TEST_POST_DATA")
if echo "$CREATE_RESPONSE" | grep -q '"success":true'; then
test_pass "创建文章 API 正常"
echo " 📝 测试文章已创建: $TEST_SLUG"
# 测试更新文章
UPDATE_DATA='{
"frontMatter": {
"title": "测试文章(已更新)"
},
"content": "这是更新后的内容。"
}'
UPDATE_RESPONSE=$(curl -s "$BASE_URL/api/posts/$TEST_SLUG" -X PUT -H "Content-Type: application/json" -d "$UPDATE_DATA")
if echo "$UPDATE_RESPONSE" | grep -q '"success":true'; then
test_pass "更新文章 API 正常"
else
test_fail "更新文章 API 异常"
fi
# 测试删除文章(移到回收站)
DELETE_RESPONSE=$(curl -s "$BASE_URL/api/posts/$TEST_SLUG" -X DELETE)
if echo "$DELETE_RESPONSE" | grep -q '"success":true'; then
test_pass "删除文章 API 正常"
echo " 🗑️ 测试文章已移到回收站"
else
test_fail "删除文章 API 异常"
fi
else
test_fail "创建文章 API 异常"
echo " 响应: $CREATE_RESPONSE"
fi
echo ""
# 测试回收站功能
echo "9. 测试回收站功能"
RECYCLE_RESPONSE=$(curl -s "$BASE_URL/api/recycle")
if echo "$RECYCLE_RESPONSE" | grep -q '"posts"'; then
test_pass "回收站 API 正常"
else
test_fail "回收站 API 异常"
fi
echo ""
# 测试图片上传(模拟)
echo "10. 测试图片上传功能"
# 创建一个测试图片
TEST_IMAGE="/tmp/test-image.png"
convert -size 100x100 xc:white "$TEST_IMAGE" 2>/dev/null || {
# 如果没有 imagemagick,创建一个空文件
echo -n "" > "$TEST_IMAGE"
}
if [ -f "$TEST_IMAGE" ]; then
UPLOAD_RESPONSE=$(curl -s "$BASE_URL/api/upload" -X POST -F "file=@$TEST_IMAGE" 2>/dev/null || echo '{"error":"upload failed"}')
if echo "$UPLOAD_RESPONSE" | grep -q '"success"'; then
test_pass "图片上传 API 正常"
else
test_fail "图片上传 API 异常"
fi
rm -f "$TEST_IMAGE"
else
echo -e "${YELLOW}⚠${NC} 无法创建测试图片,跳过上传测试"
fi
echo ""
# 测试前端页面
echo "11. 测试前端页面"
PAGES=("/" "/edit" "/comments" "/feeds" "/links" "/images" "/recycle")
for page in "${PAGES[@]}"; do
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL$page")
if [ "$RESPONSE" = "200" ]; then
test_pass "页面 $page 正常"
else
test_fail "页面 $page 异常 (HTTP $RESPONSE)"
fi
done
echo ""
# 总结
echo "==================================="
echo " 测试完成"
echo "==================================="
echo ""
echo "通过: $TESTS_PASSED"
echo "失败: $TESTS_FAILED"
echo ""
if [ $TESTS_FAILED -gt 0 ]; then
echo -e "${RED}部分测试失败${NC}"
exit 1
else
echo -e "${GREEN}所有测试通过!${NC}"
echo ""
echo "在线写作功能:✓ 正常"
echo "Telegram Bot:$([ -n "$TG_BOT_TOKEN" ] && echo "✓ 已配置" || echo "⚠ 未配置")"
echo ""
echo "访问地址: $BASE_URL"
exit 0
fi