#!/usr/bin/env python3 """tests/irc_typing.py -- IRCv3 typing tags (T3.4/T4.3). usage: tests/irc_typing.py [path-to-dtchat-irc] The server offers message-tags, acknowledges the request, sends two TAGMSG lines with +typing from alice, and records the adapter's own TAGMSG lines for a typing command. """ import json import queue import socket import subprocess import sys import threading import time ADAPTER = sys.argv[1] if len(sys.argv) > 1 else \ "build/mcde-host/programs/dtchat/dtchat-irc" failures = [] outgoing = [] cap_req = [] def check(cond, what): print(("ok " if cond else "FAIL ") + what, flush=True) if not cond: failures.append(what) srv = socket.socket() srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srv.bind(("127.0.0.1", 0)) srv.listen(1) port = srv.getsockname()[1] online_sent = threading.Event() done = threading.Event() def server(): conn, _ = srv.accept() conn.settimeout(0.2) buf = b"" def wait_line(marker=None, timeout=6): nonlocal buf deadline = time.time() + timeout while time.time() < deadline: if marker is None and b"\r\n" in buf: line, buf = buf.split(b"\r\n", 1) return line.decode("utf-8", "replace") if marker is not None and marker in buf: return marker.decode() try: chunk = conn.recv(4096) except socket.timeout: continue if not chunk: return None buf += chunk return None wait_line() conn.sendall(b":srv CAP * LS :sasl message-tags\r\n") line = wait_line(b"CAP REQ") # wait_line returns the marker only; look at the whole line around it cap_req.append(buf is not None and b"message-tags" in buf.split(b"CAP REQ", 1)[1][:80]) conn.sendall(b":srv CAP tester ACK :message-tags\r\n") wait_line(b"CAP END") wait_line(b"USER ") conn.sendall(b":srv 001 tester :Welcome\r\n") # the channel join may follow; answer the JOIN line = wait_line() if line is not None and "JOIN" in line: conn.sendall(b":tester!t@h JOIN #cde\r\n") conn.sendall(b"@+typing=active :alice!a@h TAGMSG #cde\r\n") conn.sendall(b"@+typing=paused :alice!a@h TAGMSG #cde\r\n") online_sent.set() deadline = time.time() + 6 while time.time() < deadline: try: chunk = conn.recv(4096) except socket.timeout: continue if not chunk: break buf += chunk while b"\r\n" in buf: line, buf = buf.split(b"\r\n", 1) text = line.decode("utf-8", "replace") if "TAGMSG" in text: outgoing.append(text) if len(outgoing) >= 2: done.set() conn.close() threading.Thread(target=server, daemon=True).start() proc = subprocess.Popen([ADAPTER, "--stdio"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) 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") send({"type": "configure", "account": "t", "settings": {"host": "127.0.0.1", "port": port, "tls": False, "nick": "tester", "channels": "#cde"}, "secrets": {}}) send({"type": "connect"}) online = False typing = [] deadline = time.time() + 8 while time.time() < deadline: e = ev(1.0) if e is None: if online and len(typing) >= 2: break continue if e.get("type") == "state" and e.get("state") == "online": online = True elif e.get("type") == "typing": typing.append(e) if len(typing) >= 2: break check(online, "online") check(cap_req and cap_req[0], "the adapter asked for message-tags") check(any(t.get("on") is True and t.get("who") == "alice" for t in typing), "incoming typing active from alice") check(any(t.get("on") is False and t.get("who") == "alice" for t in typing), "incoming typing paused from alice") check(any(t.get("conv") == "#cde" for t in typing), "typing is filed under the channel") send({"type": "typing", "conv": "#cde", "on": True}) send({"type": "typing", "conv": "#cde", "on": False}) check(done.wait(6), "the adapter sent both TAGMSG lines") check(any(l.startswith("@+typing=active TAGMSG #cde") for l in outgoing), "outgoing +typing=active") check(any(l.startswith("@+typing=paused TAGMSG #cde") for l in outgoing), "outgoing +typing=paused") 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") if failures: print("test_irc_typing: FAIL (%d)" % len(failures)) sys.exit(1) print("test_irc_typing: all ok")