# AI 服务测试指南 ## 概述 本指南提供 AI 服务功能的完整测试流程,包括: - AI 对话管理 - AI 提示词系统 - AI 任务管理 - 剧本解析 ## 前置条件 ### 1. 环境准备 确保 Docker 容器正常运行: ```bash # 检查容器状态 docker ps | grep jointo-server # 应该看到以下容器: # - jointo-server-app # - jointo-server-postgres # - jointo-server-redis # - jointo-server-rabbitmq # - jointo-server-celery-ai # - jointo-server-celery-export # - jointo-server-celery-beat ``` ### 2. 数据库准备 确保数据库迁移已完成: ```bash # 检查迁移状态 docker exec jointo-server-app alembic current # 如果需要升级 docker exec jointo-server-app python scripts/db_migrate.py upgrade ``` ### 3. 测试用户准备 创建测试用户(如果还没有): ```bash docker exec -it jointo-server-postgres psql -U jointoAI -d jointo -c " INSERT INTO users (user_id, phone, nickname, password_hash, is_active) VALUES ( gen_random_uuid(), '13800138000', '测试用户', '\$2b\$12\$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYqNqYqNqYq', true ) ON CONFLICT (phone) DO NOTHING; " ``` ## 测试流程 ### 阶段 1:单元测试 运行单元测试验证核心逻辑: ```bash # 进入容器 docker exec -it jointo-server-app bash # 运行 AI 服务单元测试 pytest tests/unit/test_ai_service.py -v # 运行 AI 任务单元测试 pytest tests/unit/test_ai_tasks.py -v ``` ### 阶段 2:集成测试 运行集成测试验证 API 端点: ```bash # 运行 AI 集成测试 pytest tests/integration/test_ai_integration.py -v # 运行特定测试类 pytest tests/integration/test_ai_integration.py::TestAIConversations -v pytest tests/integration/test_ai_integration.py::TestAIPrompts -v pytest tests/integration/test_ai_integration.py::TestAIJobs -v pytest tests/integration/test_ai_integration.py::TestScreenplayParsing -v ``` ### 阶段 3:手动 API 测试 使用 curl 或 Postman 测试 API 端点。 #### 3.1 获取访问令牌 ```bash # 登录获取 token curl -X POST http://localhost:8000/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{ "phone": "13800138000", "password": "test123456" }' # 保存返回的 access_token export TOKEN="your_access_token_here" ``` #### 3.2 测试 AI 对话管理 ```bash # 创建对话 curl -X POST http://localhost:8000/api/v1/ai/conversations \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "project_id": "your_project_id", "title": "测试对话" }' # 保存返回的 conversation_id export CONV_ID="conversation_id_here" # 添加消息 curl -X POST http://localhost:8000/api/v1/ai/conversations/$CONV_ID/messages \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "role": "user", "content": "你好,请帮我生成一张图片" }' # 获取对话历史 curl -X GET http://localhost:8000/api/v1/ai/conversations/$CONV_ID/messages \ -H "Authorization: Bearer $TOKEN" ``` #### 3.3 测试 AI 提示词系统 ```bash # 获取提示词列表 curl -X GET http://localhost:8000/api/v1/ai/prompts \ -H "Authorization: Bearer $TOKEN" # 根据 key 获取提示词 curl -X GET http://localhost:8000/api/v1/ai/prompts/key/image_generation \ -H "Authorization: Bearer $TOKEN" # 获取提示词变量 curl -X GET http://localhost:8000/api/v1/ai/prompts/key/image_generation/variables \ -H "Authorization: Bearer $TOKEN" ``` #### 3.4 测试 AI 模型查询 ```bash # 获取所有模型 curl -X GET http://localhost:8000/api/v1/ai/models \ -H "Authorization: Bearer $TOKEN" # 按类型过滤 curl -X GET "http://localhost:8000/api/v1/ai/models?model_type=image" \ -H "Authorization: Bearer $TOKEN" # 按提供商过滤 curl -X GET "http://localhost:8000/api/v1/ai/models?provider=aihubmix" \ -H "Authorization: Bearer $TOKEN" ``` #### 3.5 测试 AI 任务管理 ```bash # 创建图片生成任务 curl -X POST http://localhost:8000/api/v1/ai/jobs/image \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "project_id": "your_project_id", "model": "flux-1.1-pro", "prompt": "一只可爱的猫咪,坐在窗边,阳光洒在身上", "width": 1024, "height": 1024 }' # 保存返回的 job_id export JOB_ID="job_id_here" # 查询任务状态 curl -X GET http://localhost:8000/api/v1/ai/jobs/$JOB_ID \ -H "Authorization: Bearer $TOKEN" # 获取用户任务列表 curl -X GET http://localhost:8000/api/v1/ai/jobs \ -H "Authorization: Bearer $TOKEN" # 取消任务 curl -X POST http://localhost:8000/api/v1/ai/jobs/$JOB_ID/cancel \ -H "Authorization: Bearer $TOKEN" ``` #### 3.6 测试剧本解析 ```bash # 创建剧本(如果还没有) curl -X POST http://localhost:8000/api/v1/screenplays \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "project_id": "your_project_id", "title": "测试剧本", "content": "场景1:咖啡馆 - 白天\n\n小明走进咖啡馆。\n\n小明:你好!" }' # 保存返回的 screenplay_id export SCREENPLAY_ID="screenplay_id_here" # 解析剧本 curl -X POST http://localhost:8000/api/v1/screenplays/$SCREENPLAY_ID/parse \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4", "auto_create_elements": true, "auto_create_tags": true, "auto_create_storyboards": false }' # 保存返回的 job_id,然后查询状态 curl -X GET http://localhost:8000/api/v1/ai/jobs/$JOB_ID \ -H "Authorization: Bearer $TOKEN" ``` ### 阶段 4:Celery 任务测试 验证 Celery 任务是否正常执行: ```bash # 查看 Celery Worker 日志 docker logs jointo-server-celery-ai -f # 查看 Celery Beat 日志(定时任务) docker logs jointo-server-celery-beat -f # 进入 Redis 查看任务队列 docker exec -it jointo-server-redis redis-cli > KEYS celery* > LLEN celery > LRANGE celery 0 -1 ``` ### 阶段 5:性能测试 使用 Apache Bench 或 wrk 进行压力测试: ```bash # 安装 Apache Bench apt-get install apache2-utils # 测试 AI 模型列表接口(100 个请求,10 个并发) ab -n 100 -c 10 \ -H "Authorization: Bearer $TOKEN" \ http://localhost:8000/api/v1/ai/models # 测试提示词列表接口 ab -n 100 -c 10 \ -H "Authorization: Bearer $TOKEN" \ http://localhost:8000/api/v1/ai/prompts ``` ## 测试检查清单 ### ✅ 功能测试 - [ ] AI 对话创建成功 - [ ] AI 对话消息添加成功 - [ ] AI 对话 @ 提及功能正常 - [ ] AI 提示词查询正常 - [ ] AI 提示词变量解析正常 - [ ] AI 模型列表查询正常 - [ ] AI 模型过滤功能正常 - [ ] 图片生成任务创建成功 - [ ] 视频生成任务创建成功 - [ ] 音效生成任务创建成功 - [ ] 配音生成任务创建成功 - [ ] 字幕生成任务创建成功 - [ ] 任务状态查询正常 - [ ] 任务取消功能正常 - [ ] 剧本解析任务创建成功 - [ ] 剧本解析结果正确 ### ✅ 权限测试 - [ ] 未登录用户无法访问 API - [ ] 用户只能访问自己的对话 - [ ] 用户只能访问自己的任务 - [ ] 管理员可以管理提示词 - [ ] 普通用户不能管理提示词 ### ✅ 异常测试 - [ ] 无效的 project_id 返回 404 - [ ] 无效的 conversation_id 返回 404 - [ ] 无效的 job_id 返回 404 - [ ] 空剧本解析返回 400 - [ ] 无效的模型名称返回 400 - [ ] 超出配额返回 402 ### ✅ 性能测试 - [ ] AI 模型列表响应时间 < 100ms - [ ] AI 提示词列表响应时间 < 100ms - [ ] 任务创建响应时间 < 200ms - [ ] 任务状态查询响应时间 < 50ms - [ ] 并发 10 个请求无错误 ## 常见问题 ### 1. Celery Worker 无法连接 RabbitMQ **症状**:任务创建后一直处于 pending 状态 **解决方案**: ```bash # 检查 RabbitMQ 状态 docker exec jointo-server-rabbitmq rabbitmqctl status # 重启 Celery Worker docker restart jointo-server-celery-ai ``` ### 2. AI Provider 返回错误 **症状**:任务失败,错误信息显示 API 调用失败 **解决方案**: ```bash # 检查环境变量 docker exec jointo-server-app env | grep AIHUBMIX # 检查 AI Provider 日志 docker logs jointo-server-app | grep "AIHubMix" ``` ### 3. 数据库连接超时 **症状**:API 请求超时或返回 500 错误 **解决方案**: ```bash # 检查数据库连接 docker exec jointo-server-postgres pg_isready # 检查连接池 docker exec jointo-server-app python -c " from app.core.database import engine print(engine.pool.status()) " ``` ## 下一步 测试通过后,可以: 1. 部署到测试环境 2. 进行用户验收测试(UAT) 3. 监控生产环境性能 4. 收集用户反馈并优化 ## 相关文档 - [AI 服务故障排查指南](./ai-service-troubleshooting.md) - [AI 服务实施总结](../changelogs/2026-02-03-ai-services-implementation-summary.md) - [AI API 路由实施](../changelogs/2026-02-03-ai-api-routes-implementation.md) - [AI Celery 任务验证](../changelogs/2026-02-03-ai-celery-tasks-verification.md)