penpot/common/test/common_tests/logic/comp_expected_geometry_test.cljc
alonso.torres fc8dff9fe1 ✨ Keep the children of a transformed copy shape derived
Decision (f) of the component contract, with a fix it needed and a check of
decision (g) (Phase 3c).

- Return empty modifiers for a scale constraint axis.

  The constraint of each axis of a child gives the modifiers that the
  parent's resize adds to the child; :scale has no method of its own and
  took the default, which returned an empty vector. Adding a vector to
  modifiers reads its :last-order as nil: JavaScript adds nil as 0, the JVM
  throws, so a child scaled on one axis and constrained on the other (scale
  and top, say) could not be resized on the JVM. The default now returns
  empty modifiers.

  geom-shapes-constraints-test checks the default is empty and adds to
  modifiers.

- Keep the children of a transformed copy shape derived.

  Decision (f) of the component contract: transforming a copy shape touches
  that shape only; its children change as a consequence and stay untouched,
  and sync gives them their expected geometry, their main's placed against
  their parent and resized through their constraints.

  Touch. The placement step decided per shape whether a transform touched it
  by comparing it with its copy root, so the children of a resized root that
  stretched (scale, left and right) were touched (F10), a derived width or
  height always touched (F22), and a resize that scales the content touched
  the strokes, radii and the like of every child (F39). The editor now tells
  the placement step which shapes the user transformed (the keys of the
  modifiers given, or those the stored modifiers were set for); any other
  shape of the transform changes only as a consequence of theirs (a child
  through its constraints, a board that hugs them) and touches nothing.

  Sync. Sync placed every main shape by the offset between the roots only,
  so the children of a resized copy root that a resize leaves untouched
  (center) went back to their main's place at the next sync of the main
  (F10). Now, before it walks the children of a copy shape that keeps its
  own geometry, sync resizes, turns and moves the main shape's subtree to
  match it, its children following through their constraints and layouts,
  and walks those: each child gets that geometry unless it has its own.

  comp-expected-geometry-test: a copy root resized as the editor does places
  a child constrained left, right, center, scale and left-right, leaves it
  untouched and keeps it there through a sync; a resize that scales the
  content scales the child's stroke and touches only the root. The frontend
  golden master records, in cases AI and the touched child under a main
  rotation, the children the resize or rotation of a root no longer touches.

- Check a copy child resize leaves its hug board untouched.

  Decision (g) of the component contract: the size of a board that hugs its
  content is the layout's, never an override. The layout reflow writes it as
  derived, and a transform touches only the shapes the user transformed
  (decision (f)), so a resize of a copy child that grows the board it sits
  in touches the child only.

  workspace-layout-owned-sizes-test pins it.

Benchmark (lower quantile, ms; baseline / now / ratio); the machine
was loaded, and the code before this change, run in between, gave the
same spread (0.44 to 0.59):
  deepest-edit-through-three-levels    35.2 /  16.7 / 0.47
  edit-through-a-chain-of-dependents   68.9 /  33.4 / 0.48
  library-update-into-copies           83.1 /  40.3 / 0.49
  main-edit-among-many-shapes           4.8 /   2.4 / 0.49
  main-edit-into-10-copies              1.8 /   1.0 / 0.54
  main-edit-into-100-copies            15.4 /   8.8 / 0.57
  main-edit-into-1000-copies          153.1 /  92.7 / 0.61
  variant-switch-into-copies           32.1 /  19.6 / 0.61

AI-assisted-by: claude-opus-5-5
2026-11-10 20:21:16 +01:00

