penpot/common/test/common_tests/logic/variants_switch_target_test.cljc
alonso.torres 69aa1e03f7 🐛 Take the first equally near variant on a switch
A switch picks the variant whose properties are nearest the wanted
ones; between equally near ones it took the last child of the variant
container. Since combine as variants keeps the order of the components
it combines, that is the last of them: switching an empty variant to
"primary" gave primary/icon+test where the QA case (PENPOT-2446)
expects primary/hover, the first primary variant. It now takes the
first child among the equally near ones.

variants-switch-target-test: two variants one value away, in both
orders of the container's children.

Benchmark against the 1e baseline (lower quantile, ms):

  world                               baseline     now  ratio
  deepest-edit-through-three-levels     35.215  16.810   0.48
  edit-through-a-chain-of-dependents    68.931  32.765   0.48
  library-update-into-copies            83.083  39.232   0.47
  main-edit-among-many-shapes            4.837   2.277   0.47
  main-edit-into-10-copies               1.790   0.807   0.45
  main-edit-into-100-copies             15.415   8.054   0.52
  main-edit-into-1000-copies           153.086  81.186   0.53
  variant-switch-into-copies            32.052  17.933   0.56

AI-assisted-by: claude-opus-5-5
2026-11-10 20:21:16 +01:00

153 lines
7.7 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.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-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')))))