✨ Replay generated programs in the browser with the WASM engine

The frontend replay of generated component programs runs in node, where
the WASM render engine is not available. A `test-wasm` build runs the same
replay in a browser page: it loads the engine on a canvas, turns on
render-wasm/v1 and loads the shapes of the replayed page into the engine
whenever they change, so every layout goes through it. Text edits commit
through the SVG editor, as the WASM editor needs its own editing session.
The page reads the programs the common tests pin, which a script writes
out, and reports each one to the console.

Build with `pnpm run build:test-wasm`, then open
`/test-wasm.html?programs=programs.js`.

Test tooling only; the benchmark is not run.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-14 12:40:35 +01:00
parent 77e17922a2
commit 5d84e9a449
7 changed files with 225 additions and 1 deletions

View File

@ -34,6 +34,7 @@
"lint:js": "exit 0",
"lint:scss": "pnpm exec stylelint '{src,resources}/**/*.scss'",
"build:test": "pnpm run build:wasm && clojure -M:dev:shadow-cljs compile test",
"build:test-wasm": "clojure -M:dev:shadow-cljs compile test-wasm && node ./scripts/build-test-wasm.js && clojure -M scripts/test-wasm-programs.clj",
"test": "pnpm run build:test && node target/tests/test.js",
"test:quiet": "node ./scripts/test-quiet.js",
"test:storybook": "vitest run --project=storybook",

View File

@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WASM replay</title>
<script src="/js/config.js"></script>
<script src="/js/polyfills.js"></script>
</head>
<body>
<script type="module">
import "/js/test-wasm/libs.js";
const mod = await import("/js/test-wasm/test-wasm.js");
</script>
</body>
</html>

View File

@ -0,0 +1,21 @@
// Bundle the npm libraries of the `test-wasm` build (its external index)
// and copy its page, which replays generated component programs in the
// browser with the WASM engine on: `/test-wasm.html?programs=<file>.js`.
import * as esbuild from "esbuild";
import { copyFile, mkdir } from "node:fs/promises";
await esbuild.build({
entryPoints: ["target/test-wasm-index.js"],
bundle: true,
format: "esm",
banner: {
js: '"use strict";\nvar global = globalThis;',
},
outfile: "resources/public/js/test-wasm/libs.js",
});
await mkdir("resources/public", { recursive: true });
await copyFile(
"resources/test-wasm/index.html",
"resources/public/test-wasm.html",
);

View File

