diff --git a/.serena/memories/common/changes-architecture.md b/.serena/memories/common/changes-architecture.md index bee2ed52c2..6d92199f4b 100644 --- a/.serena/memories/common/changes-architecture.md +++ b/.serena/memories/common/changes-architecture.md @@ -24,6 +24,7 @@ Each transaction carries `:redo-changes` and inverse `:undo-changes`. The undo s `common/src/app/common/files/changes_builder.cljc` (usually alias `pcb`) is the fluent builder. Start from `(pcb/empty-changes )` or `(pcb/empty-changes nil )` for tests. High-value builder operations: +- `pcb/add-page changes page`: derives the ID from the whole page. The redo change still carries both `:id` and `:page`; the undo deletes that same ID. `pcb/add-empty-page` takes an ID and name instead. - `pcb/with-page-id`, `pcb/with-objects`, `pcb/with-library-data`: set context for following operations. - `pcb/update-shapes ids update-fn`: emits `:mod-obj` with diff-derived `:set` ops. Options include `{:with-objects? true}`, `{:ignore-touched true}`, and `{:attrs #{...}}`. - `pcb/add-objects`, `pcb/change-parent`, `pcb/remove-objects`, `pcb/resize-parents`: shape/tree edits. diff --git a/backend/test/backend_tests/files_update_add_page_validation_test.clj b/backend/test/backend_tests/files_update_add_page_validation_test.clj new file mode 100644 index 0000000000..029f9485ad --- /dev/null +++ b/backend/test/backend_tests/files_update_add_page_validation_test.clj @@ -0,0 +1,112 @@ +;; 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 INC Sucursal en España SL + +(ns backend-tests.files-update-add-page-validation-test + (:require + [app.common.features :as cfeat] + [app.common.types.page :as ctp] + [app.common.types.shape :as cts] + [app.common.uuid :as uuid] + [app.config :as cf] + [app.rpc :as-alias rpc] + [backend-tests.helpers :as th] + [clojure.test :as t])) + +(t/use-fixtures :once th/state-init) +(t/use-fixtures :each th/database-reset) + +(defn- page-with-dangling-parent + "A page whose only shape points at a parent that is not in the page, and + that the page tree cannot reach from the root. It passes the file schema + (geometry is valid) but fails the referential check." + [page-id] + (let [shape (cts/setup-shape + {:id (uuid/next) + :name "rect" + :type :rect + :parent-id (uuid/next) + :frame-id uuid/zero})] + (-> (ctp/make-empty-page {:id page-id :name "Page 2"}) + (assoc-in [:objects (:id shape)] shape)))) + +(defn- add-page-forms + "The forms of `:add-page` that carry a whole page: the editor sends the + page with its id at the top level (duplicate page, undo of a page + deletion), `files/builder.cljc` sends the page alone, and the change + schema also admits a top-level id that differs from the page's." + [page] + {"editor form" {:type :add-page :id (:id page) :page page} + "page-only form" {:type :add-page :page page} + "differing ids" {:type :add-page :id (uuid/next) :page page}}) + +(defn- add-page! + "Sends `change` alone, or as one commit group when `commit-id` is given, + which is how the editor sends its changes." + ([profile file change] + (add-page! profile file change nil)) + ([profile file change commit-id] + (th/command! (cond-> {::th/type :update-file + ::rpc/profile-id (:id profile) + :id (:id file) + :session-id (uuid/next) + :revn 0 + :vern 0 + :features cfeat/supported-features} + (nil? commit-id) + (assoc :changes [change]) + + (some? commit-id) + (assoc :changes-with-metadata + [{:commit-id commit-id :changes [change]}]))))) + +(defn- stored-page + [profile file page-id] + (-> (th/command! {::th/type :get-file + ::rpc/profile-id (:id profile) + :id (:id file)}) + (get-in [:result :data :pages-index page-id]))) + +(t/deftest update-file-checks-a-whole-page-add-page-as-the-flags-say + ;; The page goes through the same affected check as every other page a + ;; save touches, so the deployment's validation flags decide whether an + ;; invalid page is refused or only logged. + (doseq [[mode flags refused?] + [["hard" (conj cf/flags :file-validation) true] + ["soft" (-> cf/flags (disj :file-validation) (conj :soft-file-validation)) false]] + [form change] (add-page-forms (page-with-dangling-parent (uuid/next)))] + (t/testing (str mode ", " form) + (th/database-reset + (fn [] + (let [profile (th/create-profile* 1 {:is-active true}) + file (th/create-file* 1 {:profile-id (:id profile) + :project-id (:default-project-id profile)}) + page-id (get-in change [:page :id])] + (with-redefs [cf/flags flags] + (let [out (add-page! profile file change)] + (if refused? + (do + (t/is (th/ex-of-code? (:error out) :referential-integrity)) + (t/is (nil? (stored-page profile file page-id)))) + (do + (t/is (nil? (:error out))) + (t/is (some? (stored-page profile file page-id))))))))))))) + +(t/deftest update-file-rejects-the-commit-of-an-invalid-whole-page-add-page + ;; A save that names its commits is not refused whole: the answer lists + ;; the rejected commit and the page is not stored. + (let [profile (th/create-profile* 1 {:is-active true}) + file (th/create-file* 1 {:profile-id (:id profile) + :project-id (:default-project-id profile)}) + page-id (uuid/next) + change {:type :add-page :page (page-with-dangling-parent page-id)} + commit-id (uuid/next)] + (with-redefs [cf/flags (conj cf/flags :file-validation)] + (let [out (add-page! profile file change commit-id) + rejected (get-in out [:result :rejected])] + (t/is (nil? (:error out))) + (t/is (= commit-id (:failed-commit-id rejected))) + (t/is (= :referential-integrity (get-in rejected [:error :code]))) + (t/is (nil? (stored-page profile file page-id))))))) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index 9a996e4afc..2291a10d45 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -236,7 +236,7 @@ (apply-changes-local))) (defn add-page - [changes id page] + [changes {:keys [id] :as page}] (-> changes (update :redo-changes conj {:type :add-page :id id :page page}) (update :undo-changes conj {:type :del-page :id id}) diff --git a/common/src/app/common/files/validate.cljc b/common/src/app/common/files/validate.cljc index 78f300b738..758da2b298 100644 --- a/common/src/app/common/files/validate.cljc +++ b/common/src/app/common/files/validate.cljc @@ -936,7 +936,7 @@ page-ids #{} component-ids #{}] (if-let [change (first changes)] - (let [{:keys [type page-id component-id id]} change + (let [{:keys [type page-id component-id id page]} change page-ids (case type ;; Shape-level ops are scoped to either a page or a component @@ -944,8 +944,13 @@ (cond-> page-ids page-id (conj page-id)) - ;; A new or modified page needs a full page sweep - (:add-page :mod-page) + ;; A new or modified page needs a full page sweep. An + ;; `:add-page` that carries the page whole stores it under + ;; the id inside `:page`, whatever the top level says. + :add-page + (conj page-ids (or (:id page) id)) + + :mod-page (conj page-ids id) ;; restore-component resurrects a deleted component (touches the @@ -1052,7 +1057,7 @@ (cond-> page-ids (some? page-id) (conj page-id)) :add-page - (conj page-ids (or id (:id page))) + (conj page-ids (or (:id page) id)) :mod-page (conj page-ids id) diff --git a/common/test/common_tests/files/validate_test.cljc b/common/test/common_tests/files/validate_test.cljc index d36743ee24..d75ba5a470 100644 --- a/common/test/common_tests/files/validate_test.cljc +++ b/common/test/common_tests/files/validate_test.cljc @@ -194,6 +194,24 @@ changes [{:type :add-page :id page1-id}]] (t/is (nil? (cfv/validate-file-affected file' {} changes))))) + (t/testing "add-page that carries the page alone validates that page" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thf/add-sample-page :page2)) + file' (inject-broken-child file :page2) + page2 (ctpl/get-page (:data file') (thi/id :page2)) + changes [{:type :add-page :page page2}]] + (let [errors (cfv/validate-file-affected file' {} changes)] + (t/is (some #(= :child-not-found (:code %)) errors))))) + + (t/testing "add-page validates the page it stores, not the top-level :id" + (let [file (-> (thf/sample-file :file1 :page-label :page1) + (thf/add-sample-page :page2)) + file' (inject-broken-child file :page2) + page2 (ctpl/get-page (:data file') (thi/id :page2)) + changes [{:type :add-page :id (uuid/next) :page page2}]] + (let [errors (cfv/validate-file-affected file' {} changes)] + (t/is (some #(= :child-not-found (:code %)) errors))))) + (t/testing "mod-page with :id=page2 validates page2 (broken → errors)" (let [file (-> (thf/sample-file :file1 :page-label :page1) (thf/add-sample-page :page2)) @@ -491,7 +509,11 @@ :namespace "ns" :key "k" :value "v"}]))) (t/testing "a guide on the invalid page raises" - (t/is (schema-error? file [{:type :set-guide :page-id (thi/id :page2) :id (uuid/next) :params nil}]))))) + (t/is (schema-error? file [{:type :set-guide :page-id (thi/id :page2) :id (uuid/next) :params nil}]))) + + (t/testing "add-page checks the page it stores, not the top-level :id" + (let [page2 (ctpl/get-page (:data file) (thi/id :page2))] + (t/is (schema-error? file [{:type :add-page :id (uuid/next) :page page2}])))))) (t/deftest validate-file-schema-affected!-skips-untouched-pages (let [file (-> (thf/sample-file :file1 :page-label :page1) diff --git a/common/test/common_tests/files_changes_test.cljc b/common/test/common_tests/files_changes_test.cljc index 630a4bb0d6..bd497e72a7 100644 --- a/common/test/common_tests/files_changes_test.cljc +++ b/common/test/common_tests/files_changes_test.cljc @@ -52,6 +52,19 @@ value))) data)) +(t/deftest add-page-uses-the-page-id-for-redo-and-undo + (let [data (make-file-data (uuid/next) (uuid/next)) + page-id (uuid/next) + page (ctp/make-empty-page {:id page-id :name "Duplicate"}) + changes (pcb/add-page (pcb/empty-changes nil) page) + added (ch/process-changes data (:redo-changes changes)) + undone (ch/process-changes added (:undo-changes changes))] + (t/is (= [{:type :add-page :id page-id :page page}] + (:redo-changes changes))) + (t/is (= page (get-in added [:pages-index page-id]))) + (t/is (= (:pages data) (:pages undone))) + (t/is (= (:pages-index data) (:pages-index undone))))) + (defn- assert-idempotent [data changes] ;; Modification timestamps may differ; all other stored data must agree. diff --git a/frontend/src/app/main/data/workspace/pages.cljs b/frontend/src/app/main/data/workspace/pages.cljs index ea76aa091c..04e9205a7f 100644 --- a/frontend/src/app/main/data/workspace/pages.cljs +++ b/frontend/src/app/main/data/workspace/pages.cljs @@ -318,7 +318,7 @@ objects)) changes (-> (pcb/empty-changes it) - (pcb/add-page new-page-id page) + (pcb/add-page page) (pcb/with-page page) (pcb/with-objects objects) ;; Create new components for each duplicated variant main