115 lines
6.1 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-expected-geometry-test
"A copy whose root (or another shape) keeps a geometry of its own, and the
children of that shape: a transform of the shape leaves them untouched,
and sync gives them their main's geometry placed against it, resized
through their constraints (decision (f) of the component contract)."
(:require
[app.common.files.changes-builder :as pcb]
[app.common.geom.modifiers :as gm]
[app.common.logic.libraries :as cll]
[app.common.logic.placement :as clp]
[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.modifiers :as ctm]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
(defn- component-with-child
"A 100x100 main board with a 20x20 rect at (70, 10) of it, constrained
`constraints-h`, and a copy of it at (300, 0)."
[constraints-h]
(-> (thf/sample-file :file1)
(tho/add-frame :main-root {:x 0 :y 0 :width 100 :height 100})
(tho/add-rect :main-child {:parent-label :main-root :x 70 :y 10 :width 20 :height 20
:constraints-h constraints-h :constraints-v :top})
(thc/make-component :component1 :main-root)
(thc/instantiate-component :component1 :copy-root :children-labels [:copy-child])))
(defn- resize
"Resize the shape `label` to `width` x `height`, as the editor does: the
modifiers propagate to its children through their constraints, and the
placement step decides what the transform touches. With `scale-content`,
strokes, radii and the like scale by it too."
[file label width height & {:keys [scale-content]}]
(let [page (thf/current-page file)
objects (:objects page)
shape (ths/get-shape file label)
modifiers (cond-> (ctm/change-size shape width height)
(some? scale-content) (ctm/scale-content scale-content))
modif-tree (gm/set-objects-modifiers {(:id shape) {:modifiers modifiers}}
objects)
ids (keys modif-tree)
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
ids
(clp/modifiers-transform modif-tree)
objects
{:ignore-tree (clp/calculate-ignore-tree objects ids
(clp/modifiers-transform modif-tree)
#{(:id shape)})
:attrs clp/transform-attrs})]
(thf/apply-changes file changes)))
(defn- edit-and-sync
"Change the fill of the main child and sync the copy."
[file]
(let [page (thf/current-page file)
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
#{(thi/id :main-child)}
#(assoc % :fills (ths/sample-fills-color :fill-color "#ff0000"))
(:objects page)
{})
file (thf/apply-changes file changes)]
(thf/apply-changes file (cll/generate-sync-file-changes (pcb/empty-changes) nil :components (:id file)
(thi/id :component1) (:id file)
{(:id file) file} (:id file)))))
(defn- child-box
"The position of the copy child relative to its root, and its size."
[file]
(let [root (ths/get-shape file :copy-root)
child (ths/get-shape file :copy-child)]
(mapv #(mth/round (double %))
[(- (-> child :selrect :x) (-> root :selrect :x))
(- (-> child :selrect :y) (-> root :selrect :y))
(-> child :selrect :width)
(-> child :selrect :height)])))
(t/deftest a-resized-copy-root-keeps-its-children-placed-through-their-constraints
(doseq [[constraints-h box] [[:left [70 10 20 20]]
[:right [170 10 20 20]]
[:center [120 10 20 20]]
[:scale [140 10 40 20]]
[:leftright [70 10 120 20]]]]
(t/testing (name constraints-h)
(let [resized (resize (component-with-child constraints-h) :copy-root 200 100)
synced (edit-and-sync resized)]
(t/is (= box (child-box resized)) "the resize places the child")
(t/is (contains? (:touched (ths/get-shape resized :copy-root)) :geometry-group))
(t/is (nil? (:touched (ths/get-shape resized :copy-child))) "the child is not touched")
(t/is (= box (child-box synced)) "the sync keeps it there")
(t/is (= "#ff0000" (-> (ths/get-shape synced :copy-child) :fills first :fill-color)))))))
(t/deftest a-copy-root-resized-with-its-content-touches-only-the-root
(let [file (-> (component-with-child :scale)
(ths/update-shape :main-child :strokes [{:stroke-color "#000000" :stroke-opacity 1
:stroke-width 2 :stroke-alignment :center
:stroke-style :solid}])
(thc/instantiate-component :component1 :copy2-root :children-labels [:copy2-child]))
resized (resize file :copy2-root 200 200 :scale-content 2)
child (ths/get-shape resized :copy2-child)]
(t/is (mth/close? 4 (-> child :strokes first :stroke-width)))
(t/is (nil? (:touched child)))))