1 USING: kernel tools.test accessors arrays sequences
2 io io.streams.duplex namespaces threads destructors
3 calendar irc.client.private irc.client irc.messages
4 concurrency.mailboxes classes assocs combinators irc.messages.parser ;
5 EXCLUDE: irc.messages => join ;
6 RENAME: join irc.messages => join_
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 ;
25 : spawn-client ( -- irc-client )
26 "someserver" irc-port "factorbot" f <irc-profile>
30 <test-stream> >>stream
31 dup [ spawn-irc yield ] with-irc-client ;
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 ;
38 : read-matching-message ( chat quot: ( msg -- ? ) -- irc-message )
39 [ in-messages>> 0.1 seconds ] dip mailbox-get-timeout? ;
41 : with-irc ( quot: ( -- ) -- )
42 [ spawn-client ] dip [ irc> terminate-irc ] compose with-irc-client ; inline
44 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
46 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
48 [ { t } [ irc> nick>> me? ] unit-test
50 { "factorbot" } [ irc> nick>> ] unit-test
52 ! { "someuser" } [ "someuser!n=user@some.where" parse-name ] unit-test
54 { "#factortest" } [ ":someuser!n=user@some.where PRIVMSG #factortest :hi"
55 string>irc-message forward-name ] unit-test
57 { "someuser" } [ ":someuser!n=user@some.where PRIVMSG factorbot :hi"
58 string>irc-message forward-name ] unit-test
61 { privmsg "#channel" "hello" } [
62 "#channel" "hello" strings>privmsg
63 [ class ] [ target>> ] [ trailing>> ] tri
66 ! Test login and nickname set
68 ":some.where 001 factorbot2 :Welcome factorbot2" %push-line
74 { V{ "NICK factorbot" "USER factorbot hostname servername :irc.factor" } } [
75 "someserver" irc-port "factorbot" f <irc-profile> <irc-client>
76 [ 2drop <test-stream> t ] >>connect
77 [ connect-irc ] [ stream>> out>> lines>> ] [ terminate-irc ] tri
81 [ { "JOIN #factortest" } [
83 irc> stream>> out>> lines>> pop
87 [ { join_ "#factortest" } [
88 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
89 { ":factorbot!n=factorbo@some.where JOIN :#factortest"
90 ":ircserver.net 353 factorbot @ #factortest :@factorbot "
91 ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
92 ":ircserver.net 477 factorbot #factortest :[ircserver-info] blah blah"
94 in-messages>> 0.1 seconds mailbox-get-timeout
95 [ class ] [ trailing>> ] bi
99 [ { T{ participant-changed f "somebody" +join+ } } [
100 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
101 ":somebody!n=somebody@some.where JOIN :#factortest" %push-line
102 [ participant-changed? ] read-matching-message
106 [ { privmsg "#factortest" "hello" } [
107 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
108 ":somebody!n=somebody@some.where PRIVMSG #factortest :hello" %push-line
109 [ privmsg? ] read-matching-message
110 [ class ] [ target>> ] [ trailing>> ] tri
114 [ { privmsg "factorbot" "hello" } [
115 "ircuser" <irc-nick-chat> [ %add-named-chat ] keep
116 ":ircuser!n=user@isp.net PRIVMSG factorbot :hello" %push-line
117 [ privmsg? ] read-matching-message
118 [ class ] [ target>> ] [ trailing>> ] tri
123 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
124 ":ircserver.net MODE #factortest +ns" %push-line
125 [ mode? ] read-matching-message class
129 ! Participant lists tests
130 [ { H{ { "ircuser" +normal+ } } } [
131 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
132 ":ircuser!n=user@isp.net JOIN :#factortest" %push-line
137 [ { H{ { "ircuser2" +normal+ } } } [
138 "#factortest" <irc-channel-chat>
139 H{ { "ircuser2" +normal+ }
140 { "ircuser" +normal+ } } clone >>participants
141 [ %add-named-chat ] keep
142 ":ircuser!n=user@isp.net PART #factortest" %push-line
147 [ { H{ { "ircuser2" +normal+ } } } [
148 "#factortest" <irc-channel-chat>
149 H{ { "ircuser2" +normal+ }
150 { "ircuser" +normal+ } } clone >>participants
151 [ %add-named-chat ] keep
152 ":ircuser!n=user@isp.net QUIT" %push-line
157 [ { H{ { "ircuser2" +normal+ } } } [
158 "#factortest" <irc-channel-chat>
159 H{ { "ircuser2" +normal+ }
160 { "ircuser" +normal+ } } clone >>participants
161 [ %add-named-chat ] keep
162 ":ircuser2!n=user2@isp.net KICK #factortest ircuser" %push-line
167 [ { H{ { "ircuser2" +normal+ } } } [
168 "#factortest" <irc-channel-chat>
169 H{ { "ircuser" +normal+ } } clone >>participants
170 [ %add-named-chat ] keep
171 ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
176 [ { H{ { "factorbot" +operator+ } { "ircuser" +normal+ } } } [
177 "#factortest" <irc-channel-chat>
178 H{ { "ircuser" +normal+ } } clone >>participants
179 [ %add-named-chat ] keep
180 ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
181 ":ircserver.net 353 factorbot @ #factortest :ircuser2 " %push-line
182 ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
183 ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
184 ":ircserver.net 353 factorbot @ #factortest :ircuser " %push-line
185 ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
190 ! Namelist change notification
191 [ { T{ participant-changed f f f f } } [
192 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
193 ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
194 ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
195 [ participant-changed? ] read-matching-message
199 [ { T{ participant-changed f "ircuser" +part+ f } } [
200 "#factortest" <irc-channel-chat>
201 H{ { "ircuser" +normal+ } } clone >>participants
202 [ %add-named-chat ] keep
203 ":ircuser!n=user@isp.net QUIT" %push-line
204 [ participant-changed? ] read-matching-message
208 [ { T{ participant-changed f "ircuser" +nick+ "ircuser2" } } [
209 "#factortest" <irc-channel-chat>
210 H{ { "ircuser" +normal+ } } clone >>participants
211 [ %add-named-chat ] keep
212 ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
213 [ participant-changed? ] read-matching-message
218 [ { T{ participant-changed f "ircuser" +mode+ "+o" } } [
219 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
220 ":ircserver.net MODE #factortest +o ircuser" %push-line
221 [ participant-changed? ] read-matching-message