oclaw/tests/test_whatsapp_inbound_payload_shape.py
oliver 420abac9f1 refactor: root-package imports (svc/runtime/interfaces) and fix PYTHONPATH
- Rename platform/ to svc/ to avoid shadowing stdlib platform.
- Replace from oclaw.* with from svc/runtime/interfaces; update -m CLI paths.
- tests/conftest: prepend repo root to sys.path (no parent-folder package name).
- CI: paths and offline_eval script under repo root.
- Ops scripts: PYTHONPATH must be repo root for python -m runtime.* (fixes gateway/WhatsApp sidecar startup).
- Fix default oclaw.json path in tabular/file attachment limits; stabilize attachment test config.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-13 14:51:17 +08:00

59 lines
2.2 KiB
Python

from __future__ import annotations
from fastapi.testclient import TestClient
from interfaces.http.fastapi_app import create_app
from interfaces.http import fastapi_app
def test_inbound_whatsapp_accepts_basic_payload_and_returns_replies() -> None:
client = TestClient(create_app())
old_usecase = fastapi_app.process_inbound_payload_usecase
def _fake_usecase(payload: dict[str, object]) -> dict[str, object]:
assert str(payload.get("channel") or "") == "whatsapp"
assert str(payload.get("account_id") or "") == "wa-default"
assert str(payload.get("user_id") or "") == "111@s.whatsapp.net"
assert str(payload.get("chat_id") or "") == "111@s.whatsapp.net"
return {
"ok": True,
"replies": [
{
"chat_id": payload.get("chat_id"),
"text": "ok",
"attachments": [
{
"type": "binary_ref",
"name": "hello.txt",
"mime_type": "text/plain",
"data_base64": "aGVsbG8=",
}
],
}
],
}
try:
fastapi_app.process_inbound_payload_usecase = _fake_usecase # type: ignore[assignment]
r = client.post(
"/inbound/whatsapp",
json={
"account_id": "wa-default",
"user_id": "111@s.whatsapp.net",
"chat_id": "111@s.whatsapp.net",
"text": "hello",
"metadata": {"source": "test"},
},
)
assert r.status_code == 200, r.text
data = r.json() or {}
assert data.get("ok") is True
replies = data.get("replies") if isinstance(data.get("replies"), list) else []
assert replies and isinstance(replies[0], dict)
assert str(replies[0].get("text") or "") == "ok"
atts = replies[0].get("attachments") if isinstance(replies[0].get("attachments"), list) else []
assert len(atts) == 1
assert str(atts[0].get("name") or "") == "hello.txt"
finally:
fastapi_app.process_inbound_payload_usecase = old_usecase # type: ignore[assignment]