| 1 |
#!perl |
|---|
| 2 |
use warnings; |
|---|
| 3 |
use strict; |
|---|
| 4 |
use lib qw(lib t/lib); |
|---|
| 5 |
use Test::More no_plan => 1; |
|---|
| 6 |
use FindBin qw( $Bin ); |
|---|
| 7 |
|
|---|
| 8 |
use Bot::BasicBot::Pluggable::Module::Infobot; |
|---|
| 9 |
use Bot::BasicBot::Pluggable::Store; |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
# Fake a bot store into the Module base class, so we don't have to mess around |
|---|
| 13 |
# with Bot bojects at this point. Use a non-persisting store, it's safe. |
|---|
| 14 |
my $store; |
|---|
| 15 |
no warnings 'redefine'; |
|---|
| 16 |
sub Bot::BasicBot::Pluggable::Module::store { |
|---|
| 17 |
$store ||= Bot::BasicBot::Pluggable::Store->new; |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
ok( my $ib = Bot::BasicBot::Pluggable::Module::Infobot->new ); |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
# ok, the intent here is to test / document the infobot grammar, because |
|---|
| 24 |
# every time I mess with it I get annoying regressions. In general, B::B::P |
|---|
| 25 |
# wasn't built with Test-Driven techniques, and this is hurting me recently, |
|---|
| 26 |
# it's way to hard to write tests retroactively.. |
|---|
| 27 |
|
|---|
| 28 |
ok( $ib->help, "module has help text" ); |
|---|
| 29 |
|
|---|
| 30 |
# by default, the infobot doesn't learn things that it merely overhears |
|---|
| 31 |
ok( ! indirect("foo is red"), "passive learning off by default" ); |
|---|
| 32 |
ok( ! indirect("foo?"), "no answer to passive learn" ); |
|---|
| 33 |
is( direct("foo?"), "No clue. Sorry.", "no info on foo" ); |
|---|
| 34 |
|
|---|
| 35 |
# ..but it will learn things it's told directly. |
|---|
| 36 |
is( direct("foo?"), "No clue. Sorry.", "no info on foo" ); |
|---|
| 37 |
is( direct("foo is red"), "okay.", "active learning works" ); |
|---|
| 38 |
is( direct("foo?"), "foo is red", "correct answer to active learn" ); |
|---|
| 39 |
ok( !indirect("foo?"), "passive questioning off by default" ); |
|---|
| 40 |
|
|---|
| 41 |
# you can turn on the ability to ask questions without addressing the bot |
|---|
| 42 |
ok( $ib->set("user_passive_ask", 1), "activate passive ask" ); |
|---|
| 43 |
is( indirect("foo?"), "foo is red", "passive questioning now on" ); |
|---|
| 44 |
|
|---|
| 45 |
# and the ability to add factoids without addressing the bot |
|---|
| 46 |
ok( $ib->set("user_passive_learn", 1), "activate passive learn" ); |
|---|
| 47 |
is( direct("bar is green"), "okay.", "passive learning now works" ); |
|---|
| 48 |
is( indirect("bar?"), "bar is green", "passive questioning works" ); |
|---|
| 49 |
|
|---|
| 50 |
# you can search factoids, but not in public |
|---|
| 51 |
is( direct("search for foo"), "privmsg only, please", "not searched in public"); |
|---|
| 52 |
is( private("search for foo"), "Keys: 'foo'", "searched for 'foo'"); |
|---|
| 53 |
|
|---|
| 54 |
# you can append strings to factoids |
|---|
| 55 |
is( direct("foo is also blue"), "okay.", "can append to faactoids" ); |
|---|
| 56 |
is( direct("foo?"), "foo is red or blue", "works" ); |
|---|
| 57 |
is( direct("foo is also pink"), "okay.", "can append to faactoids" ); |
|---|
| 58 |
is( direct("foo?"), "foo is red or blue or pink", "works" ); |
|---|
| 59 |
|
|---|
| 60 |
# factoids can be forgotten |
|---|
| 61 |
is( direct("forget foo"), "I forgot about foo", "forgotten foo"); |
|---|
| 62 |
is( direct("foo?"), "No clue. Sorry.", "no info on foo" ); |
|---|
| 63 |
|
|---|
| 64 |
# factoids can be replaced |
|---|
| 65 |
is( direct("bar is yellow"), "But I already know something about bar", |
|---|
| 66 |
"Can't just redefine factoids" ); |
|---|
| 67 |
is( indirect("bar?"), "bar is green", "not changed" ); |
|---|
| 68 |
is( direct("no, bar is yellow"), "okay.", "Can explicitly redefine factoids" ); |
|---|
| 69 |
is( indirect("bar?"), "bar is yellow", "changed" ); |
|---|
| 70 |
|
|---|
| 71 |
# factoids can contain RSS |
|---|
| 72 |
is( direct("rsstest is <rss=\"file://$Bin/test.rss\">"), "okay.", "set RSS" ); |
|---|
| 73 |
is( indirect("rsstest?"), "rsstest is title;", "can read rss"); |
|---|
| 74 |
|
|---|
| 75 |
# certain things can't be factoid keys. |
|---|
| 76 |
ok( $ib->set("user_stopwords", "and"), "set stopword 'and'" ); |
|---|
| 77 |
ok( !direct("and is mumu"), "can't set 'and' as factoid"); |
|---|
| 78 |
ok( !direct("dkjsdlfkdsjfglkdsfjglfkdjgldksfjglkdfjglds is mumu"), |
|---|
| 79 |
"can't set very long factoid"); |
|---|
| 80 |
|
|---|
| 81 |
# literal syntax |
|---|
| 82 |
is( direct("literal rsstest?"), "rsstest =is= <rss=\"file://$Bin/test.rss\">", |
|---|
| 83 |
"literal of rsstest" ); |
|---|
| 84 |
ok( direct("bar is also fum"), "bar also fum" ); |
|---|
| 85 |
is( direct("literal bar?"), "bar =is= yellow =or= fum", "bar" ); |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
# alternate factoids ('|') |
|---|
| 89 |
is( direct("foo is one"), "okay.", "foo is one"); |
|---|
| 90 |
is( direct("foo is also two"), "okay.", "foo is also two"); |
|---|
| 91 |
is( direct("foo is also |maybe"), "okay.", "foo is also maybe"); |
|---|
| 92 |
|
|---|
| 93 |
ok( my $reply = direct("foo?"), "got one of the foos" ); |
|---|
| 94 |
ok( ( $reply eq 'foo is maybe' or $reply eq 'foo is one or two' ), "it's one of the two"); |
|---|
| 95 |
|
|---|
| 96 |
# blech's torture test, all three in one |
|---|
| 97 |
# notes on dipsy differences: |
|---|
| 98 |
# * 'ok' is 'okay.' in a true infobot |
|---|
| 99 |
# * literal doesn't highlight =or= like it does =is= |
|---|
| 100 |
|
|---|
| 101 |
is( direct("forget foo"), "I forgot about foo", "forgotten foo"); |
|---|
| 102 |
|
|---|
| 103 |
is( direct("foo is foo"), "okay.", "simple set" ); |
|---|
| 104 |
is( direct("foo?"), "foo is foo", "simple get" ); |
|---|
| 105 |
is( direct("foo is also bar"), "okay.", "simple append"); |
|---|
| 106 |
is( direct("foo?"), "foo is foo or bar", "appended ok"); |
|---|
| 107 |
is( direct("foo is also baz or quux"), "okay.", "complex append"); |
|---|
| 108 |
is( direct("foo?"), "foo is foo or bar or baz or quux", "also ok"); |
|---|
| 109 |
is( direct("foo is also | a silly thing"), "okay.", "alternate appended"); |
|---|
| 110 |
|
|---|
| 111 |
is( direct("literal foo?"), |
|---|
| 112 |
"foo =is= foo =or= bar =or= baz =or= quux =or= |a silly thing", |
|---|
| 113 |
"entire factoid looks right"); |
|---|
| 114 |
is( direct("foo is also |<reply>this is a very silly thing"), "okay.", "and a reply"); |
|---|
| 115 |
is( direct("literal foo?"), |
|---|
| 116 |
"foo =is= foo =or= bar =or= baz =or= quux =or= |a silly thing =or= |<reply>this is a very silly thing", |
|---|
| 117 |
"entire entry looks fine to me"); |
|---|
| 118 |
|
|---|
| 119 |
# run through a few times, and see what we get out |
|---|
| 120 |
foreach my $i (0..9) { |
|---|
| 121 |
ok( $reply = direct("foo?"), "got one of the foos" ); |
|---|
| 122 |
ok( ( $reply eq 'foo is foo or bar or baz or quux' |
|---|
| 123 |
or $reply eq 'foo is a silly thing' |
|---|
| 124 |
or $reply eq 'this is a very silly thing' ), |
|---|
| 125 |
"it's '$reply'" |
|---|
| 126 |
); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
# utility functions |
|---|
| 131 |
|
|---|
| 132 |
# tell the module something privately |
|---|
| 133 |
sub private { |
|---|
| 134 |
my $message = { |
|---|
| 135 |
body => $_[0], |
|---|
| 136 |
who => "test_user", |
|---|
| 137 |
channel => "msg", |
|---|
| 138 |
address => 1, |
|---|
| 139 |
}; |
|---|
| 140 |
return $ib->told($message) || $ib->fallback($message); |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
sub direct { |
|---|
| 144 |
my $message = { |
|---|
| 145 |
body => $_[0], |
|---|
| 146 |
who => "test_user", |
|---|
| 147 |
channel => "#test", |
|---|
| 148 |
address => 1, |
|---|
| 149 |
}; |
|---|
| 150 |
return $ib->told($message) || $ib->fallback($message); |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
# the module has seen something |
|---|
| 154 |
sub indirect { |
|---|
| 155 |
my $message = { |
|---|
| 156 |
body => $_[0], |
|---|
| 157 |
who => "test_user", |
|---|
| 158 |
channel => "#test", |
|---|
| 159 |
address => 0, |
|---|
| 160 |
}; |
|---|
| 161 |
return $ib->told($message) || $ib->fallback($message); |
|---|
| 162 |
} |
|---|