diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 7964e2d1c8..30421af407 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -423,8 +423,7 @@ ;; When activated the wasm rendering we need to recreate its thumbnail on creation (if (features/active-feature? state "render-wasm/v1") - (rx/of (dwt.wasm/render-thumbnail file-id page-id (:id root)) - (dwt.wasm/persist-thumbnail file-id page-id (:id root))) + (rx/of (dwt.wasm/render-thumbnail file-id page-id (:id root) :persist? true)) (rx/empty))))))))))) (defn add-component @@ -971,8 +970,8 @@ page-id (:main-instance-page component) root-id (:main-instance-id component)] (if (features/active-feature? state "render-wasm/v1") - ;; WASM: render immediately, UI only — server persist happens - ;; on the debounced path (update-component-thumbnail). Frame + ;; WASM: ask the worker to render it for the UI, without uploading; + ;; the debounced path (update-component-thumbnail) uploads it. Frame ;; thumbnails are only shown by the SVG viewport. (when (= tag "component") (dwt.wasm/render-thumbnail file-id page-id root-id)) @@ -1701,14 +1700,18 @@ component-events-s ;; WASM only: render the thumbnail on every component - ;; change so single edits (fill, etc.) update instantly. + ;; change so single edits (fill, etc.) update without waiting + ;; for the 5 s idle upload. ;; Non-WASM persists on every render, so it stays on the ;; debounced path below to avoid per-edit backend posts. (if (features/active-feature? state "render-wasm/v1") - (->> component-events-s - (rx/filter (ptk/type? ::component-changed)) - (rx/map deref) - (rx/map render-component-thumbnail-event)) + (rx/merge + (->> component-events-s + (rx/filter (ptk/type? ::component-changed)) + (rx/map deref) + (rx/map render-component-thumbnail-event)) + ;; Shows the thumbnails the worker renders + (dwt.wasm/component-thumbnail-events)) (rx/empty)) ;; Persist to the server in batches, 5s after the user diff --git a/frontend/src/app/main/data/workspace/thumbnails_wasm.cljs b/frontend/src/app/main/data/workspace/thumbnails_wasm.cljs index 7e230ca3a9..0a880c0ec7 100644 --- a/frontend/src/app/main/data/workspace/thumbnails_wasm.cljs +++ b/frontend/src/app/main/data/workspace/thumbnails_wasm.cljs @@ -5,200 +5,42 @@ ;; Copyright (c) KALEIDOS SUBSIDIARY SL (ns app.main.data.workspace.thumbnails-wasm - "WASM-based component thumbnail rendering. - Renders component previews using the existing workspace WASM context - via render-shape-pixels (SurfaceId::Export), avoiding a separate - WASM module in the worker. - - Two-phase design: - - render-thumbnail: immediate WASM render → UI update (no server) - - persist-thumbnail: pushes current data-uri to the server (debounced)" + "Component thumbnails with render-wasm/v1. The worker renders them + from the shapes of their mains, which the main thread sends, and + uploads the ones asked for." (:require - [app.common.data.macros :as dm] [app.common.files.helpers :as cfh] - [app.common.logging :as l] - [app.common.math :as mth] - [app.common.thumbnails :as thc] - [app.common.time :as ct] [app.main.data.helpers :as dsh] [app.main.data.workspace.thumbnails :as dwt] - [app.main.repo :as rp] - [app.main.store :as st] - [app.render-wasm.api :as wasm.api] - [app.util.timers :as timers] + [app.main.worker :as mw] [app.util.webapi :as wapi] [beicon.v2.core :as rx] - [cuerdas.core :as str] [potok.v2.core :as ptk])) -(l/set-level! :warn) - -(defn- png-bytes->data-uri - "Converts a Uint8Array of PNG bytes to a data:image/png;base64 URI." - [png-bytes] - (let [blob (wapi/create-blob png-bytes "image/png") - reader (js/FileReader.)] - (js/Promise. - (fn [resolve reject] - (set! (.-onload reader) - (fn [] (resolve (.-result reader)))) - (set! (.-onerror reader) - (fn [e] (reject e))) - (.readAsDataURL reader blob))))) - -;; Bounds for a thumbnail's longest side, in pixels. Full-size renders of big -;; components block the main thread for seconds. -(def ^:private ^:const min-target-size 200) -(def ^:private ^:const max-target-size 512) - -(defn- thumbnail-scale - [longest-side] - (if (pos? longest-side) - (-> 1 - (mth/max (/ min-target-size longest-side)) - (mth/min (/ max-target-size longest-side))) - 1)) - -(defn- render-component-pixels - "Returns an observable emitting the component frame as a PNG data uri. - Waits for a frame with no viewport render pending so it never delays it." - [file-id page-id frame-id] - (rx/create - (fn [subs] - (let [req-id (volatile! nil) - render - (fn render [_] - (if (wasm.api/render-pending?) - (vreset! req-id (timers/raf render)) - (try - (let [objects (dsh/lookup-page-objects @st/state file-id page-id)] - (if-let [frame (get objects frame-id)] - (let [{ext-w :width ext-h :height} (wasm.api/get-shape-extrect frame-id) - {sel-w :width sel-h :height} (:selrect frame) - longest-side (mth/max (or ext-w sel-w) (or ext-h sel-h)) - scale (thumbnail-scale longest-side) - ;; Thumbnails are always PNG: they need the alpha - ;; channel and are consumed as a `image/png` data uri. - png-bytes (wasm.api/render-shape-pixels frame-id scale :png)] - (if (or (nil? png-bytes) (zero? (.-length png-bytes))) - (do - (l/error :hint "render-shape-pixels returned empty" :frame-id (str frame-id)) - (rx/end! subs)) - (.then - (png-bytes->data-uri png-bytes) - (fn [data-uri] - (rx/push! subs data-uri) - (rx/end! subs)) - (fn [err] - (rx/error! subs err))))) - - ;; A missing frame. Nothing to render, so end quietly. - (rx/end! subs))) - (catch :default err - (rx/error! subs err)))))] - (vreset! req-id (timers/raf render)) - #(timers/cancel-af! @req-id))))) - (defn render-thumbnail - "Renders a component thumbnail via WASM and updates the UI immediately. - When `persist?` is true, also persists the rendered thumbnail to the - server in the same observable chain (guaranteeing correct ordering)." - [file-id page-id frame-id & {:keys [persist?] :or {persist? false}}] + "Sends the worker the shapes of the main whose root is `root-id`, to + render its component thumbnail, and to upload it when `persist?` is + true. Nothing comes back: `component-thumbnail-events` shows the + render." + [file-id page-id root-id & {:keys [persist?]}] + (ptk/reify ::render-thumbnail + ptk/EffectEvent + (effect [_ state _] + (let [objects (dsh/lookup-page-objects state file-id page-id)] + ;; A removed component can bring a nil or deleted root + (when (contains? objects root-id) + (let [shapes (cfh/get-children-with-self objects root-id)] + (mw/emit! {:cmd :thumbnails/render-component + :file-id file-id + :page-id page-id + :root-id root-id + :persist? persist? + :objects (into {} (map (juxt :id identity)) shapes)}))))))) - (let [object-id (thc/fmt-object-id file-id page-id frame-id "component")] - (ptk/reify ::render-thumbnail - cljs.core/IDeref - (-deref [_] object-id) - - ptk/WatchEvent - (watch [_ state stream] - ;; When the component is removed it can arrived a render - ;; request with frame-id=null - (when (some? frame-id) - (letfn [(load-objects-stream - [] - (rx/create - (fn [subs] - (let [objects (dsh/lookup-page-objects state file-id page-id) - - ;; retrieves a subtree with only the id and its children - ;; to be loaded before rendering the thumbnail - subtree - (into {} - (map #(vector (:id %) %)) - (cfh/get-children-with-self objects frame-id))] - (try - (wasm.api/set-objects subtree #(rx/push! subs %)) - (catch :default err - (rx/error! subs err))))))) - - (get-subtree - [state] - (-> (dsh/lookup-page-objects state file-id page-id) - (cfh/get-children-with-self frame-id))) - - (persist-to-server - [data-uri] - ;; A change during the render (a rollback, an edit) makes - ;; it stale; that change brings its own render - (if (= (get-subtree state) (get-subtree @st/state)) - (let [blob (wapi/data-uri->blob data-uri)] - (->> (rp/cmd! :create-file-object-thumbnail - {:file-id file-id - :object-id object-id - :media blob - :tag "component"}) - (rx/catch rx/empty) - (rx/ignore))) - (rx/empty))) - - (do-render-thumbnail - [] - (let [tp (ct/tpoint-ms)] - (->> (render-component-pixels file-id page-id frame-id) - (rx/mapcat - (fn [data-uri] - (l/dbg :hint "component thumbnail rendered (wasm)" - :elapsed (dm/str (tp) "ms")) - (if persist? - (rx/merge - (rx/of (dwt/assoc-thumbnail object-id data-uri)) - (persist-to-server data-uri)) - (rx/of (dwt/assoc-thumbnail object-id data-uri))))) - - (rx/catch (fn [err] - (js/console.error err) - (l/error :hint "error rendering component thumbnail" :frame-id (str frame-id)) - (rx/empty))) - - (rx/take-until - (->> stream - (rx/filter (ptk/type? ::dwt/clear-thumbnail)) - (rx/filter #(= (deref %) object-id)))))))] - - (if (not= page-id (:current-page-id state)) - (->> (load-objects-stream) - (rx/mapcat do-render-thumbnail)) - (do-render-thumbnail)))))))) - -(defn persist-thumbnail - "Persists the current component thumbnail data-uri to the server. - Expects that `render-thumbnail` has already been called so the - data-uri is present in app state. If not, this is a no-op." - [file-id page-id frame-id] - (let [object-id (thc/fmt-object-id file-id page-id frame-id "component")] - (ptk/reify ::persist-thumbnail - ptk/WatchEvent - (watch [_ state _] - (let [data-uri (dm/get-in state [:thumbnails object-id :uri])] - (if (and (some? data-uri) - (str/starts-with? data-uri "data:")) - (let [blob (wapi/data-uri->blob data-uri)] - (->> (rp/cmd! :create-file-object-thumbnail - {:file-id file-id - :object-id object-id - :media blob - :tag "component"}) - (rx/catch rx/empty) - (rx/ignore))) - (rx/empty))))))) +(defn component-thumbnail-events + "Shows each component thumbnail the worker renders for + `render-thumbnail`." + [] + (->> (mw/ask-many! {:cmd :thumbnails/watch-components}) + (rx/map (fn [{:keys [object-id blob]}] + (dwt/assoc-thumbnail object-id (wapi/create-uri blob)))))) diff --git a/frontend/src/app/worker.cljs b/frontend/src/app/worker.cljs index 5df69ac365..be8c80a3c4 100644 --- a/frontend/src/app/worker.cljs +++ b/frontend/src/app/worker.cljs @@ -11,6 +11,7 @@ [app.common.schema :as sm] [app.common.types.objects-map] [app.util.object :as obj] + [app.worker.component-thumbnails] [app.worker.exports] [app.worker.impl :as impl] [app.worker.index] diff --git a/frontend/src/app/worker/component_thumbnails.cljs b/frontend/src/app/worker/component_thumbnails.cljs new file mode 100644 index 0000000000..70c918a6b5 --- /dev/null +++ b/frontend/src/app/worker/component_thumbnails.cljs @@ -0,0 +1,146 @@ +;; 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 app.worker.component-thumbnails + "Renders component thumbnails on the worker WASM module from the shapes + of their mains, which the main thread sends. Sends each render back to + be shown, and uploads the ones asked for." + (:require + [app.common.data.macros :as dm] + [app.common.logging :as log] + [app.common.math :as mth] + [app.common.render-wasm.wasm :as wasm] + [app.common.thumbnails :as thc] + [app.common.uri :as u] + [app.config :as cf] + [app.render-wasm.api :as wasm.api] + [app.util.http :as http] + [app.util.webapi :as wapi] + [app.worker.impl :as impl] + [app.worker.wasm :as wwasm] + [beicon.v2.core :as rx])) + +;; Bounds for a thumbnail's longest side, in pixels. Full-size renders of big +;; components hold the worker for seconds, delaying its other work. +(def ^:private ^:const min-target-size 200) +(def ^:private ^:const max-target-size 512) + +(def ^:private ^:const canvas-size 100) + +(defn- thumbnail-scale + [longest-side] + (if (pos? longest-side) + (-> 1 + (mth/max (/ min-target-size longest-side)) + (mth/min (/ max-target-size longest-side))) + 1)) + +(defn- render-component-root + [objects root-id] + (let [{ext-w :width ext-h :height} (wasm.api/get-shape-extrect root-id) + {sel-w :width sel-h :height} (dm/get-in objects [root-id :selrect]) + longest-side (mth/max (or ext-w sel-w) (or ext-h sel-h)) + scale (thumbnail-scale longest-side) + bytes (wasm.api/render-shape-pixels root-id scale :png)] + (when (and (some? bytes) (pos? (.-length ^js bytes))) + (wapi/create-blob bytes "image/png")))) + +(defn- render-batch + "Renders each of `requests` from the shapes of its main, in `:objects`, + to a PNG blob in one job on the worker WASM module. Emits each request + that rendered, with its `:blob`. Never fails: an error would end the + stream of requests, and with it every later render, so it is logged and + the batch ends." + [requests] + (let [objects (into {} (mapcat :objects) requests)] + (->> (rx/from @wwasm/module) + (rx/mapcat + (fn [_] + (rx/create + (fn [subs] + (let [canvas (js/OffscreenCanvas. canvas-size canvas-size) + vbox {:x 0 :y 0 :width canvas-size :height canvas-size}] + (if (wasm.api/init-canvas-context canvas) + (try + (reset! wasm/disable-request-render? true) + ;; `:on-render` runs once the images and fonts are in. + (wasm.api/initialize-viewport + objects 1 vbox + :force-sync true + :on-render + (fn [] + (try + (doseq [{:keys [root-id] :as request} requests] + (when-let [blob (render-component-root objects root-id)] + (rx/push! subs (assoc request :blob blob)))) + (rx/end! subs) + (catch :default cause + (rx/error! subs cause)) + (finally + (wasm.api/clear-canvas))))) + (catch :default cause + (wasm.api/clear-canvas) + (rx/error! subs cause))) + (rx/error! subs (ex-info "unable to init wasm canvas" {:roots (count requests)}))) + nil))))) + (rx/catch (fn [cause] + (log/error :hint "error rendering component thumbnails" + :roots (count requests) + :cause cause) + (rx/empty)))))) + +(defn- upload-thumbnail + [file-id object-id blob] + (let [params {:file-id file-id :object-id object-id :tag "component"}] + (->> (http/send! {:method :post + :uri (u/join cf/public-uri "api/main/methods/create-file-object-thumbnail") + :credentials "include" + :query params + :body (http/form-data (assoc params :media blob))}) + (rx/subs! (fn [response] + (when-not (http/success? response) + (log/wrn :hint "unable to upload component thumbnail" + :object-id object-id :status (:status response)))) + (fn [cause] + (log/wrn :hint "unable to upload component thumbnail" + :object-id object-id :cause cause)))))) + +;; The stream the main thread reads the renders from; a new +;; `:thumbnails/watch-components` ends the previous one. +(defonce ^:private output* (atom nil)) + +(defn- add-render + "Sends `render` to the main thread to be shown, and uploads it when it + asks for it." + [{:keys [file-id page-id root-id blob persist?]}] + (let [object-id (thc/fmt-object-id file-id page-id root-id "component")] + (some-> @output* (rx/push! {:object-id object-id :blob blob})) + (when persist? + (upload-thumbnail file-id object-id blob)))) + +;; The requests from the main thread, rendered in batches, one at a time. +;; A batch takes the requests up to a 300 ms pause. +(defonce ^:private requests + (let [subject (rx/subject)] + (->> subject + (rx/buffer-until (rx/debounce 300 subject)) + ;; TODO: keep one request per main, so a main asked for twice in a + ;; batch renders and uploads once. + (rx/mapcat #(wwasm/submit (partial render-batch %))) + (rx/subs! add-render)) + subject)) + +(defmethod impl/handler :thumbnails/render-component + [message _] + (rx/push! requests (select-keys message [:file-id :page-id :root-id :objects :persist?])) + nil) + +(defmethod impl/handler :thumbnails/watch-components + [_ _] + (let [output (rx/subject)] + (some-> @output* rx/end!) + (reset! output* output) + output)) diff --git a/frontend/test/frontend_tests/data/workspace_thumbnails_wasm_test.cljs b/frontend/test/frontend_tests/data/workspace_thumbnails_wasm_test.cljs new file mode 100644 index 0000000000..ca29015e73 --- /dev/null +++ b/frontend/test/frontend_tests/data/workspace_thumbnails_wasm_test.cljs @@ -0,0 +1,74 @@ +;; 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.data.workspace-thumbnails-wasm-test + "The main thread side of the component thumbnails the worker renders + and uploads: what it sends the worker, and how it shows the renders + that come back." + (:require + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] + [app.main.data.workspace.thumbnails :as dwt] + [app.main.data.workspace.thumbnails-wasm :as dwt.wasm] + [app.main.worker :as mw] + [app.util.webapi :as wapi] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock] + [frontend-tests.helpers.state :as ths] + [potok.v2.core :as ptk])) + +(t/deftest ^:async the-worker-gets-the-shapes-of-the-main + (cthi/reset-idmap!) + (let [file (-> (cthf/sample-file :file) + (ctho/add-frame-with-child :root :child) + (ctho/add-frame :other)) + state @(ths/setup-store file) + page-id (cthf/current-page-id file) + sent (atom [])] + (await + (mock/with-mocks* + {mw/emit! (mock/stub (fn [message] (swap! sent conj message) (rx/empty)))} + (ptk/effect (dwt.wasm/render-thumbnail (:id file) page-id (cthi/id :root) :persist? true) + state nil))) + (t/is (= [{:cmd :thumbnails/render-component + :file-id (:id file) + :page-id page-id + :root-id (cthi/id :root) + :persist? true}] + (mapv #(dissoc % :objects) @sent))) + (t/is (= #{(cthi/id :root) (cthi/id :child)} + (set (keys (:objects (first @sent)))))))) + +(t/deftest ^:async a-missing-main-sends-nothing + (cthi/reset-idmap!) + (let [file (cthf/sample-file :file) + state @(ths/setup-store file) + sent (atom [])] + (await + (mock/with-mocks* + {mw/emit! (mock/stub (fn [message] (swap! sent conj message) (rx/empty)))} + (ptk/effect (dwt.wasm/render-thumbnail (:id file) (cthf/current-page-id file) nil) + state nil))) + (t/is (empty? @sent)))) + +(t/deftest ^:async worker-renders-are-shown + (let [renders (rx/subject) + events (atom [])] + (await + (mock/with-mocks* + {mw/ask-many! (mock/stub (fn [_] (rx/observe-on :async renders))) + wapi/create-uri (mock/stub (fn [_] "blob:test"))} + (let [done (async/observe (dwt.wasm/component-thumbnail-events) + :on-next #(swap! events conj %))] + (rx/push! renders {:object-id "a" :blob (wapi/create-blob "a" "image/png")}) + (rx/push! renders {:object-id "b" :blob (wapi/create-blob "b" "image/png")}) + (await (async/wait-for #(= 2 (count @events)) "both renders shown")) + (t/is (every? (ptk/type? ::dwt/assoc-thumbnail) @events)) + (rx/end! renders) + (await done)))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index e5b23f03ce..395cb234b8 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -52,6 +52,7 @@ [frontend-tests.data.workspace-shortcuts-test] [frontend-tests.data.workspace-texts-test] [frontend-tests.data.workspace-thumbnails-test] + [frontend-tests.data.workspace-thumbnails-wasm-test] [frontend-tests.data.workspace-undo-test] [frontend-tests.data.workspace-undo-translation-test] [frontend-tests.data.workspace-variant-rename-test] @@ -196,6 +197,7 @@ [frontend-tests.util.code-gen-markup-svg-wasm-test] [frontend-tests.util.dom.dnd-test] [frontend-tests.worker-snap-test] + [frontend-tests.worker.component-thumbnails-test] [frontend-tests.worker.exports-test] [frontend-tests.worker.index-test] [frontend-tests.worker.messages-test] @@ -241,6 +243,7 @@ 'frontend-tests.data.fonts-test 'frontend-tests.data.imports-test 'frontend-tests.data.jobs-test + 'frontend-tests.worker.component-thumbnails-test 'frontend-tests.worker.exports-test 'frontend-tests.worker.index-test 'frontend-tests.worker.messages-test @@ -265,6 +268,7 @@ 'frontend-tests.data.workspace-shortcuts-test 'frontend-tests.data.workspace-texts-test 'frontend-tests.data.workspace-thumbnails-test + 'frontend-tests.data.workspace-thumbnails-wasm-test 'frontend-tests.data.workspace-undo-test 'frontend-tests.data.workspace-undo-translation-test 'frontend-tests.data.workspace-variant-rename-test diff --git a/frontend/test/frontend_tests/worker/component_thumbnails_test.cljs b/frontend/test/frontend_tests/worker/component_thumbnails_test.cljs new file mode 100644 index 0000000000..a721275427 --- /dev/null +++ b/frontend/test/frontend_tests/worker/component_thumbnails_test.cljs @@ -0,0 +1,95 @@ +;; 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.worker.component-thumbnails-test + "The worker batches the component thumbnails the main thread asks for, + renders the batches one at a time, sends each render back and uploads + the ones asked for." + (:require + [app.common.thumbnails :as thc] + [app.common.uuid :as uuid] + [app.util.http :as http] + [app.util.webapi :as wapi] + [app.worker.component-thumbnails :as wct] + [app.worker.impl :as impl] + [app.worker.wasm :as wwasm] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock])) + +(def ^:private file-id (uuid/next)) +(def ^:private page-id (uuid/next)) + +(defn- request-render + [root-id] + (impl/handler {:cmd :thumbnails/render-component + :file-id file-id + :page-id page-id + :root-id root-id + :objects {}} + nil)) + +(defn- sleep + [ms] + (js/Promise. (fn [resolve] (js/setTimeout resolve ms)))) + +(t/deftest ^:async a-burst-of-requests-renders-as-one-batch + (let [jobs (atom 0)] + (await + (mock/with-mocks* + {wwasm/submit (mock/stub (fn [_] (swap! jobs inc) (rx/empty)))} + (run! request-render (repeatedly 30 uuid/next)) + (await (async/wait-for #(pos? @jobs) "the batch")) + ;; Longer than the pause that would start a second batch + (await (sleep 400)))) + (t/is (= 1 @jobs)))) + +(defn- ^:async render-with-module + "Renders a batch of one main with the WASM module mocked as `module`. + Returns what came out and whether it ended." + [module] + (let [out (atom {:values [] :ended? false :error nil})] + (await + (mock/with-mocks* + {wwasm/module module} + (await (async/observe (#'wct/render-batch [{:root-id (uuid/next) :objects {}}]) + :on-next #(swap! out update :values conj %) + :on-error #(swap! out assoc :error %) + :on-complete #(swap! out assoc :ended? true))))) + @out)) + +(t/deftest ^:async a-failed-render-ends-its-batch-quietly + ;; A failed module load, and a failed job: the tests have no WASM render + (doseq [module [(delay (js/Promise.reject (ex-info "no module" {}))) + (delay (js/Promise.resolve nil))]] + (t/is (= {:values [] :ended? true :error nil} + (await (render-with-module module)))))) + +(t/deftest ^:async a-render-is-shown-and-uploaded-when-asked + (let [output (rx/subject) + shown (atom []) + requests (atom []) + file-id (uuid/next) + page-id (uuid/next) + edited (uuid/next) + undone (uuid/next) + blob (wapi/create-blob "png" "image/png")] + (rx/sub! output #(swap! shown conj %)) + (reset! @#'wct/output* output) + (await + (mock/with-mocks* + {http/send! (mock/stub (fn [request] (swap! requests conj request) (rx/of {:status 200})))} + (#'wct/add-render {:file-id file-id :page-id page-id :root-id edited :blob blob :persist? true}) + (#'wct/add-render {:file-id file-id :page-id page-id :root-id undone :blob blob :persist? false}))) + (reset! @#'wct/output* nil) + (t/is (= [{:object-id (thc/fmt-object-id file-id page-id edited "component") :blob blob} + {:object-id (thc/fmt-object-id file-id page-id undone "component") :blob blob}] + @shown)) + (t/is (= [{:file-id file-id + :object-id (thc/fmt-object-id file-id page-id edited "component") + :tag "component"}] + (mapv :query @requests)))))