nandi/jolt-nativepublic Fork 0
51145dfedf4891af7951cdab36e19534b4474147
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

tests.clj · 320 lines · 14.0 KBClojure Blame HistoryRaw
Write dvui's shape in jolt, on SDL3, with no shared object 109c7e4 Veronika Winters 10d ago1(ns glimmer-jvui.tests
2 "Every check here runs headless: no window, no SDL, no font, no display.
3
4 jvui takes its measurer as a function, so the whole stack — glimmer's
5 reconciler, this backend's walk, jvui's layout and event routing — can be
6 driven with a stub that says eight pixels a character. `jolt test`."
7 (:require [glimmer.ratom :as ra]
Settle the layout before painting it, not after 51145df nandi 9d ago8 [glimmer.backend :as b]
Make a list follow what arrives in it 95540ef nandi 9d ago9 [jvui.widgets :as w]
Write dvui's shape in jolt, on SDL3, with no shared object 109c7e4 Veronika Winters 10d ago10 [glimmer.core :as gui]
11 [glimmer-jvui.core :as jv]
12 [jvui.core :as c]))
13
14(def ^:private failures (atom 0))
15
16(defn- check! [ok? msg]
17 (when-not ok?
18 (swap! failures inc)
19 (println " FAIL:" msg)))
20
21(defn- ctx []
22 (c/context {:size [400 300]
23 :measure (fn [s _] [(* 8.0 (count s)) 16.0])
24 :line-height (fn [_] 16.0)}))
25
26(defn- walk [n] (cons n (mapcat walk (:children @n))))
27(defn- tagged [root tag] (first (filter #(= tag (:tag @%)) (walk root))))
28(defn- labels [root]
29 (map #(str (:label (:props @%)))
30 (filter #(= :label (:tag @%)) (walk root))))
31
32(defn- centre
33 "The middle of the rectangle jvui gave this node on the last walk."
34 [n]
35 (let [[x y w h] (:rect @n)] [(+ x (/ w 2.0)) (+ y (/ h 2.0))]))
36
37(defn- click-at [x y]
38 [{:kind :motion :x x :y y}
39 {:kind :mouse-down :x x :y y :button 1 :clicks 1}
40 {:kind :mouse-up :x x :y y :button 1}])
41
42;; --- the reconciler reaches the backend --------------------------------------
43
44(def ^:private clicks (ra/atom 0))
45(def ^:private on? (ra/atom false))
46
47(defn- app []
48 [:card {}
49 [:title {:label "Counter"}]
50 [:label {:label (str "Count: " (ra/deref clicks))}]
51 [:button {:label "+1" :kind :primary :on-click #(ra/swap! clicks inc)}]
52 [:checkbox {:label "loud" :checked (ra/deref on?)
53 :on-change #(ra/reset! on? %)}]])
54
55(defn- check-tree! []
56 (let [root (jv/root-node)]
57 (gui/mount root :page [app])
58 (check! (some? (tagged root :card)) "the reconciler built a card")
59 (check! (some? (tagged root :button)) "and a button")
60 (check! (some #{"Count: 0"} (labels root))
61 "and a label that read the ratom")))
62
63(defn- check-walk-places-widgets! []
64 (let [root (jv/root-node) cx (ctx)]
65 (gui/mount root :page [app])
66 (jv/render-once root cx)
67 (let [placed (vals (:data @cx))
68 card (first (filter #(and (:rect %) (> (first (:min-size % [0 0])) 100))
69 placed))]
70 (check! (some? card) "the walk placed a container with a rectangle")
71 (check! (every? (fn [[_ _ w h]] (and (>= w 0) (>= h 0)))
72 (keep :rect placed))
73 "and no rectangle came out negative"))))
74
75;; --- a click goes all the way round ------------------------------------------
76
77(defn- check-click-fires-the-handler! []
78 (let [root (jv/root-node) cx (ctx)]
79 (ra/reset! clicks 0)
80 (gui/mount root :page [app])
81 (jv/render-once root cx)
82 (jv/render-once root cx)
83 (let [before (ra/deref clicks)
84 [bx by] (centre (tagged root :button))]
85 (jv/render-once root cx (click-at bx by))
86 (check! (= (inc before) (ra/deref clicks))
87 (str "the click reached :on-click (" before " -> "
88 (ra/deref clicks) ")"))
89 (jv/render-once root cx)
90 (check! (some #{(str "Count: " (ra/deref clicks))} (labels root))
91 "and the reactive re-render reached the tree"))))
92
93(defn- check-press-alone-is-not-a-click! []
94 (let [root (jv/root-node) cx (ctx)]
95 (ra/reset! clicks 0)
96 (gui/mount root :page [app])
97 (jv/render-once root cx)
98 (jv/render-once root cx)
99 (let [[bx by] (centre (tagged root :button))]
100 (jv/render-once root cx [{:kind :motion :x bx :y by}
101 {:kind :mouse-down :x bx :y by :button 1 :clicks 1}]))
102 (check! (zero? (ra/deref clicks)) "a press alone must not fire the handler")
103 (jv/render-once root cx [{:kind :motion :x 390 :y 290}
104 {:kind :mouse-up :x 390 :y 290 :button 1}])
105 (check! (zero? (ra/deref clicks)) "and releasing off it must not either")))
106
107;; --- identity follows the node, not its index --------------------------------
108
109(defn- check-reorder-keeps-identity! []
110 ;; The reason every node carries a serial as its jvui key. Render a list,
111 ;; reorder it, and the ids the walk hands out must travel with the nodes.
112 (let [root (jv/root-node) cx (ctx)
113 items (ra/atom [:a :b :c])
114 list-app (fn []
115 (into [:vbox {}]
116 (for [k (ra/deref items)]
117 ^{:key k} [:button {:label (name k)}])))
118 ids (fn [] (into {} (map (fn [n] [(:label (:props @n)) (:key @n)])
119 (filter #(= :button (:tag @%)) (walk root)))))]
120 (gui/mount root :page [list-app])
121 (jv/render-once root cx)
122 (let [before (ids)]
123 (ra/reset! items [:c :a :b])
124 (jv/render-once root cx)
125 (let [after (ids)]
126 (check! (= 3 (count after)) "the list still has three buttons")
127 (check! (= (get before "a") (get after "a"))
128 "a keyed node keeps its jvui key across a reorder")
129 (check! (= (get before "c") (get after "c"))
130 "including the one that moved to the front")))))
131
132;; --- the vocabulary ----------------------------------------------------------
133
134(defn- check-unknown-tag-is-a-container! []
135 (let [root (jv/root-node) cx (ctx)]
136 (gui/mount root :page [(fn [] [:flerb {} [:label {:label "inside"}]])])
137 (jv/render-once root cx)
138 (check! (some #{"inside"} (labels root))
139 "an unknown tag shows its contents rather than raising")))
140
141(defn- check-entry-round-trips! []
142 (let [root (jv/root-node) cx (ctx)
143 text (ra/atom "")]
144 (gui/mount root :page
145 [(fn [] [:entry {:value (ra/deref text)
146 :on-change #(ra/reset! text %)}])])
147 (jv/render-once root cx)
148 (let [[ex ey] (centre (tagged root :entry))]
149 (jv/render-once root cx (click-at ex ey))) ; take focus
150 (jv/render-once root cx [{:kind :text :text "hi"}])
151 (check! (= "hi" (ra/deref text))
152 (str "typed text reached :on-change: " (pr-str (ra/deref text))))))
153
Report Enter from a field, and take its width request c03a752 nandi 9d ago154(defn- check-enter-sends! []
155 ;; Enter is the one key a field must not swallow. frq sends its message
156 ;; on it, so a compose box that accepted text and never reported Enter
157 ;; would take a message and have no way to say it was finished — typing
158 ;; works, sending does not, and nothing looks broken.
159 (let [root (jv/root-node) cx (ctx)
160 text (ra/atom "hello")
161 sent (ra/atom nil)]
162 (gui/mount root :page
163 [(fn [] [:entry {:value (ra/deref text)
164 :on-change #(ra/reset! text %)
Fire :on-activate with nothing, wrap a row, and break a word that cannot fit 2271a91 nandi 9d ago165 ;; A THUNK, which is what frq's handlers
166 ;; are: s/send-draft! takes no arguments
167 ;; and the text is already the caller's
168 ;; from :on-change.
169 :on-activate #(ra/reset! sent (ra/deref text))}])])
Report Enter from a field, and take its width request c03a752 nandi 9d ago170 (jv/render-once root cx)
171 (let [[ex ey] (centre (tagged root :entry))]
172 (jv/render-once root cx (click-at ex ey))) ; take focus
173 (jv/render-once root cx [{:kind :key-down :key :return}])
174 (check! (= "hello" (ra/deref sent))
175 (str "Enter reached :on-activate: " (pr-str (ra/deref sent))))))
176
Make a list follow what arrives in it 95540ef nandi 9d ago177(defn- label-node [root text]
178 (first (filter #(and (= :label (:tag @%)) (= text (str (:label (:props @%)))))
179 (walk root))))
180
Let a container fill its parent, and report "end" as the word e72d7b0 nandi 9d ago181(defn- check-rows-do-not-stretch! []
182 ;; Every box shrink-wrapping its children is how frq's chat column came
183 ;; out a couple of hundred points wide in a five-hundred-point window,
184 ;; with every message wrapped to match and the scrollbar stranded in the
185 ;; middle of the screen. A container fills its parent's cross axis.
186 ;;
187 ;; And a ROW's children must NOT: :cross rather than :horizontal is what
188 ;; keeps a line of buttons from stretching to fill the window.
189 (let [root (jv/root-node) cx (ctx)] ; a 400-wide context
190 (gui/mount root :page
191 [(fn [] [:vbox {:key :outer}
192 [:vbox {:key :inner} [:label {:label "hi"}]]
193 [:hbox {:key :row} [:button {:label "a"}]
194 [:button {:label "b"}]]])])
195 (dotimes [_ 3] (jv/render-once root cx))
196 ;; Only tags that register for events carry a rect here, so the
197 ;; filling half is checked by pixel in props-check — a card paints a
198 ;; background and a screenshot can measure it. What CAN be asserted
199 ;; from the tree is the half that would regress silently.
200 (let [buttons (filter #(and (= :button (:tag @%)) (:rect @%)) (walk root))]
201 (check! (every? #(< (nth (:rect @%) 2) 120.0) buttons)
202 (str "and buttons in a row do not stretch: "
203 (pr-str (map #(nth (:rect @%) 2) buttons)))))))
204
Make a list follow what arrives in it 95540ef nandi 9d ago205(defn- check-scroll-sticks! []
206 ;; A chat that does not follow new messages is the difference between a
207 ;; window you read and one you drag.
208 ;;
209 ;; Asserted on GEOMETRY and not on which labels exist: every line is in
210 ;; the tree whether or not it is on screen, so `labels` cannot tell a
211 ;; stuck list from a pinned one. What tells them apart is where the last
212 ;; line was PUT — inside the viewport, or far below it.
213 (let [root (jv/root-node) cx (ctx)
214 n (ra/atom 3)]
215 (gui/mount root :page
216 [(fn [] [:scroll {:height 60 :scroll-key "chat" :stick-to-bottom true}
217 (into [:vbox {}]
218 (for [i (range (ra/deref n))]
219 [:label {:key i :label (str "line " i)}]))])])
220 (dotimes [_ 3] (jv/render-once root cx))
221 (ra/reset! n 40)
222 (dotimes [_ 3] (jv/render-once root cx))
223 ;; Forty lines of sixteen in a sixty-tall viewport leaves a long way
224 ;; to scroll. A stuck list is at the far end of it; a pinned one is
225 ;; still at zero.
226 (let [off (w/scroll-offset "chat")]
227 (check! (and off (> off 100.0))
228 (str "a stuck list followed its content: offset=" off))
229 ;; And it must STOP following once the reader has moved, or they can
230 ;; never read anything but the newest line.
231 ;; The mouse has to be OVER the list: a wheel is delivered to
232 ;; whatever is under the pointer, and the pointer only moves on a
233 ;; motion event.
234 (jv/render-once root cx [{:kind :motion :x 20 :y 30}])
235 (dotimes [_ 4]
236 (jv/render-once root cx [{:kind :motion :x 20 :y 30}
237 {:kind :wheel :x 20 :y 30 :dy 3}]))
238 (let [off2 (w/scroll-offset "chat")]
239 (check! (and off2 (< off2 off))
240 (str "and let go when the reader scrolled up: " off " -> " off2))))))
241
Write dvui's shape in jolt, on SDL3, with no shared object 109c7e4 Veronika Winters 10d ago242(defn- check-checkbox-round-trips! []
243 (let [root (jv/root-node) cx (ctx)]
244 (ra/reset! on? false)
245 (gui/mount root :page [app])
246 (jv/render-once root cx)
247 (jv/render-once root cx)
248 (let [[bx by] (centre (tagged root :checkbox))]
249 (jv/render-once root cx (click-at bx by)))
250 (check! (true? (ra/deref on?))
251 (str "the checkbox reached :on-change: " (ra/deref on?)))))
252
253;; --- runner ------------------------------------------------------------------
254
Settle the layout before painting it, not after 51145df nandi 9d ago255(def ^:private rows (ra/atom 1))
256
257(defn- growing []
258 [:vbox {}
259 [:card {}
260 (for [i (range (ra/deref rows))]
261 ^{:key i} [:label {:label (str "line " i)}])]
262 [:button {:label "under"}]])
263
264(defn- check-a-patch-settles-before-it-paints! []
265 ;; What the eye actually catches. A container is as big as what its children
266 ;; asked for LAST frame, so the first walk after the reconciler patches the
267 ;; tree knows the new children and the old sizes: the card already holds four
268 ;; lines and the button under it still sits where one line put it. That frame
269 ;; is painted, and the frame after it is right — a flash of the old layout
270 ;; under the new contents, on every change the client makes.
271 ;;
272 ;; The fix is for the backend to say that it patched something, so jvui
273 ;; spends that walk settling instead of painting. Checked here rather than in
274 ;; jvui's own tests because it takes a reconciler to reproduce: the patch has
275 ;; to arrive BETWEEN two frames, which is the one thing a widget called
276 ;; directly cannot do.
277 ;;
278 ;; `loop-running?` is what makes glimmer defer a re-render onto the frame
279 ;; loop rather than run it inline, which is the arrangement a window is in
280 ;; and the only one where any of this happens.
281 (let [root (jv/root-node) cx (ctx)]
282 (ra/reset! rows 1)
283 (gui/mount root :page [growing])
284 (dotimes [_ 3] (jv/render-once root cx))
285 (reset! b/loop-running? true)
286 (try
287 (let [under #(second (:rect @(tagged root :button)))
288 was (under)]
289 (ra/reset! rows 4)
290 (jv/render-once root cx)
291 (let [painted (under)]
292 (jv/render-once root cx)
293 (check! (not= painted was)
294 "the frame after a patch has moved what the patch moved")
295 (check! (= painted (under))
296 (str "and it is already where it comes to rest: painted at "
297 painted ", settles at " (under)))))
298 (finally (reset! b/loop-running? false)))))
299
Write dvui's shape in jolt, on SDL3, with no shared object 109c7e4 Veronika Winters 10d ago300(def ^:private checks
301 [["the reconciler builds a tree" check-tree!]
302 ["the walk places widgets" check-walk-places-widgets!]
303 ["a click fires the handler" check-click-fires-the-handler!]
304 ["a press alone does not" check-press-alone-is-not-a-click!]
305 ["a reorder keeps identity" check-reorder-keeps-identity!]
306 ["an unknown tag is a container" check-unknown-tag-is-a-container!]
Let a container fill its parent, and report "end" as the word e72d7b0 nandi 9d ago307 ["a row does not stretch" check-rows-do-not-stretch!]
Make a list follow what arrives in it 95540ef nandi 9d ago308 ["a list sticks to the end" check-scroll-sticks!]
Report Enter from a field, and take its width request c03a752 nandi 9d ago309 ["Enter sends" check-enter-sends!]
Write dvui's shape in jolt, on SDL3, with no shared object 109c7e4 Veronika Winters 10d ago310 ["an entry round-trips" check-entry-round-trips!]
Settle the layout before painting it, not after 51145df nandi 9d ago311 ["a checkbox round-trips" check-checkbox-round-trips!]
312 ["a patch settles before it paints" check-a-patch-settles-before-it-paints!]])
Write dvui's shape in jolt, on SDL3, with no shared object 109c7e4 Veronika Winters 10d ago313
314(defn -main [& _]
315 (doseq [[name f] checks]
316 (println "-" name)
317 (f))
318 (if (zero? @failures)
319 (println "\nall" (count checks) "checks passed")
320 (do (println "\n" @failures "failed") (System/exit 1))))