🐛 Check the page that an add-page change stores (#12675)

* 🐛 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 <alvorithm@teje.ro>

* 🐛 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

* 🐛 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

* ♻️ 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 <alvorithm@teje.ro>
Co-authored-by: Andrey Antukh <niwi@niwi.nz>
This commit is contained in:
Álvaro Tejero-Cantero 2026-11-20 17:47:53 +01:00 committed by GitHub
parent f27bfdf76a
commit 678fcc071b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 160 additions and 7 deletions

View File

@ -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 <it> <page-id>)` or `(pcb/empty-changes nil <page-id>)` 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.

View File

@ -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)))))))

View File

@ -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})

View File

@ -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)

View File

@ -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)

View File

@ -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.

View File

@ -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