You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
8.9 KiB
8.9 KiB
AI API 测试覆盖完成
日期: 2026-01-31
类型: 测试补充
影响范围: AI API 集成测试
概述
补充了 AI API 的缺失测试用例,将测试覆盖率从 69% 提升到 100%,确保所有 13 个 API 端点都有完整的测试覆盖。
背景
在之前的测试中发现以下端点缺少测试:
/generate-voice- 配音生成(核心功能)/generate-subtitle- 字幕生成(核心功能)/process-text- 文本处理(核心功能)/generate-sound- 音效生成 501 测试(验证不扣积分)
实施内容
1. 配音生成测试(TestVoiceGenerationWorkflow)
新增 7 个测试用例:
class TestVoiceGenerationWorkflow:
"""配音生成完整工作流测试"""
async def test_voice_generation_workflow(...)
# 测试完整流程:创建任务 → 查询状态
async def test_voice_generation_different_voices(...)
# 测试 6 种语音类型:alloy, echo, fable, onyx, nova, shimmer
async def test_voice_generation_different_speeds(...)
# 测试不同语速:0.25, 0.5, 1.0, 1.5, 2.0, 4.0
async def test_voice_generation_different_languages(...)
# 测试多语言:zh-CN, en-US, ja-JP, ko-KR
async def test_voice_generation_missing_text(...)
# 测试缺少必需字段(422 错误)
async def test_voice_generation_invalid_speed(...)
# 测试无效语速(当前不验证,由 Provider 层处理)
async def test_voice_generation_with_project(...)
# 测试关联项目(项目不存在返回 500)
测试结果: ✅ 7/7 通过
2. 字幕生成测试(TestSubtitleGenerationWorkflow)
新增 5 个测试用例:
class TestSubtitleGenerationWorkflow:
"""字幕生成完整工作流测试"""
async def test_subtitle_generation_workflow(...)
# 测试完整流程:创建任务 → 查询状态
async def test_subtitle_generation_different_languages(...)
# 测试多语言:zh, en, ja, ko, es, fr
async def test_subtitle_generation_missing_audio_url(...)
# 测试缺少音频 URL(422 错误)
async def test_subtitle_generation_invalid_url(...)
# 测试无效 URL(当前不验证,由 Provider 层处理)
async def test_subtitle_generation_with_project(...)
# 测试关联项目(项目不存在返回 500)
测试结果: ✅ 5/5 通过
3. 文本处理测试(TestTextProcessingWorkflow)
新增 10 个测试用例:
class TestTextProcessingWorkflow:
"""文本处理完整工作流测试"""
async def test_screenplay_parse(...)
# 测试剧本解析
async def test_content_analysis(...)
# 测试内容分析
async def test_style_transform(...)
# 测试风格转换
async def test_prompt_generation(...)
# 测试提示词生成
async def test_text_processing_different_temperatures(...)
# 测试不同温度参数:0.0 - 2.0
async def test_text_processing_different_max_tokens(...)
# 测试不同 max_tokens:100 - 8000
async def test_text_processing_missing_text(...)
# 测试缺少文本(422 错误)
async def test_text_processing_invalid_task_type(...)
# 测试无效任务类型(400/422 错误)
async def test_text_processing_empty_text(...)
# 测试空文本(当前不验证)
async def test_text_processing_with_project(...)
# 测试关联项目(项目不存在返回 500)
测试结果: ✅ 10/10 通过
4. 音效生成 501 测试(TestSoundGenerationNotImplemented)
新增 4 个测试用例:
class TestSoundGenerationNotImplemented:
"""音效生成功能不可用测试"""
async def test_sound_generation_returns_501(...)
# 验证返回 501 状态码和正确的错误信息
async def test_sound_generation_no_credit_deduction(...)
# 验证不扣除积分
async def test_sound_generation_no_job_created(...)
# 验证不创建任务
async def test_sound_generation_different_parameters(...)
# 验证不同参数都返回 501
测试结果: ✅ 4/4 通过
测试覆盖统计
覆盖率提升
| 指标 | 之前 | 现在 | 提升 |
|---|---|---|---|
| 已测试端点 | 9/13 (69%) | 13/13 (100%) | +31% |
| 测试用例数 | ~50 | ~76 | +26 |
| 核心功能覆盖 | 部分 | 完整 | ✅ |
端点测试状态
| 端点 | 方法 | 测试状态 | 测试用例数 |
|---|---|---|---|
/generate-image |
POST | ✅ 完整 | 8 |
/generate-video |
POST | ✅ 完整 | 6 |
/generate-sound |
POST | ✅ 完整 | 4 (501 测试) |
/generate-voice |
POST | ✅ 新增 | 7 |
/generate-subtitle |
POST | ✅ 新增 | 5 |
/process-text |
POST | ✅ 新增 | 10 |
/jobs |
GET | ✅ 完整 | 5 |
/jobs/{job_id} |
GET | ✅ 完整 | 3 |
/jobs/{job_id}/cancel |
POST | ✅ 完整 | 2 |
/statistics |
GET | ✅ 完整 | 2 |
/usage/stats |
GET | ✅ 完整 | 1 |
/queue/status |
GET | ✅ 完整 | 1 |
/models |
GET | ✅ 完整 | 2 |
测试发现的问题
1. Schema 验证不严格
问题:
- 语速(speed)没有范围验证(应该是 0.25-4.0)
- URL 格式没有验证
- 空文本没有验证
当前行为: API 层接受任何值,由 Provider 层处理
建议: 在 Schema 层添加验证规则
2. 项目验证错误处理
问题: 项目不存在时返回 500(Internal Server Error)
当前行为: NotFoundError 被捕获为 500
建议: 在 API 层捕获 NotFoundError 并返回 404
3. 501 响应格式
问题: 音效生成返回 501 时使用统一响应格式
当前行为:
{
"code": 501,
"message": "音效生成功能暂不可用...",
"data": null,
"success": false
}
验证: ✅ 正确,符合统一响应格式规范
测试执行
运行所有新增测试
docker exec jointo-server-app pytest \
tests/integration/test_ai_api_workflow.py::TestVoiceGenerationWorkflow \
tests/integration/test_ai_api_workflow.py::TestSubtitleGenerationWorkflow \
tests/integration/test_ai_api_workflow.py::TestTextProcessingWorkflow \
tests/integration/test_ai_api_workflow.py::TestSoundGenerationNotImplemented \
-v
结果: ✅ 26 passed, 26 warnings in 3.92s
运行所有 AI API 测试
docker exec jointo-server-app pytest tests/integration/test_ai_api_workflow.py -v
预期结果: 所有测试通过(~76 个测试用例)
文件变更
修改的文件
server/tests/integration/test_ai_api_workflow.py- 新增
TestVoiceGenerationWorkflow类(7 个测试) - 新增
TestSubtitleGenerationWorkflow类(5 个测试) - 新增
TestTextProcessingWorkflow类(10 个测试) - 新增
TestSoundGenerationNotImplemented类(4 个测试) - 总计新增 ~500 行测试代码
- 新增
新增的文档
docs/server/TEST_COVERAGE_AI_API.md- AI API 测试覆盖分析文档docs/server/changelogs/2026-01-31-ai-api-test-coverage-completion.md- 本文档
测试质量
测试覆盖的场景
✅ 正常流程
- 任务创建成功
- 任务状态查询
- 不同参数组合
✅ 边界条件
- 最小/最大参数值
- 不同语言和语音类型
- 不同温度和 token 数
✅ 错误处理
- 缺少必需字段(422)
- 无效参数(400/422)
- 项目不存在(500)
- 功能不可用(501)
✅ 业务逻辑
- 积分扣除验证
- 任务创建验证
- 权限验证
后续优化建议
短期(本周)
-
添加 Schema 验证
- 语速范围:
ge=0.25, le=4.0 - URL 格式验证
- 文本长度验证
- 语速范围:
-
改进错误处理
- 项目不存在返回 404 而不是 500
- 统一错误响应格式
中期(下周)
-
添加性能测试
- 响应时间测试
- 并发测试
- 吞吐量测试
-
添加安全测试
- SQL 注入测试
- XSS 攻击测试
- 权限提升测试
长期(本月)
-
集成到 CI/CD
- 自动运行测试
- 覆盖率报告
- 失败通知
-
添加 E2E 测试
- 完整的用户流程测试
- 跨服务集成测试
总结
本次测试补充工作成功将 AI API 的测试覆盖率从 69% 提升到 100%,新增 26 个测试用例,覆盖了所有核心功能和边界条件。所有测试均通过,确保了 API 的稳定性和可靠性。
关键成果:
- ✅ 100% 端点覆盖
- ✅ 26 个新测试用例
- ✅ 所有测试通过
- ✅ 完整的文档记录
测试质量:
- 覆盖正常流程、边界条件、错误处理
- 验证业务逻辑(积分、权限、任务创建)
- 清晰的测试命名和文档
下一步:
- 添加 Schema 验证规则
- 改进错误处理(404 vs 500)
- 添加性能和安全测试