penpot/common/test/common_tests/logic/variants_switch_target_test.cljc
alonso.torres b27745246c 🐛 Give a switched head a grid cell when it joins the grid's flow
A copy of a hidden variant sits in a grid without a cell, as a hidden
child takes no part in the grid's flow. Switching it to a shown variant
put the parent's cells back as they were, so the shown head had no cell
and the next reflow of the grid failed.

The switch now assigns the parent grid's cells again when the new head
enters or leaves the flow, as a sync of the head does.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):

|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |  19.977 |  21.701 |   0.57 |
| edit-through-a-chain-of-dependents |    68.931 |   39.77 |  41.277 |   0.58 |
|         library-update-into-copies |    83.083 |   52.24 |  53.439 |   0.63 |
|        main-edit-among-many-shapes |     4.837 |   2.095 |   2.487 |   0.43 |
|           main-edit-into-10-copies |      1.79 |   0.962 |   1.002 |   0.54 |
|          main-edit-into-100-copies |    15.415 |   9.465 |  10.586 |   0.61 |
|         main-edit-into-1000-copies |   153.086 | 104.614 | 107.085 |   0.68 |
|         variant-switch-into-copies |    32.052 |  22.023 |  22.427 |   0.69 |

AI-assisted-by: claude-opus-5-5
2026-11-18 10:29:47 +01:00

