penpot/common/test/common_tests/logic/text_override_rule_test.cljc
alonso.torres ed7ef53ef4 ✨ Generate and check random component histories
The Phase 4 bug hunt: random component programs, an independent model
of what they should leave, and the regression programs of what they
found. The production fixes they found are the commits before this one,
each with its regression programs; this commit holds the test harness,
the rest of the tests and the memories they grew with. A regression
program that needs the harness runs once this commit is in.

- Generation (composable gen): worlds of components, nested copies,
  variants, libraries (stale and dependent), flex and grid layouts and
  texts; operations for structure, geometry, styles, texts, layout items,
  grid tracks and cells, swaps, variant switches, update main, reset,
  detach, library edits and syncs, undo and redo; shrinking, coverage
  accounting per operation kind, and PENPOT_GEN_SEED, PENPOT_GEN_NUM
  and PENPOT_GEN_HISTORIES to choose the seed, the number of programs
  and their length.
- The oracle: invariants on touched groups, overrides, refs, settled
  versus full sync, design snapshots and the model values, with its
  own model of authored intent (shadow, geometry, grid and variant
  rules) instead of production's results.
- The frontend replays generated programs through the workspace events
  and the completion contract, with undo and redo checks.
- The oracle unit tests, the test runners' registrations, the golden
  masters recorded again where a fix changed them, and the memories of
  the component sync contract and its tests.

Harness only; the benchmark is not run.

Also in this commit:

- Adapt develop's variant set and reload tests to this branch. Develop's tests
  of transforming a component into a variant set call the event with the set's
  id and expect the defining commit to hold only the main. Here the transform
  builds the whole set, its second member included, in one commit:
  transform-in-variant takes an optional variant id, and the tests expect both
  members in that commit.
- Restore the requires and test wiring the rebase merges dropped.
- Generate variant property edits and added variants. The random programs
  switched between the members of a variant set but never changed the set.

AI-assisted-by: claude-opus-5-5
2026-11-07 18:37:51 +01:00

173 lines
8.6 KiB
Clojure

