AtlatestRepositorysigil-twitch

sigil-twitch / tree / testchat-test.sgl

1;;; Tests for (twitch chat) — chat module
2;;;
3;;; Tests response parsing, body building, and data transformation
4;;; for chat messages, chatters, settings, and announcements.
5
6(import (sigil core)
7 (sigil dict)
8 (sigil string)
9 (sigil struct)
10 (sigil json)
11 (sigil test)
12 (twitch)
13 (twitch chat))
15;; ---------------------------------------------------------------
16;; Test fixtures — JSON response samples
17;; ---------------------------------------------------------------
19(define chatters-response-json
20 (json-decode
21 (string-append
22 "{\"data\": ["
23 " {\"user_id\": \"128393656\","
24 " \"user_login\": \"smittysmithers\","
25 " \"user_name\": \"SmittySmithers\"},"
26 " {\"user_id\": \"141981764\","
27 " \"user_login\": \"twitchdev\","
28 " \"user_name\": \"TwitchDev\"}"
29 "],"
30 " \"pagination\": {\"cursor\": \"eyJiI456\"},"
31 " \"total\": 8}")))
33(define chatters-empty-json
34 (json-decode
35 (string-append
36 "{\"data\": [],"
37 " \"pagination\": {},"
38 " \"total\": 0}")))
40(define chat-settings-response-json
41 (json-decode
42 (string-append
43 "{\"data\": [{"
44 " \"emote_mode\": false,"
45 " \"follower_mode\": true,"
46 " \"follower_mode_duration\": 10,"
47 " \"slow_mode\": true,"
48 " \"slow_mode_wait_time\": 30,"
49 " \"subscriber_mode\": false,"
50 " \"unique_chat_mode\": false"
51 "}]}")))
53(define chat-settings-empty-json
54 (json-decode "{\"data\": []}"))
56;; ---------------------------------------------------------------
57;; Chatters response parsing tests
58;; ---------------------------------------------------------------
60(test-group "chatters response parsing"
62 (test "parse full chatters response"
63 (let ((result (parse-chatters-response chatters-response-json)))
64 (assert-equal 8 (dict-ref result total:))
65 (assert-equal "eyJiI456" (dict-ref result cursor:))
66 (let ((users (dict-ref result data:)))
67 (assert-equal 2 (length users))
68 (let ((first-user (car users)))
69 (assert-equal "128393656" (dict-ref first-user user-id:))
70 (assert-equal "smittysmithers" (dict-ref first-user user-login:))
71 (assert-equal "SmittySmithers" (dict-ref first-user user-name:)))
72 (let ((second-user (cadr users)))
73 (assert-equal "141981764" (dict-ref second-user user-id:))
74 (assert-equal "twitchdev" (dict-ref second-user user-login:))))))
76 (test "parse empty chatters response"
77 (let ((result (parse-chatters-response chatters-empty-json)))
78 (assert-equal 0 (dict-ref result total:))
79 (assert-equal #f (dict-ref result cursor:))
80 (assert-equal '() (dict-ref result data:)))))
82;; ---------------------------------------------------------------
83;; Chat settings parsing tests
84;; ---------------------------------------------------------------
86(test-group "chat settings parsing"
88 (test "parse full chat settings response"
89 (let ((result (parse-chat-settings chat-settings-response-json)))
90 (assert-equal #f (dict-ref result emote-mode:))
91 (assert-equal #t (dict-ref result follower-mode:))
92 (assert-equal 10 (dict-ref result follower-mode-duration:))
93 (assert-equal #t (dict-ref result slow-mode:))
94 (assert-equal 30 (dict-ref result slow-mode-wait-time:))
95 (assert-equal #f (dict-ref result subscriber-mode:))
96 (assert-equal #f (dict-ref result unique-chat-mode:))))
98 (test "parse empty chat settings response"
99 (let ((result (parse-chat-settings chat-settings-empty-json)))
100 (assert-true (dict? result))
101 ;; Empty data array returns empty dict
102 (assert-equal #f (dict-ref result emote-mode: #f)))))
104;; ---------------------------------------------------------------
105;; Chat message body building tests
106;; ---------------------------------------------------------------
108(test-group "chat message body building"
110 (test "build basic message body"
111 (let ((body (build-chat-message-body "12345" "67890" "Hello chat!")))
112 (assert-equal "12345" (dict-ref body broadcaster_id:))
113 (assert-equal "67890" (dict-ref body sender_id:))
114 (assert-equal "Hello chat!" (dict-ref body message:))
115 ;; No reply ID when not specified
116 (assert-equal #f (dict-ref body reply_parent_message_id: #f))))
118 (test "build reply message body"
119 (let ((body (build-chat-message-body "12345" "67890" "Reply!"
120 #{ reply-parent-message-id: "msg-abc-123" })))
121 (assert-equal "12345" (dict-ref body broadcaster_id:))
122 (assert-equal "67890" (dict-ref body sender_id:))
123 (assert-equal "Reply!" (dict-ref body message:))
124 (assert-equal "msg-abc-123" (dict-ref body reply_parent_message_id:)))))
126;; ---------------------------------------------------------------
127;; Announcement body building tests
128;; ---------------------------------------------------------------
130(test-group "announcement body building"
132 (test "build announcement without color"
133 (let ((body (build-announcement-body "Important news!")))
134 (assert-equal "Important news!" (dict-ref body message:))
135 (assert-equal #f (dict-ref body color: #f))))
137 (test "build announcement with color"
138 (let ((body (build-announcement-body "Breaking news!" "blue")))
139 (assert-equal "Breaking news!" (dict-ref body message:))
140 (assert-equal "blue" (dict-ref body color:))))
142 (test "build announcement with each valid color"
143 (for-each
144 (lambda (color)
145 (let ((body (build-announcement-body "Test" color)))
146 (assert-equal color (dict-ref body color:))))
147 '("blue" "green" "orange" "purple" "primary"))))
149(run-tests)