From ecea59761f98253c48477de4f4be3d259263321a Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Thu, 12 Nov 2026 01:33:45 +0100 Subject: [PATCH] :sparkles: Generate edits of grid tracks in component histories The random histories edited a grid's direction and its cells, but never its rows and columns. The grid programs build worlds whose first component is a grid, with copies in grids, and add a track edit: add, remove, resize or reorder a row or column, or merge a cell into an area, through the production grid functions the frontend's track editor calls. A main edits all of them; a copy only resizes its tracks, so the edits that move cells reach copies through sync from their main. The model takes the tracks an edit asks for and the default tracks a grid grows after them to hold children that lose their cells. When a copy's inherited tracks shrink, the cells it authors move, shorten or join as the contract keeps them (move into a free slot, else into one track, else into the cell where they would start). Test: random-grid-compositions; 200 programs pass. Focused only; the full common suite runs at the end of this series. Harness only; the benchmark is not run. AI-assisted-by: claude-opus-5-5 --- .../test_helpers/composable/comp/nodes.cljc | 51 +++++++++ .../common/test_helpers/composable/gen.cljc | 78 ++++++++++++- .../common/test_helpers/composable/grid.cljc | 104 +++++++++++++++++- .../test_helpers/composable/oracle.cljc | 22 ++++ .../logic/composable_gen_test.cljc | 12 ++ 5 files changed, 264 insertions(+), 3 deletions(-) diff --git a/common/src/app/common/test_helpers/composable/comp/nodes.cljc b/common/src/app/common/test_helpers/composable/comp/nodes.cljc index 5b9f4dba6f..4789ab1226 100644 --- a/common/src/app/common/test_helpers/composable/comp/nodes.cljc +++ b/common/src/app/common/test_helpers/composable/comp/nodes.cljc @@ -285,6 +285,57 @@ [target action value] (tm/assign-id (->EditGrid target action value))) +;; --------------------------------------------------------------------------- +;; edit-grid-track — edit the rows and columns of a grid +;; +;; The track editor of a grid board bound to `target`: `:add` a track of +;; `:type` (`:row` or `:column`) with value `:track` at `:index`, `:remove` +;; the track at `:index`, `:resize` it to `:track`, `:reorder` it to `:to` +;; (with its content when `:move-content?`), or `:merge` the cell at `:row` +;; and `:column` into an area of `:row-span` by `:column-span`. As the +;; frontend's `add-layout-track`, `remove-layout-track`, +;; `change-layout-track`, `reorder-layout-track` and `merge-cells`: one +;; update of the board, which reassigns its cells, then a layout reflow. +;; --------------------------------------------------------------------------- + +(defrecord EditGridTrack [target action value] + IComponentOperation + (op-kind [_] :user) + + tm/IOperation + (apply-to [this situation] + (let [file (tm/file situation) + page (thf/current-page file) + id (tm/target-shape-id situation target) + {:keys [type index track to move-content? row column row-span column-span]} value + column? (= type :column) + edit (fn [shape objects] + (case action + :add (ctl/add-grid-track type shape track index) + :remove (if column? + (ctl/remove-grid-column shape index objects) + (ctl/remove-grid-row shape index objects)) + :resize (update-in shape [(if column? :layout-grid-columns :layout-grid-rows) index] + merge track) + :reorder (if column? + (ctl/reorder-grid-column shape index to move-content?) + (ctl/reorder-grid-row shape index to move-content?)) + :merge (-> shape + (ctl/resize-cell-area row column row column row-span column-span) + (ctl/assign-cells objects)))) + changes (-> (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) [id] + edit (:objects page) {:with-objects? true}) + (with-layout-reflow id) + (assoc ::layout-reflow? true))] + (-> situation + (tm/with-file (thf/apply-changes file changes)) + (record-changes changes) + (tm/record-application this {:target target :action action :value value}))))) + +(defn edit-grid-track + [target action value] + (tm/assign-id (->EditGridTrack target action value))) + (defprotocol IPropertyCheck "The check dual of an operation that changes a property: whether a shape the test supplies holds the value the operation set." diff --git a/common/src/app/common/test_helpers/composable/gen.cljc b/common/src/app/common/test_helpers/composable/gen.cljc index 2101c0b4d4..e959c14101 100644 --- a/common/src/app/common/test_helpers/composable/gen.cljc +++ b/common/src/app/common/test_helpers/composable/gen.cljc @@ -593,6 +593,42 @@ [{:align-self :end} {:justify-self :center} {:align-self :center :justify-self :end} {:align-self :auto :justify-self :auto}]) +(def grid-track-edits + "Track editor actions: the action and the kind of track it edits." + [[:add :row] [:add :column] [:remove :row] [:remove :column] [:resize :row] [:resize :column] + [:reorder :row] [:reorder :column] [:merge nil]]) + +(def grid-track-values + [{:type :flex :value 1} {:type :fixed :value 40} {:type :auto} {:type :percent :value 50}]) + +(defn- grid-track-edit + "The track edit `property` names on the grid `shape`, with `value` and + `scope` choosing its track, its new value and where it goes; nil when the + grid has no track it can take." + [shape property value scope] + (let [[action type] (nth grid-track-edits (mod (or property 0) (count grid-track-edits))) + value (or value 0) + scope (or scope 0) + count* (count (get shape (if (= type :column) :layout-grid-columns :layout-grid-rows))) + track (nth grid-track-values (mod scope (count grid-track-values)))] + (case action + :add {:action :add :value {:type type :index (mod value (inc count*)) :track track}} + :remove (when (> count* 1) {:action :remove :value {:type type :index (mod value count*)}}) + :resize (when (pos? count*) {:action :resize :value {:type type :index (mod value count*) :track track}}) + :reorder (when (> count* 1) {:action :reorder + :value {:type type :index (mod value count*) :to (mod scope count*) + :move-content? (odd? (quot value count*))}}) + :merge (let [rows (count (:layout-grid-rows shape)) + columns (count (:layout-grid-columns shape)) + cells (filterv #(= 1 (:row-span %) (:column-span %)) (grid/get-cells shape))] + (when (seq cells) + (let [{:keys [row column]} (nth cells (mod value (count cells))) + [row-span column-span] (nth [[1 2] [2 1] [2 2]] (mod scope 3))] + {:action :merge + :value {:row row :column column + :row-span (min row-span (inc (- rows row))) + :column-span (min column-span (inc (- columns column)))}})))))) + (defn- edit-text-style [shape unit value] (let [[style values] (nth text-style-edits (mod (or unit 0) (count text-style-edits))) @@ -615,9 +651,17 @@ [style-property style-values] (nth style-edits (mod (or property 0) (count style-edits))) items (cond->> (if (contains? library-source-kinds kind) (library-candidates situation kind scope) - (candidates situation model (if (#{:style :grid-cell :grid-direction} kind) :edit kind) scope)) - (#{:grid-cell :grid-direction} kind) + (candidates situation model (if (#{:style :grid-cell :grid-direction :grid-track} kind) :edit kind) + (if (= kind :grid-track) true scope))) + (#{:grid-cell :grid-direction :grid-track} kind) (filter #(= :grid (get-in % [:shape :layout]))) + ;; a copy edits its tracks only by size; the edits that move its + ;; cells reach it from its main + (= :grid-track kind) + (keep (fn [{:keys [shape] :as entry}] + (when-let [edit (grid-track-edit shape property value scope)] + (when (or (nil? (:shape-ref shape)) (= :resize (:action edit))) + (assoc entry :grid-track edit))))) (= :grid-cell kind) (filter #(seq (get-in % [:shape :layout-grid-cells]))) (= kind :style) @@ -679,6 +723,8 @@ (case kind (:edit :style) (n/change-property target property value) :grid-direction (n/edit-grid target :direction (nth [:row :column] (mod (or (nth tuple 3 nil) 0) 2))) + :grid-track (n/edit-grid-track target (get-in selected [:grid-track :action]) + (get-in selected [:grid-track :value])) :grid-cell (let [cells (grid/get-cells (:shape selected)) cell (nth cells (mod (or (nth tuple 2 nil) 0) (count cells))) props (nth grid-cell-edits (mod (or (nth tuple 3 nil) 0) (count grid-cell-edits)))] @@ -923,6 +969,7 @@ (update-in [id :geometry-group] shadow/anchor-position (get (all-objects after) id shape) (get (all-objects after) (:parent-id shape)))) (:grid-cell :grid-direction) (grid/edit model shape (:action op) (:value op)) + :grid-track (grid/edit-tracks model shape (:action op) (:value op)) :reset (shadow/clear-subtree model objects id) :update-main (shadow/update-main model objects (all-objects after) id) :detach (shadow/detach model objects (all-objects after) id) @@ -1104,6 +1151,9 @@ (when (#{:grid-cell :grid-direction} kind) (oracle/check-requested-grid (tm/shape-by-id after (get-in resolved [:shape :id])) (:shape resolved) (:action op) (:value op))) + (when (= :grid-track kind) + (oracle/check-requested-tracks (tm/shape-by-id after (get-in resolved [:shape :id])) + (:shape resolved) (:action op) (:value op))) (let [variant-kind (get local-variant-kind kind kind) file-before (kind-file situation library-id) file (kind-file after library-id)] @@ -1303,6 +1353,9 @@ (conj [1 (gen/fmap (fn [index] [[:library-delete index] [:library-sync] [:library-restore index] [:library-sync]]) gen/nat)]) + (contains? (set kinds) :grid-track) + (conj [6 (gen/fmap (fn [[index edit value scope]] [[:grid-track index edit value scope]]) + (gen/tuple gen/nat (gen/choose 0 (dec (count grid-track-edits))) gen/nat gen/nat))]) (every? (set kinds) [:grid-direction :grid-cell]) (conj [2 (gen/fmap (fn [[index cell props direction]] [[:grid-direction index 0 direction 1] [:grid-cell index cell props 1]]) @@ -1422,6 +1475,27 @@ :grid-square (= grid :square) :grid-empty (= grid :empty)}] :ops ops})) +(def grid-kinds (into swap-kinds [:grid-track])) + +(def grid-coverage-kinds + "The kinds the grid programs must cover: the grid edits and the steps that + carry them into copies." + [:grid-track :grid-cell :grid-direction :edit :update-main :sync :undo :redo]) + +(def grid-program-gen + "Worlds whose first component is a grid, with copies in grids, and + histories that edit its tracks and cells." + (gen/let [components (gen/choose 2 3) + depth (gen/choose 1 2) + layout-children gen/boolean + layout-item-margins gen/boolean + grid (gen/elements [nil :square :empty]) + ops (operation-gen grid-kinds)] + {:world [components depth {:variants true :rich-variants true :text true :layout :grid :layout-copies true + :layout-children layout-children :layout-item-margins layout-item-margins + :grid-square (= grid :square) :grid-empty (= grid :empty)}] + :ops ops})) + (def library-kinds (into structure-kinds [:swap :switch :library-sync])) diff --git a/common/src/app/common/test_helpers/composable/grid.cljc b/common/src/app/common/test_helpers/composable/grid.cljc index d1e5a90046..daaebd2979 100644 --- a/common/src/app/common/test_helpers/composable/grid.cljc +++ b/common/src/app/common/test_helpers/composable/grid.cljc @@ -115,6 +115,40 @@ model (cond-> intended derived? (dissoc :layout-grid-cells)))) model)) +(defn- move-track + "`tracks` with the one at `from` moved after the track at `to`, as a drop + on that track puts it; a track dropped on itself stays." + [tracks from to] + (let [track (nth tracks from) + rest (into (subvec tracks 0 from) (subvec tracks (inc from))) + at (cond (< from to) to (= from to) from :else (inc to))] + (into (conj (subvec rest 0 at) track) (subvec rest at)))) + +(defn intended-tracks + "The rows and columns the track edit `action` with `value` gives `shape`." + [shape action {:keys [type index track to]}] + (let [key (if (= type :column) :layout-grid-columns :layout-grid-rows) + tracks (vec (get shape key)) + result {:layout-grid-rows (vec (:layout-grid-rows shape)) + :layout-grid-columns (vec (:layout-grid-columns shape))}] + (case action + :add (assoc result key (into (conj (subvec tracks 0 index) track) (subvec tracks index))) + :remove (assoc result key (into (subvec tracks 0 index) (subvec tracks (inc index)))) + :resize (assoc result key (update tracks index merge track)) + :reorder (assoc result key (move-track tracks index to)) + :merge result))) + +(defn edit-tracks + "A copy authors the tracks a track edit gives it another value." + [model shape action value] + (if (:shape-ref shape) + (reduce-kv (fn [model key tracks] + (if (= tracks (vec (get shape key))) + model + (assoc-in model [(:id shape) key] {key tracks}))) + model (intended-tracks shape action value)) + model)) + (defn- extend-cells [cells before after] (into cells @@ -126,6 +160,67 @@ {:row row :column column :row-span 1 :column-span 1 :position :auto :align-self :auto :justify-self :auto :shapes []}))) +(def ^:private default-cell + {:row-span 1 :column-span 1 :position :auto :align-self :auto :justify-self :auto :shapes []}) + +(defn- fit-cells + "`cells` placed within a grid of `rows` by `columns` that flows in + `direction`, as the contract keeps a copy's cells when its inherited + tracks shrink: cells with children first, then authored empty ones, then + default ones, each where it fits, else in the first free slot, else in + one track; children left without a slot join the cell where theirs + would start. Free slots take default cells." + [cells rows columns direction] + (if (or (zero? rows) (zero? columns)) + [] + (let [coords (sort-by (flow-order direction) + (for [row (range 1 (inc rows)) column (range 1 (inc columns))] + {:row row :column column})) + area (fn [{:keys [row column row-span column-span]}] + (for [row (range row (+ row row-span)) column (range column (+ column column-span))] + [row column])) + fits? (fn [{:keys [row column row-span column-span]}] + (and (<= 1 row (+ row row-span -1) rows) (<= 1 column (+ column column-span -1) columns))) + authored? (fn [cell] (not= (dissoc default-cell :shapes) + (select-keys (merge default-cell cell) (keys (dissoc default-cell :shapes))))) + ordered (sort-by (juxt #(cond (seq (:shapes %)) 0 (authored? %) 1 :else 2) + #(if (fits? %) 0 1) :row :column) + cells) + [placed covered] + (reduce + (fn [[placed covered :as result] cell] + (let [row (min rows (:row cell)) + column (min columns (:column cell)) + bounded (assoc cell :row row :column column + :row-span (min (:row-span cell) (inc (- rows row))) + :column-span (min (:column-span cell) (inc (- columns column)))) + free? #(and (fits? %) (not-any? covered (area %))) + places (fn [cell] (cons cell (map #(merge cell %) coords))) + spot (if (or (seq (:shapes cell)) (authored? cell)) + (or (first (filter free? (places bounded))) + (first (filter free? (places (assoc bounded :row-span 1 :column-span 1))))) + (when (and (<= (:row cell) rows) (<= (:column cell) columns) (free? bounded)) + bounded))] + (cond + spot + [(conj placed spot) (into covered (area spot))] + + (seq (:shapes cell)) + [(mapv (fn [other] + (if (some #{[row column]} (area other)) + (cond-> (update other :shapes into (:shapes cell)) + (= :auto (:position other)) (assoc :position :manual)) + other)) + placed) + covered] + + :else result))) + [[] #{}] ordered)] + (into placed + (for [{:keys [row column]} coords + :when (not (contains? covered [row column]))] + (assoc default-cell :row row :column column)))))) + (defn flow-shapes "The children of the grid `shape` as `:shapes` lists a grid that orders them by the flow of its cells: in reverse flow order, with hidden and @@ -172,7 +267,14 @@ (or (not= prior (get-context shape)) (> (count missing) (count (filter #(empty? (:shapes %)) trimmed))))) reference prior) - cells (if context (extend-cells trimmed prior context) trimmed) + ;; inherited tracks that shrink move or shorten the cells + shrunk? (and prior context + (or (< (:rows context) (:rows prior)) (< (:columns context) (:columns prior)))) + cells (cond + shrunk? (fit-cells trimmed (:rows context) (:columns context) + (or (:direction context) (:layout-grid-dir shape) :row)) + context (extend-cells trimmed prior context) + :else trimmed) order (flow-order (or (:direction context) (:layout-grid-dir shape))) cells (->> cells (sort-by order) fix-after-gap) assigned (set (mapcat :shapes cells)) diff --git a/common/src/app/common/test_helpers/composable/oracle.cljc b/common/src/app/common/test_helpers/composable/oracle.cljc index a6f9a94900..376296a26b 100644 --- a/common/src/app/common/test_helpers/composable/oracle.cljc +++ b/common/src/app/common/test_helpers/composable/oracle.cljc @@ -53,6 +53,28 @@ (when-not (= expected actual) (fail :I9 {:phase :requested-grid :expected expected :actual actual})))) +(defn check-requested-tracks + "The track edit `action` gave `shape` the rows and columns it asked for. + An edit that takes cells from children lets the grid grow default tracks + after its last along its flow, columns for rows and rows for columns, to + hold them again." + [shape before action value] + (let [expected (grid/intended-tracks before action value) + actual {:layout-grid-rows (vec (:layout-grid-rows shape)) + :layout-grid-columns (vec (:layout-grid-columns shape))} + [growing fixed] (if (= :column (:layout-grid-dir before)) + [:layout-grid-rows :layout-grid-columns] + [:layout-grid-columns :layout-grid-rows]) + wanted (get expected growing) + tracks (get actual growing) + grown? (and (= (get expected fixed) (get actual fixed)) + (> (count tracks) (count wanted)) + (= wanted (subvec tracks 0 (count wanted))) + (every? #(= {:type :flex :value 1} %) (subvec tracks (count wanted))))] + (when-not (or (= expected actual) grown?) + (fail :I9 {:phase :requested-tracks :action action :value value + :expected expected :actual actual})))) + (defn snapshot ([situation] (snapshot situation {})) ([situation opts] diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index d1d0c40693..54b2f78b97 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2353,6 +2353,18 @@ (t/is (empty? (cg/coverage-errors @coverage cg/variant-coverage-kinds)) (pr-str (cg/coverage-errors @coverage cg/variant-coverage-kinds))))) +(t/deftest random-grid-compositions + (let [coverage (atom {}) + failure (atom nil)] + (smt/check! + (smt/for [program cg/grid-program-gen] + (cg/check-program program coverage failure)) + :max-size 30 + :default-num coverage-programs) + (println "Grid operation coverage:" (pr-str @coverage)) + (t/is (empty? (cg/coverage-errors @coverage cg/grid-coverage-kinds)) + (pr-str (cg/coverage-errors @coverage cg/grid-coverage-kinds))))) + (t/deftest random-scale-compositions ;; scale programs run several times longer than the others; the gates draw more (let [coverage (atom {})