✨ Add migrations to sanitize variant properties and align component names (#12957)

* ✨ Add migration to sanitize variant property text

(cherry picked from commit 77f1767240026df0cc705f4f084725e28b94230e)

* ✨ Add migration to align variant component names

(cherry picked from commit 844993796d2cd16a02d20e10469107a769ace60c)

* ✨ MR fixes

* ✨ MR review 2
This commit is contained in:
Pablo Alba 2026-11-18 15:46:38 +01:00 committed by GitHub
parent fff44ec758
commit 8e575894f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 775 additions and 1 deletions

View File

@ -14,6 +14,7 @@
[app.common.files.defaults :as cfd]
[app.common.files.helpers :as cfh]
[app.common.files.tokens :as cfo]
[app.common.files.variant :as cfv]
[app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt]
[app.common.geom.rect :as grc]
@ -2445,6 +2446,77 @@
(update :pages-index d/update-vals update-container)
(d/update-when :components d/update-vals update-container))))
;; Property names and values used to accept `,` and `=`. The formula
;; editor treats those as separators, and a later edit strips them.
;; Files saved before that strip can also hold two identical names, two
;; identical value combinations, or a label that was not saved with the
;; values. Rewrite the text in place and refresh the label.
(defn- variant-container-entries
[data]
(concat
(for [page (vals (:pages-index data))
shape (vals (:objects page))
:when (ctk/is-variant-container? shape)]
{:objects (:objects page)
:container shape})
(for [component (vals (:components data))
shape (vals (:objects component))
:when (ctk/is-variant-container? shape)]
{:objects (:objects component)
:container shape})))
(defmethod migrate-data "0038-sanitize-variant-property-text"
[data _]
(let [entries (variant-container-entries data)
used-ids (into #{}
(comp (mapcat (fn [{:keys [objects container]}]
(cfv/qualifying-variant-members data objects container)))
(map :component-id))
entries)
data (reduce (fn [data {:keys [objects container]}]
(cfv/sanitize-variant-container data objects container))
data
entries)]
(->> (vals (:components data))
(filter ctk/is-variant?)
(remove #(contains? used-ids (:id %)))
(reduce cfv/sanitize-variant-component data))))
;; The board name and each main's layer name are already normalized.
;; The library entry could still carry the previous path. Point every
;; member of the board at that board name.
(defmethod migrate-data "0039-align-variant-component-names"
[data _]
(letfn [(align-container [data objects container]
(let [board-name (or (:name container) "")
[path item-name] (cpn/split-group-name board-name)]
(reduce (fn [data child-id]
(let [child (get objects child-id)
component (when child
(get-in data [:components (:component-id child)]))]
(if (and (ctk/is-variant? child)
(= (:id container) (:variant-id child))
(some? component)
(= (:id container) (:variant-id component))
(not= board-name
(cpn/merge-path-item (:path component) (:name component))))
(update-in data [:components (:id component)]
assoc :path path :name item-name)
data)))
data
(:shapes container))))
(containers-in [objects]
(filter ctk/is-variant-container? (vals objects)))]
(reduce (fn [data objects]
(reduce #(align-container %1 objects %2)
data
(containers-in objects)))
data
(concat (map :objects (vals (:pages-index data)))
(map :objects (vals (:components data)))))))
(def available-migrations
(into (d/ordered-set)
["legacy-2"
@ -2537,4 +2609,6 @@
"0034-fix-path-content-offset"
"0035-fix-dangling-children"
"0036-clear-nil-keys-in-objects"
"0037-fix-shape-svg-attrs-and-defs"]))
"0037-fix-shape-svg-attrs-and-defs"
"0038-sanitize-variant-property-text"
"0039-align-variant-component-names"]))

View File

