重构: 迁移后端代码到 app 目录,前端移动到 WebUI,添加完整测试套件

主要变更:
- 后端代码从根目录迁移到 app/ 目录
- 前端代码从 frontend/ 重命名为 WebUI/
- 更新所有导入路径以适配新结构
- 提取公共 API 响应函数到 app/api/common.py
- 精简验证器服务代码
- 更新启动脚本和文档

测试:
- 新增完整测试套件 (tests/)
- 单元测试: 模型、仓库层
- 集成测试: 覆盖所有 22+ API 端点
- E2E 测试: 4个完整工作流场景
- 添加 pytest 配置和测试运行脚本
This commit is contained in:
祀梦
2026-04-04 13:32:36 +08:00
parent df3cc87f88
commit 38bd66128b
109 changed files with 2017 additions and 548 deletions

1
tests/e2e/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""端到端测试"""

View File

@@ -0,0 +1,182 @@
"""完整工作流 E2E 测试
这些测试模拟真实用户场景,验证整个系统的集成功能。
"""
import pytest
class TestFullWorkflow:
"""测试完整工作流"""
@pytest.mark.asyncio
async def test_proxy_management_workflow(self, client):
"""测试代理管理完整工作流
场景:
1. 查看统计信息
2. 列出代理
3. 触发爬取
4. 查看更新后的统计
5. 导出代理
6. 清理无效代理
"""
# 1. 获取初始统计
response = await client.get("/api/proxies/stats")
assert response.status_code == 200
initial_stats = response.json()["data"]
# 2. 列出代理
response = await client.post("/api/proxies", json={
"page": 1,
"page_size": 20,
})
assert response.status_code == 200
# 3. 触发所有插件爬取
response = await client.post("/api/plugins/crawl-all")
assert response.status_code == 200
crawl_result = response.json()["data"]
# 4. 获取更新后的统计
response = await client.get("/api/proxies/stats")
updated_stats = response.json()["data"]
# 5. 导出代理(所有格式)
for fmt in ["csv", "txt", "json"]:
response = await client.get(f"/api/proxies/export/{fmt}")
assert response.status_code == 200
# 6. 清理无效代理
response = await client.delete("/api/proxies/clean-invalid")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_plugin_management_workflow(self, client):
"""测试插件管理完整工作流
场景:
1. 列出所有插件
2. 禁用某个插件
3. 更新插件配置
4. 启用插件
5. 触发单个插件爬取
"""
# 1. 列出插件
response = await client.get("/api/plugins")
assert response.status_code == 200
plugins = response.json()["data"]["plugins"]
if not plugins:
pytest.skip("没有可用的插件")
plugin_id = plugins[0]["id"]
# 2. 禁用插件
response = await client.put(f"/api/plugins/{plugin_id}/toggle", json={"enabled": False})
assert response.status_code == 200
# 3. 获取插件配置
response = await client.get(f"/api/plugins/{plugin_id}/config")
assert response.status_code == 200
# 4. 更新插件配置
response = await client.post(
f"/api/plugins/{plugin_id}/config",
json={"config": {"max_pages": 3}}
)
assert response.status_code == 200
# 5. 启用插件
response = await client.put(f"/api/plugins/{plugin_id}/toggle", json={"enabled": True})
assert response.status_code == 200
# 6. 触发爬取
response = await client.post(f"/api/plugins/{plugin_id}/crawl")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_scheduler_workflow(self, client):
"""测试调度器工作流
场景:
1. 启动调度器
2. 触发立即验证
3. 检查状态
4. 停止调度器
"""
# 1. 启动调度器
response = await client.post("/api/scheduler/start")
assert response.status_code == 200
# 2. 触发立即验证
response = await client.post("/api/scheduler/validate-now")
assert response.status_code == 200
# 3. 检查状态
response = await client.get("/api/scheduler/status")
assert response.status_code == 200
assert response.json()["data"]["running"] is True
# 4. 停止调度器
response = await client.post("/api/scheduler/stop")
assert response.status_code == 200
assert response.json()["data"]["running"] is False
@pytest.mark.asyncio
async def test_settings_workflow(self, client):
"""测试设置工作流
场景:
1. 获取当前设置
2. 修改设置
3. 验证设置已保存
4. 恢复默认设置
"""
# 1. 获取当前设置
response = await client.get("/api/settings")
assert response.status_code == 200
original_settings = response.json()["data"]
# 2. 修改设置
new_settings = original_settings.copy()
new_settings["crawl_timeout"] = 45
new_settings["auto_validate"] = not original_settings["auto_validate"]
response = await client.post("/api/settings", json=new_settings)
assert response.status_code == 200
# 3. 验证设置已保存
response = await client.get("/api/settings")
saved_settings = response.json()["data"]
assert saved_settings["crawl_timeout"] == 45
# 4. 恢复原始设置
response = await client.post("/api/settings", json=original_settings)
assert response.status_code == 200
@pytest.mark.asyncio
async def test_batch_operations_workflow(self, client):
"""测试批量操作工作流
场景:
1. 批量删除不存在的代理(幂等性测试)
2. 导出所有代理
3. 获取随机代理
"""
# 1. 批量删除(幂等性)
response = await client.post("/api/proxies/batch-delete", json={
"proxies": [
{"ip": "192.168.100.1", "port": 8080},
{"ip": "192.168.100.2", "port": 8081},
]
})
assert response.status_code == 200
# 2. 导出所有格式
for fmt in ["csv", "txt", "json"]:
response = await client.get(f"/api/proxies/export/{fmt}")
assert response.status_code == 200
# 3. 获取随机代理(可能返回 200 或 404
response = await client.get("/api/proxies/random")
assert response.status_code in [200, 404]