penpot/common/test/common_tests/files_migrations_0039_test.cljc
alonso.torres 65da6b1404 🐛 Repair svg attributes an old reader split into maps
Two dashboard templates, Avataaars and Tutorial for beginners, hold the
filter attribute of some shapes as a map an old reader split out of its
text, as `{:url(#filter_2) nil}` for `url(#filter_2)`. The svg attribute
schema takes only scalars outside `:style`, so their import failed the
file validation after migrating.

Migration 0039 turns such a map back into the reference to one of the
shape's defs it names, matched to the defs id as stored though 0035 may
have kebab-cased the map, and drops any other map value. Both templates
then import valid, with their filters.

The benchmark is not run: the change is a file migration.

AI-assisted-by: claude-opus-5-5
2026-11-18 10:29:49 +01:00

55 lines
2.4 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-migrations-0039-test
(:require
[app.common.files.migrations :as cfm]
[app.common.uuid :as uuid]
[clojure.test :as t]))
;; 0039-repair-svg-attr-maps
;; Some imported files hold an svg attribute other than `:style` as a map an
;; old reader split out of its text. A reference to one of the shape's defs
;; becomes that reference again; any other map value drops.
(def ^:private migration-id
"0039-repair-svg-attr-maps")
(defn- migrate-attrs
"The `:svg-attrs` the migration leaves on a page shape with `attrs` and
`defs`, and on a deleted component's shape alike."
[attrs defs]
(let [shape-id (uuid/next)
page-id (uuid/next)
component-id (uuid/next)
shape {:id shape-id :type :rect :svg-attrs attrs :svg-defs defs}
data {:pages-index {page-id {:objects {shape-id shape}}}
:components {component-id {:objects {shape-id shape}}}}
data' (cfm/migrate-data data migration-id)]
[(get-in data' [:pages-index page-id :objects shape-id :svg-attrs])
(get-in data' [:components component-id :objects shape-id :svg-attrs])]))
(t/deftest migration-0039-restores-a-split-defs-reference
(let [defs {"filter0_d_16837_160" {:tag :filter :attrs {:id "filter0_d_16837_160"}}}]
(doseq [value [{(keyword "url(#filter0_d_16837_160)") nil}
;; as 0035 left it, kebab-cased
{(keyword "url(-filter0-d-16837-160)") nil}]
attrs (migrate-attrs {:filter value :fill "none"} defs)]
(t/is (= {:filter "url(#filter0_d_16837_160)" :fill "none"} attrs)))))
(t/deftest migration-0039-drops-other-map-values-and-keeps-style
(let [style {:fill "red"}]
(doseq [attrs (migrate-attrs {:filter {(keyword "url(#missing)") nil}
:mask {:a 1}
:style style}
{})]
(t/is (= {:style style} attrs)))))
(t/deftest migration-0039-leaves-scalar-attrs-alone
(let [attrs {:filter "url(#f)" :fill "none"}]
(doseq [attrs' (migrate-attrs attrs {"f" {:tag :filter :attrs {:id "f"}}})]
(t/is (= attrs attrs')))))