Files
mcde/tests/whatsapp_fake.py
lotherk 056bd30a8f dtchat: exercise WhatsApp incoming files with a real fake file (T9.1)
DTCHAT_WHATSAPP_FAKE_FILE makes the fake adapter offer that file (name, size, SHA-256), and on file-accept read it in 4 KiB blocks, emit file-data and a file-end carrying the hash; the real accept path (DownloadAny) now also sends the SHA-256 in file-end, and the static fake keeps its hello-world hash. tests/whatsapp_fake.py uses a 9000-byte deterministic file, checks the offer name/size/hash, reassembles every chunk byte-for-byte and verifies the file-end hash (31 checks, green stand-alone). Harness entry already exists; in-tree build and harness follow.
2026-11-17 12:29:27 +00:00

217 lines
6.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""tests/whatsapp_fake.py -- the WhatsApp adapter skeleton (T9.1).
usage: tests/whatsapp_fake.py [path-to-dtchat-whatsapp]
DTCHAT_WHATSAPP_FAKE=1 replaces whatsmeow with a QR request and an echo, so
the IPC path (hello, configure, connect, QR request, online, send,
disconnect) is tested without an account.
"""
import base64
import hashlib
import json
import os
import queue
import subprocess
import sys
import threading
import time
ADAPTER = sys.argv[1] if len(sys.argv) > 1 else \
"build/mcde-host/programs/dtchat/dtchat-whatsapp"
failures = []
def check(cond, what):
print(("ok " if cond else "FAIL ") + what, flush=True)
if not cond:
failures.append(what)
PAYLOAD = bytes((i * 53 + 7) & 0xff for i in range(9000))
FILE = "/tmp/dtchat-whatsapp-file.bin"
with open(FILE, "wb") as f:
f.write(PAYLOAD)
proc = subprocess.Popen([ADAPTER, "--stdio"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True,
env={"PATH": "/usr/bin:/bin",
"DTCHAT_WHATSAPP_FAKE": "1",
"DTCHAT_WHATSAPP_FAKE_FILE": FILE})
q = queue.Queue()
def reader():
for line in proc.stdout:
line = line.strip()
if line:
q.put(json.loads(line))
threading.Thread(target=reader, daemon=True).start()
def send(o):
proc.stdin.write(json.dumps(o) + "\n")
proc.stdin.flush()
def ev(t=5.0):
try:
return q.get(timeout=t)
except queue.Empty:
return None
hello = ev()
check(hello is not None and hello.get("type") == "hello", "hello arrives")
check(hello.get("protocol") == "whatsapp", "protocol is whatsapp")
check("qr" in (hello or {}).get("caps", []), "caps name the QR flow")
keys = [f.get("key") for f in (hello or {}).get("settings", [])]
check(keys == ["database"], "settings schema")
send({"type": "configure", "account": "w",
"settings": {"database": "/tmp/dtchat-whatsapp-test"}, "secrets": {}})
send({"type": "connect"})
state = None
request = None
deadline = time.time() + 5
while time.time() < deadline and (state != "connecting" or request is None):
e = ev(1.0)
if e is None:
continue
if e.get("type") == "state":
state = e.get("state")
elif e.get("type") == "request":
request = e
check(state == "connecting", "state connecting")
check(request is not None and request.get("kind") == "qr",
"a QR request arrives")
if request is not None:
check(bool(request.get("qrText")), "the request carries the QR text")
qr = request.get("qr")
check(qr is not None, "the request carries a QR bitmap")
if qr is not None:
w = int(qr.get("width", 0))
h = int(qr.get("height", 0))
data = base64.b64decode(qr.get("data", ""))
check(w > 10 and h == w, "QR bitmap has a square size")
check(len(data) == w * h, "QR bitmap has one byte per module")
check(0 in data and 1 in data, "QR bitmap has light and dark modules")
check(data[0] == 0, "QR bitmap starts in the quiet zone")
send({"type": "answer", "id": request.get("id"), "value": ""})
online = False
deadline = time.time() + 5
while time.time() < deadline and not online:
e = ev(1.0)
if e and e.get("type") == "state" and e.get("state") == "online":
online = True
check(online, "online after the pairing")
roster = {}
offer = None
deadline = time.time() + 5
while time.time() < deadline and (len(roster) < 2 or offer is None):
e = ev(1.0)
if e is None:
continue
if e.get("type") == "roster":
roster[e.get("contact", {}).get("id")] = e["contact"]
elif e.get("type") == "file-offer":
offer = e
check(roster.get("alice", {}).get("name") == "Alice",
"roster has alice")
check(roster.get("bob", {}).get("name") == "Bob",
"roster has bob")
check(roster.get("alice", {}).get("online") is True,
"alice is online")
check(offer is not None and
offer.get("name") == "dtchat-whatsapp-file.bin",
"a file offer arrives")
if offer is not None:
check(offer.get("size") == len(PAYLOAD), "the offer has the size")
check(offer.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
"the offer has the SHA-256")
send({"type": "file-accept", "id": offer.get("id")})
chunks = []
end = None
deadline = time.time() + 5
while time.time() < deadline:
e = ev(1.0)
if e is None:
continue
if e.get("type") == "file-data":
chunks.append(base64.b64decode(e.get("data", "")))
elif e.get("type") == "file-end":
end = e
break
received = b"".join(chunks)
check(received == PAYLOAD, "accepted file data arrives unchanged")
check(end is not None and end.get("size") == len(PAYLOAD), "file end size")
check(end is not None and
end.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
"file end SHA-256")
send({"type": "typing", "conv": "alice", "on": True})
typing = None
deadline = time.time() + 3
while time.time() < deadline and typing is None:
e = ev(1.0)
if e and e.get("type") == "typing":
typing = e
check(typing is not None and typing.get("on") is True,
"typing event from the conversation")
check(typing is not None and typing.get("conv") == "alice",
"typing is filed under the conversation")
send({"type": "file-send", "conv": "alice", "id": "f2",
"path": "/tmp/opencode/sendme.txt", "name": "sendme.txt"})
sending = None
deadline = time.time() + 3
while time.time() < deadline and sending is None:
e = ev(1.0)
if e and e.get("type") == "file-sent" and e.get("id") == "f2":
sending = e
check(sending is not None, "file-sent for an outgoing file")
send({"type": "send", "conv": "4912345@s.whatsapp.net",
"text": "hallo whatsapp", "client_id": "c1"})
sent = message = None
deadline = time.time() + 5
while time.time() < deadline and (sent is None or message is None):
e = ev(1.0)
if e is None:
continue
if e.get("type") == "sent":
sent = e
elif e.get("type") == "message":
message = e
check(sent is not None and sent.get("client_id") == "c1",
"sent event with the client id")
check(message is not None and message.get("text") == "hallo whatsapp",
"the message text")
send({"type": "disconnect"})
bye = None
deadline = time.time() + 3
while time.time() < deadline:
e = ev(1.0)
if e and e.get("type") == "bye":
bye = e
break
check(bye is not None, "bye on disconnect")
try:
check(proc.wait(timeout=3) == 0, "exit 0")
except subprocess.TimeoutExpired:
proc.kill()
check(False, "exit 0")
check(proc.stderr.read().strip() == "", "no stderr output")
os.unlink(FILE)
if failures:
print("test_whatsapp_fake: FAIL (%d)" % len(failures))
sys.exit(1)
print("test_whatsapp_fake: all ok")