Debian Codegraph @

t/04_plugins/14_logger/03_private.t

raw
1 use strict;
2 use warnings FATAL => 'all';
3 use lib 't/inc';
4 use File::Temp qw(tempdir);
5 use File::Spec::Functions qw(catfile);
6 use POE;
7 use POE::Component::IRC::State;
8 use POE::Component::IRC::Plugin::Logger;
9 use POE::Component::Server::IRC;
10 use Test::More;
11
12 my $log_dir = tempdir(CLEANUP => 1);
13
14 my $bot1 = POE::Component::IRC::State->spawn(
15 Flood => 1,
16 plugin_debug => 1,
17 );
18 my $bot2 = POE::Component::IRC::State->spawn(
19 Flood => 1,
20 plugin_debug => 1,
21 );
22 my $ircd = POE::Component::Server::IRC->spawn(
23 Auth => 0,
24 AntiFlood => 0,
25 );
26
27 $bot2->plugin_add(Logger => POE::Component::IRC::Plugin::Logger->new(
28 Path => $log_dir,
29 Notices => 1,
30 ));
31
32 my $file = catfile($log_dir, 'testbot1.log');
33 unlink $file if -e $file;
34
35 my @correct = (
36 '<TestBot1> Hello there',
37 '<TestBot2> Hi yourself',
38 '* TestBot1 is talking',
39 '* TestBot2 is too',
40 '>TestBot1< This is a notice',
41 '>TestBot2< So is this',
42 );
43
44 plan tests => 8 + @correct;
45
46 POE::Session->create(
47 package_states => [
48 main => [qw(
49 _start
50 ircd_listener_add
51 ircd_listener_failure
52 _shutdown
53 irc_001
54 irc_msg
55 irc_ctcp_action
56 irc_notice
57 irc_disconnected
58 )],
59 ],
60 );
61
62 $poe_kernel->run();
63
64 sub _start {
65 my ($kernel) = $_[KERNEL];
66
67 $ircd->yield('register', 'all');
68 $ircd->yield('add_listener');
69 $kernel->delay(_shutdown => 60, 'Timed out');
70 }
71
72 sub ircd_listener_failure {
73 my ($kernel, $op, $reason) = @_[KERNEL, ARG1, ARG3];
74 $kernel->yield('_shutdown', "$op: $reason");
75 }
76
77 sub ircd_listener_add {
78 my ($kernel, $port) = @_[KERNEL, ARG0];
79
80 $bot1->yield(register => 'all');
81 $bot1->yield(connect => {
82 nick => 'TestBot1',
83 server => '127.0.0.1',
84 port => $port,
85 });
86
87 $bot2->yield(register => 'all');
88 $bot2->yield(connect => {
89 nick => 'TestBot2',
90 server => '127.0.0.1',
91 port => $port,
92 });
93 }
94
95 sub _shutdown {
96 my ($kernel, $error) = @_[KERNEL, ARG0];
97 fail($error) if defined $error;
98
99 $kernel->alarm_remove_all();
100 $ircd->yield('shutdown');
101 $bot1->yield('shutdown');
102 $bot2->yield('shutdown');
103 }
104
105 sub irc_001 {
106 my ($heap, $server) = @_[HEAP, ARG0];
107 my $irc = $_[SENDER]->get_heap();
108
109 pass($irc->nick_name() . ' logged in');
110 $heap->{logged_in}++;
111 return if $heap->{logged_in} != 2;
112
113 $bot1->yield(privmsg => $bot2->nick_name(), 'Hello there');
114 $heap->{msg}++;
115 }
116
117 sub irc_msg {
118 my $heap = $_[HEAP];
119
120 pass('irc_msg');
121 if ($heap->{msg} == 1) {
122 $bot2->yield(privmsg => $bot1->nick_name(), 'Hi yourself');
123 $heap->{msg}++;
124 }
125 elsif ($heap->{msg} == 2) {
126 $bot1->yield(ctcp => $bot2->nick_name(), 'ACTION is talking');
127 $heap->{msg}++;
128 }
129 }
130
131 sub irc_ctcp_action {
132 my $heap = $_[HEAP];
133
134 pass('irc_ctcp_action');
135 if ($heap->{msg} == 3) {
136 $bot2->yield(ctcp => $bot1->nick_name(), 'ACTION is too');
137 $heap->{msg}++;
138 }
139 elsif ($heap->{msg} == 4) {
140 $bot1->yield(notice => $bot2->nick_name(), 'This is a notice');
141 $heap->{msg}++;
142 }
143 }
144
145 sub irc_notice {
146 my $heap = $_[HEAP];
147
148 if ($heap->{msg} == 5) {
149 $bot2->yield(notice => $bot1->nick_name(), 'So is this');
150 $heap->{msg}++;
151 }
152 elsif ($heap->{msg} == 6) {
153 $bot1->yield('quit');
154 $bot2->yield('quit');
155 }
156 }
157
158 sub irc_disconnected {
159 my ($kernel, $heap) = @_[KERNEL, HEAP];
160 pass('irc_disconnected');
161 $heap->{count}++;
162
163 if ($heap->{count} == 2) {
164 verify_log();
165 $kernel->yield('_shutdown');
166 }
167 }
168
169 sub verify_log {
170 open my $log, '<', $file or die "Can't open log file '$file': $!";
171 my @lines = <$log>;
172 close $log;
173
174 my $check = 0;
175 for my $line (@lines) {
176 next if $line =~ /^\*{3}/;
177 chomp $line;
178 $line = substr($line, 20);
179 last if !defined $correct[$check];
180
181 if (ref $correct[$check] eq 'Regexp') {
182 like($line, $correct[$check], 'Line ' . ($check+1));
183 }
184 else {
185 is($line, $correct[$check], 'Line ' . ($check+1));
186 }
187 $check++;
188 }
189
190 fail('Log too short') if $check > @correct;
191 }