AtlatestRepositorysigil-web-client
sigil-web-client / tree / testtest-client.sgl
1
(import (sigil test)2
(sigil web client))4
(test-group "web-client-root?"5
(test "accepts non-zero node handles"6
(assert-true (web-client-root? 1))7
(assert-true (web-client-root? 42)))9
(test "rejects missing or invalid handles"10
(assert-false (web-client-root? 0))11
(assert-false (web-client-root? #f))12
(assert-false (web-client-root? "1"))))14
(test-group "web-client-render"15
(test "returns SXML values directly"16
(assert-equal '(div "Hello") (web-client-render '(div "Hello"))))18
(test "evaluates nullary view procedures"19
(assert-equal '(span "Hi")20
(web-client-render (lambda () '(span "Hi"))))))22
(test-group "web-client-reset!"23
(test "clears retained root state"24
(assert-true (web-client-reset!))))26
;; Normalizing element content into a flat child list. The empty-content case27
;; is load-bearing: '() must stay '() so an emptied element reconciles as28
;; genuinely empty (old-records null => the empty->populated bulk clear fires).29
;; Before the null? case, '() fell to (else (list value)) => (()), a phantom30
;; empty text child that suppressed the clear and leaked foreign children.31
(test-group "web-client-normalize-children"32
(test "empty content stays empty"33
(assert-equal '() (web-client-normalize-children '())))35
(test "#f content is empty"36
(assert-equal '() (web-client-normalize-children #f)))38
(test "a content list passes through unchanged"39
(assert-equal '((span "a") (span "b"))40
(web-client-normalize-children '((span "a") (span "b")))))42
(test "a single atom is wrapped as one child"43
(assert-equal '("hello") (web-client-normalize-children "hello"))))45
;; The pure child-order diff behind the keyed reconciler. Node handles are46
;; plain integers here, exactly as the DOM bridge represents them. Each op is47
;; (node . ref): insert node before ref, ref #f = append at the end.48
(test-group "web-client-child-moves"49
(test "unchanged order produces zero operations"50
(assert-equal '() (web-client-child-moves '(1 2 3) '(1 2 3)))51
(assert-equal '() (web-client-child-moves '(7) '(7)))52
(assert-equal '() (web-client-child-moves '() '())))54
(test "initial population appends every node in order"55
(assert-equal '((1 . #f) (2 . #f) (3 . #f))56
(web-client-child-moves '() '(1 2 3))))58
(test "appending at the end appends only the new nodes"59
(assert-equal '((3 . #f) (4 . #f))60
(web-client-child-moves '(1 2) '(1 2 3 4))))62
(test "inserting in the middle inserts before the displaced node"63
(assert-equal '((2 . 3))64
(web-client-child-moves '(1 3) '(1 2 3))))66
(test "inserting at the front inserts before the old head"67
(assert-equal '((9 . 1))68
(web-client-child-moves '(1 2) '(9 1 2))))70
(test "moving the last node to the front is a single move"71
(assert-equal '((3 . 1))72
(web-client-child-moves '(1 2 3) '(3 1 2))))74
(test "swapping adjacent nodes is a single move"75
(assert-equal '((3 . 2))76
(web-client-child-moves '(1 2 3) '(1 3 2))))78
(test "reversal moves all but the anchor node"79
(assert-equal '((3 . 1) (2 . 1))80
(web-client-child-moves '(1 2 3) '(3 2 1))))82
(test "removal alone needs no moves (dropped nodes are pruned first)"83
;; patch-children removes dropped nodes before diffing, so current84
;; arrives already pruned: '(1 3) against target '(1 3).85
(assert-equal '() (web-client-child-moves '(1 3) '(1 3))))87
(test "mixed insert + reorder + append"88
;; current [1 2 3] -> target [2 9 1 3 4]89
;; 2 moves to front; 9 is new before 1; 3 already after 1; 4 appended.90
(assert-equal '((2 . 1) (9 . 1) (4 . #f))91
(web-client-child-moves '(1 2 3) '(2 9 1 3 4)))))