penpot/common/test/common_tests/files/variant_test.cljc
Pablo Alba 571ee80a4b 🐛 Validate and repair variant properties as a set
Validation compared each variant's property names with the first child
of its board, so it missed forbidden characters, too-long text and
repeated names, and never checked labels in deleted components. Repair
cut every variant down to the first child's names, which dropped
values, and left the file invalid.

Validation now checks the members of each board as one set: cleaned
text, and the same names in the same order, in layers-panel order as in
migration 0038. Repeated value combinations stay valid, as the editor
allows them. It also checks boards that exist only in a component's
objects, variant components that belong to no board, and the whole
board when a save touches only one of its members.

Repair writes the aligned properties and labels on each component and
its main. A live variant main outside its board is reported as
:variant-main-outside-board and detached, as the editor does, instead
of turning its parent into a board. :main-instance-not-a-variant now
repairs the main of a deleted component inside its objects.

Moving a shape or component into a board now cuts property values taken
from its name or path to 60 characters, as the schema requires.

Closes #13238

AI-assisted-by: claude-opus-5-5
2026-11-25 12:17:42 +01:00

149 lines
6.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.files.variant-test
(:require
[app.common.files.variant :as cfv]
[app.common.test-helpers.components :as thc]
[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.uuid :as uuid]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
(t/deftest test-collect-variant-properties-for-main
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02))
page (thf/current-page file)
main (ths/get-shape file :m01)
comp (thc/get-component file :c01)
collected (cfv/collect-variant-properties (:data file) (:objects page) main)]
(t/is (= {(:id comp) (:variant-properties comp)} collected))))
(t/deftest test-collect-variant-properties-for-container
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02))
page (thf/current-page file)
container (ths/get-shape file :v01)
c01 (thc/get-component file :c01)
c02 (thc/get-component file :c02)
collected (cfv/collect-variant-properties (:data file) (:objects page) container)]
(t/is (= {(:id c01) (:variant-properties c01)
(:id c02) (:variant-properties c02)}
collected))))
(t/deftest test-collect-variant-properties-for-plain-shape
(let [file (-> (thf/sample-file :file1)
(ths/add-sample-shape :rect1 :type :rect))
page (thf/current-page file)
shape (ths/get-shape file :rect1)
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-layers-panel-order
;; :shapes of the board is [m02 m01], so m01 is the top variant in the
;; layers panel and c01 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' :c01))))
(t/is (= [{:name "Property 1" :value "ab (1)"}]
(:variant-properties (thc/get-component file' :c02))))
(t/is (= "ab (1)" (:variant-name (ths/get-shape file' :m02))))))
(t/deftest test-get-variant-members-props-returns-the-top-member-first
;; :shapes of the board is [m02 m01], so m01 is the top variant.
(let [file (variant-file)
page (thf/current-page file)
container (ths/get-shape file :v01)
members (cfv/get-variant-members-props (:data file) (:objects page) container identity)]
(t/is (= [(thi/id :c01) (thi/id :c02)] (mapv :component-id members)))
(t/is (= (:variant-properties (thc/get-component file :c01))
(:props (first members))))))
(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))))))