From db3466aca606030f47483d2d3aa9a55ea6eb52cc Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Fri, 27 Nov 2026 14:42:52 +0100 Subject: [PATCH] :bug: Validate copies of unlinked libraries like the backend does (#13330) * :bug: 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 * :sparkles: Fix MR findings --- frontend/src/app/plugins/file.cljs | 3 +- frontend/src/app/plugins/shape.cljs | 10 ++- frontend/src/debug.cljs | 5 +- .../frontend_tests/plugins/file_test.cljs | 43 ++++++++++- .../plugins/shape_bugfixes_test.cljs | 72 ++++++++++++++++++- 5 files changed, 127 insertions(+), 6 deletions(-) diff --git a/frontend/src/app/plugins/file.cljs b/frontend/src/app/plugins/file.cljs index d80f55f1bc..6a4ee4a49c 100644 --- a/frontend/src/app/plugins/file.cljs +++ b/frontend/src/app/plugins/file.cljs @@ -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]}] diff --git a/frontend/src/app/plugins/shape.cljs b/frontend/src/app/plugins/shape.cljs index ab21507f0c..5e7c9be12d 100644 --- a/frontend/src/app/plugins/shape.cljs +++ b/frontend/src/app/plugins/shape.cljs @@ -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))))) diff --git a/frontend/src/debug.cljs b/frontend/src/debug.cljs index 7a1abb89af..78dcf7ba72 100644 --- a/frontend/src/debug.cljs +++ b/frontend/src/debug.cljs @@ -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)) diff --git a/frontend/test/frontend_tests/plugins/file_test.cljs b/frontend/test/frontend_tests/plugins/file_test.cljs index 5a5dab144b..f02f28618a 100644 --- a/frontend/test/frontend_tests/plugins/file_test.cljs +++ b/frontend/test/frontend_tests/plugins/file_test.cljs @@ -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")))) diff --git a/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs b/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs index 8986fbc703..95995e1e06 100644 --- a/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs +++ b/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs @@ -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)))))