AtlatestRepositorysigil-twitch

sigil-twitch / tree / testanalytics-test.sgl

1;;; Tests for (twitch analytics) — analytics module
2;;;
3;;; Tests record construction and JSON parsing for clips and videos.
4
5(import (sigil core)
6 (sigil dict)
7 (sigil string)
8 (sigil struct)
9 (sigil json)
10 (sigil test)
11 (twitch)
12 (twitch analytics))
14;; ---------------------------------------------------------------
15;; Test fixtures
16;; ---------------------------------------------------------------
18(define clip-resource-json
19 (json-decode
20 (string-append
21 "{\"id\": \"AwkwardHelplessSalamanderSwiftRage\","
22 " \"url\": \"https://clips.twitch.tv/AwkwardHelp\","
23 " \"creator_name\": \"ClipperUser\","
24 " \"video_id\": \"1234567890\","
25 " \"game_id\": \"509670\","
26 " \"title\": \"Amazing moment\","
27 " \"view_count\": 5000,"
28 " \"duration\": 30.5}")))
30(define clip-minimal-json
31 (json-decode
32 (string-append
33 "{\"id\": \"clip123\"}")))
35(define video-resource-json
36 (json-decode
37 (string-append
38 "{\"id\": \"335921245\","
39 " \"user_id\": \"141981764\","
40 " \"title\": \"Past Broadcast\","
41 " \"type\": \"archive\","
42 " \"duration\": \"3h21m45s\","
43 " \"view_count\": 8765,"
44 " \"created_at\": \"2026-03-20T14:00:00Z\"}")))
46(define video-minimal-json
47 (json-decode
48 (string-append
49 "{\"id\": \"vid999\"}")))
51;; ---------------------------------------------------------------
52;; Clip record tests
53;; ---------------------------------------------------------------
55(test-group "clip records"
57 (test "clip record construction"
58 (let ((c (twitch-clip id: "clip1"
59 url: "https://clips.twitch.tv/test"
60 creator-name: "User1"
61 title: "Cool clip"
62 view-count: 100
63 duration: 25)))
64 (assert-true (twitch-clip? c))
65 (assert-equal "clip1" (twitch-clip-id c))
66 (assert-equal "Cool clip" (twitch-clip-title c))
67 (assert-equal 100 (twitch-clip-view-count c))
68 (assert-equal 25 (twitch-clip-duration c))))
70 (test "clip record defaults"
71 (let ((c (twitch-clip id: "clip2")))
72 (assert-equal "" (twitch-clip-url c))
73 (assert-equal "" (twitch-clip-creator-name c))
74 (assert-equal 0 (twitch-clip-view-count c))
75 (assert-equal 0 (twitch-clip-duration c)))))
77;; ---------------------------------------------------------------
78;; Video record tests
79;; ---------------------------------------------------------------
81(test-group "video records"
83 (test "video record construction"
84 (let ((v (twitch-video id: "vid1"
85 user-id: "12345"
86 title: "My Stream"
87 type: "archive"
88 duration: "2h30m"
89 view-count: 500)))
90 (assert-true (twitch-video? v))
91 (assert-equal "vid1" (twitch-video-id v))
92 (assert-equal "My Stream" (twitch-video-title v))
93 (assert-equal "archive" (twitch-video-type v))
94 (assert-equal 500 (twitch-video-view-count v))))
96 (test "video record defaults"
97 (let ((v (twitch-video id: "vid2")))
98 (assert-equal "" (twitch-video-user-id v))
99 (assert-equal "" (twitch-video-title v))
100 (assert-equal "" (twitch-video-type v))
101 (assert-equal 0 (twitch-video-view-count v))
102 (assert-equal #f (twitch-video-created-at v)))))
104;; ---------------------------------------------------------------
105;; Clip parsing tests
106;; ---------------------------------------------------------------
108(test-group "clip parsing"
110 (test "parse full clip resource"
111 (let ((c (parse-clip clip-resource-json)))
112 (assert-true (twitch-clip? c))
113 (assert-equal "AwkwardHelplessSalamanderSwiftRage"
114 (twitch-clip-id c))
115 (assert-equal "https://clips.twitch.tv/AwkwardHelp"
116 (twitch-clip-url c))
117 (assert-equal "ClipperUser" (twitch-clip-creator-name c))
118 (assert-equal "1234567890" (twitch-clip-video-id c))
119 (assert-equal "509670" (twitch-clip-game-id c))
120 (assert-equal "Amazing moment" (twitch-clip-title c))
121 (assert-equal 5000 (twitch-clip-view-count c))))
123 (test "parse minimal clip resource"
124 (let ((c (parse-clip clip-minimal-json)))
125 (assert-equal "clip123" (twitch-clip-id c))
126 (assert-equal "" (twitch-clip-url c))
127 (assert-equal 0 (twitch-clip-view-count c)))))
129;; ---------------------------------------------------------------
130;; Video parsing tests
131;; ---------------------------------------------------------------
133(test-group "video parsing"
135 (test "parse full video resource"
136 (let ((v (parse-video video-resource-json)))
137 (assert-true (twitch-video? v))
138 (assert-equal "335921245" (twitch-video-id v))
139 (assert-equal "141981764" (twitch-video-user-id v))
140 (assert-equal "Past Broadcast" (twitch-video-title v))
141 (assert-equal "archive" (twitch-video-type v))
142 (assert-equal "3h21m45s" (twitch-video-duration v))
143 (assert-equal 8765 (twitch-video-view-count v))
144 (assert-equal "2026-03-20T14:00:00Z"
145 (twitch-video-created-at v))))
147 (test "parse minimal video resource"
148 (let ((v (parse-video video-minimal-json)))
149 (assert-equal "vid999" (twitch-video-id v))
150 (assert-equal "" (twitch-video-user-id v))
151 (assert-equal "" (twitch-video-title v))
152 (assert-equal 0 (twitch-video-view-count v)))))
154(run-tests)