Files
sino-mci/channel-service/tests/test_api.py
marsal bd0ac464cf feat(channel-service): add Python FastAPI pywechat channel service
- Add FastAPI project under channel-service/
- Implement mock pywechat client adapter (login, send, heartbeat)
- Add /v1/wechat-personal/login/*, /v1/wechat-personal/send/* APIs
- Add /v1/heartbeat and /v1/monitor/status for client health
- Add /monitor HTML page with Web Components
- Add pytest suite and Dockerfile
- Update .gitignore for Python/Node artifacts
2026-07-07 12:36:54 +08:00

71 lines
2.1 KiB
Python

from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health():
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "ok"
def test_login_flow():
r = client.post("/v1/wechat-personal/login/start", json={"account_id": "bot-001", "account_name": "测试号"})
assert r.status_code == 200
body = r.json()
assert body["login_id"]
assert body["qr_code_url"]
r = client.get("/v1/wechat-personal/login/bot-001/status")
assert r.status_code == 200
assert r.json()["login_status"] == "pending"
r = client.post("/v1/wechat-personal/login/bot-001/confirm")
assert r.status_code == 200
assert r.json()["success"] is True
r = client.get("/v1/wechat-personal/login/bot-001/status")
assert r.json()["login_status"] == "success"
assert r.json()["online_status"] == "online"
def test_send_after_login():
client.post("/v1/wechat-personal/login/start", json={"account_id": "bot-002"})
client.post("/v1/wechat-personal/login/bot-002/confirm")
r = client.post("/v1/wechat-personal/send/text", json={
"account_id": "bot-002",
"conversation_id": "room-001",
"text": "hello"
})
assert r.status_code == 200
assert r.json()["success"] is True
assert r.json()["message_id"]
def test_send_without_login_fails():
r = client.post("/v1/wechat-personal/send/text", json={
"account_id": "bot-offline",
"conversation_id": "room-001",
"text": "hello"
})
assert r.status_code == 200
assert r.json()["success"] is False
def test_heartbeat_and_monitor():
client.post("/v1/heartbeat", json={"account_id": "bot-003", "risk_level": 10, "sent_count_today": 5})
r = client.get("/v1/monitor/status")
assert r.status_code == 200
clients = r.json()["clients"]
assert len(clients) >= 1
assert any(c["account_id"] == "bot-003" and c["risk_level"] == 10 for c in clients)
def test_monitor_page():
r = client.get("/monitor")
assert r.status_code == 200
assert "channel-status-table" in r.text