diff --git a/frontend/package.json b/frontend/package.json
index d5b917acd0..4dbafae2c7 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -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",
diff --git a/frontend/resources/test-wasm/index.html b/frontend/resources/test-wasm/index.html
new file mode 100644
index 0000000000..c97eeb1958
--- /dev/null
+++ b/frontend/resources/test-wasm/index.html
@@ -0,0 +1,15 @@
+
+
+
+
+ WASM replay
+
+
+
+
+
+
+
diff --git a/frontend/scripts/build-test-wasm.js b/frontend/scripts/build-test-wasm.js
new file mode 100644
index 0000000000..ae66fe83ab
--- /dev/null
+++ b/frontend/scripts/build-test-wasm.js
@@ -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=.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",
+);
diff --git a/frontend/scripts/test-wasm-programs.clj b/frontend/scripts/test-wasm-programs.clj
new file mode 100644
index 0000000000..5bec11f5c3
--- /dev/null
+++ b/frontend/scripts/test-wasm-programs.clj
@@ -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))
diff --git a/frontend/shadow-cljs.edn b/frontend/shadow-cljs.edn
index ef226a62b0..c84d1433b1 100644
--- a/frontend/shadow-cljs.edn
+++ b/frontend/shadow-cljs.edn
@@ -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"
diff --git a/frontend/test/frontend_tests/composable_tests/generated_test.cljs b/frontend/test/frontend_tests/composable_tests/generated_test.cljs
index 7d193fc7bf..18531498d9 100644
--- a/frontend/test/frontend_tests/composable_tests/generated_test.cljs
+++ b/frontend/test/frontend_tests/composable_tests/generated_test.cljs
@@ -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}]
diff --git a/frontend/test/frontend_tests/wasm_replay.cljs b/frontend/test/frontend_tests/wasm_replay.cljs
new file mode 100644
index 0000000000..b86de38fa2
--- /dev/null
+++ b/frontend/test/frontend_tests/wasm_replay.cljs
@@ -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))))