From 564caf7cf23895453b4d55c9f83f74d0114c6555 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sat, 7 Nov 2026 21:23:22 +0100 Subject: [PATCH] :bug: Count a new variant's value among its own property's values Adding a variant, or duplicating a variant main into its set, gives the new member a "Value n" in its last property. The values came from the last entry of the properties grouped by name, which is a hash map once a set has nine properties, so the count could come from another property: with eight empty properties before "Property 9", the new member took "Value 2", its source's value, and duplicated it. Both actions now take, through one function, the first "Value n" no member has at the position the value goes to, n counting up from one more than the values there. Duplicating a variant main into its set could also repeat a value in use after a member was deleted, as adding a variant did. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | world | baseline | now | ratio | |------------------------------------+----------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 20.529 | 0.58 | | edit-through-a-chain-of-dependents | 68.931 | 40.136 | 0.58 | | library-update-into-copies | 83.083 | 45.708 | 0.55 | | main-edit-among-many-shapes | 4.837 | 1.972 | 0.41 | | main-edit-into-10-copies | 1.790 | 0.920 | 0.51 | | main-edit-into-100-copies | 15.415 | 9.714 | 0.63 | | main-edit-into-1000-copies | 153.086 | 96.513 | 0.63 | | variant-switch-into-copies | 32.052 | 21.386 | 0.67 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/files/variant.cljc | 13 ++++++ common/src/app/common/logic/duplicate.cljc | 8 +--- common/src/app/common/logic/variants.cljc | 7 +-- .../logic/composable_gen_test.cljc | 9 ++++ .../common_tests/logic/variants_test.cljc | 46 +++++++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/common/src/app/common/files/variant.cljc b/common/src/app/common/files/variant.cljc index 989a80a58b..941bdf1f04 100644 --- a/common/src/app/common/files/variant.cljc +++ b/common/src/app/common/files/variant.cljc @@ -47,6 +47,19 @@ :value (->> v (map :value) distinct)} mdata)))))) +(defn new-property-value + "A \"Value n\" no component of the variant `variant-id` has at the + property position `pos`, n counting up from one more than the values + they have there." + [data objects variant-id pos] + (let [values (->> (find-variant-components data objects variant-id) + (keep #(dm/get-in % [:variant-properties pos :value])) + distinct)] + (->> (iterate inc (inc (count values))) + (map #(str ctv/value-prefix %)) + (remove (set values)) + first))) + (defn get-variant-mains [component data] (assert (ctv/valid-variant-component? component) "expected valid component variant") diff --git a/common/src/app/common/logic/duplicate.cljc b/common/src/app/common/logic/duplicate.cljc index c8839ecc3d..837bc4c73f 100644 --- a/common/src/app/common/logic/duplicate.cljc +++ b/common/src/app/common/logic/duplicate.cljc @@ -218,12 +218,8 @@ :page-id page-id :new-component-file new-component-file}) value (when into-new-variant? - (str ctv/value-prefix - (-> (cfv/extract-properties-values (:data library) objects (:id parent)) - last - :value - count - inc)))] + (cfv/new-property-value (:data library) objects (:id parent) + (-> component :variant-properties count dec)))] [shape (cond-> changes diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index a302b044d1..474e525800 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -68,12 +68,7 @@ (let [data (pcb/get-library-data changes) objects (pcb/get-objects changes) component-id (:component-id shape) - values (-> (cfv/extract-properties-values data objects variant-id) last :value) - ;; the first "Value n" from the number of values up that no member has - value (->> (iterate inc (inc (count values))) - (map #(str ctv/value-prefix %)) - (remove (set values)) - first) + value (cfv/new-property-value data objects variant-id prop-num) [new-shape changes] (-> changes (cld/generate-duplicate-component diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 0dadd8cfda..ae62a505f9 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1708,6 +1708,15 @@ [:text 26 3 0 0] [:add-variant 2] [:switch 0] [:variant-property 0 0 0 0] [:switch 20]]})))) +(t/deftest adding-a-variant-to-a-set-of-many-properties-takes-a-new-last-value + (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 [[:variant-property 0 0 0 0] [:switch 0] [:variant-property 0 0 0 0] [:switch 0] + [:variant-property 0 0 0 0] [:variant-property 0 0 0 0] [:switch 0] + [:variant-property 0 0 0 0] [:switch 0] [:variant-property 0 0 0 0] [:switch 0] + [:add-variant 0] [:switch 0] [:add-variant 0] [:switch 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_test.cljc b/common/test/common_tests/logic/variants_test.cljc index bea6b11449..989c937ad2 100644 --- a/common/test/common_tests/logic/variants_test.cljc +++ b/common/test/common_tests/logic/variants_test.cljc @@ -516,6 +516,52 @@ (t/is (= "Value 4" (get-in file' [:data :components new-id :variant-properties 0 :value]))) (t/is (apply distinct? values)))) +(t/deftest test-add-new-variant-counts-the-values-of-the-last-of-many-properties + (let [;; ==== Setup + props (fn [size last-value] + (into [{:name "Size" :value size} {:name "State" :value "Idle"} {:name "Theme" :value "Light"}] + (concat (map #(hash-map :name (str "Property " %) :value "") (range 4 9)) + [{:name "Property 9" :value last-value}]))) + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thc/update-component :c01 {:variant-properties (props "Small" "Value 2")}) + (ths/update-shape :m01 :variant-name "Small, Idle, Light, Value 2") + (thc/update-component :c02 {:variant-properties (props "Large" "Value 1")}) + (ths/update-shape :m02 :variant-name "Large, Idle, Light, Value 1")) + page (thf/current-page file) + new-id (uuid/next) + + ;; ==== Action + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects page)) + (clv/generate-add-new-variant (ths/get-shape file :m01) (thi/id :v01) new-id (uuid/next) 8)) + file' (thf/apply-changes file changes)] + + ;; ==== Check + ;; nine property names group into a map whose last entry is not the last property + (t/is (= "Value 3" (get-in file' [:data :components new-id :variant-properties 8 :value]))))) + +(t/deftest test-duplicate-variant-main-takes-a-value-no-member-has + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thc/update-component :c02 {:variant-properties [{:name "Property 1" :value "Value 3"}]}) + (ths/update-shape :m02 :variant-name "Value 3")) + + ;; ==== Action + file' (tho/duplicate-shape file :m01) + + ;; ==== Get + values (->> (vals (get-in file' [:data :components])) + (remove :deleted) + (map #(-> % :variant-properties first :value)))] + + ;; ==== Check + ;; two values make the next one "Value 3", which a member already has + (t/is (= 3 (count values))) + (t/is (apply distinct? values)))) + (t/deftest test-delete-variant ;; When a variant container becomes empty, it id automatically deleted (let [;; ==== Setup