penpot/frontend/test/frontend_tests/logic/comp_watcher_test.cljs
alonso.torres 3e6c98c01a ✨ Generate and check random component histories
The Phase 4 bug hunt: random component programs, an independent model
of what they should leave, and the regression programs of what they
found. The production fixes they found are the commits before this one,
each with its regression programs; this commit holds the test harness,
the rest of the tests and the memories they grew with. A regression
program that needs the harness runs once this commit is in.

- Generation (composable gen): worlds of components, nested copies,
  variants, libraries (stale and dependent), flex and grid layouts and
  texts; operations for structure, geometry, styles, texts, layout items,
  grid tracks and cells, swaps, variant switches, update main, reset,
  detach, library edits and syncs, undo and redo; shrinking, coverage
  accounting per operation kind, and PENPOT_GEN_SEED, PENPOT_GEN_NUM
  and PENPOT_GEN_HISTORIES to choose the seed, the number of programs
  and their length.
- The oracle: invariants on touched groups, overrides, refs, settled
  versus full sync, design snapshots and the model values, with its
  own model of authored intent (shadow, geometry, grid and variant
  rules) instead of production's results.
- The frontend replays generated programs through the workspace events
  and the completion contract, with undo and redo checks.
- The oracle unit tests, the test runners' registrations, the golden
  masters recorded again where a fix changed them, and the memories of
  the component sync contract and its tests.

Harness only; the benchmark is not run.

Also in this commit:

- Adapt develop's variant set and reload tests to this branch. Develop's tests
  of transforming a component into a variant set call the event with the set's
  id and expect the defining commit to hold only the main. Here the transform
  builds the whole set, its second member included, in one commit:
  transform-in-variant takes an optional variant id, and the tests expect both
  members in that commit.
- Restore the requires and test wiring the rebase merges dropped.
- Generate variant property edits and added variants. The random programs
  switched between the members of a variant set but never changed the set.

AI-assisted-by: claude-opus-5-5
2026-11-18 10:29:46 +01:00

136 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.refs :as refs]
[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]
[okulary.core :as l]
[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))))))