From 678fcc071bd63e5c80428475e36b08eef26d5b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Tejero-Cantero?= <807608+alvorithm@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:47:53 +0100 Subject: [PATCH] :bug: Check the page that an add-page change stores (#12675) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :bug: Check pages added with a full page value in soft mode An :add-page change can carry a whole page under :page instead of an id and a name. extract-affected-ids reads the page to check from the change's top-level :id, so that form left the new page unchecked and a referential error in it was stored. Collect those page ids after the changes are applied and run the affected check on them in soft mode: log the file id, the page ids and the error codes, and never refuse the save. This is the first step of the two-step rollout. The follow-up moves the page id into extract-affected-ids and makes the check hard. The counters of #12665 decide when to flip it. Relates to #12666 AI-assisted-by: claude-opus-5-5, deepseek-v4.1-flash Signed-off-by: Álvaro Tejero Cantero * :bug: Collect the page of a whole-page add-page in the affected check An `:add-page` change that carries the page alone, with its id inside `:page` and none at the top level, escaped the referential check: `extract-affected-ids` read only the top-level `:id`. The schema side (`touched-page-ids`) already reads `(or id (:id page))`; the referential side now does the same. This replaces the separate soft-mode check of the first commit. The page now goes through `validate-file-affected` like any page a save touches, so the deployment's flags decide whether an invalid page is refused (`:file-validation`) or only logged (`:soft-file-validation`), and no second log hint or rollout step is needed. AI-assisted-by: claude-opus-5-5 * :bug: Check the page an add-page stores, not its top-level id An :add-page that carries the page whole stores it under the id inside :page. extract-affected-ids and touched-page-ids read the top-level :id first, so a change whose two ids differ stored one page and checked another, which does not exist. Both now read the id of the page first. The unit test of the affected check moves to the common validate tests, with a case for the differing ids on the referential and the schema side. The backend test sends that form through update-file as well, and a new test sends the page as a commit group, the way the editor does, and expects the commit under :rejected and no stored page. AI-assisted-by: claude-fable-5-2 * :recycle: Derive the add-page change ID from the page Use the page ID for both redo and undo so the builder cannot emit conflicting IDs. Keep the change format and the library API unchanged. AI-assisted-by: gpt-6.2-sol --------- Signed-off-by: Álvaro Tejero Cantero Co-authored-by: Andrey Antukh --- .../memories/common/changes-architecture.md | 1 + .../files_update_add_page_validation_test.clj | 112 ++++++++++++++++++ .../src/app/common/files/changes_builder.cljc | 2 +- common/src/app/common/files/validate.cljc | 13 +- .../common_tests/files/validate_test.cljc | 24 +++- .../test/common_tests/files_changes_test.cljc | 13 ++ .../src/app/main/data/workspace/pages.cljs | 2 +- 7 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 backend/test/backend_tests/files_update_add_page_validation_test.clj 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