AtlatestRepositorysigil-twitch

sigil-twitch / tree / testeventsub-test.sgl

1;;; Tests for (twitch eventsub) — EventSub module
2;;;
3;;; Tests message parsing, session extraction, and notification handling
4;;; using fixture data (no real WebSocket connection).
5
6(import (sigil core)
7 (sigil dict)
8 (sigil string)
9 (sigil struct)
10 (sigil json)
11 (sigil test)
12 (twitch)
13 (twitch eventsub))
15;; ---------------------------------------------------------------
16;; Test fixtures — WebSocket message samples
17;; ---------------------------------------------------------------
19(define welcome-message-json
20 (string-append
21 "{\"metadata\": {"
22 " \"message_id\": \"96a3f3b5-5dec-4eed-908e-e11ee657416c\","
23 " \"message_type\": \"session_welcome\","
24 " \"message_timestamp\": \"2026-03-25T14:00:00Z\""
25 " },"
26 " \"payload\": {"
27 " \"session\": {"
28 " \"id\": \"AQoQILE98fqzi_WP7l4b-38N4A\","
29 " \"status\": \"connected\","
30 " \"keepalive_timeout_seconds\": 30,"
31 " \"reconnect_url\": null"
32 " }"
33 " }}"))
35(define notification-message-json
36 (string-append
37 "{\"metadata\": {"
38 " \"message_id\": \"befa7b53-d79d-478f-86b9-120f112b044e\","
39 " \"message_type\": \"notification\","
40 " \"message_timestamp\": \"2026-03-25T14:05:00Z\","
41 " \"subscription_type\": \"stream.online\","
42 " \"subscription_version\": \"1\""
43 " },"
44 " \"payload\": {"
45 " \"subscription\": {"
46 " \"id\": \"f1c2a387-161a-49f9-a165-0f21d7a4e1c4\","
47 " \"type\": \"stream.online\","
48 " \"version\": \"1\""
49 " },"
50 " \"event\": {"
51 " \"id\": \"9001\","
52 " \"broadcaster_user_id\": \"141981764\","
53 " \"broadcaster_user_login\": \"twitchdev\","
54 " \"broadcaster_user_name\": \"TwitchDev\","
55 " \"type\": \"live\","
56 " \"started_at\": \"2026-03-25T14:05:00Z\""
57 " }"
58 " }}"))
60(define keepalive-message-json
61 (string-append
62 "{\"metadata\": {"
63 " \"message_id\": \"keepalive-1\","
64 " \"message_type\": \"session_keepalive\","
65 " \"message_timestamp\": \"2026-03-25T14:01:00Z\""
66 " },"
67 " \"payload\": {}}"))
69(define reconnect-message-json
70 (string-append
71 "{\"metadata\": {"
72 " \"message_id\": \"reconnect-1\","
73 " \"message_type\": \"session_reconnect\","
74 " \"message_timestamp\": \"2026-03-25T14:10:00Z\""
75 " },"
76 " \"payload\": {"
77 " \"session\": {"
78 " \"id\": \"AQoQILE98fqzi_WP7l4b-38N4A\","
79 " \"status\": \"reconnecting\","
80 " \"keepalive_timeout_seconds\": 30,"
81 " \"reconnect_url\": \"wss://eventsub.wss.twitch.tv/ws?token=abc\""
82 " }"
83 " }}"))
85;; ---------------------------------------------------------------
86;; Session record tests
87;; ---------------------------------------------------------------
89(test-group "eventsub session records"
91 (test "session record construction"
92 (let ((s (twitch-eventsub-session
93 id: "session1"
94 status: "connected"
95 keepalive-timeout: 30)))
96 (assert-true (twitch-eventsub-session? s))
97 (assert-equal "session1" (twitch-eventsub-session-id s))
98 (assert-equal "connected" (twitch-eventsub-session-status s))
99 (assert-equal 30 (twitch-eventsub-session-keepalive-timeout s))
100 (assert-equal #f (twitch-eventsub-session-reconnect-url s))))
102 (test "session record defaults"
103 (let ((s (twitch-eventsub-session id: "s2")))
104 (assert-equal "connected" (twitch-eventsub-session-status s))
105 (assert-equal 10 (twitch-eventsub-session-keepalive-timeout s)))))
107;; ---------------------------------------------------------------
108;; Message type detection tests
109;; ---------------------------------------------------------------
111(test-group "message type detection"
113 (test "welcome message type"
114 (let ((msg (parse-eventsub-message welcome-message-json)))
115 (assert-equal "session_welcome" (eventsub-message-type msg))))
117 (test "notification message type"
118 (let ((msg (parse-eventsub-message notification-message-json)))
119 (assert-equal "notification" (eventsub-message-type msg))))
121 (test "keepalive message type"
122 (let ((msg (parse-eventsub-message keepalive-message-json)))
123 (assert-equal "session_keepalive" (eventsub-message-type msg))))
125 (test "reconnect message type"
126 (let ((msg (parse-eventsub-message reconnect-message-json)))
127 (assert-equal "session_reconnect" (eventsub-message-type msg)))))
129;; ---------------------------------------------------------------
130;; Session extraction tests
131;; ---------------------------------------------------------------
133(test-group "session extraction from welcome"
135 (test "extract session from welcome message"
136 (let* ((msg (parse-eventsub-message welcome-message-json))
137 (session (eventsub-session-from-welcome msg)))
138 (assert-true (twitch-eventsub-session? session))
139 (assert-equal "AQoQILE98fqzi_WP7l4b-38N4A"
140 (twitch-eventsub-session-id session))
141 (assert-equal "connected"
142 (twitch-eventsub-session-status session))
143 (assert-equal 30
144 (twitch-eventsub-session-keepalive-timeout session))
145 (assert-equal #f
146 (twitch-eventsub-session-reconnect-url session))))
148 (test "extract session from reconnect message"
149 (let* ((msg (parse-eventsub-message reconnect-message-json))
150 (session (eventsub-session-from-welcome msg)))
151 (assert-equal "reconnecting"
152 (twitch-eventsub-session-status session))
153 (assert-equal "wss://eventsub.wss.twitch.tv/ws?token=abc"
154 (twitch-eventsub-session-reconnect-url session)))))
156;; ---------------------------------------------------------------
157;; Notification extraction tests
158;; ---------------------------------------------------------------
160(test-group "notification extraction"
162 (test "extract event from notification"
163 (let* ((msg (parse-eventsub-message notification-message-json))
164 (event (eventsub-notification-event msg)))
165 (assert-true (dict? event))
166 (assert-equal "141981764"
167 (dict-ref event broadcaster_user_id:))
168 (assert-equal "live" (dict-ref event type:))
169 (assert-equal "2026-03-25T14:05:00Z"
170 (dict-ref event started_at:))))
172 (test "extract subscription type from notification"
173 (let ((msg (parse-eventsub-message notification-message-json)))
174 (assert-equal "stream.online"
175 (eventsub-notification-subscription-type msg)))))
177(run-tests)