"""Check that the Notes demo stores exactly what the user types. The harness runs weft-appd on a nested desktop with the real runtime and app shell, seeds the Notes data with text containing a literal backslash sequence, quotes, a tab and non-ASCII characters, launches `org.weft.demo.notes` and edits it with real keyboard input: 1. Typing at the end and pressing Ctrl+S must store the seeded text plus the typed text, byte for byte; a load or save that rewrote `\\n`, quotes or tabs fails. 2. Text typed right after Ctrl+S, while the save is in flight, must still be in the editor afterwards: a second Ctrl+S stores it too. 3. After the file is changed on disk (as by another Notes window), a save must be refused and the other window's text kept. Requires Xvfb, xwd, ImageMagick's convert and xdotool. """ import argparse import shutil import subprocess import sys import tempfile import time from pathlib import Path sys.dont_write_bytecode = True from check_counter import AppdClient, free_port, read_endpoint # noqa: E402 from session import ROOT, Desktop, SessionError, missing_tools # noqa: E402 APP_ID = "org.weft.demo.notes" SEED = 'path C:\\new\\table "quoted"\tcaf\u00e9 \u2603' def wait_for_file(path, expected, timeout=15): deadline = time.monotonic() + timeout while time.monotonic() < deadline: if path.exists() and path.read_text(encoding="utf-8") == expected: return time.sleep(0.2) actual = path.read_text(encoding="utf-8") if path.exists() else None raise AssertionError(f"stored notes {actual!r}, expected {expected!r}") def run(args, desktop, home, xdotool): data_home = home / "share" notes = data_home / "weft/app-data" / APP_ID / "notes.txt" notes.parent.mkdir(parents=True) notes.write_text(SEED, encoding="utf-8") t = Path(args.target) desktop.launch_client("appd", [t / "weft-appd"], { "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(free_port()), "HOME": str(home), "XDG_DATA_HOME": str(data_home), }) port, token = read_endpoint(desktop.runtime, 30) appd = AppdClient(port, token) appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0}) reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 120) if reply.get("type") != "APP_READY": raise AssertionError(f"Notes did not start: {reply}") time.sleep(2) def act(*arguments): subprocess.run([xdotool, *arguments], env={"DISPLAY": desktop.display}, check=True, timeout=30) act("search", "--class", "weft-compositor", "windowfocus", "--sync") # The editor fills the window below the header. act("mousemove", "--sync", "100", "300") act("click", "1") act("key", "ctrl+End") act("type", "--delay", "40", ' +\\t "x"') act("key", "ctrl+s") expected = SEED + ' +\\t "x"' act("type", "--delay", "40", " more") wait_for_file(notes, expected) time.sleep(1) act("key", "ctrl+s") expected += " more" wait_for_file(notes, expected) other = "written by another window" notes.write_text(other, encoding="utf-8") act("type", "--delay", "40", " lost?") act("key", "ctrl+s") time.sleep(3) if notes.read_text(encoding="utf-8") != other: raise AssertionError("a save replaced notes changed elsewhere") if list(notes.parent.glob(".*saving")): raise AssertionError("a staging file was left behind") def main(argv=None): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--compositor", type=Path, default=ROOT / "target/debug/weft-compositor") parser.add_argument("--target", type=Path, default=ROOT / "target/debug", help="directory with weft-appd, weft-runtime and weft-app-shell") parser.add_argument("--store", type=Path, default=ROOT / "examples", help="app store containing org.weft.demo.notes") parser.add_argument("--output", type=Path, default=ROOT / "target/notes-check") args = parser.parse_args(argv) xdotool = shutil.which("xdotool") missing = missing_tools() + ([] if xdotool else ["xdotool"]) missing += [str(p) for p in (args.compositor, args.target / "weft-appd", args.target / "weft-runtime", args.target / "weft-app-shell") if not p.is_file()] if missing: print("missing: " + ", ".join(missing), file=sys.stderr) return 2 home = Path(tempfile.mkdtemp(prefix="weft-home-")) try: with Desktop(args.compositor, args.output, outputs=["appd.log", "notes.ppm"]) as desktop: try: run(args, desktop, home, xdotool) except AssertionError: (args.output / "notes.ppm").write_bytes(desktop.capture()) raise except (AssertionError, SessionError, TimeoutError) as failure: print(f"notes check failed: {failure}", file=sys.stderr) print(f"logs and capture in {args.output}", file=sys.stderr) return 1 finally: shutil.rmtree(home, ignore_errors=True) print("notes check passed") return 0 if __name__ == "__main__": raise SystemExit(main())