penpot/common/test/common_tests/files_migrations_0033_test.cljc
Eva Marco fff44ec758
🐛 Fix shared files 500 on shapes with invalid child ids (#12528)
* 🐛 Fix shared files 500 on shapes with invalid child ids

Old files can list in :shapes ids that are not uuids or that point to
no shape. Migration 0033 walked them and hit the uuid assert in
get-shape, which throws an AssertionError. The shared files list only
caught Exception, so that Error broke the whole list with a 500.

Make migration 0033 skip those ids, and catch Throwable when loading
each shared file, so a broken file is skipped and logged.

Closes #12343

AI-assisted-by: claude-opus-5-5

* 🐛 Repair invalid child ids in migration 0033 v2

Skipping invalid child ids in migration 0033 was not enough: the ids
stayed in :shapes, so the file still failed schema validation and
could not load. Files that already applied the migration would also
never run a changed version of it.

The migration now repairs each container before the naming walk:
string ids are parsed with uuid/coerce, nil and ids that point to no
shape are dropped, the duplicate that 0032 adds when a child is
listed as a string is removed, and a :shapes list becomes a vector.
Only the shapes that change are re-assoc'ed, so a healthy ObjectsMap
is not marked as modified and its page is not rewritten.

It ships as 0033-normalize-main-instance-names-v2 at the same
position in available-migrations, so files that recorded the old id
run it again. That position also makes it parse strings before
0035-fix-dangling-children drops the ids it cannot find.

The shared files listing restores the interrupt flag when the cause
of a skipped file is an InterruptedException.

Tests cover children outside the root walk, legacy components, the
objects-map wrapper of the unrealized save path, a list turned into a
vector, a shape without a :shapes key, a file that applied the old
id, and the end-to-end load of a broken shared file.

Relates to #12343

AI-assisted-by: claude-opus-5-5
AI-assisted-by: inkling-2-small-free
2026-11-18 13:30:40 +01:00

139 lines
6.2 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-0033-test
"Regression coverage for the shape tree traversal of the
`0033-normalize-main-instance-names-v2` migration.
The migration walks a container from its root down, following the
`:shapes` vectors. A file can hold a `:shapes` entry pointing to a
shape that is not in the `:objects` map (a dangling reference). The
repair that runs before the walk drops it instead of storing a nil
shape into the `:objects` map, and the walk keeps normalizing the
names of the shapes that do exist."
(:require
[app.common.files.migrations :as cfm]
[app.common.types.shape :as cts]
[app.common.uuid :as uuid]
[clojure.test :as t]))
(def ^:private migration "0033-normalize-main-instance-names-v2")
(defn- make-root
[children]
(cts/setup-shape {:id uuid/zero
:type :frame
:name "Root"
:shapes children}))
(defn- make-main-instance
"A frame declared as the main instance of a component."
[main-id file-id component-id name]
(-> (cts/setup-shape {:id main-id :type :frame :name name})
(assoc :main-instance true
:component-id component-id
:component-file file-id)))
(defn- page-data
"File data with a single page owning `objects`."
[page-id objects]
{:pages-index {page-id {:id page-id :objects objects}}})
(defn- component-data
"File data with a single component owning `objects`."
[component-id objects]
{:components {component-id {:id component-id :objects objects}}})
;; --- A missing child is dropped, and never becomes a nil shape
(t/deftest missing-child-of-a-page-does-not-create-a-nil-shape
(let [page-id (uuid/next)
child-id (uuid/next)
root (make-root [child-id])
data (page-data page-id {uuid/zero root})
migrated (cfm/migrate-data data migration)
objects' (get-in migrated [:pages-index page-id :objects])]
(t/is (not (contains? objects' nil))
"no nil key is added to the objects map")
(t/is (= 1 (count objects'))
"the objects map keeps only the shapes that exist")
(t/is (= (assoc root :shapes []) (get objects' uuid/zero))
"the dangling child is dropped, the rest of the root is left untouched")))
(t/deftest missing-child-of-a-component-does-not-create-a-nil-shape
(let [component-id (uuid/next)
child-id (uuid/next)
root (make-root [child-id])
data (component-data component-id {uuid/zero root})
migrated (cfm/migrate-data data migration)
objects' (get-in migrated [:components component-id :objects])]
(t/is (not (contains? objects' nil))
"no nil key is added to the objects map")
(t/is (= 1 (count objects'))
"the objects map keeps only the shapes that exist")
(t/is (= (assoc root :shapes []) (get objects' uuid/zero))
"the dangling child is dropped, the rest of the component root is left untouched")))
(t/deftest missing-child-deep-in-the-tree-does-not-create-a-nil-shape
(let [page-id (uuid/next)
frame-id (uuid/next)
missing-id (uuid/next)
frame (-> (cts/setup-shape {:id frame-id :type :frame :name "Frame"})
(assoc :shapes [missing-id]))
data (page-data page-id {uuid/zero (make-root [frame-id])
frame-id frame})
migrated (cfm/migrate-data data migration)
objects' (get-in migrated [:pages-index page-id :objects])]
(t/is (not (contains? objects' nil))
"no nil key is added to the objects map")
(t/is (= (assoc frame :shapes []) (get objects' frame-id))
"the missing child is dropped, the rest of its parent is left untouched")))
;; --- The behavior: names keep being normalized next to a missing child
(t/deftest main-instance-of-a-page-is-renamed-next-to-a-missing-child
(let [file-id (uuid/next)
page-id (uuid/next)
component-id (uuid/next)
main-id (uuid/next)
missing-id (uuid/next)
main-shape (make-main-instance main-id file-id component-id "Group/Name")
data (assoc (page-data page-id {uuid/zero (make-root [missing-id main-id])
main-id main-shape})
:components {component-id {:id component-id
:name "Name"
:path "Group"
:main-instance-id main-id}})
migrated (cfm/migrate-data data migration)
objects' (get-in migrated [:pages-index page-id :objects])]
(t/is (not (contains? objects' nil))
"no nil key is added to the objects map")
(t/is (= "Group / Name" (get-in objects' [main-id :name]))
"the main instance takes the path and name of its component")))
(t/deftest main-instance-of-a-component-is-renamed-next-to-a-missing-child
(let [file-id (uuid/next)
component-id (uuid/next)
main-id (uuid/next)
missing-id (uuid/next)
main-shape (make-main-instance main-id file-id component-id "Group/Name")
data (component-data component-id {uuid/zero (make-root [missing-id main-id])
main-id main-shape})
data (assoc-in data [:components component-id :name] "Name")
data (assoc-in data [:components component-id :path] "Group")
data (assoc-in data [:components component-id :main-instance-id] main-id)
migrated (cfm/migrate-data data migration)
objects' (get-in migrated [:components component-id :objects])]
(t/is (not (contains? objects' nil))
"no nil key is added to the objects map")
(t/is (= "Group / Name" (get-in objects' [main-id :name]))
"the main instance takes the path and name of its component")))