🐛 Keep variant mains and boards when repairing lost main flags

The repair of :variant-not-main removed a variant main that had only
lost :main-instance, with its children. Its component still pointed to
the removed shape and validation reported nothing after it. When the
live component still names the shape as its main, the repair now sets
:main-instance back; it removes only shapes no component claims.

The :ref-shape-not-found repair of the same pass now leaves a main
alone, so it does not give the restored main a :shape-ref.

The repair of :nested-main-not-allowed moved the shape to the page root
every time. It now moves it only when a main surrounds it; a main in a
plain board keeps its place and gets :component-root.

Closes #13257

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Pablo Alba 2026-11-24 12:47:26 +01:00 committed by Pablo Alba
parent db3466aca6
commit a6282d0cdc
2 changed files with 146 additions and 29 deletions

View File

@ -308,11 +308,11 @@
(ctpl/get-page file-data page-id) libraries current
:include-deleted? true))
lost-main (when (some? current)
lost-main (when (and (some? current) (not (ctk/main-instance? current)))
(let [page (ctpl/get-page file-data page-id)]
(find-lost-main file-data (ctn/get-component-shape (:objects page) shape))))
matching-shape (when (some? current)
matching-shape (when (and (some? current) (not (ctk/main-instance? current)))
(let [page (ctpl/get-page file-data page-id)
root-shape (ctn/get-component-shape (:objects page) shape)
component-file (if (= (:component-file root-shape) (:id file-data))
@ -327,7 +327,7 @@
near-shape-1
;; Check if it points to any random shape in the page. If so, try to find a matchng
;; shape in the near main component.
(when-let [random-shape (ctn/get-shape page (:shape-ref shape))]
(when-let [random-shape (some->> (:shape-ref shape) (ctn/get-shape page))]
(if-let [near-shape-2 (d/seek #(= (:id %) (:shape-ref random-shape)) component-shapes)]
near-shape-2
;; If not, check if it's a fostered copy and find a direct main.
@ -356,9 +356,9 @@
;; and link to it. If not, detach the shape.
(log/debug :hint "repairing shape :ref-shape-not-found" :id (:id shape) :name (:name shape) :page-id page-id)
(cond
(or (nil? current) (some? ref-shape))
(or (nil? current) (some? ref-shape) (ctk/main-instance? current))
(do
(log/debug :hint " -> shape removed or ref shape found, nothing to repair")
(log/debug :hint " -> shape removed, ref shape found or shape is a main, nothing to repair")
(pcb/empty-changes nil page-id))
(some? lost-main)
@ -488,17 +488,27 @@
(defmethod repair-error :nested-main-not-allowed
[_ {:keys [shape page-id] :as error} file-data _]
(let [repair-shape
(let [objects (-> (ctpl/get-page file-data page-id) :objects)
current (get objects (:id shape))
inside-main? (some ctk/main-instance? (cfh/get-parents objects (:id shape)))
repair-shape
(fn [shape]
;; Convert the shape in a top main head.
;; Convert the shape in a top main head. It moves to the page
;; root only when an ancestor is a main; otherwise it stays in place.
(log/debug :hint " -> set :component-root")
(assoc shape :component-root true))]
(log/debug :hint "repairing shape :nested-main-not-allowed" :id (:id shape) :name (:name shape) :page-id page-id)
(-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data)
(pcb/update-shapes [(:id shape)] repair-shape)
(pcb/change-parent uuid/zero [shape] nil {:allow-altering-copies true}))))
(if (nil? current)
(do
(log/debug :hint " -> shape removed, nothing to repair")
(pcb/empty-changes nil page-id))
(cond-> (-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data)
(pcb/update-shapes [(:id shape)] repair-shape))
inside-main?
(pcb/change-parent uuid/zero [current] nil {:allow-altering-copies true})))))
(defmethod repair-error :root-copy-not-allowed
[_ {:keys [shape page-id] :as error} file-data _]
@ -857,12 +867,33 @@
(defmethod repair-error :variant-not-main
[_ {:keys [shape page-id]} file-data _]
(let [page (ctpl/get-page file-data page-id)
shape-ids (cfh/get-children-ids-with-self (:objects page) (:id shape))]
current (get (:objects page) (:id shape))
component (ctkl/get-component file-data (:component-id shape))
;; A stale :main-instance-page is fixed by the
;; :invalid-main-instance-page repair once the shape is a main again.
claimed? (and (some? component)
(= (:component-file shape) (:id file-data))
(= (:main-instance-id component) (:id shape)))]
(log/debug :hint "repairing shape :variant-not-main" :id (:id shape) :name (:name shape) :page-id page-id)
(log/debug :hint " -> delete shapes" :shape-ids shape-ids)
(-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data)
(pcb/remove-objects shape-ids))))
(cond
(nil? current)
(do
(log/debug :hint " -> shape removed, nothing to repair")
(pcb/empty-changes nil page-id))
claimed?
(do
(log/debug :hint " -> set :main-instance")
(-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data)
(pcb/update-shapes [(:id shape)] #(assoc % :main-instance true))))
:else
(let [shape-ids (cfh/get-children-ids-with-self (:objects page) (:id shape))]
(log/debug :hint " -> delete shapes" :shape-ids shape-ids)
(-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data)
(pcb/remove-objects shape-ids))))))
(defmethod repair-error :parent-not-variant
[_ {:keys [shape page-id]} file-data _]

View File

@ -403,25 +403,57 @@
(t/is (nil? (ths/get-shape file' :inner)))
(t/is (not-any? removed (:shapes (ths/get-shape file' :variant1))))))
;; Guards against a crash, not a lost fix: without the removed-shape check
;; in the :ref-shape-not-found repair, its lookups throw on the removed
;; root1. Removing the variant's main here is the behaviour #13257 tracks;
;; revisit this fixture when that issue changes it.
;; Guards against a crash: without the removed-shape check in the
;; :ref-shape-not-found repair, its lookups throw on a shape that the
;; :variant-not-main repair removed earlier in the same pass.
(t/testing ":ref-shape-not-found on a shape that an earlier repair of the same pass removed"
(let [file (-> (thf/sample-file :file1 :page-label :page1)
(thv/add-variant :variant1 :component1 :root1 :component2 :root2)
(ths/update-shape :root1 :main-instance nil))
;; A variant-like frame that claims :component1 but is not its main
(ths/add-sample-shape :stray :type :frame :parent-label :variant1
:component-id (thi/id :component1)
:component-file (thi/id :file1)
:component-root true
:variant-id (thi/id :variant1)
:variant-name "Value3"))
errors (cfv/validate-file file {})
result (cfr/repair-file-until-valid
file {}
(fn [file _] (cfv/validate-file file {})))]
result (repair-until-valid file)]
(t/is (= [[:variant-not-main (thi/id :root1)]
[:invalid-main-instance (thi/id :root1)]
[:ref-shape-not-found (thi/id :root1)]]
(t/is (= [[:variant-not-main (thi/id :stray)]
[:variant-main-bad-variant-name (thi/id :stray)]
[:ref-shape-not-found (thi/id :stray)]]
(mapv (juxt :code :shape-id) errors)))
(t/is (= 1 (:iterations result)))
(t/is (nil? (:errors result))))))
(t/is (nil? (ths/get-shape (:file result) :stray)))
(t/is (nil? (:errors result)))
(t/is (true? (:main-instance (ths/get-shape (:file result) :root1))))
(t/is (= (thi/id :root1)
(:main-instance-id (thc/get-component (:file result) :component1)))))))
(t/deftest repair-nested-main-not-allowed
(t/testing "a main in a plain board keeps its place and gets :component-root"
(let [file (-> (thf/sample-file :file1 :page-label :page1)
(ths/add-sample-shape :outer :type :frame)
(ths/add-sample-shape :root1 :type :frame :parent-label :outer)
(thc/make-component :component1 :root1)
(ths/update-shape :root1 :component-root nil))
result (repair-until-valid file)
root' (ths/get-shape (:file result) :root1)]
(t/is (nil? (:errors result)))
(t/is (true? (:component-root root')))
(t/is (= (thi/id :outer) (:parent-id root')))))
(t/testing "a main inside another main moves to the page root"
(let [file (-> (thf/sample-file :file1 :page-label :page1)
(ths/add-sample-shape :outer :type :frame)
(thc/make-component :component1 :outer)
(ths/add-sample-shape :root1 :type :frame :parent-label :outer)
(thc/make-component :component2 :root1)
(ths/update-shape :root1 :component-root nil))
result (repair-until-valid file)
root' (ths/get-shape (:file result) :root1)]
(t/is (nil? (:errors result)))
(t/is (true? (:component-root root')))
(t/is (= uuid/zero (:parent-id root'))))))
(t/deftest repair-variant-not-main
(t/testing "detect and repair a non-main-instance shape inside a variant container"
@ -1418,6 +1450,60 @@
(t/is (= "SoloMain" (:path comp')))
(t/is (= "ab" (:name comp')))))
(t/deftest repair-variant-not-main-claimed-by-component
(t/testing "a variant main that lost :main-instance gets it back"
(let [file (-> (thf/sample-file :file1 :page-label :page1)
(thv/add-variant :variant1 :component1 :root1 :component2 :root2)
(ths/add-sample-shape :child1 :type :rect :parent-label :root1)
(ths/update-shape :root1 :main-instance nil))
result (repair-until-valid file)
root' (ths/get-shape (:file result) :root1)]
(t/is (nil? (:errors result)))
;; One pass: the :ref-shape-not-found of the same pass leaves the main alone
(t/is (= 1 (:iterations result)))
(t/is (true? (:main-instance root')))
(t/is (nil? (:shape-ref root')))
(t/is (= (thi/id :variant1) (:variant-id root')))
(t/is (= [(thi/id :child1)] (:shapes root')))))
(t/testing "a variant main whose component has a stale page gets :main-instance back"
(let [file (-> (thf/sample-file :file1 :page-label :page1)
(thv/add-variant :variant1 :component1 :root1 :component2 :root2)
(ths/add-sample-shape :child1 :type :rect :parent-label :root1)
(ths/update-shape :root1 :main-instance nil)
(thc/update-component :component1 {:main-instance-page (uuid/next)}))
result (repair-until-valid file)
root' (ths/get-shape (:file result) :root1)
comp' (thc/get-component (:file result) :component1)]
(t/is (nil? (:errors result)))
(t/is (true? (:main-instance root')))
(t/is (= [(thi/id :child1)] (:shapes root')))
(t/is (= (thi/id :root1) (:main-instance-id comp')))
(t/is (= (thi/id :page1) (:main-instance-page comp')))))
(t/testing "a claimed variant main off its board is restored, then detached to a plain main"
(let [file (-> (live-solo-file (uuid/next) (uuid/next) [{:name "SizeBig" :value "ab"}])
(ths/update-shape :root1 :main-instance nil))
result (repair-until-valid file)
root' (ths/get-shape (:file result) :root1)
comp' (thc/get-component (:file result) :component1)]
(t/is (nil? (:errors result)))
(t/is (true? (:main-instance root')))
(t/is (nil? (:variant-id root')))
(t/is (nil? (:variant-id comp')))))
(t/testing "a variant main of a deleted component is removed with its children"
(let [file (-> (thf/sample-file :file1 :page-label :page1)
(thv/add-variant :variant1 :component1 :root1 :component2 :root2)
(ths/add-sample-shape :child1 :type :rect :parent-label :root1)
(ths/update-shape :root1 :main-instance nil)
(thc/update-component :component1 {:deleted true}))
result (repair-until-valid file)]
(t/is (nil? (:errors result)))
(t/is (nil? (ths/get-shape (:file result) :root1)))
(t/is (nil? (ths/get-shape (:file result) :child1)))
(t/is (some? (ths/get-shape (:file result) :root2))))))
(t/deftest repair-variant-main-outside-board
(t/testing "a live solo on an ordinary frame reports only :variant-main-outside-board among the variant codes"
(let [variant-id (uuid/next)