230 lines
12 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.variants-switch-target-test
(:require
[app.common.data :as d]
[app.common.files.changes-builder :as pcb]
[app.common.logic.libraries :as cll]
[app.common.logic.variants :as clv]
[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.test-helpers.variants :as thv]
[app.common.types.shape.layout :as ctl]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
(defn- libraries
[file]
{(:id file) file})
(defn- add-third-variant
"Add a component :c03 with properties p1v2, p2v1 to the variant :v01 built
by `thv/add-variant-two-properties`."
[file]
(let [variant-id (thi/id :v01)]
(-> file
(ths/add-sample-shape :m03 :type :frame :parent-label :v01
:variant-id variant-id :variant-name "p1v2, p2v1")
(thc/make-component :c03 :m03)
(thc/update-component :c03 {:variant-id variant-id
:variant-properties [{:name "Property 1" :value "p1v2"}
{:name "Property 2" :value "p2v1"}]}))))
(t/deftest switch-target-has-the-requested-value
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
(thc/instantiate-component :c01 :copy01))
target (clv/find-switch-target (libraries file) (ths/get-shape file :copy01) 0 "Value2")]
(t/is (= (thi/id :c02) (:id target)))))
(t/deftest switch-target-is-nil-when-the-value-is-current
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
(thc/instantiate-component :c01 :copy01))]
(t/is (nil? (clv/find-switch-target (libraries file) (ths/get-shape file :copy01) 0 "Value1")))))
(t/deftest switch-target-is-nil-when-no-variant-has-the-value
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
(thc/instantiate-component :c01 :copy01))]
(t/is (nil? (clv/find-switch-target (libraries file) (ths/get-shape file :copy01) 0 "Missing")))))
(t/deftest switch-target-keeps-the-other-properties-when-it-can
;; From p1v1, p2v1, setting Property 1 to p1v2 can give c02 (p1v2, p2v2)
;; or c03 (p1v2, p2v1). c03 keeps Property 2, so it is nearer.
(let [file (-> (thf/sample-file :file1)
(thv/add-variant-two-properties :v01 :c01 :m01 :c02 :m02)
(add-third-variant)
(thc/instantiate-component :c01 :copy01))
target (clv/find-switch-target (libraries file) (ths/get-shape file :copy01) 0 "p1v2")]
(t/is (= (thi/id :c03) (:id target)))))
(t/deftest switch-target-between-equally-near-variants-is-the-first-of-the-container
;; From p1v1, p2v1, setting Property 1 to p1v2 can give c02 (p1v2, p2v2) or
;; c03 (p1v2, p2v3), both one value away; the one placed first among the
;; container's children wins.
(let [file (-> (thf/sample-file :file1)
(thv/add-variant-two-properties :v01 :c01 :m01 :c02 :m02)
(ths/add-sample-shape :m03 :type :frame :parent-label :v01
:variant-id (thi/id :v01) :variant-name "p1v2, p2v3")
(thc/make-component :c03 :m03)
(thc/update-component :c03 {:variant-id (thi/id :v01)
:variant-properties [{:name "Property 1" :value "p1v2"}
{:name "Property 2" :value "p2v3"}]})
(thc/instantiate-component :c01 :copy01))
target (fn [order]
(let [file (ths/update-shape file :v01 :shapes (mapv thi/id order))]
(:id (clv/find-switch-target (libraries file) (ths/get-shape file :copy01) 0 "p1v2"))))]
(t/is (= (thi/id :c02) (target [:m01 :m02 :m03])))
(t/is (= (thi/id :c03) (target [:m01 :m03 :m02])))))
(t/deftest switch-into-own-main-is-a-nesting-loop
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
(thc/instantiate-component :c01 :nested :parent-label :m02)
(thc/instantiate-component :c01 :copy01))
objects (:objects (thf/current-page file))
loop? #(clv/swap-nesting-loop? objects (ths/get-shape file %) (:data file) (thi/id :c02))]
(t/is (true? (loop? :nested)))
(t/is (false? (loop? :copy01)))))
(t/deftest swap-through-a-held-main-is-a-nesting-loop
;; :cy holds a copy of :cz whose own copy of :cx was swapped away; the
;; main of :cz still holds :cx, which a reset brings back
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component :cw :mw :mw-child)
(tho/add-simple-component :cv :mv :mv-child)
(tho/add-frame :mx)
(thc/instantiate-component :cw :x-head :parent-label :mx)
(thc/make-component :cx :mx)
(tho/add-frame :mz)
(thc/instantiate-component :cx :z-x :parent-label :mz)
(thc/make-component :cz :mz)
(tho/add-frame :my)
(thc/instantiate-component :cz :y-z :parent-label :my :children-labels [:y-z-x])
(thc/make-component :cy :my)
(thc/component-swap :y-z-x :cv :y-z-v))
objects (:objects (thf/current-page file))]
(t/is (true? (clv/swap-nesting-loop? objects (ths/get-shape file :x-head) (:data file) (thi/id :cy))))
(t/is (false? (clv/swap-nesting-loop? objects (ths/get-shape file :x-head) (:data file) (thi/id :cv))))))
(t/deftest update-main-through-a-held-main-is-a-nesting-loop
;; a copy of :ck holds :ca; :ca's copy of :cb and :cb's copy of :cc each
;; swapped their head away, but the main of :cc still holds :ck, so
;; updating :ck from the copy would loop
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component :cw :mw :mw-child)
(tho/add-simple-component :cv :mv :mv-child)
(tho/add-frame :mk)
(thc/instantiate-component :cw :k-head :parent-label :mk)
(thc/make-component :ck :mk)
(tho/add-frame :mc)
(thc/instantiate-component :ck :c-k :parent-label :mc)
(thc/make-component :cc :mc)
(tho/add-frame :mb)
(thc/instantiate-component :cc :b-c :parent-label :mb :children-labels [:b-c-k])
(thc/make-component :cb :mb)
(thc/component-swap :b-c-k :cv :b-c-v)
(tho/add-frame :ma)
(thc/instantiate-component :cb :a-b :parent-label :ma :children-labels [:a-b-c])
(thc/make-component :ca :ma)
(thc/component-swap :a-b-c :cv :a-b-v)
(thc/instantiate-component :ck :copyk :children-labels [:copyk-head])
(thc/component-swap :copyk-head :ca :copyk-a))
objects (:objects (thf/current-page file))]
(t/is (true? (cll/update-main-nesting-loop? (libraries file) {:objects objects} (thi/id :copyk))))
(t/is (false? (cll/update-main-nesting-loop? (libraries file) {:objects objects} (thi/id :a-b))))))
(t/deftest swap-in-place-keeps-index-and-layout-attrs
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
(ths/add-sample-shape :board :type :frame)
(ths/add-sample-shape :first :parent-label :board)
(thc/instantiate-component :c01 :copy01 :parent-label :board)
(ths/add-sample-shape :last :parent-label :board)
(ths/update-shape :copy01 :layout-item-z-index 7))
page (thf/current-page file)
board (ths/get-shape file :board)
index (d/index-of (:shapes board) (thi/id :copy01))
[new-shape changes _]
(clv/generate-component-swap-in-place (pcb/empty-changes nil (:id page))
page
(libraries file)
(:data file)
(ths/get-shape file :copy01)
(thi/id :c02)
false)
file' (thf/apply-changes file changes)
new-shape (ths/get-shape-by-id file' (:id new-shape))]
(t/is (= (thi/id :c02) (:component-id new-shape)))
(t/is (= index (d/index-of (:shapes (ths/get-shape file' :board)) (:id new-shape))))
(t/is (= 7 (:layout-item-z-index new-shape)))))
(defn- add-grid-board
"Add a board :board with a one-row, three-column grid layout, holding a shape
:first in the first column and a copy :copy01 of :c01 in the third, so the
second cell is free."
[file]
(-> file
(tho/add-grid-frame :board)
(ths/add-sample-shape :first :parent-label :board)
(thc/instantiate-component :c01 :copy01 :parent-label :board)
(tho/place-in-grid-columns :board 3 {1 :first 3 :copy01})))
(t/deftest swap-in-place-keeps-the-grid-cell
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
(add-grid-board))
page (thf/current-page file)
cell (ctl/get-cell-by-shape-id (ths/get-shape file :board) (thi/id :copy01))
[new-shape changes _]
(clv/generate-component-swap-in-place (pcb/empty-changes nil (:id page))
page
(libraries file)
(:data file)
(ths/get-shape file :copy01)
(thi/id :c02)
false)
file' (thf/apply-changes file changes)
cell' (ctl/get-cell-by-shape-id (ths/get-shape file' :board) (:id new-shape))]
(t/is (= 3 (:column cell)))
(t/is (= (:id cell) (:id cell')))
(t/is (= [(:id new-shape)] (:shapes cell')))))
(t/deftest switching-a-hidden-grid-child-to-a-shown-variant-gives-it-a-cell
(let [;; ==== Setup: :m01 is hidden, so its copy in the grid board holds no cell
file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02 :variant1-params {:hidden true})
(tho/add-grid-frame :board)
(ths/add-sample-shape :first :parent-label :board)
(thc/instantiate-component :c01 :copy01 :parent-label :board)
(tho/place-in-grid-columns :board 2 {1 :first}))
page (thf/current-page file)
;; ==== Action
[new-shape changes _]
(clv/generate-component-swap-in-place (pcb/empty-changes nil (:id page))
page
(libraries file)
(:data file)
(ths/get-shape file :copy01)
(thi/id :c02)
true)
file' (thf/apply-changes file changes)
new-shape (ths/get-shape-by-id file' (:id new-shape))
cell' (ctl/get-cell-by-shape-id (ths/get-shape file' :board) (:id new-shape))]
;; ==== Check: the shown head takes the free cell
(t/is (:hidden (ths/get-shape file :copy01)))
(t/is (nil? (ctl/get-cell-by-shape-id (ths/get-shape file :board) (thi/id :copy01))))
(t/is (not (:hidden new-shape)))
(t/is (= 2 (:column cell')))))