#!/usr/bin/env python3 """Prove the store bundle answers the page through the stdlib framework. Serves the built bundle directory (store.wasm, the emitted $runtime/, the app's store-bridge.mjs), loads a page that starts it with startBundle, and speaks the store's own JSON-array protocol: a KV put, a KV get, a KV get-all. Both directions have to work - the page must receive the module's SR_* replies, and the module must receive the page's requests - so a bundle that boots but cannot be talked to fails here rather than looking healthy. Usage: store-bundle-test.py [--dir DIR] """ import functools, http.server, json, pathlib, socketserver, sys, threading try: from playwright.sync_api import sync_playwright except ImportError: print("playwright is required: pip install playwright") sys.exit(2) DIR = pathlib.Path(sys.argv[sys.argv.index("--dir") + 1]) if "--dir" in sys.argv else pathlib.Path("/tmp/smesh-store-proof") PAGE = """
""" class Handler(http.server.SimpleHTTPRequestHandler): def do_GET(self): if self.path in ("/", "/index.html"): body = PAGE.encode() self.send_response(200) self.send_header("Content-Type", "text/html") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) return super().do_GET() def end_headers(self): self.send_header("Cross-Origin-Opener-Policy", "same-origin") self.send_header("Cross-Origin-Embedder-Policy", "require-corp") super().end_headers() def log_message(self, *a): pass def main(): if not (DIR / "store.wasm").exists(): print(f"no store.wasm in {DIR}") return 1 srv = socketserver.TCPServer(("127.0.0.1", 0), functools.partial(Handler, directory=str(DIR))) srv.allow_reuse_address = True port = srv.server_address[1] threading.Thread(target=srv.serve_forever, daemon=True).start() with sync_playwright() as pw: browser = pw.chromium.launch() page = browser.new_page() page.on("console", lambda m: print(" console: " + m.text)) page.goto(f"http://127.0.0.1:{port}/", wait_until="load") page.wait_for_function("window.__done === true", timeout=30000) out = page.evaluate("() => window.__store") browser.close() srv.shutdown() print(" store bundle said: " + json.dumps(out)) if not out or not out.get("ok"): print("FAIL: the store bundle did not complete the exchange") return 1 if out.get("kv") != "hello-from-page": print("FAIL: KVGet did not return what KVPut stored") return 1 if ["probe", "hello-from-page"] not in (out.get("kvAll") or []): print("FAIL: KVGetAll did not walk the store") return 1 if "SR_READY" not in (out.get("replies") or []): print("FAIL: the page never received the worker's ready reply") return 1 print("PASS: the store bundle answers the page through the stdlib framework") return 0 if __name__ == "__main__": sys.exit(main())