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

94 lines
3.3 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.

#!/usr/bin/env python3
"""
微信消息 E2E 端到端验证脚本
验证完整链路API → SDK → Agent → 微信 → 执行结果回传
前置条件:
1. SDK 运行localhost:8899
2. Agent 连接emulator-5554
3. 模拟器微信已登录
4. 测试联系人:文件传输助手(每台微信必有)
可配置环境变量:
- SDK_BASE_URLSDK 地址,默认 http://localhost:8899
- SDK_DEVICE_ID设备 ID默认 emulator-5554
- SDK_E2E_TO_ID发送目标联系人/群),默认 文件传输助手
- SDK_E2E_CONTENT发送内容默认 [E2E测试] 工作手机SDK微信发送验证
"""
import httpx
import asyncio
import os
import sys
BASE_URL = os.environ.get("SDK_BASE_URL", "http://localhost:8899")
DEVICE_ID = os.environ.get("SDK_DEVICE_ID", "emulator-5554")
TO_ID = os.environ.get("SDK_E2E_TO_ID", "文件传输助手")
CONTENT = os.environ.get("SDK_E2E_CONTENT", "[E2E测试] 工作手机SDK微信发送验证")
async def check_wechat_send_e2e():
"""完整 E2E发送微信消息并验证回传"""
print("=" * 50)
print("微信消息 E2E 端到端验证")
print("=" * 50)
# 1. 健康检查
async with httpx.AsyncClient(timeout=10) as client:
try:
resp = await client.get(f"{BASE_URL}/health")
health = resp.json()
devices_online = health.get("devices_online", 0)
print(f"✓ SDK 健康: devices_online={devices_online}")
if devices_online == 0:
print("❌ 无设备在线,请先启动 Agent:")
print(" cd sdk/agent && python3 agent.py -d emulator-5554 -s ws://127.0.0.1:8899/ws/device")
return False
except Exception as e:
print(f"❌ SDK 未运行: {e}")
return False
# 2. 发送消息(微信操作较慢:启动+搜索+输入+发送,预留 90s
print(f"\n→ 发送消息: {TO_ID} | {CONTENT}")
async with httpx.AsyncClient(timeout=90) as client:
resp = await client.post(
f"{BASE_URL}/api/v3/message/send",
json={
"device_id": DEVICE_ID,
"platform": "wechat",
"to_id": TO_ID,
"content": CONTENT,
"msg_type": "text"
}
)
result = resp.json()
print(f"← 响应: {result}")
# 3. 验证回传
ok = (
resp.status_code == 200
and result.get("code") == 200
and result.get("data", {}).get("success") is True
)
if ok:
msg_id = result.get("data", {}).get("message_id", "")
print(f"\n✅ E2E 验证通过")
print(f" - message_id: {msg_id}")
print(f" - channel_used: {result.get('channel_used', 'N/A')}")
return True
err = result.get("data", {}).get("error") or result.get("detail") or result.get("message", "未知")
if err == "timeout":
print(f"\n⚠️ 设备响应超时API 已正确返回 success=false, error=timeout")
print(f" - 请检查 Agent 是否卡住、to_id 是否存在、MESSAGE_SEND_TIMEOUT 是否过短")
return True # API 行为正确,算通过
print(f"\n❌ E2E 验证失败: {err}")
return False
if __name__ == "__main__":
success = asyncio.run(check_wechat_send_e2e())
sys.exit(0 if success else 1)