penpot/common/test/common_tests/logic/comp_flex_interactions_test.cljc
alonso.torres 98ae356fac ♻️ Simplify the component sync walk and its changes
- The sync walk took its direction as a map of seven closures
  (direct-sync, inverse-sync). The two directions are fixed, so each
  is now a plain recursive function: sync-copy-recursive (main to
  copy, a sync or a reset) and update-main-recursive (copy to main),
  with sync-copy-shape and update-main-shape for the pair itself.
  Both share compare-children, which takes the children, a pair?
  predicate and four callbacks instead of 13 arguments; the inverse
  branch's call of the swapped callback, which could never run, goes.
- The container of the main shapes is found once, at the start of the
  walk. The walk holds the :remote-synced a reset or an update main
  gives the copy shapes, instead of initial-root? and two branches.
  add-shape-to-instance and add-shape-to-main take the parent from the
  pair being walked instead of searching the whole subtree for it.
- pcb/add-changes takes a container and the redo and undo changes, and
  applies them locally. Eight sync functions and the library
  typography sync each wrote that block; make-change goes from both
  namespaces. One list holds the attributes that link a copy shape
  with its main, and one shape-label prints shapes in the log lines.
- change-touched chose what to write from three flags. It is now
  set-touched, which takes the value each caller computes. The swap's
  restore of the kept properties passes ignore-touched instead of
  calling it afterwards to put the groups back.
- generate-sync-shape was a multimethod over the three asset types; it
  is a case in generate-sync-container. generate-sync-file loses its
  unused arity. The 100-line comment that opened the sync helpers
  described an older model; a short note points at the component sync
  contract. text-content-with-overrides only called
  txt/merge-overrides, which sync and switch now call.
- update-flex-child-copy-attrs and update-flex-child-main-attrs are one
  function, update-flex-children, and the omit-touched? argument that
  was always false goes.
- In the variant switch, sized-geometry-value builds the resized
  :selrect and :points that two geometry policies each built, and a
  binding of skip? shadowed by a second one is skip-attr?.

add-shape-to-main keeps two changes that look wrong, now marked F102:
its :reg-objects names the component, so it only reaches a deleted
component's objects, and the undo of its new shapes deletes them from
the copy's page.

No behavior change: the golden masters and the suites are unchanged.
libraries.cljc: 1816 -> 1284 lines.

Benchmark (lower quantile, ms; baseline / now / ratio):
  deepest-edit-through-three-levels    35.2 /  15.2 / 0.43
  edit-through-a-chain-of-dependents   68.9 /  35.7 / 0.52
  library-update-into-copies           83.1 /  40.5 / 0.49
  main-edit-among-many-shapes           4.8 /   2.4 / 0.51
  main-edit-into-10-copies              1.8 /   0.8 / 0.46
  main-edit-into-100-copies            15.4 /   7.8 / 0.51
  main-edit-into-1000-copies          153.1 /  97.4 / 0.64
  variant-switch-into-copies           32.1 /  17.6 / 0.55
(The machine was busy for the 1000-copies world: its mean was 119 ms,
against about 83 in the runs of the earlier steps, at 0.49-0.52.)

AI-assisted-by: claude-opus-5-5

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-15 00:35:54 +02:00

155 lines
6.5 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 common-tests.logic.comp-flex-interactions-test
(:require
[app.common.files.changes-builder :as pcb]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.logic.libraries :as cll]
[app.common.logic.shapes :as cls]
[app.common.math :as mth]
[app.common.test-helpers.components :as thc]
[app.common.test-helpers.compositions :as tho]
[app.common.test-helpers.files :as thf]
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[app.common.types.container :as ctn]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
(defn- setup-file
"Flex component and copy whose children navigate to different boards."
[]
(-> (thf/sample-file :file1)
(tho/add-simple-component-with-copy :component1
:main-root
:main-child
:copy-root
:main-root-params {:layout :flex
:layout-flex-dir :row}
:copy-root-params {:children-labels [:copy-child]})
(ths/add-sample-shape :main-popup :type :frame)
(ths/add-sample-shape :copy-popup :type :frame)
(ths/add-interaction :main-child :main-popup)
(ths/add-interaction :copy-child :copy-popup)))
(defn- update-shape
[file label update-fn]
(let [page (thf/current-page file)]
(thf/apply-changes
file
(cls/generate-update-shapes (pcb/empty-changes nil (:id page))
#{(thi/id label)}
update-fn
(:objects page)
{}))))
(defn- destinations
[shape]
(mapv :destination (:interactions shape)))
(t/deftest test-sync-flex-main-keeps-copy-interactions
(let [;; ==== Setup
file (setup-file)
margin {:m1 5 :m2 5 :m3 5 :m4 5}
;; ==== Action
updated-file (update-shape file :main-child
#(assoc % :hidden true :layout-item-margin margin))
changes (cll/generate-sync-file-changes (pcb/empty-changes)
nil
:components
(:id updated-file)
(thi/id :component1)
(:id updated-file)
{(:id updated-file) updated-file}
(:id updated-file))
file' (thf/apply-changes updated-file changes)
;; ==== Get
copy-child' (ths/get-shape file' :copy-child)]
;; ==== Check
(t/is (true? (:hidden copy-child')))
(t/is (= margin (:layout-item-margin copy-child')))
(t/is (= [(thi/id :copy-popup)] (destinations copy-child')))))
(t/deftest test-sync-flex-copy-to-main-keeps-main-interactions
(let [;; ==== Setup
file (setup-file)
;; ==== Action
updated-file (update-shape file :copy-child #(assoc % :hidden true))
page (thf/current-page updated-file)
container (ctn/make-container page :page)
changes (-> (pcb/empty-changes)
(pcb/with-container container)
(cll/generate-sync-shape-inverse {(:id updated-file) updated-file}
container
(thi/id :copy-root)))
file' (thf/apply-changes updated-file changes)
;; ==== Get
main-child' (ths/get-shape file' :main-child)]
;; ==== Check
(t/is (true? (:hidden main-child')))
(t/is (= [(thi/id :main-popup)] (destinations main-child')))))
(defn- setup-library-and-file
"Library flex component with a nested copy, and a file instance whose
nested copy has an interaction."
[]
(let [library (-> (thf/sample-file :library)
(tho/add-nested-component :component1
:main1-root
:main1-child
:component2
:main2-root
:nested-head
:main2-root-params {:layout :flex
:layout-flex-dir :row}))
file (-> (thf/sample-file :file)
(ths/add-sample-shape :copy-popup :type :frame)
(thc/instantiate-component :component2
:copy2-root
:library library
:children-labels [:copy-nested-head])
(ths/add-interaction :copy-nested-head :copy-popup))]
[library file]))
(defn- sync-file-from-library
[file library]
(cll/generate-sync-file-changes (pcb/empty-changes)
nil
nil
(:id file)
nil
(:id library)
{(:id library) library
(:id file) file}
(:id file)))
(t/deftest test-sync-library-flex-keeps-nested-copy-interactions
(let [;; ==== Setup
[library file] (setup-library-and-file)
copy-x (:x (tho/bottom-shape file :copy2-root))
;; ==== Action
library' (-> library
(update-shape :main1-child #(gsh/move % (gpt/point 1 0)))
(tho/propagate-component-changes :component1))
file' (thf/apply-changes file (sync-file-from-library file library'))
;; ==== Get
copy-nested-head' (ths/get-shape file' :copy-nested-head)]
;; ==== Check
(t/is (mth/close? (inc copy-x) (:x (tho/bottom-shape file' :copy2-root))))
(t/is (= [(thi/id :copy-popup)] (destinations copy-nested-head')))))