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_
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 ;
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 [ f %push-line ] 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
73 stream>> [ in>> [ f ] dip push-line ] [ out>> lines>> ] bi
77 [ { "JOIN #factortest" } [
79 irc> stream>> out>> lines>> pop
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"
90 in-messages>> 0.1 seconds mailbox-get-timeout
91 [ class ] [ trailing>> ] bi
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
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
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
119 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
120 ":ircserver.net MODE #factortest +ns" %push-line
121 [ mode? ] read-matching-message class
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
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
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
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
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
172 ! Namelist change notification
173 [ { T{ participant-changed f f f f } } [
174 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
175 ":ircserver.net 353 factorbot @ #factortest :@factorbot " %push-line
176 ":ircserver.net 366 factorbot #factortest :End of /NAMES list." %push-line
177 [ participant-changed? ] read-matching-message
181 [ { T{ participant-changed f "ircuser" +part+ f } } [
182 "#factortest" <irc-channel-chat>
183 H{ { "ircuser" +normal+ } } clone >>participants
184 [ %add-named-chat ] keep
185 ":ircuser!n=user@isp.net QUIT" %push-line
186 [ participant-changed? ] read-matching-message
190 [ { T{ participant-changed f "ircuser" +nick+ "ircuser2" } } [
191 "#factortest" <irc-channel-chat>
192 H{ { "ircuser" +normal+ } } clone >>participants
193 [ %add-named-chat ] keep
194 ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
195 [ participant-changed? ] read-matching-message