penpot/frontend/test/frontend_tests/logic/comp_watcher_test.cljs
alonso.torres 4719ababe8 📎 Restore merged imports and multipart test setup
Keep the string helper required by the variant rename predicate after
removing the old swap-slot helpers. Sort the merged test imports.

Send import-audit fixtures through the existing multipart RPC helper so
they exercise auditing through the required upload path. Start the edit
watcher in the isolated component test so it forwards recorded transitions.

AI-assisted-by: GPT-6
2026-11-10 20:21:42 +01:00

134 lines
5.4 KiB
Clojure

;; 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.logic.comp-watcher-test
"Automatic propagation of main edits to copies, driven by the real
`dwl/watch-component-changes` on an isolated store."
(:require
[app.common.test-helpers.compositions :as ctho]
[app.common.test-helpers.files :as cthf]
[app.common.test-helpers.ids-map :as cthi]
[app.common.test-helpers.shapes :as cths]
[app.main.data.changes :as dch]
[app.main.data.helpers :as dsh]
[app.main.data.workspace :as dw]
[app.main.data.workspace.libraries :as dwl]
[app.main.data.workspace.pages :as dwpg]
[app.main.data.workspace.shapes :as dwsh]
[app.main.data.workspace.thumbnails-wasm :as dwt.wasm]
[app.main.store :as st]
[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.pages :as thp]
[frontend-tests.helpers.state :as ths]
[frontend-tests.helpers.wasm :as thw]
[potok.v2.core :as ptk]))
(t/use-fixtures :each
{:before (fn []
(thp/reset-idmap!)
(thw/setup-wasm-mocks!))
:after thw/teardown-wasm-mocks!})
(defn- setup-file
[]
;; {:main-root} [:name Frame1] # [Component :component1]
;; :main-child [:name Rect1]
;;
;; :copy-root [:name Frame1] #--> [Component :component1] :main-root
;; :copy-child [:name Rect1] ---> :main-child
;;
;; :outside [:name Frame1]
(-> (cthf/sample-file :file1)
(ctho/add-simple-component-with-copy :component1
:main-root
:main-child
:copy-root
:copy-root-params {:children-labels [:copy-child]})
(ctho/add-frame :outside)))
(defn- noop-render-thumbnail
[& _]
(ptk/reify ::noop-render-thumbnail
ptk/WatchEvent
(watch [_ _ _] (rx/empty))))
(defn- copy-children
[store]
(let [file (ths/get-file-from-state @store)]
(:shapes (cths/get-shape file :copy-root))))
(t/deftest ^:async child-moved-out-of-main-leaves-copy-when-another-edit-follows
;; The move and fill edit commit in the same tick. The move must use
;; its prior data, where the rect still sits in the main. The edit
;; watcher records undo before forwarding each transition to sync.
(let [file (setup-file)
store (ths/setup-store file)]
(await
(mock/with-mocks*
{;; Thumbnail renders read the global store.
st/state store
dwt.wasm/render-thumbnail noop-render-thumbnail}
(ptk/emit! store
(dw/initialize-edit-watchers)
(dwl/watch-component-changes))
(t/is (= 1 (count (copy-children store)))
"the copy starts with its rect")
(ptk/emit! store
(dwsh/relocate-shapes #{(cthi/id :main-child)} (cthi/id :outside) 0)
(dwsh/update-shapes [(cthi/id :outside)]
#(assoc % :fills [{:fill-color "#ff0000"
:fill-opacity 1}])))
(await (async/wait-for #(empty? (copy-children store))
"the copy to lose its rect"))
(let [file' (ths/get-file-from-state @store)]
(t/is (= (cthi/id :outside)
(:parent-id (cths/get-shape file' :main-child)))
"the rect moved out of the main")
(t/is (empty? (copy-children store))
"the copy lost its rect"))
(ptk/emit! store
(dw/finalize-edit-watchers)
(dwpg/finalize-page (:id file) (cthf/current-page-id file)))
(await (async/settle))))))
;; Scenario: a rollback reverts the opacity of the main's child. A rollback
;; is not a commit, so the change watch sees no component change.
;; Proves: the component's thumbnail is rendered again and uploaded, so
;; neither the tab nor the server keeps a thumbnail of the rejected data.
(t/deftest ^:async a-rollback-of-a-main-renders-and-uploads-its-thumbnail
(let [file (setup-file)
store (ths/setup-store file)
page-id (cthf/current-page-id file)
renders (atom [])]
(await
(mock/with-mocks*
{refs/workspace-data (l/derived dsh/lookup-file-data store)
st/state store
dwt.wasm/render-thumbnail (fn [_ _ frame-id & {:as opts}]
(swap! renders conj [frame-id opts])
(noop-render-thumbnail))}
(ptk/emit! store (dwl/watch-component-changes))
(ptk/emit! store (dch/rebase-commits
(:id file)
{:undo []
:remote [{:type :mod-obj :id (cthi/id :main-child) :page-id page-id
:operations [{:type :set :attr :opacity :val 0.5}]}]
:redo []}))
(await (async/wait-for #(seq @renders) "the thumbnail to render"))
(t/is (= [[(cthi/id :main-root) {:persist? true}]] @renders))
(ptk/emit! store (dwpg/finalize-page (:id file) page-id))
(await (async/settle))))))