"""Smoke-test a published Linux Avalonia build on a disposable Xvfb display.""" from ctypes import CDLL, POINTER, byref, c_char_p, c_int, c_uint, c_ulong, c_void_p from datetime import datetime, timedelta, timezone from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer import json import os from pathlib import Path import subprocess import sys import tempfile import threading import time def window_titles(): x11 = CDLL('libX11.so.6') x11.XOpenDisplay.argtypes = [c_char_p] x11.XOpenDisplay.restype = c_void_p x11.XDefaultRootWindow.argtypes = [c_void_p] x11.XDefaultRootWindow.restype = c_ulong x11.XQueryTree.argtypes = [c_void_p, c_ulong, POINTER(c_ulong), POINTER(c_ulong), POINTER(POINTER(c_ulong)), POINTER(c_uint)] x11.XFetchName.argtypes = [c_void_p, c_ulong, POINTER(c_char_p)] x11.XFree.argtypes = [c_void_p] x11.XCloseDisplay.argtypes = [c_void_p] display = x11.XOpenDisplay(None) if not display: raise OSError('Run this smoke test under Xvfb') children = POINTER(c_ulong)() count = c_uint() titles = set() try: root = x11.XDefaultRootWindow(display) x11.XQueryTree(display, root, byref(c_ulong()), byref(c_ulong()), byref(children), byref(count)) for child in children[:count.value]: title = c_char_p() if x11.XFetchName(display, child, byref(title)) and title.value: titles.add(title.value.decode('utf-8', errors='replace')) x11.XFree(title) return titles finally: if children: x11.XFree(children) x11.XCloseDisplay(display) def main(*command): now = datetime.now(timezone.utc) state = { 'dryRun': True, 'lastAction': None, 'endpoint': {'host': '127.0.0.1', 'port': 3493, 'name': 'h1000m'}, 'update': {'error': None, 'newShutdownSignal': True, 'lastReading': { 'at': now.isoformat(), 'values': {'ups.status': 'OB FSD', 'battery.charge': '71'}}}, 'pending': {'id': '7a581f0d-9ab1-4f81-a814-1e236e1ed309', 'deadline': (now + timedelta(minutes=1)).isoformat()}, } observed = threading.Event() class Handler(BaseHTTPRequestHandler): def do_GET(self): assert self.path == '/status' and self.headers['X-LanUps-Token'] == 'A' * 64 payload = json.dumps(state).encode() self.send_response(200) self.send_header('Content-Type', 'application/json') self.send_header('Content-Length', str(len(payload))) self.end_headers() self.wfile.write(payload) observed.set() def log_message(self, *_): pass with ThreadingHTTPServer(('127.0.0.1', 0), Handler) as server, tempfile.TemporaryDirectory() as tmp: threading.Thread(target=server.serve_forever, daemon=True).start() token = Path(tmp) / 'agent.token' token.write_text('A' * 64) environment = dict(os.environ, LANUPS_AGENT_PORT=str(server.server_port), LANUPS_AGENT_TOKEN=str(token), XDG_DATA_HOME=tmp) process = subprocess.Popen(command, env=environment) try: for _ in range(80): if process.poll() is not None: raise AssertionError(f'Desktop exited with code {process.returncode}') if observed.is_set() and 'UPS 关机确认' in window_titles(): print('PASS: published Avalonia desktop read IPC state and displayed FSD confirmation', flush=True) return time.sleep(.2) raise AssertionError(f'FSD dialog not found: {window_titles()}') finally: process.terminate() process.wait(timeout=10) server.shutdown() if __name__ == '__main__': main(*sys.argv[1:])