penpot/common/test/common_tests/files/changes_builder_test.cljc
Pablo Alba 8d0ff7984b 🐛 Keep swapped children on switch and undo of a nested variant copy
Switching a variant copy that sits inside another copy dropped a component
swapped inside it, and the new variant showed its default child. Undoing
a variant switch also lost a swapped child: the old variant came back with
no child in that place.

When the swap was made in the copy, the original shape of the swapped
child was looked up from the nearest main, which is the copy in the outer
main, so it had no match in the variant main. Look it up from the match
of its parent in the variant main instead.

When the swap was made in the outer main, the child had no slot of its
own, so it was deleted with the old copy and never moved. The new
ctf/get-outer-swaps finds these heads by walking their ref chain up to
the variant main. The delete step and keep-touched both use it, so they
pick the same heads: kept off the delete list, moved into the new copy
and pointed to the swapped-in component, with the slot of the child they
replace. The walk that advances the refs of a subtree moves to
ctf/get-advanced-refs, which the clipboard now shares.

When the switch kept a swapped child, the undo of its move into the new
copy put it back under the old copy id. That id is also the id of the new
copy, so undoing the creation of the new copy removed the child.
change-parent now takes the undo parent from its objects, as it already
did for the previous sibling.

When the new variant had no match for a swapped child, the child was
removed with the old copy, with no undo entry. Move it to the root first
and delete it from there, after the rest of the switch, so undo brings it
back after the old copy.

Closes #13179
Closes #13181

AI-assisted-by: claude-opus-5-5
2026-11-23 16:06:09 +01:00

47 lines
2.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.files.changes-builder-test
(:require
[app.common.files.changes-builder :as pcb]
[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]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
(t/deftest test-change-parent-undo-uses-parent-in-objects
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(tho/add-frame :parent1)
(tho/add-frame :parent2)
(tho/add-frame :parent3)
(tho/add-rect :rect0 :parent-label :parent1)
(tho/add-rect :rect1 :parent-label :parent1)
(tho/add-rect :rect2 :parent-label :parent1))
page (thf/current-page file)
;; The map of the shape as it is before both moves
rect1 (ths/get-shape file :rect1)
;; ==== Action
changes (-> (pcb/empty-changes nil (:id page))
(pcb/with-objects (:objects page))
(pcb/change-parent (thi/id :parent2) [rect1])
(pcb/change-parent (thi/id :parent3) [rect1]))
file' (thf/apply-changes file changes)
file'' (thf/apply-undo-changes file' changes)]
(t/is (= (thi/id :parent3) (:parent-id (ths/get-shape file' :rect1))))
;; The undo of the second move, run first, takes the shape back to
;; :parent2, where the first move left it, not to the parent in the map
(t/is (= (thi/id :parent2) (:parent-id (first (:undo-changes changes)))))
;; The whole undo puts the shape back in its place in :parent1
(t/is (= (thi/id :parent1) (:parent-id (ths/get-shape file'' :rect1))))
(t/is (= (:shapes (ths/get-shape file :parent1))
(:shapes (ths/get-shape file'' :parent1))))))