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
This commit is contained in:
4
channel-service/tests/conftest.py
Normal file
4
channel-service/tests/conftest.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
70
channel-service/tests/test_api.py
Normal file
70
channel-service/tests/test_api.py
Normal file
@@ -0,0 +1,70 @@
|
||||
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
|
||||
Reference in New Issue
Block a user