]> gitweb.factorcode.org Git - factor.git/blob - extra/irc/client/client-tests.factor
622b5eaa2ce3a20e149920bd8791b2f542d33d21
[factor.git] / extra / irc / client / client-tests.factor
1 USING: kernel tools.test accessors arrays sequences qualified
2        io io.streams.duplex namespaces threads destructors
3        calendar irc.client.private irc.client irc.messages.private
4        concurrency.mailboxes classes assocs combinators ;
5 EXCLUDE: irc.messages => join ;
6 RENAME: join irc.messages => join_
7 IN: irc.client.tests
8
9 ! Streams for testing
10 TUPLE: mb-writer lines last-line disposed ;
11 TUPLE: mb-reader lines disposed ;
12 : <mb-writer> ( -- mb-writer ) V{ } clone V{ } clone f mb-writer boa ;
13 : <mb-reader> ( -- mb-reader ) <mailbox> f mb-reader boa ;
14 : push-line ( line test-reader-stream -- ) lines>> mailbox-put ;
15 : <test-stream> ( -- stream ) <mb-reader> <mb-writer> <duplex-stream> ;
16 M: mb-writer stream-write ( line mb-writer -- ) last-line>> push ;
17 M: mb-writer stream-flush ( mb-writer -- ) drop ;
18 M: mb-reader stream-readln ( mb-reader -- str/f ) lines>> mailbox-get ;
19 M: mb-writer stream-nl ( mb-writer -- )
20     [ [ last-line>> concat ] [ lines>> ] bi push ] keep
21     V{ } clone >>last-line drop ;
22 M: mb-reader dispose f swap push-line ;
23 M: mb-writer dispose drop ;
24
25 : spawn-client ( -- irc-client )
26     "someserver" irc-port "factorbot" f <irc-profile>
27     <irc-client>
28         t >>is-ready
29         t >>is-running
30         <test-stream> >>stream
31     dup [ spawn-irc yield ] with-irc-client ;
32
33 ! to be used inside with-irc-client quotations
34 : %add-named-chat ( chat -- ) irc> attach-chat ;
35 : %push-line ( line -- ) irc> stream>> in>> push-line yield ;
36 : %join ( channel -- ) <irc-channel-chat> irc> attach-chat ;
37
38 : read-matching-message ( chat quot: ( msg -- ? ) -- irc-message )
39     [ in-messages>> 0.1 seconds ] dip mailbox-get-timeout? ;
40
41 : with-irc ( quot: ( -- ) -- )
42     [ spawn-client ] dip [ irc> terminate-irc ] compose with-irc-client ; inline
43
44 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
45 !                       TESTS
46 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47
48 [ { t } [ irc> nick>> me? ] unit-test
49
50   { "factorbot" } [ irc> nick>> ] unit-test
51
52   { "someuser" } [ "someuser!n=user@some.where" parse-name ] unit-test
53
54   { "#factortest" } [ ":someuser!n=user@some.where PRIVMSG #factortest :hi"
55                       parse-irc-line forward-name ] unit-test
56
57   { "someuser" } [ ":someuser!n=user@some.where PRIVMSG factorbot :hi"
58                    parse-irc-line forward-name ] unit-test
59 ] with-irc
60
61 ! Test login and nickname set
62 [ { "factorbot2" } [
63     ":some.where 001 factorbot2 :Welcome factorbot2" %push-line
64     irc> nick>>
65   ] unit-test
66 ] with-irc
67
68 ! Test connect
69 { V{ "NICK factorbot" "USER factorbot hostname servername :irc.factor" } } [
70     "someserver" irc-port "factorbot" f <irc-profile> <irc-client>
71     [ 2drop <test-stream> t ] >>connect
72     [ connect-irc ] [ stream>> out>> lines>> ] [ terminate-irc ] tri
73 ] unit-test
74
75 ! Test join
76 [ { "JOIN #factortest" } [
77       "#factortest" %join
78       irc> stream>> out>> lines>> pop
79   ] unit-test
80 ] with-irc
81
82 [ { join_ "#factortest" } [
83       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
84       { ":factorbot!n=factorbo@some.where JOIN :#factortest"
85         ":ircserver.net 353 factorbot @ #factortest :@factorbot "
86         ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
87         ":ircserver.net 477 factorbot #factortest :[ircserver-info] blah blah"
88       } [ %push-line ] each
89       in-messages>> 0.1 seconds mailbox-get-timeout
90       [ class ] [ trailing>> ] bi
91   ] unit-test
92 ] with-irc
93
94 [ { T{ participant-changed f "somebody" +join+ } } [
95       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
96       ":somebody!n=somebody@some.where JOIN :#factortest" %push-line
97       [ participant-changed? ] read-matching-message
98   ] unit-test
99 ] with-irc
100
101 [ { privmsg "#factortest" "hello" } [
102       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
103       ":somebody!n=somebody@some.where PRIVMSG #factortest :hello" %push-line
104       [ privmsg? ] read-matching-message
105       [ class ] [ name>> ] [ trailing>> ] tri
106   ] unit-test
107 ] with-irc
108
109 [ { privmsg "factorbot" "hello" } [
110       "ircuser" <irc-nick-chat>  [ %add-named-chat ] keep
111       ":ircuser!n=user@isp.net PRIVMSG factorbot :hello" %push-line
112       [ privmsg? ] read-matching-message
113       [ class ] [ name>> ] [ trailing>> ] tri
114   ] unit-test
115 ] with-irc
116
117 [ { mode } [
118       "#factortest" <irc-channel-chat>  [ %add-named-chat ] keep
119       ":ircserver.net MODE #factortest +ns" %push-line
120       [ mode? ] read-matching-message class
121   ] unit-test
122 ] with-irc
123
124 ! Participant lists tests
125 [ { H{ { "ircuser" +normal+ } } } [
126       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
127       ":ircuser!n=user@isp.net JOIN :#factortest" %push-line
128       participants>>
129   ] unit-test
130 ] with-irc
131
132 [ { H{ { "ircuser2" +normal+ } } } [
133       "#factortest" <irc-channel-chat>
134           H{ { "ircuser2" +normal+ }
135              { "ircuser" +normal+ } } clone >>participants
136       [ %add-named-chat ] keep
137       ":ircuser!n=user@isp.net PART #factortest" %push-line
138       participants>>
139   ] unit-test
140 ] with-irc
141
142 [ { H{ { "ircuser2" +normal+ } } } [
143       "#factortest" <irc-channel-chat>
144           H{ { "ircuser2" +normal+ }
145              { "ircuser" +normal+ } } clone >>participants
146       [ %add-named-chat ] keep
147       ":ircuser!n=user@isp.net QUIT" %push-line
148       participants>>
149   ] unit-test
150 ] with-irc
151
152 [ { H{ { "ircuser2" +normal+ } } } [
153       "#factortest" <irc-channel-chat>
154           H{ { "ircuser2" +normal+ }
155              { "ircuser" +normal+ } } clone >>participants
156       [ %add-named-chat ] keep
157       ":ircuser2!n=user2@isp.net KICK #factortest ircuser" %push-line
158       participants>>
159   ] unit-test
160 ] with-irc
161
162 [ { H{ { "ircuser2" +normal+ } } } [
163       "#factortest" <irc-channel-chat>
164           H{ { "ircuser" +normal+ } } clone >>participants
165       [ %add-named-chat ] keep
166       ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
167       participants>>
168   ] unit-test
169 ] with-irc
170
171 [ { H{ { "factorbot" +operator+ } { "ircuser" +normal+ } } } [
172       "#factortest" <irc-channel-chat>
173           H{ { "ircuser" +normal+ } } clone >>participants
174       [ %add-named-chat ] keep
175       ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
176       ":ircserver.net 353 factorbot @ #factortest :ircuser2 " %push-line
177       ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
178       ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
179       ":ircserver.net 353 factorbot @ #factortest :ircuser " %push-line
180       ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
181       participants>>
182   ] unit-test
183 ] with-irc
184
185 ! Namelist change notification
186 [ { T{ participant-changed f f f f } } [
187       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
188       ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
189       ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
190       [ participant-changed? ] read-matching-message
191   ] unit-test
192 ] with-irc
193
194 [ { T{ participant-changed f "ircuser" +part+ f } } [
195       "#factortest" <irc-channel-chat>
196           H{ { "ircuser" +normal+ } } clone >>participants
197       [ %add-named-chat ] keep
198       ":ircuser!n=user@isp.net QUIT" %push-line
199       [ participant-changed? ] read-matching-message
200   ] unit-test
201 ] with-irc
202
203 [ { T{ participant-changed f "ircuser" +nick+ "ircuser2" } } [
204       "#factortest" <irc-channel-chat>
205           H{ { "ircuser" +normal+ } } clone >>participants
206       [ %add-named-chat ] keep
207       ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
208       [ participant-changed? ] read-matching-message
209   ] unit-test
210 ] with-irc
211
212 ! Mode change
213 [ { T{ participant-changed f "ircuser" +mode+ "+o" } } [
214       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
215       ":ircserver.net MODE #factortest +o ircuser" %push-line
216       [ participant-changed? ] read-matching-message
217   ] unit-test
218 ] with-irc