🐛 Give an added variant a value no member of its set has

Adding a variant names the new member's last value "Value n", n one
more than the number of values that property has. Once a member is
deleted, that count can repeat a value still in use: after adding
"Value 3" and "Value 4" and deleting "Value 3", the next member added
from "Value 4" got "Value 4" again, the same properties as its source.
The new member takes the first "Value n" from that number up that no
member has.

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

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  21.352 |  0.61 |
| edit-through-a-chain-of-dependents |   68.931 |  39.364 |  0.57 |
| library-update-into-copies         |   83.083 |  46.730 |  0.56 |
| main-edit-among-many-shapes        |    4.837 |   1.943 |  0.40 |
| main-edit-into-10-copies           |    1.790 |   1.051 |  0.59 |
| main-edit-into-100-copies          |   15.415 |   9.726 |  0.63 |
| main-edit-into-1000-copies         |  153.086 | 101.415 |  0.66 |
| variant-switch-into-copies         |   32.052 |  20.744 |  0.65 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-07 18:42:23 +01:00
parent 3e6c98c01a
commit a64c20465f
3 changed files with 39 additions and 6 deletions

View File

@ -68,12 +68,12 @@
(let [data (pcb/get-library-data changes)
objects (pcb/get-objects changes)
component-id (:component-id shape)
value (str ctv/value-prefix
(-> (cfv/extract-properties-values data objects variant-id)
last
:value
count
inc))
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)
[new-shape changes] (-> changes
(cld/generate-duplicate-component

View File

@ -1693,6 +1693,13 @@
:ops [[:add-variant 0] [:switch 0] [:add-variant 1] [:undo] [:redo] [:sync 0]]})]
(t/is (= 2 (get-in result [:executed :add-variant])))))
(t/deftest adding-a-variant-after-deleting-a-member-takes-a-value-no-member-has
(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 [[:add-variant 0] [:switch 0] [:add-variant 0 0 0 0] [:delete 2 0 0 0]
[:add-variant 0 0 0 0]]}))))
(t/deftest random-variant-compositions
(let [coverage (atom {})
failure (atom nil)]

View File

@ -22,6 +22,7 @@
[app.common.test-helpers.variants :as thv]
[app.common.types.component :as ctk]
[app.common.types.variant :as ctv]
[app.common.uuid :as uuid]
[clojure.test :as t]
[cuerdas.core :as str]))
@ -490,6 +491,31 @@
(t/is (= (count objects') 7))))
(t/deftest test-add-new-variant-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"))
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) 0))
file' (thf/apply-changes file changes)
;; ==== Get
values (map #(-> % :variant-properties first :value)
(vals (get-in file' [:data :components])))]
;; ==== Check
;; two values make the next one "Value 3", which a member already has
(t/is (= "Value 4" (get-in file' [:data :components new-id :variant-properties 0 :value])))
(t/is (apply distinct? values))))
(t/deftest test-delete-variant
;; When a variant container becomes empty, it id automatically deleted
(let [;; ==== Setup