|
| 1 | +#!perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use Test::More tests => 5; |
| 6 | +use Net::Jabber::Bot; |
| 7 | + |
| 8 | +use FindBin; |
| 9 | +use lib "$FindBin::Bin/lib"; |
| 10 | +use MockJabberClient; |
| 11 | + |
| 12 | +my $bot_alias = 'grace_test_bot'; |
| 13 | +my $server = 'talk.google.com'; |
| 14 | + |
| 15 | +my %forums_and_responses; |
| 16 | +my $forum1 = 'test_forum1'; |
| 17 | +$forums_and_responses{$forum1} = ["jbot:", ""]; |
| 18 | + |
| 19 | +my $messages_seen = 0; |
| 20 | + |
| 21 | +# Use a short but nonzero grace period so the test runs fast |
| 22 | +my $bot = Net::Jabber::Bot->new({ |
| 23 | + server => $server, |
| 24 | + conference_server => "conference.$server", |
| 25 | + port => 5222, |
| 26 | + username => 'test_username', |
| 27 | + password => 'test_pass', |
| 28 | + alias => $bot_alias, |
| 29 | + message_function => sub { $messages_seen++ }, |
| 30 | + background_function => sub {}, |
| 31 | + loop_sleep_time => 5, |
| 32 | + process_timeout => 5, |
| 33 | + forums_and_responses => \%forums_and_responses, |
| 34 | + ignore_server_messages => 1, |
| 35 | + ignore_self_messages => 0, |
| 36 | + out_messages_per_second => 5, |
| 37 | + max_message_size => 800, |
| 38 | + max_messages_per_hour => 100, |
| 39 | + forum_join_grace => 2, |
| 40 | +}); |
| 41 | + |
| 42 | +isa_ok($bot, "Net::Jabber::Bot"); |
| 43 | +is($bot->forum_join_grace, 2, "Forum join grace set to 2 seconds"); |
| 44 | + |
| 45 | +# Enable responding to self messages (safety_mode forces ignore_self_messages on, |
| 46 | +# and the mock echoes messages back with the same resource as the bot) |
| 47 | +$bot->respond_to_self_messages(1); |
| 48 | + |
| 49 | +# Send a message immediately — should be ignored (within grace period) |
| 50 | +my $personal_address = "test_user\@$server/$bot_alias"; |
| 51 | +$bot->SendPersonalMessage($personal_address, "Hello during grace period"); |
| 52 | +$bot->Process(1); |
| 53 | +is($messages_seen, 0, "Message during grace period is ignored"); |
| 54 | + |
| 55 | +# Wait past the grace period |
| 56 | +sleep 3; |
| 57 | + |
| 58 | +# Send another message — should be processed now |
| 59 | +$messages_seen = 0; |
| 60 | +$bot->SendPersonalMessage($personal_address, "Hello after grace period"); |
| 61 | +$bot->Process(1); |
| 62 | +is($messages_seen, 1, "Message after grace period is processed"); |
| 63 | + |
| 64 | +# Test that forum_join_grace => 0 means no grace period at all |
| 65 | +my $bot2 = Net::Jabber::Bot->new({ |
| 66 | + server => $server, |
| 67 | + conference_server => "conference.$server", |
| 68 | + port => 5222, |
| 69 | + username => 'test_username2', |
| 70 | + password => 'test_pass', |
| 71 | + alias => $bot_alias, |
| 72 | + message_function => sub { $messages_seen++ }, |
| 73 | + background_function => sub {}, |
| 74 | + loop_sleep_time => 5, |
| 75 | + process_timeout => 5, |
| 76 | + forums_and_responses => \%forums_and_responses, |
| 77 | + ignore_server_messages => 1, |
| 78 | + ignore_self_messages => 0, |
| 79 | + out_messages_per_second => 5, |
| 80 | + max_message_size => 800, |
| 81 | + max_messages_per_hour => 100, |
| 82 | + forum_join_grace => 0, |
| 83 | +}); |
| 84 | + |
| 85 | +$bot2->respond_to_self_messages(1); |
| 86 | +$messages_seen = 0; |
| 87 | +$bot2->SendPersonalMessage($personal_address, "Immediate message with zero grace"); |
| 88 | +$bot2->Process(1); |
| 89 | +is($messages_seen, 1, "Message processed immediately when forum_join_grace is 0"); |
0 commit comments