From 25281f47f20b3d0ee865f6288c71a1b1eef7992c Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 9 Nov 2026 06:00:43 +0100 Subject: [PATCH] :bug: Keep no switched swap that would nest a component in itself A copy whose child was swapped to a variant, switched to that variant, kept the swap. That put a copy of the new component inside a copy of itself, so the move of the kept child was refused and the child stayed at the page root, leaving an invalid file. A switch now keeps a swap only when its subtree holds no copy of the new component, nor of a component the copy is inside; the new variant's own child takes its place. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+--------+--------| | deepest-edit-through-three-levels | 35.215 | 19.0 | 20.802 | 0.54 | | edit-through-a-chain-of-dependents | 68.931 | 36.673 | 38.166 | 0.53 | | library-update-into-copies | 83.083 | 44.204 | 46.142 | 0.53 | | main-edit-among-many-shapes | 4.837 | 2.106 | 2.224 | 0.44 | | main-edit-into-10-copies | 1.79 | 0.917 | 0.981 | 0.51 | | main-edit-into-100-copies | 15.415 | 8.858 | 9.458 | 0.57 | | main-edit-into-1000-copies | 153.086 | 89.013 | 93.164 | 0.58 | | variant-switch-into-copies | 32.052 | 19.844 | 20.613 | 0.62 | Common suite: 2371 tests, 1 failure, the known insufficient-random-coverage of random-stale-library-compositions (swap and library-add-variant attempted too few times in 20 programs). AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/variants.cljc | 22 ++++++++++++++++--- .../logic/composable_gen_test.cljc | 19 ++++++++++++++++ .../logic/variants_switch_test.cljc | 18 +++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 42982a1301..4b7f3c0aae 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -338,6 +338,16 @@ (remove #(= (:id shape) (:id %))) (some ctk/swapped?))) +(defn- nests-in-switch? + "Whether the swapped shape `swapped` of `objects` holds a copy of the + component `component-id` a switch of the copy head `head` goes to, or of + a component the head is inside, so keeping it would nest a component in + itself." + [objects swapped head component-id] + (cfh/components-nesting-loop? (cfh/get-children-with-self objects (:id swapped)) + (cons {:component-id component-id} + (cfh/get-parents objects (:id head))))) + (defn- find-shape-ref-child-of "Get the shape referenced by the shape-ref of the near main of the shape, recursively repeated until find a shape-ref with parent-id as ancestor. @@ -515,10 +525,14 @@ (some #(and (ctk/swapped? %) (not= (:id %) (:id old-ref))) (rest (ctf/get-ref-chain-until-target-ref container libraries old (:id old-ref)))))] - ;; a swap is kept only where the new variant holds a copy + ;; a swap is kept only where the new variant holds a copy, + ;; and when it does not nest the new variant in itself (when (and new (not swapped-in-chain?) - (or (not swapped?) (ctk/instance-head? new))) + (or (not swapped?) + (and (ctk/instance-head? new) + (not (nests-in-switch? page-objects old original-shape + (:component-id new-shape)))))) {:old old :old-ref old-ref :new new :swapped? swapped?}))) orig-touched) @@ -668,7 +682,8 @@ "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 holds a copy - in. The others go with the old head." + in, and that would not nest a component in itself. The others go with + the old head." [page libraries ldata shape component-id] (let [objects (:objects page) container (ctn/make-container page :page) @@ -688,6 +703,7 @@ (into #{} (comp (filter ctk/swapped?) (remove #(child-of-swapped? % objects (:id shape))) + (remove #(nests-in-switch? objects % shape component-id)) (filter (fn [old] (let [old-ref (find-shape-ref-child-of container libraries old (:id base-ref))] (and (some? old-ref) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 6cfb9de89e..57b8ccab05 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1891,6 +1891,16 @@ [:library-add-variant 0] [:library-sync] [:switch 0] [:library-text-style 6 8 1] [:library-variant-property 0 0 0 0] [:library-sync] [:switch 19]]})))) +(t/deftest switching-a-copy-to-the-variant-its-child-swaps-to-drops-the-swap + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library-variants true :stale-libraries true + :layout nil :layout-item-margins false :grid-square false :library-depth 1 + :library true :rich-variants true :layout-children false :library-text true + :grid-empty false :layout-copies true :text true}] + :ops [[:library-make-variant 8 0 0 0] [:library-combine 0 0 0 0] [:detach 0 0 0 0] + [:library-add-variant 16] [:library-sync] [:switch 4] + [:library-variant-property 0 0 0 0] [:library-sync] [:switch 6] + [:library-variant-property 0 0 0 0] [:library-sync] [:switch 4]]})))) + (t/deftest restoring-a-variant-an-undo-removed-keeps-its-swap (t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :layout nil :variant-diff false :layout-item-margins false :grid-square false :rich-variants true @@ -1899,6 +1909,15 @@ [:combine 15] [:switch 19] [:combine 0 0 0 0] [:make-variant 14 0 0 0] [:undo 0 0 0 0] [:restore 0 0 0 0]]})))) +(t/deftest switching-a-copy-to-the-variant-its-swap-holds-carries-no-override + (t/is (map? (cg/run {:world [3 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 [[:nest 18 0 0 0] [:swap 0 0 0 0] [:switch 0 0 0 0] [:add-variant 0 0 0 0] + [:combine 17 0 0 0] [:make-variant 2 0 0 0] [:make-variant 6 0 0 0] + [:swap 11 0 0 0] [:make-variant 4] [:switch 9] [:switch 16 0 0 0] + [:combine 7] [:switch 11]]})))) + (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 e8c84a6763..2a46ea93e7 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -3620,6 +3620,24 @@ (t/is (not (ctk/instance-head? icon'))) (t/is (nil? (ths/get-shape file' :copy-icon-b))))) +(t/deftest test-switch-drops-a-swap-to-the-variant-it-switches-to + (let [;; ==== Setup: the copy's icon is swapped for a copy of the second variant + file (-> (thf/sample-file :file1) + (tho/add-simple-component :icon-a :icon-a-root :icon-a-child) + (thv/add-variant-with-copy :v01 :c01 :m01 :c02 :m02 :m01-icon :m02-icon :icon-a) + (thc/instantiate-component :c01 :copy01 :children-labels [:copy-icon]) + (thc/component-swap :copy-icon :c02 :copy-icon-c02)) + + ;; ==== 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: a copy of the second variant cannot hold another one + (t/is (= 1 (count (:shapes (ths/get-shape file' :copy02))))) + (t/is (= (:id (ths/get-shape file :m02-icon)) (:shape-ref icon'))) + (t/is (nil? (ths/get-shape file' :copy-icon-c02))))) + (defn- resize-width [shape width] (gsh/transform-shape shape (ctm/change-size shape width nil)))