From 4b671476c19eb028784fca988fab1955afe0871f Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sun, 8 Nov 2026 19:58:11 +0100 Subject: [PATCH] :bug: Keep a switched swap only where the new variant holds a copy A switch kept a swapped child of the copy wherever the new variant had a shape of the same name and ancestor kinds, even a plain board a main had detached. The kept swap then referred to a shape that is no copy head and the file failed validation. The switch now keeps a swap only where the new main holds a copy, and the other swaps go with the old head. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 20.978 | 21.326 | 0.6 | | edit-through-a-chain-of-dependents | 68.931 | 40.47 | 41.523 | 0.59 | | library-update-into-copies | 83.083 | 47.521 | 50.404 | 0.57 | | main-edit-among-many-shapes | 4.837 | 2.175 | 2.476 | 0.45 | | main-edit-into-10-copies | 1.79 | 0.982 | 1.003 | 0.55 | | main-edit-into-100-copies | 15.415 | 9.423 | 9.949 | 0.61 | | main-edit-into-1000-copies | 153.086 | 99.435 | 101.227 | 0.65 | | variant-switch-into-copies | 32.052 | 21.14 | 21.923 | 0.66 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/variants.cljc | 16 ++++++++------ .../logic/composable_gen_test.cljc | 8 +++++++ .../logic/variants_switch_test.cljc | 22 +++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 0bbcca249c..260985de52 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -506,10 +506,11 @@ old-ref (if root? orig-base-ref-shape (find-shape-ref-child-of container libraries old (:id orig-base-ref-shape))) - new (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref)))] - (when new - {:old old :old-ref old-ref :new new - :swapped? (and (not root?) (ctk/swapped? old))}))) + new (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref))) + swapped? (and (not root?) (ctk/swapped? old))] + ;; a swap is kept only where the new variant holds a copy + (when (and new (or (not swapped?) (ctk/instance-head? new))) + {:old old :old-ref old-ref :new new :swapped? swapped?}))) orig-touched) ;; A swapped child is kept as it is, and the others carry their @@ -657,8 +658,8 @@ (defn- kept-swapped-ids "The swapped shapes in the subtree of the copy head `shape` that a switch to the component `component-id` of `ldata` keeps: those whose place in - their main, by name and ancestor kinds, the new component has. The others - go with the old head." + their main, by name and ancestor kinds, the new component holds a copy + in. The others go with the old head." [page libraries ldata shape component-id] (let [objects (:objects page) container (ctn/make-container page :page) @@ -671,7 +672,8 @@ component (ctkl/get-component ldata component-id) main (ctf/get-component-root ldata component) main-objects (:objects (ctf/get-component-container ldata component)) - new-paths (into #{} (map :shape-path) + ;; the paths of the new main where it holds a copy + new-paths (into #{} (comp (filter ctk/instance-head?) (map :shape-path)) (add-unique-path (reverse (cfh/get-children-with-self main-objects (:id main))) main-objects (:id main)))] (into #{} diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 4ae8249ad2..ed4687d6f9 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1849,6 +1849,14 @@ [:delete 7 0 0 0] [:make-variant 8 0 0 0] [:transform 13 3 7 0] [:combine 7] [:switch 7] [:resize 10 0 0 0] [:combine 1] [:switch 10]]})))) +(t/deftest switching-a-copy-drops-a-swap-where-the-new-variant-holds-a-plain-board + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:combine 0] [:switch 1] [:make-variant 1 0 0 0] [:add-variant 4] [:switch 8] + [:make-variant 0 0 0 0] [:detach 0 0 0 0] [:variant-property 0 0 0 0] + [:switch 12]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 35d142abe3..cfe7b932dd 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -12,6 +12,7 @@ [app.common.geom.point :as gpt] [app.common.geom.rect :as grc] [app.common.geom.shapes :as gsh] + [app.common.logic.instances :as cli] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] [app.common.math :as mth] @@ -3572,6 +3573,27 @@ (t/is (gpt/close? (offset-in-root file :m02 :m02-icon) (offset-in-root file' :copy02 :copy-icon-b))))) +(t/deftest test-switch-drops-a-swap-the-new-variant-holds-no-copy-for + (let [;; ==== Setup: the second variant's icon is detached into a plain board + file (setup-variant-with-swapped-icon) + page (thf/current-page file) + file (thf/apply-changes file (cli/generate-detach-instance (-> (pcb/empty-changes nil) + (pcb/with-page page) + (pcb/with-objects (:objects page))) + page + {(:id file) file} + (thi/id :m02-icon))) + + ;; ==== Action + file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 + :keep-touched? true}) + icon' (ths/get-shape-by-id file' (-> (ths/get-shape file' :copy02) :shapes first))] + + ;; ==== Check + (t/is (= (:id (ths/get-shape file :m02-icon)) (:shape-ref icon'))) + (t/is (not (ctk/instance-head? icon'))) + (t/is (nil? (ths/get-shape file' :copy-icon-b))))) + (defn- resize-width [shape width] (gsh/transform-shape shape (ctm/change-size shape width nil)))