@ -0,0 +1,34 @@
;; Write the generated component programs the common tests pin
;; (`composable_gen_test.cljc`) for the `test-wasm` page: the `cg/run`
;; calls whose program is written out in full, as EDN in a `.js` file, the
;; only kind the development server serves there.
;;
;; clojure -M scripts/test-wasm-programs.clj
(require '[clojure.walk :as walk])
(import '[java.io PushbackReader StringReader])
(def source "../common/test/common_tests/logic/composable_gen_test.cljc")
(def target "resources/public/js/test-wasm/programs.js")
(defn- literal?
"Whether `form` holds no symbol: a program written out in full."
[form]
(not-any? symbol? (tree-seq coll? seq form)))
(let [reader (PushbackReader. (StringReader. (slurp source)))
programs (atom [])]
(binding [*read-eval* false]
(loop []
(let [form (read {:eof ::eof :read-cond :allow :features #{:clj}} reader)]
(when-not (= form ::eof)
(walk/postwalk (fn [x]
(when (and (seq? x) (= 'cg/run (first x)) (map? (second x))
(literal? (second x)))
(swap! programs conj (second x)))
x)
form)
(recur)))))
(.mkdirs (.getParentFile (java.io.File. target)))
(spit target (pr-str (vec (distinct @programs))))
(println (count (distinct @programs)) "programs written to" target))

View File

@ -160,6 +160,20 @@
{:test {:init-fn frontend-tests.runner/init
:prepend-js ";if (typeof globalThis.navigator?.userAgent === 'undefined') { globalThis.navigator = {userAgent: ''}; };"}}}
:test-wasm
{:target :esm
:output-dir "resources/public/js/test-wasm/"
:asset-path "/js/test-wasm"
:runtime :browser
:js-options {:entry-keys ["module" "browser" "main"]
:export-conditions ["module" "import", "browser" "require" "default"]
:js-provider :external
:external-index "target/test-wasm-index.js"
:external-index-format :esm}
:modules {:test-wasm {:init-fn frontend-tests.wasm-replay/init}}
:compiler-options {:output-feature-set :es2020
:warnings {:fn-deprecated false}}}
:bench
{:target :node-script
:output-to "target/bench.js"

View File

@ -40,7 +40,7 @@
(when-let [id (get by-path path)] [id (grid/remap-groups groups ids-map)]))))
(cg/forest (:situation state)))))
(defn- ^:async replay-program
(defn ^:async replay-program
"Replay a JVM-shrunk EDN program against the async frontend. The pure
runner chooses targets independently and both must make the same choices."
[{:keys [world ops] :as program}]

View File

@ -0,0 +1,139 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.wasm-replay
"Replays generated component programs through the frontend in a browser,
with the WASM render engine laying the shapes out. The page loads the
engine on a canvas and fetches the programs from `programs-uri`; each
program replays as the frontend generated tests replay it, and the
results go to the console and to `globalThis.wasmReplay`."
(:require
[app.common.test-helpers.composable.comp.nodes :as n]
[app.main.data.helpers :as dsh]
[app.main.store :as st]
[app.render-wasm.api :as wasm.api]
[cljs.reader :as reader]
[cljs.test :as t]
[frontend-tests.composable-tests.generated-test :as gt]
[frontend-tests.composable-tests.interpreter :as ftm]
[promesa.core :as p]))
(defn- programs-uri
"The programs file the page names in its `programs` query parameter: EDN
in a `.js` file, the only kind the development server serves there."
[]
(let [params (js/URLSearchParams. (.. js/globalThis -location -search))]
(str "/js/test-wasm/" (or (.get params "programs") "programs.js"))))
(def ^:private plain-install! ftm/install!)
(defn- load-objects!
"Load the shapes of the current page of `state` into the WASM engine when
they differ from those of `old`."
[_ _ old state]
(let [objects (dsh/lookup-page-objects state)]
(when (and (some? objects) (not (identical? objects (dsh/lookup-page-objects old))))
(wasm.api/set-objects objects))))
(defn- wasm-install!
"Install `situation` as `ftm/install!` does, with the WASM engine on and
holding the shapes of the current page, which it loads again whenever
they change: the shapes stay plain maps, which the engine does not
follow by themselves."
[situation]
(plain-install! situation)
(st/emit! #(update % :features (fnil conj #{}) "render-wasm/v1"))
(wasm.api/set-objects (dsh/lookup-page-objects @st/state))
(add-watch st/state ::wasm-objects load-objects!)
situation)
(def ^:private plain-op->events ftm/op->events)
(defn- svg-text-events
"The events of `op` as `ftm/op->events` gives them, but a text edit runs
with the WASM engine off: the WASM text editor commits through its own
editing session, which the headless page does not have, so the edit goes
through the SVG editor's commit and the engine lays out what follows."
[op situation]
(let [events (plain-op->events op situation)]
(if (instance? n/EditText op)
(-> [#(update % :features disj "render-wasm/v1")]
(into events)
(conj #(update % :features (fnil conj #{}) "render-wasm/v1")))
events)))
(def ^:private wasm-layouts
"How many times the WASM engine propagated modifiers."
(atom 0))
(def ^:private plain-propagate wasm.api/propagate-modifiers)
(defn- counted-propagate
[entries snap-pixel? snap-ignore-axis]
(swap! wasm-layouts inc)
(plain-propagate entries snap-pixel? snap-ignore-axis))
(def ^:private plain-step! ftm/run-generated-step)
(defn- logged-step!
"Run a step as `ftm/run-generated-step` does, logging it."
[live op]
(js/console.log "WASM-REPLAY step" (pr-str (type op)))
(plain-step! live op))
(defn- ^:async replay
"Replay `program`, returning its failures and errors."
[program]
(t/set-env! (t/empty-env))
((:before ftm/fixture))
(try
(await (gt/replay-program program))
(catch :default cause
(js/console.log "WASM-REPLAY error" (ex-message cause) (some-> (.-stack cause) (subs 0 1500)))
(t/do-report {:type :error :message (ex-message cause) :actual cause}))
(finally
(remove-watch st/state ::wasm-objects)
((:after ftm/fixture))))
(let [counters (:report-counters (t/get-current-env))]
(select-keys counters [:pass :fail :error])))
(defn- ^:async run-programs
[programs]
(loop [index 0 results []]
(if (< index (count programs))
(let [program (nth programs index)
result (await (replay program))]
(js/console.log "WASM-REPLAY" index (pr-str result) (pr-str program))
(set! (.-wasmReplayProgress js/globalThis) (inc index))
(recur (inc index) (conj results (assoc result :index index))))
(let [failed (filterv #(or (pos? (:fail % 0)) (pos? (:error % 0))) results)]
(js/console.log "WASM-REPLAY done" (count results) "programs," (count failed) "failed"
(pr-str (mapv :index failed)) "WASM layouts" @wasm-layouts)
(set! (.-wasmReplay js/globalThis)
(clj->js {:done true :total (count results) :failed (mapv :index failed)
:layouts @wasm-layouts}))
results))))
(defn ^:export init
"Load the WASM engine on a canvas of the page, then replay the programs."
[]
(enable-console-print!)
(set! ftm/install! wasm-install!)
(set! ftm/run-generated-step logged-step!)
(set! ftm/op->events svg-text-events)
(set! wasm.api/propagate-modifiers counted-propagate)
(.addEventListener js/globalThis "unhandledrejection"
#(js/console.log "WASM-REPLAY unhandled" (str (.-reason %))))
(p/let [_ @wasm.api/module
canvas (js/document.createElement "canvas")
_ (set! (.-width canvas) 800)
_ (set! (.-height canvas) 600)
_ (.appendChild js/document.body canvas)
ready (wasm.api/init-canvas-context canvas)
resp (js/fetch (programs-uri))
text (.text resp)]
(js/console.log "WASM-REPLAY engine" (if ready "ready" "not ready"))
(run-programs (reader/read-string text))))