🐛 Validate copies of unlinked libraries like the backend does (#13330)

* 🐛 Validate copies of unlinked libraries like the backend does

Copies of components from a library that is no longer linked failed
plugin validation with component-not-found and ref-shape-not-found,
while the backend accepted every save on them. The validator skips
copies whose library is not in the map it gets, but the plugin passed
the whole :files state, which also holds dashboard entries without
data. A library in the same project then looked present but empty.

The plugin validate, and the debug validate and repair, now pass only
the file and its linked libraries, the same set the backend uses. The
debug repair would otherwise detach those copies.

resetOverrides in the plugin API now refuses, with a message, when the
main component of the copy cannot be found or is deleted. Before, it
emitted a reset that changed nothing. The workspace menu already hides
the option in that case.

Closes #13323

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

* ✨ Fix MR findings
This commit is contained in:
Pablo Alba 2026-11-27 14:42:52 +01:00 committed by GitHub
parent 401d646cdd
commit db3466aca6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 127 additions and 6 deletions

View File

@ -17,6 +17,7 @@
[app.main.data.plugins :as dp]
[app.main.data.workspace :as dw]
[app.main.data.workspace.versions :as dwv]
[app.main.refs :as refs]
[app.main.repo :as rp]
[app.main.store :as st]
[app.main.worker :as mw]
@ -181,7 +182,7 @@
:validate
(fn []
(let [file (u/locate-file id)
libraries (get @st/state :files)]
libraries (refs/select-libraries (:files @st/state) id)]
(try
(->> (cfv/validate-file file libraries)
(map (fn [{:keys [code hint shape-id page-id]}]

View File

@ -1632,7 +1632,9 @@
:resetOverrides
(fn []
(let [shape (u/locate-shape file-id page-id id)]
(let [shape (u/locate-shape file-id page-id id)
component (when (ctk/instance-head? shape)
(u/locate-library-component (:component-file shape) (:component-id shape)))]
(cond
(not (r/check-permission plugin-id "content:write"))
(u/not-valid plugin-id :resetOverrides "Plugin doesn't have 'content:write' permission")
@ -1643,6 +1645,12 @@
(not (ctk/in-component-copy? shape))
(u/not-valid plugin-id :resetOverrides "The shape is not a component copy instance")
(not (ctk/instance-head? shape))
(u/not-valid plugin-id :resetOverrides "The shape is not a component copy head")
(or (nil? component) (:deleted component))
(u/not-valid plugin-id :resetOverrides "The main component does not exist or is deleted")
:else
(st/emit! (dwl/reset-component id)))))

View File

@ -41,6 +41,7 @@
[app.main.data.workspace.shortcuts]
[app.main.data.workspace.undo :as dwu]
[app.main.errors :as errors]
[app.main.refs :as refs]
[app.main.repo :as rp]
[app.main.store :as st]
[app.util.debug :as dbg]
@ -546,7 +547,7 @@
([] (validate nil))
([shape-id]
(let [file (dsh/lookup-file @st/state)
libraries (get @st/state :files)]
libraries (refs/select-libraries (:files @st/state) (:id file))]
(try
(->> (if-let [shape-id (some-> shape-id uuid/parse)]
(let [page (dm/get-in file [:data :pages-index (get @st/state :current-page-id)])]
@ -575,7 +576,7 @@
sid (:session-id state)
file (dsh/lookup-file state)
libs (get state :files)
libs (refs/select-libraries (:files state) (:id file))
errors (cfv/validate-file file libs)
_ (l/dbg :hint "repair current file" :errors (count errors))

View File

@ -6,14 +6,21 @@
(ns frontend-tests.plugins.file-test
(:require
[app.common.test-helpers.components :as cthc]
[app.common.test-helpers.files :as cthf]
[app.common.test-helpers.shapes :as cths]
[app.common.time :as ct]
[app.common.uuid :as uuid]
[app.main.repo :as rp]
[app.main.store :as st]
[app.plugins.file :as file]
[app.plugins.register :as r]
[beicon.v2.core :as rx]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.mock :as mock]))
[frontend-tests.helpers.mock :as mock]
[frontend-tests.helpers.state :as ths]))
(def ^:private plugin-id "00000000-0000-0000-0000-000000000000")
(t/deftest file-version-created-at-returns-stored-date
(let [created-at (js/Date.)
@ -158,3 +165,37 @@
:files {other-id {:id other-id
:features #{"variants/v1" "components/v2"}}}}]
(t/is (nil? (file/get-file-features state other-id)))))
(defn- library-with-component
[]
(-> (cthf/sample-file :library-file :page-label :library-page)
(cths/add-sample-shape :main :type :frame)
(cthc/make-component :comp :main)))
(defn- consumer-with-copy
[library]
(-> (cthf/sample-file :consumer-file :page-label :consumer-page)
(cthc/instantiate-component :comp :copy :library library)))
(defn- validation-codes
[file libraries]
(let [store (ths/setup-store file {:libraries libraries})]
(mock/with-mocks*
{st/state store}
(mapv #(.-code ^js %) (.validate (file/file-proxy plugin-id (:id file)))))))
(t/deftest ^:async file-validate-ignores-data-less-library-entries
;; A dashboard stub of a library that is not linked to the file has no
;; `:data` and no `:library-of`; its copies must not be reported.
(let [library (library-with-component)
file (consumer-with-copy library)
stub {:id (:id library) :name "Library"}]
(t/is (= [] (await (validation-codes file [stub]))))))
(t/deftest ^:async file-validate-checks-linked-libraries
(let [library (library-with-component)
file (consumer-with-copy library)
linked (-> library
(assoc :library-of (:id file))
(assoc-in [:data :components] {}))]
(t/is (contains? (set (await (validation-codes file [linked]))) "component-not-found"))))

View File

@ -7,7 +7,10 @@
(ns frontend-tests.plugins.shape-bugfixes-test
(:require
[app.common.data :as d]
[app.common.test-helpers.components :as cthc]
[app.common.test-helpers.files :as cthf]
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as cths]
[app.common.types.component :as ctk]
[app.common.uuid :as uuid]
[app.main.data.workspace :as dw]
@ -27,7 +30,8 @@
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.mock :as mock]
[frontend-tests.helpers.state :as ths]
[frontend-tests.helpers.wasm :as thw]))
[frontend-tests.helpers.wasm :as thw]
[potok.v2.core :as ptk]))
(def ^:private plugin-id "00000000-0000-0000-0000-000000000000")
@ -535,3 +539,69 @@
(t/is (= 1 (count @errors)))
(t/is (= variant-append-msg (last (first @errors))))
(t/is (empty? @emitted))))))))
;; ---------------------------------------------------------------------------
;; resetOverrides
;; ---------------------------------------------------------------------------
(defn- reset-overrides-setup
"A consumer file with a copy (and a child inside it) of a component of a
library. `library-fn` receives the library and returns the entry stored in
`:files` for it."
[library-fn]
(let [library (-> (cthf/sample-file :library-file :page-label :library-page)
(cths/add-sample-shape :main :type :frame)
(cths/add-sample-shape :main-child :parent-label :main)
(cthc/make-component :comp :main))
file (-> (cthf/sample-file :consumer-file :page-label :consumer-page)
(cthc/instantiate-component :comp :copy
:library library
:children-labels [:copy-child]))
store (ths/setup-store file {:libraries [(library-fn library file)]})]
(ptk/emit! store #(assoc-in % [:plugins :flags plugin-id :throw-validation-errors] true))
{:file file :store store}))
(defn- reset-overrides-on
"Calls `resetOverrides` on the shape labelled `label`; returns the thrown
message (or nil) and the emitted events."
[{:keys [file store]} label]
(let [events (atom [])]
(mock/with-mocks*
{st/state store
st/emit! (mock/stub (fn [& emitted] (swap! events into emitted)))
r/check-permission (mock/stub (constantly true))}
(let [shape (shape/shape-proxy plugin-id (:id file) (:id (cthf/current-page file)) (thi/id label))
error (try (.resetOverrides shape) nil
(catch js/Error cause (ex-message cause)))]
{:error error :events @events}))))
(t/deftest ^:async reset-overrides-rejects-a-copy-of-a-library-without-data
(let [setup (reset-overrides-setup (fn [library _] (select-keys library [:id :name])))
result (await (reset-overrides-on setup :copy))]
(t/is (re-find #"main component does not exist" (:error result)))
(t/is (empty? (:events result)))))
(t/deftest ^:async reset-overrides-rejects-a-copy-of-a-deleted-component
(let [setup (reset-overrides-setup
(fn [library file]
(-> library
(assoc :library-of (:id file))
(assoc-in [:data :components (thi/id :comp) :deleted] true))))
result (await (reset-overrides-on setup :copy))]
(t/is (re-find #"main component does not exist" (:error result)))
(t/is (empty? (:events result)))))
(t/deftest ^:async reset-overrides-resets-the-copy-of-an-existing-component
(let [setup (reset-overrides-setup (fn [library file] (assoc library :library-of (:id file))))
result (await (reset-overrides-on setup :copy))
event (first (:events result))]
(t/is (nil? (:error result)))
(t/is (= 1 (count (:events result))))
(t/is (= ::dwl/reset-component (ptk/type event)))
(t/is (= (thi/id :copy) (.-id event)))))
(t/deftest ^:async reset-overrides-rejects-a-nested-shape
(let [setup (reset-overrides-setup (fn [library file] (assoc library :library-of (:id file))))
result (await (reset-overrides-on setup :copy-child))]
(t/is (re-find #"not a component copy head" (:error result)))
(t/is (empty? (:events result)))))