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.private
4 concurrency.mailboxes classes assocs combinators ;
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 parse-irc-line forward-name ] unit-test
57 { "someuser" } [ ":someuser!n=user@some.where PRIVMSG factorbot :hi"
58 parse-irc-line forward-name ] unit-test
61 ! Test login and nickname set
63 ":some.where 001 factorbot2 :Welcome factorbot2" %push-line
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
76 [ { "JOIN #factortest" } [
78 irc> stream>> out>> lines>> pop
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"
89 in-messages>> 0.1 seconds mailbox-get-timeout
90 [ class ] [ trailing>> ] bi
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
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
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
118 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
119 ":ircserver.net MODE #factortest +ns" %push-line
120 [ mode? ] read-matching-message class
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
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
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
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
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
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
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
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
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
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