penpot/common/test/common_tests/logic/text_notation_test.cljc
alonso.torres 3e6c98c01a ✨ 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-18 10:29:46 +01:00

153 lines
8.1 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-notation-test
"The text notation of `app.common.test-helpers.texts`."
(:require
[app.common.test-helpers.files :as thf]
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[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 setting-a-named-color-replaces-explicit-fills
(doseq [fills [nil [] [{:fill-color "#123456" :fill-opacity 0.5}]]]
(let [text [[["Library text" {:fills fills :size 18}]]]
styled (thtx/restyle text :color :red)]
(t/is (= [[["Library text" {:color :red :size 18}]]]
(thtx/normalize styled)))
(t/is (= [{:fill-color "#FF0000" :fill-opacity 1}]
(get-in (thtx/content styled) [:children 0 :children 0 :children 0 :fills]))))))
(t/deftest setting-explicit-fills-replaces-a-named-color
(let [styled (thtx/restyle [[["Library text" {:color :red :size 18}]]] :fills nil)]
(t/is (= [[["Library text" {:fills nil :size 18}]]] styled))
(t/is (nil? (get-in (thtx/content styled) [:children 0 :children 0 :children 0 :fills])))))
(defn- get-run
[text]
(first (txt/node-seq txt/is-text-node? (thtx/content text))))
(t/deftest font-family-edit-replaces-the-raw-family-identity
(let [text (thtx/normalize [[["Custom font" {:family "Roboto" :font-id "custom-font"
:weight 700 :font-style "oblique"
:font-variant-id "opaque-variant" :size 18 :color :blue}]]])
before (get-run text)
after (get-run (thtx/restyle text :family "Inter"))]
(t/is (= {:font-family "Inter" :font-id "gfont-inter"}
(select-keys after [:font-family :font-id])))
(t/is (= (select-keys before [:font-weight :font-style :font-variant-id])
(select-keys after [:font-weight :font-style :font-variant-id]))
"a family edit preserves the weight unit's opaque identity")
(t/is (= (select-keys before [:text :font-size :fills])
(select-keys after [:text :font-size :fills])))))
(t/deftest font-weight-edit-replaces-the-raw-variant-identity
(let [text (thtx/normalize [[["Custom font" {:family "Roboto" :font-id "custom-font"
:font-variant-id "opaque-variant" :italic true
:size 18 :color :blue}]]])
before (get-run text)
after (get-run (thtx/restyle text :weight 700))]
(t/is (= {:font-weight "700" :font-style "italic" :font-variant-id "700italic"}
(select-keys after [:font-weight :font-style :font-variant-id])))
(t/is (= (select-keys before [:font-family :font-id])
(select-keys after [:font-family :font-id])))
(t/is (= (select-keys before [:text :font-size :fills])
(select-keys after [:text :font-size :fills])))))
(t/deftest font-style-edit-replaces-the-raw-style-and-variant-identities
(doseq [[italic style variant] [[true "italic" "700italic"] [false "normal" "700"]]]
(let [text (thtx/normalize [[["Custom font" {:family "Roboto" :font-id "custom-font"
:weight 700 :font-style "oblique"
:font-variant-id "opaque-variant" :size 18 :color :blue}]]])
before (get-run text)
after (get-run (thtx/restyle text :italic italic))]
(t/is (= {:font-weight "700" :font-style style :font-variant-id variant}
(select-keys after [:font-weight :font-style :font-variant-id])))
(t/is (= (select-keys before [:font-family :font-id])
(select-keys after [:font-family :font-id])))
(t/is (= (select-keys before [:text :font-size :fills])
(select-keys after [:text :font-size :fills]))))))
(t/deftest other-text-edits-keep-opaque-font-identities
(let [text (thtx/normalize [[["Custom font" {:family "Roboto" :font-id "custom-font"
:weight 700 :font-style "oblique"
:font-variant-id "opaque-variant" :size 18 :color :blue}]]])
before (get-run text)
after (get-run (thtx/restyle text :size 24))]
(t/is (= "24" (:font-size after)))
(t/is (= (dissoc before :font-size) (dissoc after :font-size)))))
(t/deftest content-is-written-as-the-editors-write-it
(let [content (thtx/content [[["Price: "] ["$10" {:weight 700 :color :red}]]])
paragraph (first (txt/node-seq txt/is-paragraph-node? content))
[run1 run2] (txt/node-seq txt/is-text-node? content)]
(t/testing "every run carries every run style, fills included"
(t/is (= (assoc thtx/base-style :text "Price: ") run1))
(t/is (= (assoc thtx/base-style
:text "$10"
:font-weight "700"
:font-variant-id "bold"
:fills [{:fill-color "#FF0000" :fill-opacity 1}])
run2)))
(t/testing "a paragraph carries its styles and those of its first run"
(t/is (= (merge thtx/base-style thtx/base-paragraph-style)
(dissoc paragraph :type :children))))
(t/testing "the root carries the vertical alignment"
(t/is (= "top" (:vertical-align content))))))
(t/deftest view-reads-content-back-into-the-notation
(thi/new-id! :file1)
(thi/new-id! :body)
(doseq [text [[[["Hello world"]]]
[[["Price: "] ["$10" {:weight 700}]]]
[[["Add to cart" {:family "Roboto" :weight 600 :italic true :size 18 :spacing 2
:case "uppercase" :decoration "underline" :color :blue}]]]
[{:valign "bottom"}
[{:align "center" :direction "rtl" :line-height 1.5} ["Summer " {:weight 700}] ["sale"]]
[["Ends Sunday"]]]
[[["Read the guide" {:typography :body}]]]
[[["odd" {:font-variant-id "bold"}]]]]]
(t/is (= text (thtx/normalize text)))))
(t/deftest view-joins-runs-with-equal-styles
(t/is (= [[["Hello world"]]]
(thtx/normalize [[["Hello"] [" "] ["world"]]])))
(t/is (= [[["Hello"]] [[""]]]
(thtx/normalize [[["Hello"] [""]] [[""]]]))))
(t/deftest edits-of-a-text-in-the-notation
(thi/new-id! :file1)
(thi/new-id! :body)
(let [typed (thtx/apply-typography [[["Read"]]] :body {:family "Roboto" :size 16 :line-height 1.6})]
(t/is (= [[{:line-height 1.6} ["Read" {:family "Roboto" :size 16 :typography :body}]]]
typed))
(t/testing "changing a style the typography sets drops the typography"
(t/is (= [[{:line-height 1.6} ["Read" {:family "Roboto" :size 20}]]]
(thtx/restyle typed :size 20)))
(t/is (= [[{:line-height 2} ["Read" {:family "Roboto" :size 16}]]]
(thtx/restyle-paragraphs typed :line-height 2))))
(t/testing "changing another style keeps it"
(t/is (= [[{:line-height 1.6} ["Read" {:family "Roboto" :size 16 :typography :body :color :red}]]]
(thtx/restyle typed :color :red)))))
(t/is (= [[{:align "center"} ["Buy now" {:weight 700}]]
[{:align "center"} ["Ends Sunday" {:weight 700}]]]
(thtx/set-letters [[{:align "center"} ["Add " {:weight 700}] ["to cart"]]]
"Buy now\nEnds Sunday")))
(t/is (= [{:valign "bottom"} [["Hello"]]]
(thtx/set-valign [[["Hello"]]] "bottom"))))
(t/deftest set-text-commits-the-content-through-the-change-path
(let [file (-> (thf/sample-file :file1)
(thtx/add-text-component-with-copy [[["Hello world"]]])
(thtx/set-text :copy-child [[["Hello world" {:color :red}]]]))]
(t/is (= [[["Hello world" {:color :red}]]] (thtx/text-of file :copy-child)))
(t/is (= #{:content-group :text-content-fills}
(:touched (ths/get-shape file :copy-child))))))