Files
workphone-sdk/sdk/tests/test_api.py

142 lines
4.1 KiB
Python
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.

"""
工作手机SDK v3.0 - API测试
"""
import httpx
import asyncio
BASE_URL = "http://localhost:8899"
API_KEY = "workphone-secret-key-2026"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
async def check_health():
"""测试健康检查"""
async with httpx.AsyncClient() as client:
resp = await client.get(f"{BASE_URL}/health")
print("健康检查:", resp.json())
assert resp.status_code == 200
async def check_devices():
"""测试设备列表"""
async with httpx.AsyncClient() as client:
resp = await client.get(f"{BASE_URL}/api/v3/devices", headers=headers)
print("设备列表:", resp.json())
assert resp.status_code == 200
async def check_send_message():
"""测试发送消息(模拟)"""
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{BASE_URL}/api/v3/message/send",
headers=headers,
json={
"device_id": "test-device",
"platform": "wechat",
"to_id": "测试联系人",
"content": "测试消息",
"msg_type": "text"
}
)
data = resp.json()
print("发送消息:", data)
assert resp.status_code == 200
assert data.get("code") == 200
assert "data" in data
assert "success" in data["data"]
if not data["data"]["success"] and data["data"].get("error"):
assert data["data"].get("error_code") in (None, "timeout", "contact_not_found")
if data["data"].get("timeout_seconds"):
assert isinstance(data["data"]["timeout_seconds"], (int, type(None)))
async def check_batch_send_message():
"""测试批量发送消息契约data.sent + data.failed + data.total"""
async with httpx.AsyncClient(timeout=30) as client:
resp = await client.post(
f"{BASE_URL}/api/v3/message/batch-send",
headers=headers,
json={
"device_id": "test-device",
"platform": "wechat",
"to_ids": ["A", "B"],
"content": "batch test",
"msg_type": "text",
"interval": 1.0
}
)
data = resp.json()
print("批量发消息:", data)
assert resp.status_code in [200, 503]
if resp.status_code == 200:
assert data.get("code") == 200
d = data.get("data", {})
assert "sent" in d and "failed" in d and "total" in d
assert d["total"] == 2
assert len(d["sent"]) + len(d["failed"]) == 2
async def check_agent_execute():
"""测试AI Agent模拟"""
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{BASE_URL}/api/v3/agent/execute",
headers=headers,
json={
"device_id": "test-device",
"task": "打开微信",
"llm_provider": "deepseek",
"max_steps": 10
},
timeout=60
)
print("AI Agent:", resp.json())
async def main():
"""运行所有测试"""
print("=" * 50)
print("工作手机SDK v3.0 API测试")
print("=" * 50)
try:
await check_health()
print("✅ 健康检查通过\n")
except Exception as e:
print(f"❌ 健康检查失败: {e}\n")
try:
await check_devices()
print("✅ 设备列表通过\n")
except Exception as e:
print(f"❌ 设备列表失败: {e}\n")
try:
await check_send_message()
print("✅ 发送消息通过\n")
except Exception as e:
print(f"❌ 发送消息失败: {e}\n")
try:
await check_batch_send_message()
print("✅ 批量发消息通过\n")
except Exception as e:
print(f"❌ 批量发消息失败: {e}\n")
try:
await check_agent_execute()
print("✅ AI Agent通过\n")
except Exception as e:
print(f"❌ AI Agent失败: {e}\n")
print("=" * 50)
print("测试完成")
if __name__ == "__main__":
asyncio.run(main())