-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
147 lines (127 loc) · 4.38 KB
/
Copy pathtest_api.py
File metadata and controls
147 lines (127 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
简单的API测试脚本
用于验证后端各个端点是否正常工作
"""
import requests
import json
import sys
import io
# 设置输出编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
API_BASE = "http://localhost:8001"
def test_health():
"""测试健康检查"""
print("1. 测试健康检查...")
response = requests.get(f"{API_BASE}/health")
print(f" 状态码: {response.status_code}")
print(f" 响应: {response.json()}")
assert response.status_code == 200
print(" ✅ 通过\n")
def test_create_session():
"""测试创建会话"""
print("2. 测试创建会话...")
resume = {
"basics": {
"name": "张三",
"email": "zhangsan@example.com",
"phone": "138****1234"
},
"sections": [
{
"id": "test-section-1",
"title": "工作经历",
"type": "text",
"content": "2020-2023 ABC公司 - Python开发工程师"
}
]
}
response = requests.post(
f"{API_BASE}/session/create",
json={"resume": resume}
)
print(f" 状态码: {response.status_code}")
data = response.json()
print(f" 会话ID: {data.get('session_id')}")
assert response.status_code == 200
assert 'session_id' in data
print(" ✅ 通过\n")
return data['session_id']
def test_generate_plan(session_id):
"""测试生成计划"""
print("3. 测试生成计划...")
response = requests.post(
f"{API_BASE}/session/{session_id}/plan",
json={"user_intent": "我想申请Python后端工程师职位"}
)
print(f" 状态码: {response.status_code}")
data = response.json()
print(f" 任务数量: {len(data.get('plan', {}).get('tasks', []))}")
if data.get('plan', {}).get('tasks'):
print(f" 第一个任务: {data['plan']['tasks'][0]['section']}")
assert response.status_code == 200
assert 'plan' in data
print(" ✅ 通过\n")
return data['plan']
def test_get_progress(session_id):
"""测试获取进度"""
print("4. 测试获取进度...")
response = requests.get(f"{API_BASE}/session/{session_id}/progress")
print(f" 状态码: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" 总任务: {data.get('total_tasks')}")
print(f" 当前任务索引: {data.get('current_task_idx')}")
print(" ✅ 通过\n")
else:
print(f" 错误: {response.text}")
print(" ⚠️ 跳过此测试\n")
def test_guide_step(session_id):
"""测试Guide交互"""
print("5. 测试Guide Agent交互...")
response = requests.post(
f"{API_BASE}/session/{session_id}/guide",
json={"user_input": "我主要负责用户系统和订单系统的开发"}
)
print(f" 状态码: {response.status_code}")
data = response.json()
print(f" 回复: {data.get('reply', '')[:100]}...")
print(f" 状态: {data.get('state')}")
print(f" 是否需要确认: {data.get('is_confirming')}")
assert response.status_code == 200
print(" ✅ 通过\n")
def test_get_session_info(session_id):
"""测试获取会话信息"""
print("6. 测试获取会话信息...")
response = requests.get(f"{API_BASE}/session/{session_id}")
print(f" 状态码: {response.status_code}")
data = response.json()
print(f" 当前阶段: {data.get('current_stage')}")
print(f" 是否有计划: {data.get('has_plan')}")
assert response.status_code == 200
print(" ✅ 通过\n")
def main():
print("=" * 50)
print("开始API测试")
print("=" * 50 + "\n")
try:
# 1. 健康检查
test_health()
# 2. 创建会话
session_id = test_create_session()
# 3. 生成计划
plan = test_generate_plan(session_id)
# 4. 获取进度
test_get_progress(session_id)
# 5. Guide交互
test_guide_step(session_id)
# 6. 获取会话信息
test_get_session_info(session_id)
print("=" * 50)
print("✅ 所有测试通过!")
print("=" * 50)
except Exception as e:
print(f"\n❌ 测试失败: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()