@ -100,3 +100,92 @@
:component-id
(ctcl/get-component data))
component)))
(defn- variant-main-shape-path
"Path in file data to the main shape of `component`: inside the
component `:objects` when it is deleted, or on its page. Nil when the
component has no main."
[component]
(let [main-id (:main-instance-id component)
page-id (:main-instance-page component)]
(cond
(and (:deleted component) main-id)
[:components (:id component) :objects main-id]
(and page-id main-id)
[:pages-index page-id :objects main-id]
:else
nil)))
(defn get-variant-main-shape
"Return the main shape map for `component`, or nil when it is absent."
[data component]
(some->> (variant-main-shape-path component)
(get-in data)))
(defn variant-main-label-changed?
"True when the main shape exists and its :variant-name is not
`properties-to-name` of `props`."
[data component props]
(let [shape (get-variant-main-shape data component)]
(and (map? shape)
(not= (ctv/properties-to-name props)
(:variant-name shape)))))
(defn- qualifying-variant-member
[data objects container child-id]
(when-let [child (get objects child-id)]
(let [component (get-in data [:components (:component-id child)])]
(when (and (ctc/is-variant? child)
(= (:id container) (:variant-id child))
(some? component)
(= (:id container) (:variant-id component)))
{:component component
:component-id (:id component)}))))
(defn qualifying-variant-members
"Return qualifying members of `container` in `:shapes` order.
Each member is {:component component :component-id id}."
[data objects container]
(into []
(keep #(qualifying-variant-member data objects container %))
(:shapes container)))
(defn update-variant-properties
"Apply `f` to the `:variant-properties` of `component` and write the
result to `data`. Also refresh the `:variant-name` of its main shape
when it no longer matches. Return `data` unchanged when nothing differs."
[data component f]
(let [old-props (vec (or (:variant-properties component) []))
props (f old-props)
data (if (= old-props props)
data
(assoc-in data [:components (:id component) :variant-properties] props))]
(if (variant-main-label-changed? data component props)
(assoc-in data
(conj (variant-main-shape-path component) :variant-name)
(ctv/properties-to-name props))
data)))
(defn update-variant-container-properties
"Apply `f` to the property vectors of the members of `container`, in
`:shapes` order, and write each result with `update-variant-properties`.
`f` takes and returns a vector of property vectors."
[data objects container f]
(let [components (mapv :component (qualifying-variant-members data objects container))
props-seq (f (mapv #(vec (or (:variant-properties %) [])) components))]
(reduce (fn [data [component props]]
(update-variant-properties data component (constantly props)))
data
(map vector components props-seq))))
(defn sanitize-variant-component
"Sanitize the properties of one variant component on its own."
[data component]
(update-variant-properties data component ctv/sanitize-properties))
(defn sanitize-variant-container
"Sanitize the properties of every member of `container` as one set."
[data objects container]
(update-variant-container-properties data objects container ctv/sanitize-properties-set))

View File

@ -235,6 +235,125 @@
:value (:value prop)}))
[])))
;; Sanitize stored property sets. Unlike `prepare-property-name` and
;; `prepare-property-value`, which reject invalid user input with nil,
;; these helpers always return valid text, so they can repair data.
(defn clip-property-text
"Cut `text` to at most `max-length` characters."
[text max-length]
(if (> (count text) max-length)
(subs text 0 max-length)
text))
(defn- numbered-property-text
"Join `base` and the ` (n)` suffix. Clip and trim `base` so the result
fits in `property-max-length`, whatever the length of the suffix. An
empty base yields \"(n)\"."
[base n]
(let [suffix (str " (" n ")")
base (-> base
(clip-property-text (- property-max-length (count suffix)))
str/trim)]
(if (str/empty? base)
(str "(" n ")")
(str base suffix))))
(defn unique-property-text
"Return `text`, trimmed and fitted in `property-max-length`. When the
result is in `taken`, add or raise a ` (n)` suffix until it is not.
A ` (n)` suffix is never cut: the base is clipped to leave room for it."
[taken text]
(let [text (str/trim (or text ""))
[base num] (split-base-name-and-number text)
text (if (and (pos? num) (> (count text) property-max-length))
(numbered-property-text base num)
(-> text
(clip-property-text property-max-length)
str/trim))
taken (set taken)]
(if-not (contains? taken text)
text
(let [[base num] (split-base-name-and-number text)]
(->> (iterate inc (max num 1))
(map #(numbered-property-text base %))
(d/seek #(not (contains? taken %))))))))
(defn sanitize-property-name
"Return a valid property name for `name`, unique against the names in
`taken`. Strip forbidden characters and trim. A blank or non-string
name becomes a generated `Property N`."
[name taken]
(let [text (or (sanitize-property-text name) "")
text (if (str/blank? text)
(str property-prefix
(next-property-number (map (fn [name] {:name name}) taken)))
text)]
(unique-property-text taken text)))
(defn sanitize-property-value
"Return a valid property value for `value`. Strip forbidden characters,
trim and clip. A non-string value becomes \"\"."
[value]
(-> (or (sanitize-property-text value) "")
(clip-property-text property-max-length)
str/trim))
(defn sanitize-properties
"Sanitize the name and value of every property in `props`, keeping
names unique. Other keys of each property are kept."
[props]
(reduce (fn [acc prop]
(conj acc (assoc prop
:name (sanitize-property-name (:name prop) (map :name acc))
:value (sanitize-property-value (:value prop)))))
[]
props))
(defn properties-names
"Return every property name in `props-seq` (a seq of property vectors),
without repeats, in the order they first appear."
[props-seq]
(into [] (comp cat (map :name) (distinct)) props-seq))
(defn align-properties
"Return the properties of `props` in the order of `names`. A name with
no property in `props` gets an empty value."
[props names]
(mapv (fn [name]
(or (d/seek #(= name (:name %)) props)
{:name name :value ""}))
names))
(defn unique-values-tuple
"Return `props` with a value combination that is not in `seen` (a seq of
value vectors). On a clash, number the last non-blank value, or the
first value when all are blank."
[props seen]
(let [seen (set seen)]
(if (or (empty? props)
(not (contains? seen (mapv :value props))))
props
(let [idx (or (last (keep-indexed (fn [idx prop]
(when-not (str/blank? (:value prop)) idx))
props))
0)
taken (map #(nth % idx "") seen)]
(update-in props [idx :value] #(unique-property-text taken %))))))
(defn sanitize-properties-set
"Sanitize the property vectors of all the variants of one set, given in
board order. Every vector gets the same names in the same order, and no
two vectors share a value combination. Earlier vectors keep their values."
[props-seq]
(let [cleaned (mapv sanitize-properties props-seq)
names (properties-names cleaned)]
(reduce (fn [acc props]
(conj acc (unique-values-tuple (align-properties props names)
(map #(mapv :value %) acc))))
[]
cleaned)))
(defn find-index-for-property-name
"Finds the index of a name in a property map"
[props name]

View File

@ -12,6 +12,7 @@
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[app.common.test-helpers.variants :as thv]
[app.common.uuid :as uuid]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
@ -47,3 +48,89 @@
collected (cfv/collect-variant-properties (:data file) (:objects page) shape)]
(t/is (= {} collected))))
(defn- variant-file
[]
(-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)))
(t/deftest test-update-variant-properties-writes-props-and-label
(let [file (variant-file)
props [{:name "Property 1" :value "New"}]
data' (cfv/update-variant-properties (:data file) (thc/get-component file :c01) (constantly props))
file' (assoc file :data data')]
(t/is (= props (:variant-properties (thc/get-component file' :c01))))
(t/is (= "New" (:variant-name (ths/get-shape file' :m01))))))
(t/deftest test-update-variant-properties-leaves-clean-data-unchanged
(let [file (variant-file)
data (:data file)]
(t/is (= data (cfv/update-variant-properties data (thc/get-component file :c01) identity)))))
(t/deftest test-update-variant-properties-writes-the-label-of-a-deleted-component
(let [file (variant-file)
deleted-id (uuid/next)
main-id (uuid/next)
component {:id deleted-id
:name "Gone"
:path ""
:deleted true
:variant-id (thi/id :v01)
:main-instance-id main-id
:main-instance-page (:id (thf/current-page file))
:variant-properties [{:name "Size" :value "big"}]
:objects {main-id {:id main-id
:type :frame
:name "Gone"
:variant-id (thi/id :v01)
:variant-name "stale"}}}
data (assoc-in (:data file) [:components deleted-id] component)
data' (cfv/update-variant-properties data component identity)]
(t/is (= "big" (get-in data' [:components deleted-id :objects main-id :variant-name])))))
(t/deftest test-sanitize-variant-container-cleans-members-in-board-order
;; :shapes of the board is [m02 m01], so c02 keeps its value.
(let [props [{:name "Property 1" :value "a=b"}]
file (-> (variant-file)
(thc/update-component :c01 {:variant-properties props})
(thc/update-component :c02 {:variant-properties props}))
page (thf/current-page file)
container (ths/get-shape file :v01)
data' (cfv/sanitize-variant-container (:data file) (:objects page) container)
file' (assoc file :data data')]
(t/is (= [{:name "Property 1" :value "ab"}]
(:variant-properties (thc/get-component file' :c02))))
(t/is (= [{:name "Property 1" :value "ab (1)"}]
(:variant-properties (thc/get-component file' :c01))))
(t/is (= "ab (1)" (:variant-name (ths/get-shape file' :m01))))))
(t/deftest test-sanitize-variant-component
(let [file (-> (variant-file)
(thc/update-component :c01 {:variant-properties [{:name "Size=Big" :value "a,b"}]}))
data' (cfv/sanitize-variant-component (:data file) (thc/get-component file :c01))
file' (assoc file :data data')]
(t/is (= [{:name "SizeBig" :value "ab"}]
(:variant-properties (thc/get-component file' :c01))))
(t/is (= "ab" (:variant-name (ths/get-shape file' :m01))))))
(t/deftest test-sanitize-variant-container-skips-a-child-of-another-set
;; m03 belongs to v02 but is listed in the :shapes of v01.
(let [dirty [{:name "Property 1" :value "a=b"}]
file (-> (variant-file)
(thv/add-variant :v02 :c03 :m03 :c04 :m04)
(thc/update-component :c03 {:variant-properties dirty}))
shapes (conj (:shapes (ths/get-shape file :v01)) (thi/id :m03))
file (ths/update-shape file :v01 :shapes shapes)
page (thf/current-page file)
container (ths/get-shape file :v01)
data' (cfv/sanitize-variant-container (:data file) (:objects page) container)
file' (assoc file :data data')]
(t/is (= dirty (:variant-properties (thc/get-component file' :c03))))
(t/is (= (:variant-name (ths/get-shape file :m03))
(:variant-name (ths/get-shape file' :m03))))))

View File

@ -0,0 +1,137 @@
;; 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.files-migrations-0038-test
(:require
[app.common.files.migrations :as cfm]
[app.common.test-helpers.components :as thk]
[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.file :as ctf]
[app.common.types.variant :as ctv]
[app.common.uuid :as uuid]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
;; ======================================================================
;; 0038-sanitize-variant-property-text
;; Property names and values used to accept `,` and `=`, which the
;; formula editor treats as separators. Files saved before those were
;; stripped can also hold two identical names, two identical value
;; combinations, or a main whose `:variant-name` does not match its
;; properties. The migration rewrites the text in place and refreshes
;; the label.
;; ======================================================================
(def ^:private migration-id
"0038-sanitize-variant-property-text")
(defn- migrate-text
[file]
(let [data' (cfm/migrate-data (ctf/file-data file) migration-id)]
(assoc file :data data')))
(defn- variant-file
[]
(-> (thf/sample-file :file1)
(thv/add-variant :variant1 :component1 :root1 :component2 :root2)))
(t/deftest migration-0038-strips-forbidden-characters
;; root2 is the first child of :shapes, so its names lead the set.
(let [file (-> (variant-file)
(thk/update-component :component2 {:variant-properties [{:name "Size=Big"
:value "a=b,"
:hidden true}]})
(thk/instantiate-component :component1 :copy2)
(ths/update-shape :copy2 :name "Loose copy"))
copy (ths/get-shape file :copy2)
file' (migrate-text file)
comp2' (thk/get-component file' :component2)
root2' (ths/get-shape file' :root2)
comp1' (thk/get-component file' :component1)
copy' (ths/get-shape file' :copy2)
data' (ctf/file-data file')]
(t/is (= "SizeBig" (-> comp2' :variant-properties first :name)))
(t/is (= "ab" (-> comp2' :variant-properties first :value)))
(t/is (true? (-> comp2' :variant-properties first :hidden)))
(t/is (= "ab" (:variant-name root2')))
(t/is (= "Value1" (->> (:variant-properties comp1')
(some #(when (= "Property 1" (:name %)) %))
:value)))
(t/is (= copy copy') "copies are left untouched")
(t/is (= data' (cfm/migrate-data data' migration-id)))))
(t/deftest migration-0038-refreshes-a-stale-variant-name
(let [props [{:name "State" :value "active"}
{:name "Tone" :value "crítica"}]
file (-> (variant-file)
(thk/update-component :component2 {:variant-properties props})
(thk/update-component :component1 {:variant-properties [{:name "State" :value "active"}
{:name "Tone" :value "hostil"}]})
(ths/update-shape :root2 :variant-name "active, hostil"))
file' (migrate-text file)
comp' (thk/get-component file' :component2)
root' (ths/get-shape file' :root2)
data' (ctf/file-data file')]
(t/is (= props (:variant-properties comp')))
(t/is (= "active, crítica" (:variant-name root')))
(t/is (= (ctv/properties-to-name props) (:variant-name root')))
(t/is (= data' (cfm/migrate-data data' migration-id)))))
(t/deftest migration-0038-does-not-take-names-from-a-detached-component
(let [deleted-id (uuid/next)
main-id (uuid/next)
file (variant-file)
variant-id (thi/id :variant1)
page-id (:id (thf/current-page file))
file (update file :data
(fn [data]
(assoc-in data [:components deleted-id]
{:id deleted-id
:name "Gone"
:path ""
:deleted true
:variant-id variant-id
:main-instance-id main-id
:main-instance-page page-id
:variant-properties [{:name "Legacy=Name" :value "a=b"}]
:objects {main-id {:id main-id
:type :frame
:name "Gone"
:variant-id variant-id
:variant-name "stale"}}})))
file' (migrate-text file)
live-names (mapv :name (:variant-properties (thk/get-component file' :component2)))
deleted (get-in file' [:data :components deleted-id])
data' (ctf/file-data file')]
(t/is (= ["Property 1"] live-names))
(t/is (= "LegacyName" (-> deleted :variant-properties first :name)))
(t/is (= "ab" (-> deleted :variant-properties first :value)))
(t/is (= "ab" (get-in deleted [:objects main-id :variant-name])))
(t/is (= data' (cfm/migrate-data data' migration-id)))))
(t/deftest migration-0038-leaves-a-clean-set-unchanged
(let [data (ctf/file-data (variant-file))
data' (cfm/migrate-data data migration-id)]
(t/is (= data data'))
(t/is (= data' (cfm/migrate-data data' migration-id)))))
(t/deftest migration-0038-runs-through-file-migration
(let [file (-> (variant-file)
(thk/update-component :component2 {:variant-properties [{:name "Size=Big" :value "red"}]})
(assoc :migrations (disj cfm/available-migrations migration-id)))
file' (cfm/migrate-file file {})]
(t/is (contains? (:migrations file') migration-id))
(t/is (= "SizeBig" (-> (thk/get-component file' :component2) :variant-properties first :name)))
(t/is (some? (ctf/check-file-data (:data file'))))))

View File

@ -0,0 +1,136 @@
;; 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.files-migrations-0039-test
(:require
[app.common.files.migrations :as cfm]
[app.common.path-names :as cpn]
[app.common.test-helpers.components :as thk]
[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.file :as ctf]
[app.common.types.variant :as ctv]
[app.common.uuid :as uuid]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
;; ======================================================================
;; 0039-align-variant-component-names
;; The variant board name and the layer name of each main are already
;; normalized, but the library entry of a member could still carry an
;; older path and name. The migration sets the `:path` and `:name` of
;; every member component from the board name.
;; ======================================================================
(def ^:private migration-id
"0039-align-variant-component-names")
(def ^:private text-migration-id
"0038-sanitize-variant-property-text")
(defn- named-board
[board-name]
(-> (thf/sample-file :file1)
(thv/add-variant :variant1 :component1 :root1 :component2 :root2)
(ths/update-shape :variant1 :name board-name)
(ths/update-shape :root1 :name board-name)
(ths/update-shape :root2 :name board-name)))
(t/deftest migration-0039-aligns-component-path-with-the-board
(let [file (-> (named-board "Group / Button")
(thk/update-component :component1 {:path "UI" :name "Button"})
(thk/update-component :component2 {:path "UI" :name "Button"})
(thk/instantiate-component :component2 :copy2)
(ths/update-shape :copy2 :name "Loose copy"))
before {:root1 (ths/get-shape file :root1)
:props1 (:variant-properties (thk/get-component file :component1))
:label1 (:variant-name (ths/get-shape file :root1))
:copy (ths/get-shape file :copy2)}
data' (cfm/migrate-data (ctf/file-data file) migration-id)
file' (assoc file :data data')
comp1' (thk/get-component file' :component1)
comp2' (thk/get-component file' :component2)
root1' (ths/get-shape file' :root1)
copy' (ths/get-shape file' :copy2)]
(t/is (= "Group" (:path comp1') (:path comp2')))
(t/is (= "Button" (:name comp1') (:name comp2')))
(t/is (= "Group / Button" (cpn/merge-path-item (:path comp1') (:name comp1'))))
(t/is (= (:name (:root1 before)) (:name root1')))
(t/is (= (:props1 before) (:variant-properties comp1')))
(t/is (= (:label1 before) (:variant-name root1')))
(t/is (= (:name (:copy before)) (:name copy')))
(t/is (= data' (cfm/migrate-data data' migration-id)))))
(t/deftest migration-0039-keeps-a-board-named-empty
;; 0033 stores a blank board name as "(empty)"; this migration copies
;; that name as it is.
(let [file (-> (named-board "(empty)")
(thk/update-component :component1 {:path "Old" :name "Thing"})
(thk/update-component :component2 {:path "Old" :name "Thing"}))
data' (cfm/migrate-data (ctf/file-data file) migration-id)
file' (assoc file :data data')
comp' (thk/get-component file' :component1)]
(t/is (= "" (:path comp')))
(t/is (= "(empty)" (:name comp')))
(t/is (= "(empty)" (cpn/merge-path-item (:path comp') (:name comp'))))
(t/is (= data' (cfm/migrate-data data' migration-id)))))
(t/deftest migration-0039-leaves-a-matching-component-unchanged
(let [file (-> (named-board "Group / Button")
(thk/update-component :component1 {:path "Group" :name "Button"})
(thk/update-component :component2 {:path "Group" :name "Button"}))
data (ctf/file-data file)
data' (cfm/migrate-data data migration-id)]
(t/is (= data data'))
(t/is (= data' (cfm/migrate-data data' migration-id)))))
(t/deftest migration-0039-skips-a-component-with-no-board
(let [orphan-id (uuid/next)
file (update (thf/sample-file :file1) :data
(fn [data]
(assoc-in data [:components orphan-id]
{:id orphan-id
:name "Name"
:path "Old"
:variant-id (uuid/next)})))
data (ctf/file-data file)
data' (cfm/migrate-data data migration-id)]
(t/is (= data data'))))
(t/deftest migration-0038-then-0039-cleans-text-and-the-library-name
(let [file (-> (named-board "Group / Button")
(thk/update-component :component2 {:path "UI"
:name "Button"
:variant-properties [{:name "Size=Big" :value "red"}]})
(thk/update-component :component1 {:path "UI"
:name "Button"
:variant-properties [{:name "Size=Big" :value "blue"}]}))
data' (-> (ctf/file-data file)
(cfm/migrate-data text-migration-id)
(cfm/migrate-data migration-id))
file' (assoc file :data data')
comp2 (thk/get-component file' :component2)
comp1 (thk/get-component file' :component1)
root2 (ths/get-shape file' :root2)
root1 (ths/get-shape file' :root1)]
(t/is (= "SizeBig" (-> comp2 :variant-properties first :name)))
(t/is (= "red" (-> comp2 :variant-properties first :value)))
(t/is (= "blue" (-> comp1 :variant-properties first :value)))
(t/is (= (ctv/properties-to-name (:variant-properties comp2)) (:variant-name root2)))
(t/is (= (ctv/properties-to-name (:variant-properties comp1)) (:variant-name root1)))
(t/is (= "Group" (:path comp2) (:path comp1)))
(t/is (= "Button" (:name comp2) (:name comp1)))
(t/is (= data' (-> data'
(cfm/migrate-data text-migration-id)
(cfm/migrate-data migration-id))))))

View File

@ -27,6 +27,8 @@
[common-tests.files-migrations-0035-test]
[common-tests.files-migrations-0036-test]
[common-tests.files-migrations-0037-test]
[common-tests.files-migrations-0038-test]
[common-tests.files-migrations-0039-test]
[common-tests.files-migrations-test]
[common-tests.files.comp-processors-test]
[common-tests.files.helpers-test]
@ -146,6 +148,8 @@
'common-tests.files-migrations-0035-test
'common-tests.files-migrations-0036-test
'common-tests.files-migrations-0037-test
'common-tests.files-migrations-0038-test
'common-tests.files-migrations-0039-test
'common-tests.files-migrations-test
'common-tests.files.comp-processors-test
'common-tests.files.helpers-test

View File

@ -107,6 +107,134 @@
(t/is (< dist2 dist4))
(t/is (< dist4 dist3))))
(def ^:private long-text (apply str (repeat 60 "n")))
(defn- numbered-long-text
[n]
(let [suffix (str " (" n ")")]
(str (subs long-text 0 (- 60 (count suffix))) suffix)))
(t/deftest clip-property-text
(t/is (= "abc" (ctv/clip-property-text "abc" 5)))
(t/is (= "abc" (ctv/clip-property-text "abcdef" 3))))
(t/deftest unique-property-text
(t/testing "free text is kept"
(t/is (= "Size" (ctv/unique-property-text [] "Size")))
(t/is (= "A (1)" (ctv/unique-property-text ["A"] "A (1)"))))
(t/testing "taken text gets a number"
(t/is (= "Size (1)" (ctv/unique-property-text ["Size"] "Size")))
(t/is (= "A (2)" (ctv/unique-property-text ["A" "A (1)"] "A (1)"))))
(t/testing "empty text is numbered without a leading space"
(t/is (= "(1)" (ctv/unique-property-text [""] "")))
(t/is (= "(2)" (ctv/unique-property-text ["" "(1)"] ""))))
(t/testing "long text is clipped"
(t/is (= long-text (ctv/unique-property-text [] (str long-text "n")))))
(t/testing "the base leaves room for the suffix"
(t/is (= (numbered-long-text 1) (ctv/unique-property-text [long-text] long-text)))
(let [taken (into [long-text] (map numbered-long-text) (range 1 11))
result (ctv/unique-property-text taken long-text)]
(t/is (= (str (apply str (repeat 55 "n")) " (11)") result))
(t/is (= 60 (count result)))
(t/is (= result (ctv/unique-property-text taken result)))
(t/is (= result (ctv/unique-property-text (remove #{result} taken) result)))))
(t/testing "a numbered text over the limit keeps its suffix"
(let [over-1 (str (subs long-text 0 57) " (1)")
over-12 (str (subs long-text 0 58) " (12)")]
(t/is (= (numbered-long-text 1) (ctv/unique-property-text [] over-1)))
(t/is (= (numbered-long-text 12) (ctv/unique-property-text [] over-12)))
(t/is (= (numbered-long-text 1)
(ctv/unique-property-text [] (numbered-long-text 1))))
(t/is (= (numbered-long-text 2)
(ctv/unique-property-text [(numbered-long-text 1)] over-1))))))
(t/deftest sanitize-property-name
(t/is (= "SizeBig" (ctv/sanitize-property-name "Size=Big" [])))
(t/is (= "Size (1)" (ctv/sanitize-property-name "Size" ["Size"])))
(t/is (= "Property 1" (ctv/sanitize-property-name "=,=" [])))
(t/is (= "Property 2" (ctv/sanitize-property-name "=,=" ["Color"])))
(t/is (= "Property 1" (ctv/sanitize-property-name 5 []))))
(t/deftest sanitize-property-value
(t/is (= "ab" (ctv/sanitize-property-value "a=b,")))
(t/is (= "x" (ctv/sanitize-property-value " x ")))
(t/is (= "" (ctv/sanitize-property-value 5)))
(t/is (= "" (ctv/sanitize-property-value nil)))
(t/is (= long-text (ctv/sanitize-property-value (str long-text "n")))))
(t/deftest sanitize-properties
(t/testing "other keys are kept"
(t/is (= [{:name "SizeBig" :value "ab" :hidden true}]
(ctv/sanitize-properties [{:name "Size=Big" :value "a=b," :hidden true}]))))
(t/testing "names are unique"
(t/is (= ["SizeBig" "SizeBig (1)"]
(mapv :name (ctv/sanitize-properties [{:name "Size=Big" :value "one"}
{:name "SizeBig" :value "two"}])))))
(t/testing "long names stay distinct and keep their values"
(t/is (= [{:name long-text :value "x"}
{:name (numbered-long-text 1) :value "y"}]
(ctv/sanitize-properties [{:name long-text :value "x"}
{:name long-text :value "y"}]))))
(t/testing "a name numbered past the limit keeps its suffix"
(let [base (subs long-text 0 57)]
(t/is (= [base (numbered-long-text 1)]
(mapv :name (ctv/sanitize-properties [{:name base :value "x"}
{:name (str base " (1)") :value "y"}])))))))
(t/deftest properties-names
(t/is (= ["A" "B" "C"]
(ctv/properties-names [[{:name "A"} {:name "B"}]
[{:name "B"} {:name "C"}]]))))
(t/deftest align-properties
(t/is (= [{:name "A" :value ""} {:name "B" :value "b" :hidden true}]
(ctv/align-properties [{:name "B" :value "b" :hidden true}] ["A" "B"]))))
(t/deftest unique-values-tuple
(t/testing "no clash"
(t/is (= [{:name "S" :value "a"}]
(ctv/unique-values-tuple [{:name "S" :value "a"}] [["b"]])))
(t/is (= [] (ctv/unique-values-tuple [] [[]]))))
(t/testing "the last non-blank value is numbered"
(t/is (= [{:name "S" :value "a (1)"}]
(ctv/unique-values-tuple [{:name "S" :value "a"}] [["a"]])))
(t/is (= [{:name "S" :value "x"} {:name "C" :value "red (2)"} {:name "D" :value ""}]
(ctv/unique-values-tuple [{:name "S" :value "x"} {:name "C" :value "red"} {:name "D" :value ""}]
[["x" "red" ""] ["y" "red (1)" ""]]))))
(t/testing "all values blank"
(t/is (= [{:name "S" :value "(1)"}]
(ctv/unique-values-tuple [{:name "S" :value ""}] [[""]])))))
(t/deftest sanitize-properties-set
(t/testing "a colliding value tuple is numbered"
(t/is (= [[{:name "Size" :value "ab"}]
[{:name "Size" :value "ab (1)"}]]
(ctv/sanitize-properties-set [[{:name "Size" :value "a=b"}]
[{:name "Size" :value "a=b"}]]))))
(t/testing "every vector gets the same names"
(t/is (= [[{:name "SizeBig" :value "one"} {:name "SizeBig (1)" :value "two"}]
[{:name "SizeBig" :value "only"} {:name "SizeBig (1)" :value ""}]]
(ctv/sanitize-properties-set [[{:name "Size=Big" :value "one"}
{:name "SizeBig" :value "two"}]
[{:name "Size=Big" :value "only"}]]))))
(t/testing "a clean set is unchanged and a second run changes nothing"
(let [props-seq [[{:name "Size" :value "big"} {:name "Color" :value "red"}]
[{:name "Size" :value "small"} {:name "Color" :value "red"}]]
dirty [[{:name "a=b" :value long-text} {:name "a=b" :value ""}]
[{:name "ab" :value long-text}]
[{:name "=" :value 5}]]
once (ctv/sanitize-properties-set dirty)]
(t/is (= props-seq (ctv/sanitize-properties-set props-seq)))
(t/is (= once (ctv/sanitize-properties-set once))))))