penpot/frontend/scripts/test-wasm-programs.clj
alonso.torres 5d84e9a449 ✨ 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
2026-11-18 10:29:49 +01:00

35 lines
1.4 KiB
Clojure

;; 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))