;; 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 SUBSIDIARY SL
(ns common-tests.logic.text-override-rule-test
"The override rule for texts (`thtx/override-units`): which overrides a
copy text must hold against its main."
(:require
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.texts :as thtx]
[app.common.types.text :as txt]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
(t/deftest absent-and-nil-typography-fields-do-not-author-a-reference
(let [base (thtx/content [[["Hello world"]]])
path [:children 0 :children 0 :children 0]
id (thi/new-id! :typography)
file (thi/new-id! :file)
refs {:typography-ref-id id :typography-ref-file file}]
(doseq [[left right expected]
[[{} {:typography-ref-id nil :typography-ref-file nil} #{}]
[{:typography-ref-id nil :typography-ref-file nil} {} #{}]
[refs {} #{:typography}]
[refs (assoc refs :typography-ref-id (thi/new-id! :other-typography)) #{:typography}]
[refs (assoc refs :typography-ref-file (thi/new-id! :other-file)) #{:typography}]]]
(let [clear #(dissoc % :typography-ref-id :typography-ref-file)
main (update-in base path #(merge (clear %) left))
copy (update-in base path #(merge (clear %) right))]
(t/is (= expected (thtx/override-units main copy)))
(t/is (= (if (seq expected) #{:text-content-typography} #{})
(txt/override-groups main copy)))))))
(defn- overrides
"The overrides of `copy` against `main` by the reference rule, checked
against the production one (`txt/override-groups`)."
[main copy]
(let [main (thtx/content main)
copy (thtx/content copy)
units (thtx/override-units main copy)]
(t/is (= (into #{} (map #(keyword (str "text-content-" (name (get {:letters :text} % %))))) units)
(txt/override-groups main copy))
"the production rule agrees")
units))
(t/deftest shared-letters-come-in-stretches-of-three
(t/is (= {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10}
(thtx/shared-letters "Hello world" "Hello world!!!")))
;; "llo world" is shared; the lone "H" is not
(t/is (= (zipmap (range 2 11) (range 2 11))
(thtx/shared-letters "Hello world" "Hallo world")))
(t/is (= {} (thtx/shared-letters "OK" "Ok")))
(t/is (= {} (thtx/shared-letters "Price: $10" "Sale"))))
(def ^:private price [[["Price: "] ["$10" {:weight 700}]]])
(t/deftest an-edit-overrides-only-what-it-changes
(t/testing "typing at the end changes the letters only"
(t/is (= #{:letters} (overrides [[["Hello world"]]] [[["Hello world!!!"]]]))))
(t/testing "retyping all of a two-style text changes the letters only"
(t/is (= #{:letters} (overrides price [[["Sale"]]]))))
(t/testing "a new line in the style of the letter before it changes the letters only"
(t/is (= #{:letters}
(overrides [[["Summer " {:weight 700}] ["sale"]]]
[[["Summer " {:weight 700}] ["sale"]] [["Ends Sunday"]]]))))
(t/testing "bolding a word changes the weight only"
(t/is (= #{:weight} (overrides [[["Hello world"]]] [[["Hello" {:weight 700}] [" world"]]]))))
(t/testing "each style is a unit of its own"
(doseq [[unit copy] {:fills [[["Hello world" {:color :red}]]]
:family [[["Hello world" {:family "Roboto"}]]]
:size [[["Hello world" {:size 18}]]]
:spacing [[["Hello world" {:spacing 2}]]]
:decoration [[["Hello world" {:decoration "underline"}]]]
:case [[["Hello world" {:case "uppercase"}]]]
:align [[{:align "center"} ["Hello world"]]]
:direction [[{:direction "rtl"} ["Hello world"]]]
:line-height [[{:line-height 1.5} ["Hello world"]]]
:valign [{:valign "bottom"} [["Hello world"]]]}]
(t/is (= #{unit} (overrides [[["Hello world"]]] copy)) (str unit))))
(t/testing "a family change keeps the weight though the family names its variants otherwise"
(let [main [[["Hello world" {:weight 700}]]]
copy [[["Hello world" {:family "Roboto" :weight 700}]]]]
(t/is (not= (get-in (thtx/content main) [:children 0 :children 0 :children 0 :font-variant-id])
(get-in (thtx/content copy) [:children 0 :children 0 :children 0 :font-variant-id])))
(t/is (= #{:family} (overrides main copy)))))
(t/testing "a typography sets each unit it changes, and its reference"
(thi/new-id! :file1)
(thi/new-id! :heading)
(t/is (= #{:family :weight :size :spacing :case :line-height :typography}
(overrides [[["Hello world"]]]
(thtx/apply-typography [[["Hello world"]]] :heading
{:family "Inter" :weight 700 :size 24 :spacing 1
:case "uppercase" :line-height 1.4}))))))
(t/deftest the-expected-copies-hold-exactly-their-overrides
;; The copies `text-override-sync-test` expects after a sync, against the
;; main they synced from: each must hold the overrides of its own edits
;; and nothing else.
(doseq [[title main copy expected]
[["bold word, main blue"
[[["Hello world" {:color :blue}]]]
[[["Hello" {:weight 700 :color :blue}] [" world" {:color :blue}]]]
#{:weight}]
["bold word, main letters"
[[["Hello there"]]]
[[["Hello" {:weight 700}] [" there"]]]
#{:weight}]
["bold word, main replaces it"
[[["Goodbye world"]]]
[[["Goodbye world"]]]
#{}]
["bold word, main changes one letter"
[[["Hallo world"]]]
[[["Hallo" {:weight 700}] [" world"]]]
#{:weight}]
["bold letter of a short label"
[[["Ok"]]]
[[["Ok"]]]
#{}]
["retyped price, main family"
[[["Price: " {:family "Roboto"}] ["$10" {:family "Roboto" :weight 700}]]]
[[["Sale" {:family "Roboto"}]]]
#{:letters}]
["new price, main bolds Price"
[[["Price" {:weight 700}] [": "] ["$10" {:weight 700}]]]
[[["Price" {:weight 700}] [": "] ["$20" {:weight 700}]]]
#{:letters}]
["added letters, main colors a word"
[[["Hello "] ["world" {:color :red}]]]
[[["Hello "] ["world!!!" {:color :red}]]]
#{:letters}]
["new line, main family"
[[["Summer " {:family "Roboto" :weight 700}] ["sale" {:family "Roboto"}]]]
[[["Summer " {:family "Roboto" :weight 700}] ["sale" {:family "Roboto"}]]
[["Ends Sunday" {:family "Roboto"}]]]
#{:letters}]
["across runs, main colors the price"
[[["Price: "] ["$10" {:weight 700 :color :red}]]]
[[["Cost: "] ["$10" {:weight 700 :color :red}]]]
#{:letters}]
["worked example, main colors a word"
[[["Hello "] ["world" {:color :red}]]]
[[["Hello" {:weight 700}] [" "] ["world!!!" {:color :red}]]]
#{:letters :weight}]
["worked example, main centers"
[[{:align "center"} ["Hello world"]]]
[[{:align "center"} ["Hello" {:weight 700}] [" world!!!"]]]
#{:letters :weight}]
["worked example, main light"
[[["Hello world" {:weight 300}]]]
[[["Hello" {:weight 700}] [" world!!!"]]]
#{:letters :weight}]]]
(t/is (= expected (overrides main copy)) title)))
(t/deftest wrong-sync-results-show-as-overrides
;; What today's sync leaves (pinned in `text-override-sync-test`) differs
;; from the main where the copy has no override: a scanner finds it.
(t/testing "F78: the bold lands on the wrong letters"
(t/is (= #{:letters :weight}
(overrides [[["Price" {:weight 700}] [": "] ["$10" {:weight 700}]]]
[[["Price: " {:weight 700}] ["$20"]]]))))
(t/testing "F78: the main's red never reaches the shared word"
(t/is (= #{:letters :fills}
(overrides [[["Hello "] ["world" {:color :red}]]]
[[["Hello world!!!"]]]))))
(t/testing "F72: a recolored copy keeps the old size"
(t/is (= #{:fills :size}
(overrides [[["Add to cart" {:size 18}]]]
[[["Add to cart" {:color :red}]]])))))