penpot/common/test/common_tests/logic/move_shapes_test.cljc
alonso.torres 1ca23bcbda 🐛 Restore existing component nesting when undoing a move
Sync and swapped children can leave finite repeated component nesting.
Moving a copy out succeeds, but undo rejects its old position under the
normal component-loop guard. Record restore-component-nesting only on
inverses whose original position already repeats a component.

Normal forward moves retain that guard. The applier still rejects physical
object-tree cycles and changes to copy structure. The new optional change
field defaults to the current behavior and changes no stored shape data.

Portable regression: exact undo fails before the fix. Five assertions now
pass, including redo, normal loop refusal and object-cycle refusal using
the generated inverse. Focused JVM and golden checks: 195 tests, 2189
assertions. Both original shrunk programs pass. Live WASM undo/redo restores
complete snapshots. Common/frontend lint and formatting pass.

Component benchmark ratios: mains 0.57/0.62/0.67; levels 0.80;
chain 0.82; library 0.56; many shapes 0.55; variant switch 0.91.
All worlds pass the unchanged baseline gate.

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

316 lines
17 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.move-shapes-test
(:require
[app.common.files.changes-builder :as pcb]
[app.common.logic.shapes :as cls]
[app.common.test-helpers.components :as thc]
[app.common.test-helpers.composable.comp.nodes :as n]
[app.common.test-helpers.composable.comp.runner :as r]
[app.common.test-helpers.composable.core :as tm]
[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.normalize :as thn]
[app.common.test-helpers.shapes :as ths]
[app.common.test-helpers.tokens :as tht]
[app.common.types.tokens-lib :as ctob]
[app.common.types.tokens-status :as ctos]
[app.common.uuid :as uuid]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
(t/deftest moving-out-of-existing-component-nesting-undoes-exactly
(let [file (-> (thf/sample-file :file)
(tho/add-simple-component :a :main-a :rect-a)
(tho/add-simple-component :other :main-other :rect-other)
(tho/add-frame :main-b)
(thc/instantiate-component :a :nested-a :parent-label :main-b)
(thc/make-component :b :main-b)
(tho/add-frame :main-c)
(thc/instantiate-component :b :nested-b :parent-label :main-c)
(thc/make-component :c :main-c)
(thc/instantiate-component :c :copy-c :children-labels [:copy-b :copy-a])
(tho/add-simple-component :destination-component :destination :destination-child))
nested (-> (tm/make-situation file)
(r/step (n/swap-shape :copy-a :other)) r/settle
(r/step (n/nest-copy :copy-c :main-a)) r/settle)
moved (-> nested (r/step (n/nest-copy :nested-a :destination)) r/settle)
undone (r/step moved (n/undo))
page (thf/current-page (tm/file moved))
forward (cls/generate-relocate (-> (pcb/empty-changes nil (:id page))
(pcb/with-objects (:objects page))
(pcb/with-library-data (:data (tm/file moved))))
(thi/id :main-b) 0 #{(thi/id :nested-a)})
refused (thf/apply-changes (tm/file moved) forward)
old-page (thf/current-page (tm/file nested))
move-changes (-> (pcb/empty-changes nil (:id old-page))
(pcb/with-objects (:objects old-page))
(pcb/change-parent (thi/id :destination) [(tm/resolve-shape nested :nested-a)]))
inverse (first (filter #(= :mov-objects (:type %)) (:undo-changes move-changes)))
cyclic (assoc inverse :parent-id (first (:shapes (tm/resolve-shape moved :nested-a))))
cycle-refused (thf/apply-changes (tm/file moved) {:redo-changes [cyclic]})]
(t/is (= (thi/id :destination) (:parent-id (tm/resolve-shape moved :nested-a))))
(t/is (= (thi/id :destination) (:parent-id (ths/get-shape refused :nested-a))))
(t/is (= (thn/normalize-file (tm/file moved)) (thn/normalize-file cycle-refused)))
(t/is (= (thn/normalize-file (tm/file nested)) (thn/normalize-file (tm/file undone))))
(t/is (= (thn/normalize-file (tm/file moved))
(thn/normalize-file (tm/file (r/step undone (n/redo))))))))
(t/deftest moving-a-child-out-of-a-grid-clears-its-old-cell
(let [file (-> (thf/sample-file :file1)
(tho/add-grid-frame :grid :width 200 :height 100)
(tho/add-rect :first :parent-label :grid)
(tho/add-rect :second :parent-label :grid)
(tho/place-in-grid-columns :grid 2 {1 :first 2 :second})
(tho/add-frame :destination))
page (thf/current-page file)
changes (cls/generate-relocate (-> (pcb/empty-changes nil (:id page))
(pcb/with-objects (:objects page))
(pcb/with-library-data (:data file)))
(thi/id :destination) 0 #{(thi/id :first)})
moved (thf/apply-changes file changes)
grid (ths/get-shape moved :grid)
members (mapcat :shapes (vals (:layout-grid-cells grid)))
undone (thf/apply-changes moved {:redo-changes (:undo-changes changes)})]
(t/is (= #{(thi/id :second)} (set members)))
(t/is (= (thn/normalize-file file) (thn/normalize-file undone)))))
(t/deftest deleting-a-grid-child-clears-its-cell-and-keeps-undo
(let [file (-> (thf/sample-file :file1)
(tho/add-grid-frame :grid :width 200 :height 100)
(tho/add-rect :first :parent-label :grid)
(tho/add-rect :second :parent-label :grid)
(tho/place-in-grid-columns :grid 2 {1 :first 2 :second}))
page (thf/current-page file)
[_ changes] (cls/generate-delete-shapes (pcb/empty-changes) (:data file) page
(:objects page) #{(thi/id :first)} {})
removed (thf/apply-changes file changes)
grid (ths/get-shape removed :grid)
members (mapcat :shapes (vals (:layout-grid-cells grid)))
undone (thf/apply-changes removed {:redo-changes (:undo-changes changes)})
redone (thf/apply-changes undone changes)]
(t/is (not-any? #{(thi/id :first)} members))
(t/is (= #{(thi/id :second)} (set members)))
(t/is (= (thn/normalize-file file) (thn/normalize-file undone)))
(t/is (= (thn/normalize-file removed) (thn/normalize-file redone)))))
(t/deftest test-relocate-shape
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(tho/add-frame :frame-to-move)
(tho/add-frame :frame-parent))
page (thf/current-page file)
frame-to-move (ths/get-shape file :frame-to-move)
frame-parent (ths/get-shape file :frame-parent)
;; ==== Action
changes (cls/generate-relocate (-> (pcb/empty-changes nil)
(pcb/with-page-id (:id page))
(pcb/with-objects (:objects page)))
(:id frame-parent) ;; parent-id
0 ;; to-index
#{(:id frame-to-move)}) ;; ids
file' (thf/apply-changes file changes)
;; ==== Get
frame-to-move' (ths/get-shape file' :frame-to-move)
frame-parent' (ths/get-shape file' :frame-parent)]
;; ==== Check
;; frame-to-move has moved
(t/is (= (:parent-id frame-to-move) uuid/zero))
(t/is (= (:parent-id frame-to-move') (:id frame-parent')))))
(t/deftest test-relocate-shape-out-of-group
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(tho/add-frame :frame-1)
(tho/add-group :group-1 :parent-label :frame-1)
(ths/add-sample-shape :circle-1 :parent-label :group-1))
page (thf/current-page file)
circle (ths/get-shape file :circle-1)
group (ths/get-shape file :group-1)
;; ==== Action
changes (cls/generate-relocate (-> (pcb/empty-changes nil)
(pcb/with-page-id (:id page))
(pcb/with-objects (:objects page)))
uuid/zero ;; parent-id
0 ;; to-index
#{(:id circle)}) ;; ids
file' (thf/apply-changes file changes)
;; ==== Get
circle' (ths/get-shape file' :circle-1)
group' (ths/get-shape file' :group-1)]
;; ==== Check
;; the circle has moved, and the group is deleted
(t/is (= (:parent-id circle) (:id group)))
(t/is (= (:parent-id circle') uuid/zero))
(t/is group)
(t/is (nil? group'))))
(t/deftest test-relocate-shape-out-of-layout-manual
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(tho/add-frame :frame-1
:layout :flex ;; TODO: those values come from main.data.workspace.shape_layout/default-layout-params
:layout-flex-dir :row ;; it should be good to use it directly, but first it should be moved to common.logic
:layout-gap-type :multiple
:layout-gap {:row-gap 0 :column-gap 0}
:layout-align-items :start
:layout-justify-content :start
:layout-align-content :stretch
:layout-wrap-type :nowrap
:layout-padding-type :simple
:layout-padding {:p1 0 :p2 0 :p3 0 :p4 0})
(ths/add-sample-shape :circle-1 :parent-label :frame-1
:layout-item-margin {:m1 10 :m2 10 :m3 10 :m4 10}
:layout-item-margin-type :multiple
:layout-item-h-sizing :auto
:layout-item-v-sizing :auto
:layout-item-max-h 1000
:layout-item-min-h 100
:layout-item-max-w 2000
:layout-item-min-w 200
:layout-item-absolute false
:layout-item-z-index 10))
page (thf/current-page file)
circle (ths/get-shape file :circle-1)
;; ==== Action
changes (cls/generate-relocate (-> (pcb/empty-changes nil)
(pcb/with-page-id (:id page))
(pcb/with-objects (:objects page)))
uuid/zero ;; parent-id
0 ;; to-index
#{(:id circle)}) ;; ids
;; ==== Get
file' (thf/apply-changes file changes)
circle' (ths/get-shape file' :circle-1)]
;; ==== Check
;; the layout item attributes are removed
(t/is (nil? (:layout-item-margin circle')))
(t/is (nil? (:layout-item-margin-type circle')))
(t/is (nil? (:layout-item-h-sizing circle')))
(t/is (nil? (:layout-item-v-sizing circle')))
(t/is (nil? (:layout-item-max-h circle')))
(t/is (nil? (:layout-item-min-h circle')))
(t/is (nil? (:layout-item-max-w circle')))
(t/is (nil? (:layout-item-min-w circle')))
(t/is (nil? (:layout-item-absolute circle')))
(t/is (nil? (:layout-item-z-index circle')))))
(t/deftest test-relocate-shape-out-of-layout-with-tokens
(let [;; ==== Setup
file (-> (tht/sample-file-with-tokens
:lib-fn #(-> %
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :test-token-set)
:name "test-token-set"))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :test-theme)
:name "test-theme"
:sets #{"test-token-set"}))
(ctob/add-token (thi/id :test-token-set)
(ctob/make-token :id (thi/new-id! :token-sizing)
:name "token-sizing"
:type :sizing
:value 10))
(ctob/add-token (thi/id :test-token-set)
(ctob/make-token :id (thi/new-id! :token-spacing)
:name "token-spacing"
:type :spacing
:value 30)))
:status-fn #(-> %
(ctos/set-tokens-status #{(thi/id :test-theme)}
#{(thi/id :test-token-set)})))
;; Sync tokens-status from lib's internal active-themes
(tho/add-frame :frame-1
:layout :flex ;; TODO: those values come from main.data.workspace.shape_layout/default-layout-params
:layout-flex-dir :row ;; it should be good to use it directly, but first it should be moved to common.logic
:layout-gap-type :multiple
:layout-gap {:row-gap 0 :column-gap 0}
:layout-align-items :start
:layout-justify-content :start
:layout-align-content :stretch
:layout-wrap-type :nowrap
:layout-padding-type :simple
:layout-padding {:p1 0 :p2 0 :p3 0 :p4 0})
(ths/add-sample-shape :circle-1 :parent-label :frame-1)
(tht/apply-token-to-shape :circle-1
"token-sizing"
[:layout-item-max-h :layout-item-max-w :layout-item-min-h :layout-item-min-w]
[:layout-item-max-h :layout-item-max-w :layout-item-min-h :layout-item-min-w]
10)
(tht/apply-token-to-shape :circle-1
"token-spacing"
[:m1 :m2 :m3 :m4]
[:layout-item-margin]
{:m1 30 :m2 30 :m3 30 :m4 30}))
page (thf/current-page file)
circle (ths/get-shape file :circle-1)
;; ==== Action
changes (cls/generate-relocate (-> (pcb/empty-changes nil)
(pcb/with-page-id (:id page))
(pcb/with-objects (:objects page)))
uuid/zero ;; parent-id
0 ;; to-index
#{(:id circle)}) ;; ids
;; ==== Get
file' (thf/apply-changes file changes)
circle' (ths/get-shape file' :circle-1)]
;; ==== Check
;; the layout item attributes and tokens are removed
(t/is (empty? (:applied-tokens circle')))
(t/is (nil? (:layout-item-margin circle')))
(t/is (nil? (:layout-item-margin-type circle')))
(t/is (nil? (:layout-item-h-sizing circle')))
(t/is (nil? (:layout-item-v-sizing circle')))
(t/is (nil? (:layout-item-max-h circle')))
(t/is (nil? (:layout-item-min-h circle')))
(t/is (nil? (:layout-item-max-w circle')))
(t/is (nil? (:layout-item-min-w circle')))
(t/is (nil? (:layout-item-absolute circle')))
(t/is (nil? (:layout-item-z-index circle')))))
(t/deftest moving-a-shape-into-a-grid-without-a-target-cell-assigns-it
(let [file (-> (thf/sample-file :file1)
(tho/add-grid-frame :grid :width 200 :height 100)
(tho/add-rect :existing {:parent-label :grid})
(tho/place-in-grid-columns :grid 2 {1 :existing})
(tho/add-rect :moving))
page (thf/current-page file)
changes (-> (pcb/empty-changes nil (:id page))
(pcb/with-objects (:objects page))
(cls/generate-relocate (thi/id :grid) 1 #{(thi/id :moving)}))
moved (thf/apply-changes file changes)
grid (ths/get-shape moved :grid)
cells (sort-by :column (vals (:layout-grid-cells grid)))
undone (thf/apply-changes moved {:redo-changes (:undo-changes changes)})]
(t/is (= [[(thi/id :existing)] [(thi/id :moving)]] (mapv :shapes cells)))
(t/is (= (thn/normalize-file file) (thn/normalize-file undone)))))