#!/usr/bin/env python3 """Headless end-to-end test of the chat client against tools/mock_chat.py. Boots floppy.img/root.img in QEMU with an NE2000 on slirp plus a USB keyboard and mouse, starts the mock Chat + OIDC server on the host (reachable from the guest as 10.0.2.2), then drives the chat app with QMP send-key / input-send-event. Scenarios (one per run): full (default) native login, send, live poll + read marker, PgUp, older paging (Home), conversation switch (Esc, Down), browse + join (Ctrl+B), new DM (Ctrl+N), sign out (click) and a relaunch that resumes the saved /share/chat.cfg session --totp TOTP account: wrong code first, then the right one --consent login followed by the consent interaction --passkey passkey-only account: must land on the cookie screen --wrong wrong password first, then the right one Screenshots go to --out (PNG). Exit code 0 = pass. tools/qmp_chat_test.py [--out DIR] [--totp|--consent|--passkey|--wrong] """ import json import os import shutil import socket import struct import subprocess import sys import tempfile import time import urllib.request import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CHAT_PORT = 18080 LOGIN_PORT = 18081 def qmp_recv(s): buf = b"" while True: chunk = s.recv(65536) if not chunk: raise RuntimeError("QMP connection closed") buf += chunk while b"\n" in buf: line, buf = buf.split(b"\n", 1) line = line.strip() if line: msg = json.loads(line) if "event" in msg: continue return msg def qmp_cmd(s, execute, arguments=None): req = {"execute": execute} if arguments is not None: req["arguments"] = arguments s.sendall((json.dumps(req) + "\n").encode()) msg = qmp_recv(s) if "error" in msg: print("QMP error:", msg) return msg KEYMAP = {" ": ["spc"], ":": ["shift", "semicolon"], "/": ["slash"], ".": ["dot"], "-": ["minus"], "_": ["shift", "minus"], "\n": ["ret"], "\t": ["tab"], ",": ["comma"], "!": ["shift", "1"], "@": ["shift", "2"], "?": ["shift", "slash"], "'": ["apostrophe"]} def key_list(ch): if ch in KEYMAP: return KEYMAP[ch] if ch.isdigit() or ("a" <= ch <= "z"): return [ch] if "A" <= ch <= "Z": return ["shift", ch.lower()] raise ValueError("no qcode for %r" % ch) def send_keys(s, keys, delay=0.12): qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": k} for k in keys]}) time.sleep(delay) def type_text(s, text): for ch in text: send_keys(s, key_list(ch)) def ppm_to_png(ppm, png): data = open(ppm, "rb").read() parts = data.split(b"\n", 3) w, h = [int(x) for x in parts[1].split()] px = parts[3] raw = b"".join(b"\x00" + px[y * w * 3:(y + 1) * w * 3] for y in range(h)) def chunk(tag, body): return (struct.pack(">I", len(body)) + tag + body + struct.pack(">I", zlib.crc32(tag + body) & 0xffffffff)) with open(png, "wb") as f: f.write(b"\x89PNG\r\n\x1a\n") f.write(chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0))) f.write(chunk(b"IDAT", zlib.compress(raw, 6))) f.write(chunk(b"IEND", b"")) def move(s, dx, dy): while dx or dy: ex = max(-100, min(100, dx)) ey = max(-100, min(100, dy)) qmp_cmd(s, "input-send-event", {"events": [ {"type": "rel", "data": {"axis": "x", "value": ex}}, {"type": "rel", "data": {"axis": "y", "value": ey}}]}) dx -= ex dy -= ey time.sleep(0.05) def click(s, x, y): """Move to the top-left corner (the cursor clamps), then to x,y.""" move(s, -3000, -3000) time.sleep(0.2) move(s, x, y) time.sleep(0.3) for down in (True, False): qmp_cmd(s, "input-send-event", {"events": [ {"type": "btn", "data": {"down": down, "button": "left"}}]}) time.sleep(0.2) def shot(s, workdir, out, name): ppm = os.path.join(workdir, "s.ppm") if os.path.exists(ppm): os.unlink(ppm) qmp_cmd(s, "screendump", {"filename": ppm}) for _ in range(50): if os.path.exists(ppm) and os.path.getsize(ppm) > 100: break time.sleep(0.1) time.sleep(0.3) png = os.path.join(out, name + ".png") ppm_to_png(ppm, png) print("screenshot:", png) def mock_get(path): with urllib.request.urlopen("http://127.0.0.1:%d%s" % (CHAT_PORT, path), timeout=5) as r: return json.loads(r.read().decode()) def mock_post(path, body): req = urllib.request.Request("http://127.0.0.1:%d%s" % (CHAT_PORT, path), data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=5) as r: return json.loads(r.read().decode()) def wait_for(pred, timeout, what): end = time.time() + timeout while time.time() < end: try: if pred(): return True except Exception: pass time.sleep(0.5) print("TIMEOUT waiting for", what) return False def main(): out = os.path.join(ROOT, "chat-test-out") if "--out" in sys.argv: out = sys.argv[sys.argv.index("--out") + 1] os.makedirs(out, exist_ok=True) totp = "--totp" in sys.argv consent = "--consent" in sys.argv passkey = "--passkey" in sys.argv wrong = "--wrong" in sys.argv full = not (totp or consent or passkey or wrong) keep = "--keep" in sys.argv mocklog = os.path.join(out, "mock.log") def logged(sub): return any(sub in l for l in open(mocklog)) for f in ("floppy.img", "root.img"): if not os.path.exists(os.path.join(ROOT, f)): print("%s missing - run `make all` first" % f) return 1 workdir = tempfile.mkdtemp(prefix="nebos-chat-") sock = os.path.join(workdir, "qmp.sock") serial = os.path.join(out, "serial.log") mock = subprocess.Popen([sys.executable, os.path.join(ROOT, "tools", "mock_chat.py"), "--chat-port", str(CHAT_PORT), "--login-port", str(LOGIN_PORT), "-v"], stdout=open(os.path.join(out, "mock.log"), "w"), stderr=subprocess.STDOUT) qemu = ["qemu-system-i386", "-M", "pc", "-m", "512", "-device", "pci-ohci", "-device", "usb-mouse", "-device", "usb-kbd", "-device", "ne2k_pci,netdev=n0", "-netdev", "user,id=n0", "-drive", "file=floppy.img,format=raw,if=floppy", "-drive", "file=root.img,format=raw,if=floppy", "-display", "none", "-serial", "file:" + serial, "-qmp", "unix:%s,server,nowait" % sock] proc = subprocess.Popen(qemu, cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) results = [] def check(name, ok): results.append((name, ok)) print("%-40s %s" % (name, "ok" if ok else "FAIL"), flush=True) return ok s = None try: for _ in range(100): if os.path.exists(sock): break time.sleep(0.1) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sock) qmp_recv(s) qmp_cmd(s, "qmp_capabilities") wait_for(lambda: b"booted" in open(serial, "rb").read(), 60, "boot line") time.sleep(12.0) # WM, terminal, DHCP shot(s, workdir, out, "0-desktop") type_text(s, "chat\n") time.sleep(6.0) shot(s, workdir, out, "1-server") send_keys(s, ["ctrl", "u"]) type_text(s, "http://10.0.2.2:%d\n" % CHAT_PORT) check("login form fetched", wait_for(lambda: any("interaction" in l and '" 200' in l for l in open(os.path.join(out, "mock.log"))), 30, "login page")) time.sleep(2.0) shot(s, workdir, out, "2-credentials") user = "totp" if totp else "consent" if consent else \ "passkey" if passkey else "test" if wrong: type_text(s, user + "\t" + "nope\n") check("wrong password rejected", wait_for(lambda: logged("login attempt user='test'") and logged('/login HTTP/1.1" 401'), 30, "401")) time.sleep(2.0) shot(s, workdir, out, "2a-wrong") # the password field is cleared and focused type_text(s, "test\n") else: type_text(s, user + "\t" + "test" + "\n") if passkey: check("passkey falls back to cookie screen", wait_for(lambda: logged("login attempt user='passkey'"), 30, "passkey attempt")) time.sleep(3.0) shot(s, workdir, out, "2c-passkey") check("no session for passkey account", "u-passkey" not in mock_get("/debug/state")["sessions"].values()) raise StopIteration if totp: time.sleep(5.0) shot(s, workdir, out, "2b-totp") type_text(s, "654321\n") check("wrong TOTP rejected", wait_for(lambda: logged("totp='654321'"), 30, "bad totp")) time.sleep(3.0) shot(s, workdir, out, "2b-totp-wrong") type_text(s, "123456\n") check("session established", wait_for(lambda: ("u-" + user) in mock_get("/debug/state")["sessions"].values(), 40, "session")) if consent: check("consent confirmed", logged("/confirm HTTP/1.1\" 303")) check("history loaded", wait_for(lambda: logged("/api/channels/c-general/messages?limit=50"), 30, "history")) time.sleep(4.0) shot(s, workdir, out, "3-main") if not full: raise StopIteration type_text(s, "Hello from NebOS, it works!\n") check("message sent", wait_for(lambda: any(m["body"] == "Hello from NebOS, it works!" and m["userId"] == "u-" + user and (m["clientId"] or "").startswith("nebos-") for m in mock_get("/debug/state")["messages"]["c-general"]), 30, "sent message")) time.sleep(3.0) shot(s, workdir, out, "4-sent") injected = mock_post("/debug/inject", {"channel": "c-general", "user": "u-alice", "body": "Live reply from Alice \u2013 polled"}) check("live message read", wait_for(lambda: mock_get("/debug/state")["reads"].get( "c-general|u-" + user) == injected["id"], 30, "read marker")) time.sleep(1.5) shot(s, workdir, out, "5-live") for _ in range(4): send_keys(s, ["pgup"], 0.4) time.sleep(2.0) shot(s, workdir, out, "6-scrolled") send_keys(s, ["esc"]) send_keys(s, ["home"]) check("older page loaded", wait_for(lambda: logged("messages?limit=50&before="), 30, "older")) time.sleep(3.0) shot(s, workdir, out, "7-older") send_keys(s, ["end"]) send_keys(s, ["down"]) check("switched to #secret", wait_for(lambda: logged("/api/channels/c-secret/messages?limit=50") and logged("POST /api/channels/c-secret/read"), 30, "secret")) time.sleep(2.0) shot(s, workdir, out, "8-secret") send_keys(s, ["ctrl", "b"]) check("public channels listed", wait_for(lambda: logged("GET /api/channels/public"), 20, "browse")) time.sleep(2.0) shot(s, workdir, out, "9-browse") send_keys(s, ["down"]) # sorted: design, random send_keys(s, ["ret"]) check("joined #random", wait_for(lambda: "u-test" in mock_get("/debug/state")["channels"] ["c-random"]["members"], 20, "join")) time.sleep(3.0) shot(s, workdir, out, "10-joined") send_keys(s, ["ctrl", "n"]) check("users listed", wait_for(lambda: logged("GET /api/users"), 20, "users")) type_text(s, "Bob") time.sleep(1.0) shot(s, workdir, out, "11-newdm") send_keys(s, ["ret"]) check("DM with Bob created", wait_for(lambda: any(c["type"] == "dm" and sorted(c["members"]) == ["u-bob", "u-test"] for c in mock_get("/debug/state")["channels"].values()), 20, "dm")) time.sleep(3.0) shot(s, workdir, out, "12-dm") type_text(s, "Hi Bob\n") check("DM message sent", wait_for(lambda: any(m["body"] == "Hi Bob" for cid, ms in mock_get("/debug/state")["messages"].items() if cid.startswith("d-") for m in ms), 20, "dm msg")) # Close the window (title-bar x), relaunch: the saved session in # /share/chat.cfg must log in without the password. click(s, 1028, 81) time.sleep(2.0) click(s, 200, 300) # focus the terminal time.sleep(1.0) n_me = sum(1 for l in open(mocklog) if "GET /api/me" in l) type_text(s, "chat\n") check("saved session resumed", wait_for(lambda: sum(1 for l in open(mocklog) if "GET /api/me" in l) > n_me and sum(1 for l in open(mocklog) if "GET /api/channels HTTP" in l) >= 2, 40, "resume")) time.sleep(4.0) shot(s, workdir, out, "13-resumed") click(s, 987, 112) # Sign out check("signed out", wait_for(lambda: logged("GET /auth/logout"), 20, "logout")) time.sleep(2.0) shot(s, workdir, out, "14-signed-out") except StopIteration: pass # scenario finished early finally: try: qmp_cmd(s, "quit") proc.wait(timeout=10) except Exception: pass if proc.poll() is None: proc.kill() proc.wait() mock.terminate() mock.wait() if not keep: shutil.rmtree(workdir, ignore_errors=True) failed = [n for n, ok in results if not ok] print("chat e2e: %s" % ("PASS" if results and not failed else "FAIL " + ", ".join(failed))) return 0 if results and not failed else 1 if __name__ == "__main__": sys.exit(main())