From 571ee80a4b35e340743b703ca8e5cde46e2cfe04 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Tue, 10 Nov 2026 19:28:44 +0100 Subject: [PATCH] :bug: 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 --- common/src/app/common/files/repair.cljc | 151 ++- common/src/app/common/files/validate.cljc | 268 ++++- common/src/app/common/files/variant.cljc | 54 +- .../app/common/logic/variant_properties.cljc | 14 +- common/src/app/common/types/variant.cljc | 24 +- .../test/common_tests/files/repair_test.cljc | 1014 ++++++++++++++++- .../test/common_tests/files/variant_test.cljc | 11 + .../common_tests/logic/variants_test.cljc | 23 + .../test/common_tests/types/variant_test.cljc | 18 + common/test/common_tests/variant_test.cljc | 5 + 10 files changed, 1445 insertions(+), 137 deletions(-) diff --git a/common/src/app/common/files/repair.cljc b/common/src/app/common/files/repair.cljc index 2765a1e5fb..f9b944a51d 100644 --- a/common/src/app/common/files/repair.cljc +++ b/common/src/app/common/files/repair.cljc @@ -12,6 +12,7 @@ [app.common.files.changes-builder :as pcb] [app.common.files.helpers :as cfh] [app.common.files.tokens :as cfo] + [app.common.files.variant :as cfv] [app.common.logging :as log] [app.common.path-names :as cpn] [app.common.types.component :as ctk] @@ -712,17 +713,24 @@ (defmethod repair-error :main-instance-not-a-variant [_ {:keys [shape page-id args]} file-data _] - (let [repair-shape + (let [variant-id (:variant-id args) + component (get-in file-data [:components (:component-id args)]) + repair-shape (fn [shape] - (let [variant-id (:variant-id args)] - ;; Set the desired variant-id - (log/debug :hint (str " -> set variant-id to " variant-id)) - (assoc shape :variant-id variant-id)))] + ;; Set the desired variant-id + (log/debug :hint (str " -> set variant-id to " variant-id)) + (assoc shape :variant-id variant-id))] (log/debug :hint "repairing shape :main-instance-not-a-variant" :id (:id shape) :name (:name shape) :page-id page-id) - (-> (pcb/empty-changes nil page-id) - (pcb/with-file-data file-data) - (pcb/update-shapes [(:id shape)] repair-shape)))) + (if (:deleted component) + ;; The main of a deleted component lives in the component's objects + (-> (pcb/empty-changes nil) + (pcb/with-library-data file-data) + (pcb/update-component (:id component) + #(update-in % [:objects (:id shape)] repair-shape))) + (-> (pcb/empty-changes nil page-id) + (pcb/with-file-data file-data) + (pcb/update-shapes [(:id shape)] repair-shape))))) (defmethod repair-error :main-instance-invalid-variant-id [_ {:keys [shape page-id args]} file-data _] @@ -739,32 +747,98 @@ (pcb/with-file-data file-data) (pcb/update-shapes [(:id shape)] repair-shape)))) +(defn- repair-variant-member-changes + [file-data member] + (let [component (:component member) + props (:props member) + variant-name (ctv/properties-to-name props) + changed? (cfv/variant-props-changed? component props) + main-id (:main-instance-id component) + page-id (:main-instance-page component) + deleted? (:deleted component) + shape (cfv/get-variant-main-shape file-data component) + label-differs? (cfv/variant-main-label-changed? file-data component props) + comp-changes + (when (or changed? + (and deleted? label-differs?)) + (log/debug :hint " -> write variant properties" + :component-id (:id component) + :changed changed? + :label-differs label-differs?) + (-> (pcb/empty-changes nil) + (pcb/with-library-data file-data) + (pcb/update-component + (:id component) + (fn [component] + (cond-> component + changed? + (assoc :variant-properties props) + + (and (not deleted?) changed?) + (dissoc :objects) + + (and deleted? (nil? (:objects component))) + (assoc :objects {}) + + (and deleted? + label-differs? + (map? (get-in component [:objects main-id]))) + (assoc-in [:objects main-id :variant-name] variant-name)))))) + shape-changes + (when (and (not deleted?) label-differs? (map? shape)) + (log/debug :hint " -> set variant-name" :variant-name variant-name :page-id page-id) + (-> (pcb/empty-changes nil page-id) + (pcb/with-file-data file-data) + (pcb/update-shapes [main-id] + (fn [shape] + (assoc shape :variant-name variant-name)))))] + (cond-> (or comp-changes (pcb/empty-changes nil)) + shape-changes (pcb/concat-changes shape-changes)))) + +(defn- repair-variant-property-set + [file-data shape page-id owner-id] + (let [objects (if (some? owner-id) + (get-in file-data [:components owner-id :objects]) + (get-in file-data [:pages-index page-id :objects])) + ;; An earlier repair of this pass may have changed the board + board (get objects (:id shape) shape) + members (cfv/get-variant-members-props file-data objects board ctv/align-properties-set)] + (reduce (fn [changes member] + (pcb/concat-changes changes + (repair-variant-member-changes file-data member))) + (pcb/empty-changes nil) + members))) + +(defn- repair-solo-variant-properties + [file-data component] + (repair-variant-member-changes + file-data + {:component component + :props (ctv/sanitize-properties (or (:variant-properties component) []))})) + (defmethod repair-error :invalid-variant-properties [_ {:keys [shape page-id args]} file-data _] - (let [prop-names (:prop-names args) + (log/debug :hint "repairing :invalid-variant-properties" :id (:id shape) :page-id page-id) + (if (ctk/is-variant-container? shape) + (repair-variant-property-set file-data shape page-id (:component-id args)) + (repair-solo-variant-properties file-data + (get-in file-data [:components (:id shape)])))) - component (get-in file-data [:components (:component-id shape)]) - prop-values (into {} (map (juxt :name :value)) (:variant-properties component)) - properties' (mapv (fn [name] {:name name :value (get prop-values name "")}) prop-names) - variant-name (ctv/properties-to-name properties') - - repair-component - (fn [component] - ;; Rebuild component properties, removing any extra ones and adding missing ones with empty value - (log/debug :hint " -> rebuild properties" :component-id (:id component) :prop-names (str prop-names)) - (assoc component :variant-properties properties')) - - repair-shape - (fn [shape] - (log/debug :hint " -> set variant-name" :variant-name variant-name) - (assoc shape :variant-name variant-name))] - - (log/debug :hint "repairing shape :invalid-variant-properties" :id (:id shape) :name (:name shape) :page-id page-id) +(defmethod repair-error :variant-main-outside-board + [_ {:keys [args]} file-data _] + ;; Detach the variant, as the editor does when a variant leaves its board. + ;; The error comes from the main shape or from its component. + (let [component (get-in file-data [:components (:component-id args)]) + page-id (:main-instance-page component) + main (cfv/get-variant-main-shape file-data component) + props (ctv/sanitize-properties (or (:variant-properties component) [])) + new-name (ctv/variant-name-to-name + (assoc main :variant-name (ctv/properties-to-name props)))] + (log/debug :hint "repairing component :variant-main-outside-board" :id (:id component) :page-id page-id) (-> (pcb/empty-changes nil page-id) (pcb/with-file-data file-data) (pcb/with-library-data file-data) - (pcb/update-component (:component-id shape) repair-component) - (pcb/update-shapes [(:id shape)] repair-shape)))) + (cfv/generate-detach-variant main new-name)))) (defmethod repair-error :variant-not-main [_ {:keys [shape page-id]} file-data _] @@ -835,10 +909,29 @@ (pcb/with-file-data file-data) (pcb/update-shapes [(:id shape)] repair-fn)))) +(defn- get-rewritten-variant-props + "Props the property repair would store for this main. On its board, + the props of its member in the board's set, or nil when it is not a + qualifying member, so the caller keeps the stored vector. Elsewhere, + its own cleaned props. Reads the main as earlier repairs of this pass + left it." + [file-data shape component page-id] + (let [objects (get-in file-data [:pages-index page-id :objects]) + shape (get objects (:id shape) shape) + parent (get objects (:parent-id shape))] + (if (and (ctk/is-variant-container? parent) + (= (:variant-id shape) (:id parent))) + (some (fn [member] + (when (= (:id component) (:component-id member)) + (:props member))) + (cfv/get-variant-members-props file-data objects parent ctv/align-properties-set)) + (ctv/sanitize-properties (or (:variant-properties component) []))))) + (defmethod repair-error :variant-main-bad-variant-name [_ {:keys [shape page-id]} file-data _] (let [component (get-in file-data [:components (:component-id shape)]) - variant-name (ctv/properties-to-name (:variant-properties component)) + props (get-rewritten-variant-props file-data shape component page-id) + variant-name (ctv/properties-to-name (or props (:variant-properties component))) repair-fn (fn [shape] (log/debug :hint " -> set :variant-name" :variant-name variant-name) diff --git a/common/src/app/common/files/validate.cljc b/common/src/app/common/files/validate.cljc index 78f300b738..aeaa1a762d 100644 --- a/common/src/app/common/files/validate.cljc +++ b/common/src/app/common/files/validate.cljc @@ -76,6 +76,7 @@ :variant-main-bad-variant-name :variant-component-bad-name :variant-component-bad-id + :variant-main-outside-board :missing-tokens-status}) (def ^:private schema:error @@ -109,6 +110,11 @@ ;; a page's validation pass. (def ^:dynamic ^:private *children-sets* nil) +;; Variant-component ids already checked in this pass: qualifying members +;; of a walked board, and components whose main the page walk reported +;; outside its board. `check-component` skips the solo check for them. +(def ^:dynamic ^:private *checked-variant-component-ids* nil) + (defn- build-children-sets "Return a {parent-id → #{child-ids}} map built from `objects` in a single `reduce-kv` pass. Only shapes that have at least one child get an entry." @@ -576,10 +582,51 @@ (check-empty-swap-slot shape file page) (run! #(check-shape % file page libraries :context :not-component) (:shapes shape))) +(defn- record-checked-variant-components + [component-ids] + (when-let [ids *checked-variant-component-ids*] + (vswap! ids into component-ids))) + +(defn- variant-member-props-changed? + [member] + (cfv/variant-props-changed? (:component member) (:props member))) + +(defn- variant-member-label-changed? + [data member] + (cfv/variant-main-label-changed? data (:component member) (:props member))) + +(defn- variant-properties-need-repair? + [data members compare-label] + (boolean + (some (fn [member] + (or (variant-member-props-changed? member) + (and compare-label + (variant-member-label-changed? data member)))) + members))) + +(defn- check-variant-property-set + "Check the members of the board `shape` as one set. Skip a board that + was already checked in this pass: its members are recorded, and a + member belongs to one board only." + [shape file page objects data & {:keys [compare-label owner-id]}] + (let [members (cfv/get-variant-members-props data objects shape ctv/align-properties-set) + checked (some-> *checked-variant-component-ids* deref)] + (when-not (some #(contains? checked (:component-id %)) members) + (record-checked-variant-components (map :component-id members)) + (when (variant-properties-need-repair? data members compare-label) + (if (some? owner-id) + (report-error :invalid-variant-properties + (str/ffmt "Variant container % has invalid properties" (:id shape)) + shape file page + :component-id owner-id) + (report-error :invalid-variant-properties + (str/ffmt "Variant container % has invalid properties" (:id shape)) + shape file page)))))) + (defn- check-variant-container "Shape is a variant container, so: -all its children should be variants with variant-id equals to the shape-id - -all the components should have the same properties + -the stored properties should match the shared rewrite -the name of the container should be normalized for paths (separator / with one space at each side)." [shape file page] (let [shape-id (:id shape) @@ -587,8 +634,6 @@ shapes (:shapes shape) objects (:objects page) file-data (:data file) - first-child (get objects (first shapes)) - prop-names (cfv/extract-properties-names first-child file-data) clean-path (cpn/clean-path container-name) clean-path (if (str/blank? clean-path) "(empty)" ;; this may occur if the name contains only separators @@ -605,18 +650,66 @@ (str/ffmt "Main instance shape % should be a variant" (:id child)) child file page :variant-id shape-id) - (do - (when (not= (:variant-id child) shape-id) - (report-error :main-instance-invalid-variant-id - (str/ffmt "Main instance in variant % should have the variant-id of the container but has %" (:id child) (:variant-id child)) - child file page - :variant-id shape-id)) - (when (not= prop-names (cfv/extract-properties-names child file-data)) - (report-error :invalid-variant-properties - (str/ffmt "Variant % has invalid properties %" (:id child) (vec prop-names)) - child file page - :prop-names prop-names)))))) - shapes))) + (when (not= (:variant-id child) shape-id) + (report-error :main-instance-invalid-variant-id + (str/ffmt "Main instance in variant % should have the variant-id of the container but has %" (:id child) (:variant-id child)) + child file page + :variant-id shape-id))))) + shapes) + (check-variant-property-set shape file page objects file-data + :compare-label false))) + +(defn- parent-is-board? + "True when `parent` is the board of the variant main `shape`: a variant + container, or the shape whose id is the shape's :variant-id." + [shape parent] + (and (some? parent) + (or (ctk/is-variant-container? parent) + (= (:variant-id shape) (:id parent))))) + +(defn- find-variant-board + "Find the board of `component` by its :variant-id: on the page of its + main, or else in some component's `:objects`. Only a shape flagged as a + variant container counts, as in the page walk and migration 0038. Return {:objects + :container :page} or {:objects :container :owner-id}." + [data component] + (let [board-id (:variant-id component) + page (get-in data [:pages-index (:main-instance-page component)]) + board (get-in page [:objects board-id])] + (if (ctk/is-variant-container? board) + {:objects (:objects page) :container board :page page} + (some (fn [other] + (let [board (get-in other [:objects board-id])] + (when (ctk/is-variant-container? board) + {:objects (:objects other) + :container board + :owner-id (:id other)}))) + (vals (:components data)))))) + +(defn- variant-board-member? + [data {:keys [objects container]} component] + (some #(= (:id component) (:component-id %)) + (cfv/qualifying-variant-members data objects container))) + +(defn- variant-component-checked? + "True when `component` was already checked in this pass, or is a + qualifying member of its board." + [data component] + (or (contains? (some-> *checked-variant-component-ids* deref) (:id component)) + (when-let [board (find-variant-board data component)] + (variant-board-member? data board component)))) + +(defn- main-outside-board? + "True when `component` is live and its main is on its page, is a + variant, and sits under a parent that is not a board. A main that lost + its :variant-id is left to :main-instance-not-a-variant first." + [data component] + (when-not (:deleted component) + (when-let [main (cfv/get-variant-main-shape data component)] + (when (ctk/is-variant? main) + (let [parent (get-in data [:pages-index (:main-instance-page component) + :objects (:parent-id main)])] + (not (parent-is-board? main parent))))))) (defn- renamed-container? [container] @@ -626,44 +719,61 @@ (defn- check-variant "Shape is a variant, so -it should be a main component - -its parent should be a variant-container + -the parent is the board when it is a variant container, or when + the shape's :variant-id equals the parent's id + -:parent-not-variant, :variant-main-bad-name, + :variant-component-bad-name, and :variant-component-bad-id + report only then + -otherwise, when it is the main of a live component that belongs + to no board, :variant-main-outside-board reports it -its variant-name is derived from the properties -its name should be the same as its parent's " [shape file page] - (let [parent (ctst/get-shape page (:parent-id shape)) - component (ctkl/get-component (:data file) (:component-id shape) true) - variant-name (ctv/properties-to-name (:variant-properties component))] + (let [parent (ctst/get-shape page (:parent-id shape)) + component (ctkl/get-component (:data file) (:component-id shape) true) + variant-name (ctv/properties-to-name (:variant-properties component)) + on-board? (parent-is-board? shape parent)] (when-not (ctk/main-instance? shape) (report-error :variant-not-main (str/ffmt "Variant % is not a main instance" (:id shape)) shape file page)) - (when-not (ctk/is-variant-container? parent) - (report-error :parent-not-variant - (str/ffmt "Variant % has an invalid parent" (:id shape)) - shape file page)) (when-not (= variant-name (:variant-name shape)) (report-error :variant-main-bad-variant-name (str/ffmt "Variant % has an invalid variant-name" (:id shape)) shape file page :variant-name variant-name)) - ;; If the container is going to be renamed all main and components will be renamed, too - (when-not (renamed-container? parent) - (when-not (= (:name parent) (:name shape)) - (report-error :variant-main-bad-name - (str/ffmt "Main instance inside variant % has an invalid name" (:id shape)) + (when on-board? + (when-not (ctk/is-variant-container? parent) + (report-error :parent-not-variant + (str/ffmt "Variant % has an invalid parent" (:id shape)) + shape file page)) + ;; If the container is going to be renamed all main and components will be renamed, too + (when-not (renamed-container? parent) + (when-not (= (:name parent) (:name shape)) + (report-error :variant-main-bad-name + (str/ffmt "Main instance inside variant % has an invalid name" (:id shape)) + shape file page + :variant-name (:name parent))) + (when-not (= (:name parent) (cpn/merge-path-item (:path component) (:name component))) + (report-error :variant-component-bad-name + (str/ffmt "Component % has an invalid name" (:id shape)) + shape file page + :variant-container-name (:name parent)))) + (when-not (= (:variant-id component) (:variant-id shape)) + (report-error :variant-component-bad-id + (str/ffmt "Variant % has a :variant-id that does not point to the container" (:id shape)) shape file page - :variant-name (:name parent))) - (when-not (= (:name parent) (cpn/merge-path-item (:path component) (:name component))) - (report-error :variant-component-bad-name - (str/ffmt "Component % has an invalid name" (:id shape)) - shape file page - :variant-container-name (:name parent)))) - (when-not (= (:variant-id component) (:variant-id shape)) - (report-error :variant-component-bad-id - (str/ffmt "Variant % has a :variant-id that does not point to the container" (:id shape)) + :variant-id (:parent-id shape)))) + (when (and (not on-board?) + (= (:id shape) (:main-instance-id component)) + (main-outside-board? (:data file) component) + (not (variant-component-checked? (:data file) component))) + (record-checked-variant-components [(:id component)]) + (report-error :variant-main-outside-board + (str/ffmt "Variant % is not on a variant board" (:id shape)) shape file page - :variant-id (:parent-id shape))))) + :component-id (:id component))))) (defn- check-shape "Validate referential integrity and semantic coherence of @@ -777,9 +887,8 @@ component file nil :cycles-ids cycles-ids)))) (defn- check-variant-component - "Component is a variant, so: - -Its main should be a variant - -It should have at least one variant property" + "Component is a variant, so its main should be a variant. A deleted + component's main is the one inside its `:objects`." [component file] (let [component-page (ctf/get-component-page (:data file) component) main-instance (if (:deleted component) @@ -790,7 +899,8 @@ (report-error :main-instance-not-a-variant (str/ffmt "Main instance shape % should be a variant" (:id main-instance)) main-instance file component-page - :variant-id (:variant-id component))))) + :variant-id (:variant-id component) + :component-id (:id component))))) (defn- check-main-inside-main [component file] @@ -813,6 +923,65 @@ "A non-deleted component cannot have shapes inside" component file nil))) +(defn- board-on-members-page? + "True when the board `shape`, found in `objects`, is also a shape on the + page of one of its qualifying members' mains. The page walk checks + that board. Only those pages are read, never every page of the file." + [data objects shape] + (some (fn [{:keys [component]}] + (some? (get-in data [:pages-index (:main-instance-page component) + :objects (:id shape)]))) + (cfv/qualifying-variant-members data objects shape))) + +(defn- check-objects-only-variant-boards + "Walk variant boards that exist only in this component's `:objects`." + [component file] + (when-let [objects (:objects component)] + (when-let [boards (seq (filter ctk/is-variant-container? (vals objects)))] + (let [data (:data file)] + (doseq [shape boards] + (when-not (board-on-members-page? data objects shape) + (check-variant-property-set shape file nil objects data + :compare-label true + :owner-id (:id component)))))))) + +(defn- check-solo-variant-component + "A variant component that belongs to no board. A live one whose main + sits under a parent that is not a board is reported so the repair + detaches it. Otherwise, its properties are cleaned on their own and + its main's label must match them." + [component file] + (let [data (:data file)] + (if (main-outside-board? data component) + (report-error :variant-main-outside-board + (str/ffmt "Variant component % is not on a variant board" (:id component)) + component file nil + :component-id (:id component)) + (let [member {:component component + :props (ctv/sanitize-properties + (or (:variant-properties component) []))}] + (when (or (variant-member-props-changed? member) + (variant-member-label-changed? data member)) + (report-error :invalid-variant-properties + (str/ffmt "Variant component % has invalid properties" (:id component)) + component file nil)))))) + +(defn- check-variant-component-properties + "Check the properties of a variant component that no board check of this + pass covered. A qualifying member is checked with its whole board, so a + save that touches only the component still checks the set. The board + is not walked in this pass, so labels are compared too. Any other + variant component is checked on its own." + [component file] + (let [data (:data file)] + (when-not (contains? (some-> *checked-variant-component-ids* deref) (:id component)) + (let [board (find-variant-board data component)] + (if (and (some? board) (variant-board-member? data board component)) + (check-variant-property-set (:container board) file (:page board) (:objects board) data + :compare-label true + :owner-id (:owner-id board)) + (check-solo-variant-component component file)))))) + (defn- check-component "Validate semantic coherence of a component. Report all errors found." [component file] @@ -828,7 +997,8 @@ (check-ref-cycles component file)) (when (ctk/is-variant? component) - (check-variant-component component file))) + (check-variant-component component file) + (check-variant-component-properties component file))) (defn- get-orphan-shapes "Return the ids of shapes unreachable from the root traversal: shapes @@ -875,7 +1045,8 @@ Return a list of errors or `nil`" [{:keys [data features] :as file} libraries] (when (contains? features "components/v2") - (binding [*errors* (volatile! [])] + (binding [*errors* (volatile! []) + *checked-variant-component-ids* (volatile! #{})] ;; `reduce-kv` is used throughout this function (and `validate-file-affected`) ;; instead of `(run! f (vals m))` or `(doseq [[k v] m])` because persistent @@ -898,7 +1069,8 @@ (reduce-kv (fn [_ _ component] - (check-component component file)) + (check-component component file) + (check-objects-only-variant-boards component file)) nil (:components data)) @@ -986,7 +1158,8 @@ [{:keys [data features] :as file} libraries changes] (when (contains? features "components/v2") (let [{:keys [page-ids component-ids]} (extract-affected-ids changes)] - (binding [*errors* (volatile! [])] + (binding [*errors* (volatile! []) + *checked-variant-component-ids* (volatile! #{})] (reduce-kv (fn [_ page-id page] @@ -1006,7 +1179,8 @@ (reduce-kv (fn [_ id component] (when (contains? component-ids id) - (check-component component file))) + (check-component component file) + (check-objects-only-variant-boards component file))) nil (:components data)) diff --git a/common/src/app/common/files/variant.cljc b/common/src/app/common/files/variant.cljc index e4fcfcae6d..aa8dccd5fe 100644 --- a/common/src/app/common/files/variant.cljc +++ b/common/src/app/common/files/variant.cljc @@ -6,6 +6,8 @@ (ns app.common.files.variant (:require [app.common.data.macros :as dm] + [app.common.files.changes-builder :as pcb] + [app.common.path-names :as cpn] [app.common.types.component :as ctc] [app.common.types.components-list :as ctcl] [app.common.types.variant :as ctv])) @@ -81,6 +83,22 @@ (and (seq shapes) (not= (:main-instance-id component) (last shapes))))) +(defn generate-detach-variant + "Add to `changes` the edits that turn the variant main `shape` into a + plain component main named `new-name`: drop the variant fields from the + shape and its component, and take the component's path and name from + `new-name`." + [changes shape new-name] + (let [[cpath cname] (cpn/split-group-name new-name)] + (-> changes + (pcb/update-component (:component-id shape) + #(-> (dissoc % :variant-id :variant-properties) + (assoc :name cname :path cpath)) + {:apply-changes-local-library? true}) + (pcb/update-shapes [(:id shape)] + #(-> (dissoc % :variant-id :variant-name) + (assoc :name new-name)))))) + (defn get-primary-variant [data component] (let [page-id (:main-instance-page component) @@ -133,6 +151,13 @@ (not= (ctv/properties-to-name props) (:variant-name shape))))) +(defn variant-props-changed? + "True when the stored `:variant-properties` of `component` differ from + `props`. A missing property list counts as empty." + [component props] + (not= (vec (or (:variant-properties component) [])) + (vec props))) + (defn- qualifying-variant-member [data objects container child-id] (when-let [child (get objects child-id)] @@ -168,21 +193,26 @@ (ctv/properties-to-name props)) data))) +(defn get-variant-members-props + "Return the qualifying members of `container` in layers-panel order + (top first, the reverse of `:shapes`). Each member also gets `:props`, + its vector in the result of `f` over all their property vectors. `f` + takes and returns a vector of property vectors." + [data objects container f] + (let [members (vec (rseq (qualifying-variant-members data objects container))) + props-seq (f (mapv #(vec (or (get-in % [:component :variant-properties]) [])) + members))] + (mapv #(assoc %1 :props %2) members props-seq))) + (defn update-variant-container-properties "Apply `f` to the property vectors of the members of `container`, in - layers-panel order (top first, the reverse of `:shapes`), and write each - result with `update-variant-properties`. `f` takes and returns a vector - of property vectors." + layers-panel order, and write each result with + `update-variant-properties`. See `get-variant-members-props`." [data objects container f] - (let [components (->> (qualifying-variant-members data objects container) - (map :component) - (reverse) - (vec)) - 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)))) + (reduce (fn [data {:keys [component props]}] + (update-variant-properties data component (constantly props))) + data + (get-variant-members-props data objects container f))) (defn sanitize-variant-component "Sanitize the properties of one variant component on its own." diff --git a/common/src/app/common/logic/variant_properties.cljc b/common/src/app/common/logic/variant_properties.cljc index 7938cdbd06..05310e49e0 100644 --- a/common/src/app/common/logic/variant_properties.cljc +++ b/common/src/app/common/logic/variant_properties.cljc @@ -166,17 +166,7 @@ (defn- generate-make-shape-no-variant [changes shape] - (let [new-name (ctv/variant-name-to-name shape) - [cpath cname] (cpn/split-group-name new-name)] - (-> changes - (pcb/update-component (:component-id shape) - #(-> (dissoc % :variant-id :variant-properties) - (assoc :name cname - :path cpath)) - {:apply-changes-local-library? true}) - (pcb/update-shapes [(:id shape)] - #(-> (dissoc % :variant-id :variant-name) - (assoc :name new-name)))))) + (cfv/generate-detach-variant changes shape (ctv/variant-name-to-name shape))) (defn generate-make-shapes-no-variant [changes shapes] @@ -196,7 +186,7 @@ props (ctv/add-new-props props (repeat new-props ""))] (if add-name? - (ctv/add-new-prop props (or (ctv/sanitize-property-text (:name component)) "")) + (ctv/add-new-prop props (ctv/sanitize-property-value (:name component))) props))) (defn- create-new-properties-from-non-variant diff --git a/common/src/app/common/types/variant.cljc b/common/src/app/common/types/variant.cljc index 50200e5e74..27cb004456 100644 --- a/common/src/app/common/types/variant.cljc +++ b/common/src/app/common/types/variant.cljc @@ -134,6 +134,8 @@ :value v}))] (into props xf values))) +(declare sanitize-property-value) + (defn path-to-properties "From a list of properties and a name with path, assign each token of the path as value of a different property" @@ -141,7 +143,7 @@ (path-to-properties path properties 0)) ([path properties min-props] (let [cpath (->> (cpn/split-path path) - (mapv #(or (sanitize-property-text %) ""))) + (mapv sanitize-property-value)) total-props (max (count cpath) min-props) assigned (mapv #(assoc % :value (nth cpath %2 "")) properties (range)) ;; Add empty strings to the end of cpath to reach the minimum number of properties @@ -341,19 +343,23 @@ taken (map #(nth % idx "") seen)] (update-in props [idx :value] #(unique-property-text taken %)))))) -(defn sanitize-properties-set +(defn align-properties-set "Sanitize the property vectors of all the variants of one set, given in layers-panel order (top first). Every vector gets the same names in the - same order, and no two vectors share a value combination. Earlier - vectors keep their values." + same order. Two vectors may keep the same value combination." [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))) + (mapv #(align-properties % names) cleaned))) + +(defn sanitize-properties-set + "Like `align-properties-set`, and also no two vectors share a value + combination. Earlier vectors keep their values." + [props-seq] + (reduce (fn [acc props] + (conj acc (unique-values-tuple props (map #(mapv :value %) acc)))) + [] + (align-properties-set props-seq))) (defn find-index-for-property-name "Finds the index of a name in a property map" diff --git a/common/test/common_tests/files/repair_test.cljc b/common/test/common_tests/files/repair_test.cljc index 9cce68647e..f4214a7aef 100644 --- a/common/test/common_tests/files/repair_test.cljc +++ b/common/test/common_tests/files/repair_test.cljc @@ -12,8 +12,11 @@ generate accurate errors, and that the repair functions return the file to a stable state." (:require + [app.common.files.changes-builder :as pcb] + [app.common.files.migrations :as cfm] [app.common.files.repair :as cfr] [app.common.files.validate :as cfv] + [app.common.logic.variant-properties :as clvp] [app.common.test-helpers.components :as thc] [app.common.test-helpers.compositions :as tho] [app.common.test-helpers.files :as thf] @@ -25,6 +28,19 @@ (t/use-fixtures :each thi/test-fixture) +(defn- repair-until-valid + [file] + (cfr/repair-file-until-valid + file {} + (fn [file _] (cfv/validate-file file {})))) + +(defn- repair-until-valid-affected + [file changes] + (cfr/repair-file-until-valid + file {} + (fn [file applied] + (cfv/validate-file-affected file {} (into applied changes))))) + (t/deftest repair-invalid-main-instance-name (t/testing "detect and repair a main instance whose name doesn't match the component's name" (let [file (-> (thf/sample-file :file1 :page-label :page1) @@ -84,7 +100,36 @@ (t/is (= :main-instance-not-a-variant (:code (first errors)))) (t/is (nil? errors')) - (t/is (= (thi/id :variant1) (:variant-id root1')))))) + (t/is (= (thi/id :variant1) (:variant-id root1'))))) + + (t/testing "detect and repair a deleted variant component whose main in its objects is not a variant" + (let [deleted-id (uuid/next) + main-id (uuid/next) + variant-id (uuid/next) + file (thf/sample-file :file1 :page-label :page1) + page-id (thf/current-page-id file) + file (update file :data + (fn [data] + (assoc-in data [:components deleted-id] + {:id deleted-id + :name "Legacy" + :path "" + :deleted true + :variant-id variant-id + :main-instance-id main-id + :main-instance-page page-id + :variant-properties [{:name "Size" :value "big"}] + :objects {main-id {:id main-id + :type :frame + :variant-name "big"}}}))) + errors (cfv/validate-file file {}) + result (repair-until-valid file) + file' (:file result)] + + (t/is (= [:main-instance-not-a-variant] (mapv :code errors))) + (t/is (= variant-id (get-in file' [:data :components deleted-id :objects main-id :variant-id]))) + (t/is (nil? (ths/get-shape-by-id file' main-id))) + (t/is (nil? (:errors result)))))) (t/deftest repair-invalid-variant-id-variant-component-bad-id (t/testing "detect and repair a variant main whose variant id does not match the container's id" @@ -129,7 +174,7 @@ (let [file (-> (thf/sample-file :file1 :page-label :page1) (thv/add-variant :variant1 :component1 :root1 :component2 :root2) ;; Component1 has ["Property 1", "Property 2"], component2 gets ["Property 1", "Property 3"] - ;; This breaks validation: prop-names mismatch (missing "Property 2", extra "Property 3") + ;; The shared list keeps every name, in layers-panel order (root1 is the top variant). (thc/update-component :component1 {:variant-properties [{:name "Property 1" :value "Value1"} {:name "Property 2" :value "ValueA"}]}) (thc/update-component :component2 {:variant-properties [{:name "Property 1" :value "Value2"} @@ -138,8 +183,8 @@ (ths/update-shape :root2 :variant-name "Value2, ValueB")) errors (cfv/validate-file file {}) - changes (cfr/repair-file file {} errors) - file' (thf/apply-changes file {:redo-changes changes} :validate? false) + result (repair-until-valid file) + file' (:file result) errors' (cfv/validate-file file' {}) comp1' (thc/get-component file' :component1) @@ -151,34 +196,32 @@ (t/is (= :invalid-variant-properties (:code (first errors)))) (t/is (nil? errors')) + (t/is (nil? (:errors result))) - ;; After repair, component1's properties are rebuilt to match component2's property names - ;; (the first child in the variant container is root2, so prop-names come from component2) - ;; "Property 1" keeps its value, "Property 3" is added with empty value, "Property 2" is removed - (t/is (= [{:name "Property 1" :value "Value1"} - {:name "Property 3" :value ""}] - (:variant-properties comp1'))) - - (t/is (= "Value1" (:variant-name root1'))) - - ;; Component2 is unchanged (it was the reference for the property names) + ;; :shapes is [root2 root1], so root1 is the top variant and the + ;; shared list is Property 1, Property 2, Property 3 (t/is (= [{:name "Property 1" :value "Value2"} + {:name "Property 2" :value ""} {:name "Property 3" :value "ValueB"}] (:variant-properties comp2'))) - (t/is (= "Value2, ValueB" (:variant-name root2')))))) + (t/is (= [{:name "Property 1" :value "Value1"} + {:name "Property 2" :value "ValueA"} + {:name "Property 3" :value ""}] + (:variant-properties comp1'))) + + (t/is (= "Value2, ValueB" (:variant-name root2'))) + (t/is (= "Value1, ValueA" (:variant-name root1')))))) (t/deftest repair-two-errors-on-one-variant-component-in-one-pass (t/testing "the properties fix and the variant id fix of the same component both survive one repair pass" (let [file (-> (thf/sample-file :file1 :page-label :page1) (thv/add-variant :variant1 :component1 :root1 :component2 :root2) - ;; The first child of the container is root2, so component2 sets the - ;; property names. Component1 has an extra property and a variant id - ;; that does not point to the container. + ;; Component1 has a variant id that does not point to the container, + ;; so it belongs to no board and its dirty text is checked on its own. (thc/update-component :component1 {:variant-id (uuid/next) - :variant-properties [{:name "Property 1" :value "Value1"} - {:name "Extra" :value "x"}]}) - (ths/update-shape :root1 :variant-name "Value1, x")) + :variant-properties [{:name "Property 1" :value "a=b"}]}) + (ths/update-shape :root1 :variant-name "a=b")) errors (cfv/validate-file file {}) changes (cfr/repair-file file {} errors) @@ -187,11 +230,11 @@ comp1' (thc/get-component file' :component1)] - (t/is (= [:invalid-variant-properties :variant-component-bad-id] (mapv :code errors))) + (t/is (= [:variant-component-bad-id :invalid-variant-properties] (mapv :code errors))) (t/is (nil? errors')) (t/is (= (thi/id :variant1) (:variant-id comp1'))) - (t/is (= [{:name "Property 1" :value "Value1"}] (:variant-properties comp1')))))) + (t/is (= [{:name "Property 1" :value "ab"}] (:variant-properties comp1')))))) (t/deftest repair-two-errors-on-one-component-in-any-order (let [stray-id (uuid/next) @@ -215,6 +258,32 @@ (t/is (not (contains? comp1' :objects))) (t/is (= (thi/id :variant1) (:variant-id comp1')))))))) +(t/deftest repair-dirty-properties-and-stale-label-in-any-order + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component2 {:variant-properties [{:name "Property 1" :value "a=b"}]}) + (ths/update-shape :root2 :variant-name "stale")) + errors (cfv/validate-file file {})] + + (t/is (= [:invalid-variant-properties :variant-main-bad-variant-name] + (mapv :code errors))) + + (doseq [errors [errors (reverse errors)]] + (t/testing (str "both repairs agree with the errors in order " (mapv :code errors)) + (let [changes (cfr/repair-file file {} errors) + file' (thf/apply-changes file {:redo-changes changes} :validate? false)] + (t/is (nil? (cfv/validate-file file' {}))) + (t/is (= "ab" (:variant-name (ths/get-shape file' :root2))))))) + + ;; The property repair rewrites the label in either order, so only the + ;; label repair on its own shows that it writes the label of the + ;; cleaned properties. + (t/testing "the label repair alone writes the label of the cleaned properties" + (let [label-errors (filterv #(= :variant-main-bad-variant-name (:code %)) errors) + changes (cfr/repair-file file {} label-errors) + file' (thf/apply-changes file {:redo-changes changes} :validate? false)] + (t/is (= "ab" (:variant-name (ths/get-shape file' :root2)))))))) + (t/deftest repair-errors-on-shapes-removed-by-an-earlier-repair (t/testing "repairs on shapes that an earlier repair of the same pass removed change nothing" (let [file (-> (thf/sample-file :file1 :page-label :page1) @@ -286,11 +355,10 @@ bad-shape' (ths/get-shape file' :bad-shape) bad-child' (ths/get-shape file' :bad-child)] - (t/is (= 4 (count errors))) ;; The bad container also triggers other errors - (t/is (= :invalid-variant-properties (:code (nth errors 0)))) - (t/is (= :variant-not-main (:code (nth errors 1)))) - (t/is (= :variant-component-bad-name (:code (nth errors 2)))) - (t/is (= :variant-component-bad-id (:code (nth errors 3)))) + (t/is (= 3 (count errors))) ;; The bad shape is not a qualifying member + (t/is (= :variant-not-main (:code (nth errors 0)))) + (t/is (= :variant-component-bad-name (:code (nth errors 1)))) + (t/is (= :variant-component-bad-id (:code (nth errors 2)))) (t/is (nil? errors')) (t/is (nil? bad-shape')) @@ -725,3 +793,893 @@ (t/is (nil? (:errors result))) (t/is (nil? (:component-id copy1'))) (t/is (nil? (:shape-ref copy1')))))) + +(defn- add-deleted-objects-only-board + [file props2 props1 & {:keys [label2 label1] + :or {label2 "stale" label1 "stale"}}] + (let [owner-id (uuid/next) + board-id (uuid/next) + root2-id (uuid/next) + root1-id (uuid/next) + comp2-id (uuid/next) + comp1-id (uuid/next) + page-id (:id (thf/current-page file)) + member (fn [id root-id props label] + {:id id + :name "Member" + :path "" + :deleted true + :variant-id board-id + :main-instance-id root-id + :main-instance-page page-id + :variant-properties props + :objects {root-id {:id root-id + :type :frame + :variant-id board-id + :variant-name label}}})] + {:file (-> file + (update :data + (fn [data] + (-> data + (assoc-in [:components owner-id] + {:id owner-id + :name "Owner" + :path "" + :deleted true + :main-instance-id board-id + :main-instance-page page-id + :objects {board-id {:id board-id + :type :frame + :name "Board" + :is-variant-container true + :shapes [root2-id root1-id]} + root2-id {:id root2-id + :type :frame + :parent-id board-id + :variant-id board-id + :component-id comp2-id + :main-instance true + :variant-name label2} + root1-id {:id root1-id + :type :frame + :parent-id board-id + :variant-id board-id + :component-id comp1-id + :main-instance true + :variant-name label1}}}) + (assoc-in [:components comp2-id] (member comp2-id root2-id props2 label2)) + (assoc-in [:components comp1-id] (member comp1-id root1-id props1 label1)))))) + :owner-id owner-id + :board-id board-id + :comp2-id comp2-id + :comp1-id comp1-id + :root2-id root2-id + :root1-id root1-id})) + +(t/deftest repair-invalid-variant-properties-dirty-matching-text + (t/testing "a board whose children both store dirty matching text is reported and repaired" + (let [props [{:name "Size=Big" :value "a=b," :hidden true}] + file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component2 {:variant-properties props}) + (thc/update-component :component1 {:variant-properties [{:name "Size=Big" :value "other" :hidden true}]})) + errors (cfv/validate-file file {}) + result (repair-until-valid file) + file' (:file result) + result2 (repair-until-valid file') + comp2' (thc/get-component file' :component2) + root2' (ths/get-shape file' :root2)] + + (t/is (= 1 (count (filter #(= :invalid-variant-properties (:code %)) errors)))) + (t/is (= [{:name "SizeBig" :value "ab" :hidden true}] + (:variant-properties comp2'))) + (t/is (true? (:hidden (first (:variant-properties comp2'))))) + (t/is (= "ab" (:variant-name root2'))) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2))))))) + +(t/deftest repair-invalid-variant-properties-one-pass-keeps-sanitized-label + (t/testing "one repair-file call keeps a label that already matches the sanitized value" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component2 {:variant-properties [{:name "Property 1" :value "a=b,"}]}) + (ths/update-shape :root2 :variant-name "ab")) + errors (cfv/validate-file file {}) + changes (cfr/repair-file file {} errors) + file' (thf/apply-changes file {:redo-changes changes} :validate? false) + comp2' (thc/get-component file' :component2) + root2' (ths/get-shape file' :root2)] + + (t/is (= "ab" (-> comp2' :variant-properties first :value))) + (t/is (= "ab" (:variant-name root2')))))) + +(t/deftest repair-invalid-variant-properties-empty-duplicate-value + (t/testing "two values that clean to empty both stay \"\"" + (let [props [{:name "Property 1" :value "=,="}] + file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component2 {:variant-properties props}) + (thc/update-component :component1 {:variant-properties props})) + result (repair-until-valid file) + file' (:file result) + result2 (repair-until-valid file') + comp2' (thc/get-component file' :component2) + comp1' (thc/get-component file' :component1) + root1' (ths/get-shape file' :root1)] + + (t/is (= "" (-> comp2' :variant-properties first :value))) + (t/is (= "" (-> comp1' :variant-properties first :value))) + (t/is (= "" (:variant-name root1'))) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2))))))) + +(t/deftest repair-invalid-variant-properties-long-duplicate-name + (t/testing "two 60-character copies of one name stay distinct and keep the value" + (let [long-name (apply str (repeat 60 "n")) + file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component2 {:variant-properties [{:name long-name :value "other"} + {:name long-name :value "kept"}]}) + (thc/update-component :component1 {:variant-properties [{:name long-name :value "x"} + {:name long-name :value "y"}]})) + result (repair-until-valid file) + file' (:file result) + props' (:variant-properties (thc/get-component file' :component2)) + first-n (:name (first props')) + second (second props')] + + (t/is (not= first-n (:name second))) + (t/is (<= (count (:name second)) 60)) + (t/is (<= (count first-n) 60)) + (t/is (= "kept" (:value second))) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-invalid-variant-properties-non-string-value + (t/testing "a non-string value becomes an empty string" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component2 {:variant-properties [{:name "Property 1" :value 5}]})) + result (repair-until-valid file) + file' (:file result) + comp2' (thc/get-component file' :component2) + root2' (ths/get-shape file' :root2)] + + (t/is (= "" (-> comp2' :variant-properties first :value))) + (t/is (= "" (:variant-name root2'))) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-invalid-variant-properties-same-value-tuple + (t/testing "two members with the same clean value tuple are valid and left alone" + (let [props [{:name "Property 1" :value "red"}] + file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component2 {:variant-properties props}) + (thc/update-component :component1 {:variant-properties props}) + (ths/update-shape :root2 :variant-name "red") + (ths/update-shape :root1 :variant-name "red")) + errors (cfv/validate-file file {}) + result (repair-until-valid file)] + + (t/is (nil? errors)) + (t/is (= (:data file) (:data (:file result)))))) + + (t/testing "an editor value change that repeats another member's values passes validation" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2)) + comp1 (thc/get-component file :component1) + value (-> (thc/get-component file :component2) :variant-properties first :value) + changes (-> (pcb/empty-changes nil (thf/current-page-id file)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects (thf/current-page file))) + (clvp/generate-update-property-value (:id comp1) 0 value)) + file' (thf/apply-changes file changes :validate? false) + errors (cfv/validate-file file' {}) + affected (cfv/validate-file-affected file' {} (:redo-changes changes))] + + (t/is (= value (-> (thc/get-component file' :component1) :variant-properties first :value))) + (t/is (nil? errors)) + (t/is (nil? affected))))) + +(t/deftest repair-invalid-variant-properties-clean-board + (t/testing "a clean board yields no property error" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2)) + errors (cfv/validate-file file {})] + + (t/is (nil? (seq (filter #(= :invalid-variant-properties (:code %)) errors))))))) + +(t/deftest repair-invalid-variant-properties-no-properties-key + (t/testing "a board whose members have no :variant-properties key is left alone" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (ths/update-shape :root1 :variant-name "") + (ths/update-shape :root2 :variant-name "") + (update :data + (fn [data] + (-> data + (update-in [:components (thi/id :component1)] dissoc :variant-properties) + (update-in [:components (thi/id :component2)] dissoc :variant-properties))))) + errors (cfv/validate-file file {}) + result (repair-until-valid file) + file' (:file result)] + + (t/is (nil? (seq (filter #(= :invalid-variant-properties (:code %)) errors)))) + (t/is (= (:data file) (:data file'))) + (t/is (= "" (:variant-name (ths/get-shape file' :root1)))) + (t/is (= "" (:variant-name (ths/get-shape file' :root2))))))) + +(t/deftest repair-invalid-variant-properties-ignores-detached-component + (t/testing "a detached component with the same :variant-id does not add its names to the live set" + (let [deleted-id (uuid/next) + main-id (uuid/next) + file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2)) + 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"}}}))) + result (repair-until-valid file) + file' (:file result) + live-names (mapv :name (:variant-properties (thc/get-component file' :component2))) + deleted (get-in file' [:data :components deleted-id])] + + (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 (nil? (:errors result)))))) + +(t/deftest repair-invalid-variant-properties-objects-only-board + (t/testing "a variant board that exists only in a component's objects is repaired as one set" + (let [{:keys [file owner-id comp2-id comp1-id root2-id root1-id]} + (add-deleted-objects-only-board + (thf/sample-file :file1 :page-label :page1) + [{:name "Size" :value "red"}] + [{:name "Color" :value "bl=ue"}]) + errors (cfv/validate-file file {}) + affected (cfv/validate-file-affected file {} [{:type :mod-component :id owner-id}]) + result (repair-until-valid file) + file' (:file result) + result2 (repair-until-valid file') + comp2' (get-in file' [:data :components comp2-id]) + comp1' (get-in file' [:data :components comp1-id])] + + (t/is (= 1 (count (filter #(= :invalid-variant-properties (:code %)) errors)))) + (t/is (= 1 (count (filter #(= :invalid-variant-properties (:code %)) affected)))) + ;; :shapes is [root2 root1], so root1 is the top variant and its + ;; name leads the set + (t/is (= [{:name "Color" :value ""} {:name "Size" :value "red"}] + (:variant-properties comp2'))) + (t/is (= [{:name "Color" :value "blue"} {:name "Size" :value ""}] + (:variant-properties comp1'))) + (t/is (= "red" (get-in comp2' [:objects root2-id :variant-name]))) + (t/is (= "blue" (get-in comp1' [:objects root1-id :variant-name]))) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2))))))) + +(t/deftest repair-invalid-variant-properties-objects-only-stale-label + (t/testing "an objects-only board with clean properties and stale deleted-main labels is reported and repaired" + (let [{:keys [file comp2-id comp1-id root2-id root1-id]} + (add-deleted-objects-only-board + (thf/sample-file :file1 :page-label :page1) + [{:name "Property 1" :value "red"}] + [{:name "Property 1" :value "blue"}]) + errors (cfv/validate-file file {}) + result (repair-until-valid file) + file' (:file result) + result2 (repair-until-valid file') + comp2' (get-in file' [:data :components comp2-id]) + comp1' (get-in file' [:data :components comp1-id])] + + (t/is (= 1 (count (filter #(= :invalid-variant-properties (:code %)) errors)))) + (t/is (= "red" (get-in comp2' [:objects root2-id :variant-name]))) + (t/is (= "blue" (get-in comp1' [:objects root1-id :variant-name]))) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2))))))) + +(t/deftest repair-invalid-variant-properties-live-component-objects + (t/testing "a live component whose objects hold a variant board loses :objects and members are repaired" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (tho/add-simple-component :owner :owner-root :owner-child)) + owner-id (thi/id :owner) + board-id (uuid/next) + root2-id (uuid/next) + root1-id (uuid/next) + comp2-id (uuid/next) + comp1-id (uuid/next) + page-id (:id (thf/current-page file)) + member (fn [id root-id] + {:id id + :name "Member" + :path "" + :deleted true + :variant-id board-id + :main-instance-id root-id + :main-instance-page page-id + :variant-properties [{:name "Size" :value "red"}] + :objects {root-id {:id root-id + :type :frame + :variant-id board-id + :variant-name "stale"}}}) + file (update file :data + (fn [data] + (-> data + (assoc-in [:components owner-id :objects] + {board-id {:id board-id + :type :frame + :name "Board" + :is-variant-container true + :shapes [root2-id root1-id]} + root2-id {:id root2-id + :type :frame + :parent-id board-id + :variant-id board-id + :component-id comp2-id + :main-instance true + :variant-name "stale"} + root1-id {:id root1-id + :type :frame + :parent-id board-id + :variant-id board-id + :component-id comp1-id + :main-instance true + :variant-name "stale"}}) + (assoc-in [:components comp2-id] (member comp2-id root2-id)) + (assoc-in [:components comp1-id] (member comp1-id root1-id))))) + result (repair-until-valid file) + file' (:file result) + owner' (thc/get-component file' :owner) + comp2' (get-in file' [:data :components comp2-id]) + comp1' (get-in file' [:data :components comp1-id])] + + (t/is (nil? (:objects owner'))) + (t/is (= "red" (-> comp2' :variant-properties first :value))) + (t/is (= "red" (-> comp1' :variant-properties first :value))) + (t/is (= "red" (get-in comp2' [:objects root2-id :variant-name]))) + (t/is (= "red" (get-in comp1' [:objects root1-id :variant-name]))) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-invalid-variant-properties-matches-migration + (t/testing "repair and migration 0038 give the same properties and labels for one dirty board" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component1 {:variant-properties [{:name "Size=Big" :value "a=b"}]}) + (thc/update-component :component2 {:variant-properties [{:name "Color" :value "re,d"}]})) + migrated (assoc file :data (cfm/migrate-data (:data file) "0038-sanitize-variant-property-text")) + repaired (:file (repair-until-valid file)) + props-of (fn [file label] (:variant-properties (thc/get-component file label))) + label-of (fn [file label] (:variant-name (ths/get-shape file label)))] + + (t/is (= (props-of migrated :component1) (props-of repaired :component1))) + (t/is (= (props-of migrated :component2) (props-of repaired :component2))) + (t/is (= (label-of migrated :root1) (label-of repaired :root1))) + (t/is (= (label-of migrated :root2) (label-of repaired :root2)))))) + +(t/deftest repair-invalid-variant-properties-solo-deleted + (t/testing "a deleted variant that is not on a board is prepared alone" + (let [deleted-id (uuid/next) + main-id (uuid/next) + file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2)) + live-id (thi/id :variant1) + other-id (uuid/next) + page-id (:id (thf/current-page file)) + file (update file :data + (fn [data] + (assoc-in data [:components deleted-id] + {:id deleted-id + :name "Legacy" + :path "" + :deleted true + :variant-id other-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 + :variant-id other-id + :variant-name "stale"}}}))) + result (repair-until-valid file) + file' (:file result) + deleted (get-in file' [:data :components deleted-id]) + live-names (mapv :name (:variant-properties (thc/get-component file' :component2)))] + + (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 (not= live-id other-id)) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-unflagged-board-member-save + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (ths/update-shape :variant1 :is-variant-container false) + (thc/update-component :component1 {:variant-properties [{:name "Size" :value "Big"}]}) + (ths/update-shape :root1 :variant-name "Big"))] + + (t/testing "a save that touches only a member of a board that lost its flag is not stuck" + (let [changes [{:type :mod-component :id (thi/id :component1)}] + affected (cfv/validate-file-affected file {} changes) + result (repair-until-valid-affected file changes)] + ;; No set error on the frame that is no longer flagged as a board + (t/is (not-any? #(and (= :invalid-variant-properties (:code %)) + (= (thi/id :variant1) (:shape-id %))) + affected)) + (t/is (nil? (:errors result))))) + + (t/testing "a full repair restores the flag and then aligns the set" + (let [result (repair-until-valid file) + file' (:file result)] + (t/is (true? (:is-variant-container (ths/get-shape file' :variant1)))) + ;; :shapes is [root2 root1], so root1 is the top variant and its name leads + (t/is (= [{:name "Size" :value "Big"} {:name "Property 1" :value ""}] + (:variant-properties (thc/get-component file' :component1)))) + (t/is (= [{:name "Size" :value ""} {:name "Property 1" :value "Value2"}] + (:variant-properties (thc/get-component file' :component2)))) + (t/is (nil? (:errors result))))))) + +(t/deftest repair-invalid-variant-properties-member-checked-with-its-board + (t/testing "a save that touches only a board member checks and repairs the whole set" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component1 {:variant-properties [{:name "Size=Big" :value "a=b"}]})) + changes [{:type :mod-component :id (thi/id :component1)}] + prop-err (fn [errors] (filter #(= :invalid-variant-properties (:code %)) errors)) + affected (prop-err (cfv/validate-file-affected file {} changes)) + full (prop-err (cfv/validate-file file {})) + result (repair-until-valid-affected file changes) + file' (:file result)] + + (t/is (= 1 (count affected))) + (t/is (= 1 (count full))) + ;; :shapes is [root2 root1], so root1 is the top variant and its name leads + (t/is (= [{:name "SizeBig" :value "ab"} {:name "Property 1" :value ""}] + (:variant-properties (thc/get-component file' :component1)))) + (t/is (= [{:name "SizeBig" :value ""} {:name "Property 1" :value "Value2"}] + (:variant-properties (thc/get-component file' :component2)))) + (t/is (= "ab" (:variant-name (ths/get-shape file' :root1)))) + (t/is (= "Value2" (:variant-name (ths/get-shape file' :root2)))) + (t/is (nil? (:errors result))))) + + (t/testing "a save that touches only a member of an objects-only board checks that board" + (let [{:keys [file owner-id comp2-id comp1-id root2-id root1-id]} + (add-deleted-objects-only-board + (thf/sample-file :file1 :page-label :page1) + [{:name "Size" :value "re=d"}] + [{:name "Size" :value "blue"}]) + changes [{:type :mod-component :id comp1-id}] + affected (filter #(= :invalid-variant-properties (:code %)) + (cfv/validate-file-affected file {} changes)) + result (repair-until-valid-affected file changes) + comp2' (get-in (:file result) [:data :components comp2-id]) + comp1' (get-in (:file result) [:data :components comp1-id])] + + (t/is (= 1 (count affected))) + (t/is (= owner-id (get-in (first affected) [:args :component-id]))) + (t/is (= [{:name "Size" :value "red"}] (:variant-properties comp2'))) + (t/is (= [{:name "Size" :value "blue"}] (:variant-properties comp1'))) + (t/is (= "red" (get-in comp2' [:objects root2-id :variant-name]))) + (t/is (= "blue" (get-in comp1' [:objects root1-id :variant-name]))) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-invalid-variant-properties-solo-missing-key + (t/testing "a solo variant whose :variant-properties key is already missing yields no property error" + (let [variant-id (uuid/next) + file (-> (thf/sample-file :file1 :page-label :page1) + (ths/add-sample-shape :frame1 :type :frame :name "Ordinary") + (ths/add-sample-shape :root1 :type :frame :parent-label :frame1 + :name "SoloMain" + :variant-id variant-id + :variant-name "") + (thc/make-component :component1 :root1) + (thc/update-component :component1 {:variant-id variant-id}) + (update :data + (fn [data] + (update-in data [:components (thi/id :component1)] + dissoc :variant-properties)))) + errors (cfv/validate-file file {})] + + (t/is (nil? (seq (filter #(= :invalid-variant-properties (:code %)) errors))))))) + +(defn- live-solo-file + [shape-variant-id comp-variant-id props] + (-> (thf/sample-file :file1 :page-label :page1) + (ths/add-sample-shape :frame1 :type :frame :name "Ordinary") + (ths/add-sample-shape :rect1 :type :rect :parent-label :frame1) + (ths/add-sample-shape :root1 :type :frame :parent-label :frame1 + :name "SoloMain" + :variant-id shape-variant-id + :variant-name "stale") + (thc/make-component :component1 :root1) + (thc/update-component :component1 {:variant-id comp-variant-id + :path "Lib" + :name "Widget" + :variant-properties props}))) + +(defn- assert-live-solo-detached + [file] + (let [frame' (ths/get-shape file :frame1) + rect' (ths/get-shape file :rect1) + root' (ths/get-shape file :root1) + comp' (thc/get-component file :component1)] + (t/is (not (true? (:is-variant-container frame')))) + (t/is (some? rect')) + (t/is (nil? (:variant-id root'))) + (t/is (nil? (:variant-name root'))) + (t/is (= "SoloMain / ab" (:name root'))) + (t/is (nil? (:variant-id comp'))) + (t/is (nil? (:variant-properties comp'))) + (t/is (= "SoloMain" (:path comp'))) + (t/is (= "ab" (:name comp'))))) + +(t/deftest repair-variant-main-outside-board + (t/testing "a live solo on an ordinary frame reports only :variant-main-outside-board among the variant codes" + (let [variant-id (uuid/next) + file (live-solo-file variant-id variant-id [{:name "SizeBig" :value "ab"}]) + codes (set (map :code (cfv/validate-file file {})))] + (t/is (contains? codes :variant-main-outside-board)) + (t/is (not-any? #{:invalid-variant-properties + :parent-not-variant + :variant-main-bad-name + :variant-component-bad-name + :variant-component-bad-id} + codes)))) + + (t/testing "a live solo with dirty text is detached without adopting the frame" + (let [variant-id (uuid/next) + file (live-solo-file variant-id variant-id [{:name "Size=Big" :value "a=b"}]) + changes [{:type :mod-component :id (thi/id :component1)}] + result (repair-until-valid-affected file changes) + file' (:file result) + result2 (repair-until-valid-affected file' changes)] + (assert-live-solo-detached file') + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2)))))) + + (t/testing "a live solo with clean text and a stale label is detached by a full repair" + (let [variant-id (uuid/next) + file (live-solo-file variant-id variant-id [{:name "SizeBig" :value "ab"}]) + result (repair-until-valid file)] + (assert-live-solo-detached (:file result)) + (t/is (nil? (:errors result))))) + + (t/testing "a live solo whose main also lost its :variant-id is fully detached" + (let [variant-id (uuid/next) + file (-> (live-solo-file variant-id variant-id [{:name "SizeBig" :value "ab"}]) + (ths/update-shape :root1 :variant-id nil)) + codes (set (map :code (cfv/validate-file file {}))) + result (repair-until-valid file)] + (t/is (contains? codes :main-instance-not-a-variant)) + (t/is (not (contains? codes :variant-main-outside-board))) + (assert-live-solo-detached (:file result)) + (t/is (nil? (:errors result))))) + + (t/testing "a live solo whose component id differs from the shape id is detached" + (let [file (live-solo-file (uuid/next) (uuid/next) [{:name "Size=Big" :value "a=b"}]) + changes [{:type :mod-component :id (thi/id :component1)}] + result (repair-until-valid-affected file changes) + file' (:file result) + result2 (repair-until-valid-affected file' changes)] + (assert-live-solo-detached file') + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2))))))) + +(t/deftest repair-variant-main-outside-board-with-stray-objects + (t/testing "a live solo outside its board that also holds stray objects is detached once" + (let [variant-id (uuid/next) + stray-id (uuid/next) + file (-> (live-solo-file variant-id variant-id [{:name "SizeBig" :value "ab"}]) + (assoc-in [:data :components (thi/id :component1) :objects] + {stray-id {:id stray-id :type :rect :name "Stray"}})) + result (repair-until-valid file) + file' (:file result)] + (assert-live-solo-detached file') + (t/is (empty? (:objects (thc/get-component file' :component1)))) + (t/is (= 1 (:iterations result))) + (t/is (nil? (:errors result))))) + + (t/testing "a live solo outside its board whose objects are nil is detached once" + (let [variant-id (uuid/next) + file (-> (live-solo-file variant-id variant-id [{:name "SizeBig" :value "ab"}]) + (assoc-in [:data :components (thi/id :component1) :objects] nil)) + result (repair-until-valid file)] + (assert-live-solo-detached (:file result)) + (t/is (= 1 (:iterations result))) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-variant-main-outside-board-page-only-move + (t/testing "a save that moves a variant main off its board with page changes only is caught and repaired" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (ths/add-sample-shape :frame1 :type :frame :name "Ordinary")) + page (thf/current-page file) + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-objects (:objects page)) + (pcb/change-parent (thi/id :frame1) [(ths/get-shape file :root1)])) + file (thf/apply-changes file changes :validate? false) + redo (:redo-changes changes) + affected (cfv/validate-file-affected file {} redo) + full (cfv/validate-file file {}) + result (repair-until-valid-affected file redo) + file' (:file result) + root1' (ths/get-shape file' :root1) + comp1' (thc/get-component file' :component1)] + + (t/is (every? #(not= :mod-component (:type %)) redo)) + (t/is (= 1 (count (filter #(= :variant-main-outside-board (:code %)) affected)))) + (t/is (= 1 (count (filter #(= :variant-main-outside-board (:code %)) full)))) + (t/is (nil? (:variant-id root1'))) + (t/is (nil? (:variant-id comp1'))) + (t/is (nil? (:variant-properties comp1'))) + (t/is (not (true? (:is-variant-container (ths/get-shape file' :frame1))))) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-deleted-solo-not-a-variant-with-stale-label + (t/testing "a deleted solo whose main lost its :variant-id and has a stale label ends valid" + (let [deleted-id (uuid/next) + main-id (uuid/next) + variant-id (uuid/next) + file (thf/sample-file :file1 :page-label :page1) + page-id (thf/current-page-id file) + file (update file :data + (fn [data] + (assoc-in data [:components deleted-id] + {:id deleted-id + :name "Legacy" + :path "" + :deleted true + :variant-id variant-id + :main-instance-id main-id + :main-instance-page page-id + :variant-properties [{:name "Size" :value "big"}] + :objects {main-id {:id main-id + :type :frame + :variant-name "stale"}}}))) + codes (set (map :code (cfv/validate-file file {}))) + result (repair-until-valid file) + main' (get-in (:file result) [:data :components deleted-id :objects main-id])] + + (t/is (contains? codes :main-instance-not-a-variant)) + (t/is (contains? codes :invalid-variant-properties)) + (t/is (= variant-id (:variant-id main'))) + (t/is (= "big" (:variant-name main'))) + (t/is (= 1 (:iterations result))) + (t/is (nil? (:errors result)))))) + +(t/deftest repair-bad-variant-name-on-board-not-a-member + (t/testing "a main on a board whose component points to another board ends valid with a clean label" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component1 {:variant-id (uuid/next) + :variant-properties [{:name "Property 1" :value "a=b"}]})) + result (repair-until-valid file) + file' (:file result) + comp1' (thc/get-component file' :component1) + root1' (ths/get-shape file' :root1)] + + (t/is (= (thi/id :variant1) (:variant-id comp1'))) + (t/is (= [{:name "Property 1" :value "ab"}] (:variant-properties comp1'))) + (t/is (= "ab" (:variant-name root1'))) + (t/is (= 1 (:iterations result))) + (t/is (nil? (:errors result)))))) + +(t/deftest objects-only-board-skips-a-board-already-on-a-page + (t/testing "a board in a deleted component's objects with the id of a page board is not checked twice" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component1 {:variant-properties [{:name "Property 1" :value "a=b"}]})) + owner-id (uuid/next) + board-id (thi/id :variant1) + page (thf/current-page file) + objects (select-keys (:objects page) [board-id (thi/id :root1) (thi/id :root2)]) + file (update file :data + (fn [data] + (assoc-in data [:components owner-id] + {:id owner-id + :name "Owner" + :path "" + :deleted true + :main-instance-id board-id + :main-instance-page (:id page) + :objects objects}))) + errors (filter #(= :invalid-variant-properties (:code %)) (cfv/validate-file file {}))] + + (t/is (= 1 (count errors))) + (t/is (nil? (get-in (first errors) [:args :component-id])))))) + +(t/deftest objects-only-board-finds-its-page-through-its-members + (t/testing "a board copy whose page board sits on its members' page, not the owner's, is skipped" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thf/add-sample-page :page2) + (thv/add-variant :variant1 :component1 :root1 :component2 :root2) + (thc/update-component :component1 {:variant-properties [{:name "Property 1" :value "a=b"}]})) + owner-id (uuid/next) + board-id (thi/id :variant1) + page2 (thf/get-page file :page2) + objects (select-keys (:objects page2) [board-id (thi/id :root1) (thi/id :root2)]) + file (update file :data + (fn [data] + (assoc-in data [:components owner-id] + {:id owner-id + :name "Owner" + :path "" + :deleted true + :main-instance-id board-id + :main-instance-page (thi/id :page1) + :objects objects}))) + ;; This save does not walk page2, so only the members' page lookup + ;; tells the owner's copy apart from an objects-only board + affected (filter #(= :invalid-variant-properties (:code %)) + (cfv/validate-file-affected file {} [{:type :mod-component :id owner-id}])) + full (filter #(= :invalid-variant-properties (:code %)) + (cfv/validate-file file {}))] + + (t/is (empty? affected)) + (t/is (= 1 (count full))) + (t/is (nil? (get-in (first full) [:args :component-id])))))) + +(defn- property-errors + [result] + (filter #(= :invalid-variant-properties (:code %)) (:errors result))) + +(defn- deleted-missing-main-file + [objects props] + (let [deleted-id (uuid/next) + main-id (uuid/next) + other-id (uuid/next) + file (thf/sample-file :file1 :page-label :page1) + page-id (thf/current-page-id file) + component (cond-> {:id deleted-id + :name "Legacy" + :path "" + :deleted true + :variant-id other-id + :main-instance-id main-id + :main-instance-page page-id + :objects objects} + (some? props) + (assoc :variant-properties props))] + {:file (update file :data #(assoc-in % [:components deleted-id] component)) + :deleted-id deleted-id + :main-id main-id})) + +(defn- assert-deleted-text-rewritten + [file deleted-id main-id] + (let [deleted (get-in file [:data :components deleted-id])] + (t/is (= "SizeBig" (-> deleted :variant-properties first :name))) + (t/is (= "ab" (-> deleted :variant-properties first :value))) + (t/is (= {} (:objects deleted))) + (t/is (nil? (get (:objects deleted) main-id))))) + +(t/deftest repair-invalid-variant-properties-deleted-missing-main + (t/testing "a deleted solo with an empty objects map and a missing main rewrites text without a shape" + (let [{:keys [file deleted-id main-id]} + (deleted-missing-main-file {} [{:name "Size=Big" :value "a=b"}]) + result (repair-until-valid file) + file' (:file result) + result2 (repair-until-valid file')] + (assert-deleted-text-rewritten file' deleted-id main-id) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2)))))) + + (t/testing "the same empty objects map repaired from one component change" + (let [{:keys [file deleted-id main-id]} + (deleted-missing-main-file {} [{:name "Size=Big" :value "a=b"}]) + changes [{:type :mod-component :id deleted-id}] + result (repair-until-valid-affected file changes) + file' (:file result) + result2 (repair-until-valid-affected file' changes)] + (assert-deleted-text-rewritten file' deleted-id main-id) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2)))))) + + (t/testing "a deleted solo whose objects are nil ends with an empty map" + (let [{:keys [file deleted-id main-id]} + (deleted-missing-main-file nil [{:name "Size=Big" :value "a=b"}]) + result (repair-until-valid file) + file' (:file result) + result2 (repair-until-valid file')] + (assert-deleted-text-rewritten file' deleted-id main-id) + (t/is (nil? (seq (property-errors result)))) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2)))))) + + (t/testing "the same nil objects repaired from one component change" + (let [{:keys [file deleted-id main-id]} + (deleted-missing-main-file nil [{:name "Size=Big" :value "a=b"}]) + changes [{:type :mod-component :id deleted-id}] + result (repair-until-valid-affected file changes) + file' (:file result) + result2 (repair-until-valid-affected file' changes)] + (assert-deleted-text-rewritten file' deleted-id main-id) + (t/is (nil? (seq (property-errors result)))) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2)))))) + + (t/testing "a deleted solo with no properties and no main is left alone" + (let [{:keys [file]} (deleted-missing-main-file {} nil) + errors (cfv/validate-file file {}) + result (repair-until-valid file)] + (t/is (nil? (seq (filter #(= :invalid-variant-properties (:code %)) errors)))) + (t/is (= (:data file) (:data (:file result)))))) + + (t/testing "the clean deleted solo repaired from one component change is left alone" + (let [{:keys [file deleted-id]} (deleted-missing-main-file {} nil) + changes [{:type :mod-component :id deleted-id}] + result (repair-until-valid-affected file changes)] + (t/is (nil? (seq (property-errors result)))) + (t/is (= (:data file) (:data (:file result))))))) + +(defn- live-solo-missing-main + [props] + (let [variant-id (uuid/next) + file (-> (thf/sample-file :file1 :page-label :page1) + (ths/add-sample-shape :frame1 :type :frame :name "Ordinary") + (ths/add-sample-shape :rect1 :type :rect :parent-label :frame1) + (ths/add-sample-shape :root1 :type :frame :parent-label :frame1 + :name "SoloMain" + :variant-id variant-id + :variant-name "stale") + (thc/make-component :component1 :root1) + (thc/update-component :component1 {:variant-id variant-id + :path "Lib" + :name "Widget" + :variant-properties props})) + page-id (thf/current-page-id file) + root-id (thi/id :root1)] + (update-in file [:data :pages-index page-id :objects] dissoc root-id))) + +(t/deftest repair-invalid-variant-properties-live-missing-main + (t/testing "a live solo with clean text and a missing main does not fail the component save" + (let [file (live-solo-missing-main [{:name "SizeBig" :value "ab"}]) + changes [{:type :mod-component :id (thi/id :component1)}] + result (repair-until-valid-affected file changes)] + (t/is (nil? (seq (property-errors result)))) + (t/is (nil? (:errors result))) + (t/is (nil? (ths/get-shape (:file result) :root1))) + (t/is (= (:data file) (:data (:file result)))))) + + (t/testing "a full repair of that live solo does not create the missing main" + (let [file (live-solo-missing-main [{:name "SizeBig" :value "ab"}]) + result (repair-until-valid file) + file' (:file result)] + (t/is (nil? (seq (property-errors result)))) + (t/is (nil? (ths/get-shape file' :root1))))) + + (t/testing "a live solo with dirty text and a missing main rewrites the text and creates no shape" + (let [file (live-solo-missing-main [{:name "Size=Big" :value "a=b"}]) + changes [{:type :mod-component :id (thi/id :component1)}] + result (repair-until-valid-affected file changes) + file' (:file result) + result2 (repair-until-valid-affected file' changes) + comp' (thc/get-component file' :component1)] + (t/is (= [{:name "SizeBig" :value "ab"}] (:variant-properties comp'))) + (t/is (nil? (ths/get-shape file' :root1))) + (t/is (nil? (seq (property-errors result)))) + (t/is (nil? (:errors result))) + (t/is (= (:data file') (:data (:file result2)))))) + + (t/testing "a full repair of that dirty live solo creates no shape" + (let [file (live-solo-missing-main [{:name "Size=Big" :value "a=b"}]) + result (repair-until-valid file) + file' (:file result) + comp' (thc/get-component file' :component1)] + (t/is (= [{:name "SizeBig" :value "ab"}] (:variant-properties comp'))) + (t/is (nil? (ths/get-shape file' :root1))) + (t/is (nil? (seq (property-errors result))))))) + diff --git a/common/test/common_tests/files/variant_test.cljc b/common/test/common_tests/files/variant_test.cljc index 93dccc0934..e7b0bc5390 100644 --- a/common/test/common_tests/files/variant_test.cljc +++ b/common/test/common_tests/files/variant_test.cljc @@ -109,6 +109,17 @@ (: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"}]})) diff --git a/common/test/common_tests/logic/variants_test.cljc b/common/test/common_tests/logic/variants_test.cljc index 42cfe2fbb1..a92426b50e 100644 --- a/common/test/common_tests/logic/variants_test.cljc +++ b/common/test/common_tests/logic/variants_test.cljc @@ -482,6 +482,29 @@ (t/is (contains? values "Bigred")))) +(t/deftest test-make-shapes-variant-clips-long-component-name-as-value + ;; A component name over the property length limit becomes a value cut to + ;; that limit, so the moved variant passes validation. + (let [long-name (apply str (repeat 70 "n")) + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thv/add-variant :v02 :c03 :m03 :c04 :m04) + (thc/update-component :c03 {:name long-name})) + page (thf/current-page file) + container (ths/get-shape file :v01) + shape (ths/get-shape file :m03) + changes (-> (pcb/empty-changes nil) + (pcb/with-page-id (:id page)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects page)) + (clvp/generate-make-shapes-variant [shape] container) + (pcb/change-parent (:id container) [shape] 0)) + file' (thf/apply-changes file changes) + values (map :value (:variant-properties (thc/get-component file' :c03)))] + + (t/is (every? #(<= (count %) ctv/property-max-length) values)) + (t/is (some #(= (subs long-name 0 ctv/property-max-length) %) values)))) + (t/deftest test-make-shapes-variant-same-name-components-get-unique-values ;; Mirrors combine-as-variants with components that share the container ;; name: the first main defines the container, the rest are moved into it. diff --git a/common/test/common_tests/types/variant_test.cljc b/common/test/common_tests/types/variant_test.cljc index a82abb9e65..c5de8ba407 100644 --- a/common/test/common_tests/types/variant_test.cljc +++ b/common/test/common_tests/types/variant_test.cljc @@ -215,6 +215,24 @@ (t/is (= [{:name "S" :value "(1)"}] (ctv/unique-values-tuple [{:name "S" :value ""}] [[""]]))))) +(t/deftest align-properties-set + (t/testing "text is cleaned and every vector gets the same names" + (t/is (= [[{:name "SizeBig" :value "one"} {:name "Color" :value ""}] + [{:name "SizeBig" :value ""} {:name "Color" :value "red"}]] + (ctv/align-properties-set [[{:name "Size=Big" :value "one"}] + [{:name "Color" :value "red"}]])))) + + (t/testing "a repeated value tuple is kept" + (let [props-seq [[{:name "Size" :value "big"}] + [{:name "Size" :value "big"}]]] + (t/is (= props-seq (ctv/align-properties-set props-seq))))) + + (t/testing "a second run changes nothing" + (let [once (ctv/align-properties-set [[{:name "a=b" :value long-text} {:name "a=b" :value ""}] + [{:name "ab" :value long-text}] + [{:name "=" :value 5}]])] + (t/is (= once (ctv/align-properties-set once)))))) + (t/deftest sanitize-properties-set (t/testing "a colliding value tuple is numbered" (t/is (= [[{:name "Size" :value "ab"}] diff --git a/common/test/common_tests/variant_test.cljc b/common/test/common_tests/variant_test.cljc index bcccea9f1c..7174c432f0 100644 --- a/common/test/common_tests/variant_test.cljc +++ b/common/test/common_tests/variant_test.cljc @@ -219,3 +219,8 @@ result (ctv/path-to-properties "Button / Big, red" props)] (t/is (= "Button" (-> result first :value))) (t/is (= "Big red" (-> result second :value))))) + +(t/deftest path-to-properties-clips-long-values + (let [segment (apply str (repeat 70 "s")) + result (ctv/path-to-properties (str "Button / " segment) [{:name "Property 1" :value ""}])] + (t/is (= (subs segment 0 ctv/property-max-length) (-> result second :value)))))