]> gitweb.factorcode.org Git - factor.git/blob - extra/irc/client/client-tests.factor
irc.client: correctly handle multiple name-replys
[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 drop ;
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 [ f %push-line ] 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 ] keep
73     stream>> [ in>> [ f ] dip push-line ] [ out>> lines>> ] bi
74 ] unit-test
75
76 ! Test join
77 [ { "JOIN #factortest" } [
78       "#factortest" %join
79       irc> stream>> out>> lines>> pop
80   ] unit-test
81 ] with-irc
82
83 [ { join_ "#factortest" } [
84       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
85       { ":factorbot!n=factorbo@some.where JOIN :#factortest"
86         ":ircserver.net 353 factorbot @ #factortest :@factorbot "
87         ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
88         ":ircserver.net 477 factorbot #factortest :[ircserver-info] blah blah"
89       } [ %push-line ] each
90       in-messages>> 0.1 seconds mailbox-get-timeout
91       [ class ] [ trailing>> ] bi
92   ] unit-test
93 ] with-irc
94
95 [ { T{ participant-changed f "somebody" +join+ } } [
96       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
97       ":somebody!n=somebody@some.where JOIN :#factortest" %push-line
98       [ participant-changed? ] read-matching-message
99   ] unit-test
100 ] with-irc
101
102 [ { privmsg "#factortest" "hello" } [
103       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
104       ":somebody!n=somebody@some.where PRIVMSG #factortest :hello" %push-line
105       [ privmsg? ] read-matching-message
106       [ class ] [ name>> ] [ trailing>> ] tri
107   ] unit-test
108 ] with-irc
109
110 [ { privmsg "factorbot" "hello" } [
111       "ircuser" <irc-nick-chat>  [ %add-named-chat ] keep
112       ":ircuser!n=user@isp.net PRIVMSG factorbot :hello" %push-line
113       [ privmsg? ] read-matching-message
114       [ class ] [ name>> ] [ trailing>> ] tri
115   ] unit-test
116 ] with-irc
117
118 [ { mode } [
119       "#factortest" <irc-channel-chat>  [ %add-named-chat ] keep
120       ":ircserver.net MODE #factortest +ns" %push-line
121       [ mode? ] read-matching-message class
122   ] unit-test
123 ] with-irc
124
125 ! Participant lists tests
126 [ { H{ { "ircuser" +normal+ } } } [
127       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
128       ":ircuser!n=user@isp.net JOIN :#factortest" %push-line
129       participants>>
130   ] unit-test
131 ] with-irc
132
133 [ { H{ { "ircuser2" +normal+ } } } [
134       "#factortest" <irc-channel-chat>
135           H{ { "ircuser2" +normal+ }
136              { "ircuser" +normal+ } } clone >>participants
137       [ %add-named-chat ] keep
138       ":ircuser!n=user@isp.net PART #factortest" %push-line
139       participants>>
140   ] unit-test
141 ] with-irc
142
143 [ { H{ { "ircuser2" +normal+ } } } [
144       "#factortest" <irc-channel-chat>
145           H{ { "ircuser2" +normal+ }
146              { "ircuser" +normal+ } } clone >>participants
147       [ %add-named-chat ] keep
148       ":ircuser!n=user@isp.net QUIT" %push-line
149       participants>>
150   ] unit-test
151 ] with-irc
152
153 [ { H{ { "ircuser2" +normal+ } } } [
154       "#factortest" <irc-channel-chat>
155           H{ { "ircuser2" +normal+ }
156              { "ircuser" +normal+ } } clone >>participants
157       [ %add-named-chat ] keep
158       ":ircuser2!n=user2@isp.net KICK #factortest ircuser" %push-line
159       participants>>
160   ] unit-test
161 ] with-irc
162
163 [ { H{ { "ircuser2" +normal+ } } } [
164       "#factortest" <irc-channel-chat>
165           H{ { "ircuser" +normal+ } } clone >>participants
166       [ %add-named-chat ] keep
167       ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
168       participants>>
169   ] unit-test
170 ] with-irc
171
172 [ { H{ { "factorbot" +operator+ } { "ircuser" +normal+ } } } [
173       "#factortest" <irc-channel-chat>
174           H{ { "ircuser" +normal+ } } clone >>participants
175       [ %add-named-chat ] keep
176       ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
177       ":ircserver.net 353 factorbot @ #factortest :ircuser2 " %push-line
178       ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
179       ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
180       ":ircserver.net 353 factorbot @ #factortest :ircuser " %push-line
181       ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
182       participants>>
183   ] unit-test
184 ] with-irc
185
186 ! Namelist change notification
187 [ { T{ participant-changed f f f f } } [
188       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
189       ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
190       ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
191       [ participant-changed? ] read-matching-message
192   ] unit-test
193 ] with-irc
194
195 [ { T{ participant-changed f "ircuser" +part+ f } } [
196       "#factortest" <irc-channel-chat>
197           H{ { "ircuser" +normal+ } } clone >>participants
198       [ %add-named-chat ] keep
199       ":ircuser!n=user@isp.net QUIT" %push-line
200       [ participant-changed? ] read-matching-message
201   ] unit-test
202 ] with-irc
203
204 [ { T{ participant-changed f "ircuser" +nick+ "ircuser2" } } [
205       "#factortest" <irc-channel-chat>
206           H{ { "ircuser" +normal+ } } clone >>participants
207       [ %add-named-chat ] keep
208       ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
209       [ participant-changed? ] read-matching-message
210   ] unit-test
211 ] with-irc
212
213 ! Mode change
214 [ { T{ participant-changed f "ircuser" +mode+ "+o" } } [
215       "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
216       ":ircserver.net MODE #factortest +o ircuser" %push-line
217       [ participant-changed? ] read-matching-message
218   ] unit-test
219 ] with-irc