nerdepedia: fix failing tests and add config

This commit is contained in:
Chris Sexton 2020-10-24 14:09:20 -04:00 committed by Chris Sexton
parent af5717e9c1
commit 8817df15f0
2 changed files with 79 additions and 39 deletions

View File

@ -21,6 +21,18 @@ const (
closingTagSuffix = "\" />" closingTagSuffix = "\" />"
) )
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
var (
client HTTPClient
)
func init() {
client = &http.Client{}
}
type NerdepediaPlugin struct { type NerdepediaPlugin struct {
bot bot.Bot bot bot.Bot
config *config.Config config *config.Config
@ -37,26 +49,50 @@ func New(b bot.Bot) *NerdepediaPlugin {
return np return np
} }
func defaultSites() map[string]string {
starWars := "http://starwars.wikia.com/wiki/Special:Random"
starTrek := "http://memory-alpha.wikia.com/wiki/Special:Random"
dune := "http://dune.wikia.com/wiki/Special:Random"
lotr := "http://lotr.wikia.com/wiki/Special:Random"
pokemon := "http://pokemon.wikia.com/wiki/Special:Random"
return map[string]string{
"may the force be with you": starWars,
"help me obi-wan": starWars,
"beam me up scotty": starTrek,
"live long and prosper": starTrek,
"bless the maker": dune,
"i must not fear": dune,
"the spice must flow": dune,
"my precious": lotr,
"one ring to rule them all": lotr,
"one does not simply walk into mordor": lotr,
"pikachu i choose you": pokemon,
"gotta catch em all": pokemon,
}
}
// Message responds to the bot hook on recieving messages. // Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message. // This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins. // Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *NerdepediaPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *NerdepediaPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
lowerCase := strings.ToLower(message.Body) lowerCase := strings.ToLower(message.Body)
query := "" query := ""
if lowerCase == "may the force be with you" || lowerCase == "help me obi-wan" { queries := p.config.GetMap("nerdepedia.sites", defaultSites())
query = "http://starwars.wikia.com/wiki/Special:Random" for k, v := range queries {
} else if lowerCase == "beam me up scotty" || lowerCase == "live long and prosper" { if lowerCase == k {
query = "http://memory-alpha.wikia.com/wiki/Special:Random" query = v
} else if lowerCase == "bless the maker" || lowerCase == "i must not fear" || lowerCase == "the spice must flow" { break
query = "http://dune.wikia.com/wiki/Special:Random" }
} else if lowerCase == "my precious" || lowerCase == "one ring to rule them all" || lowerCase == "one does not simply walk into mordor" {
query = "http://lotr.wikia.com/wiki/Special:Random"
} else if lowerCase == "pikachu i choose you" || lowerCase == "gotta catch em all" {
query = "http://pokemon.wikia.com/wiki/Special:Random"
} }
if query != "" { if query != "" {
resp, err := http.Get(query) req, _ := http.NewRequest(http.MethodGet, query, nil)
resp, err := client.Do(req)
if err != nil { if err != nil {
return false return false
} }

View File

@ -3,16 +3,42 @@
package nerdepedia package nerdepedia
import ( import (
"github.com/velour/catbase/plugins/cli" "bytes"
"io"
"io/ioutil"
"net/http"
"strings" "strings"
"testing" "testing"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user" "github.com/velour/catbase/bot/user"
) )
var body = []byte(`
<meta name="description" content="Refresher Reading was a recurring feature appearing in Star Wars Insider. 20 Things You Didn&#039;t Know About the Tantive IV 20 Things You Didn&#039;t Know About the Mos Eisley Cantina 20 Things You Didn&#039;t Know About the Massassi Temples"/>
<link rel="canonical" href="https://starwars.fandom.com/wiki/Refresher_Reading"/>`)
type MockClient struct {
Status int
Body io.ReadCloser
Err error
}
func (cl MockClient) Do(req *http.Request) (*http.Response, error) {
log.Debug().Msgf("Returning mock response")
return &http.Response{
StatusCode: cl.Status,
Body: cl.Body,
}, cl.Err
}
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
isCmd := strings.HasPrefix(payload, "!") isCmd := strings.HasPrefix(payload, "!")
if isCmd { if isCmd {
@ -30,34 +56,12 @@ func TestWars(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
c := New(mb) c := New(mb)
assert.NotNil(t, c) assert.NotNil(t, c)
client = MockClient{
Status: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(body)),
Err: nil,
}
res := c.message(makeMessage("help me obi-wan")) res := c.message(makeMessage("help me obi-wan"))
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
} }
func TestTrek(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("live long and prosper"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}
func TestDune(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("bless the maker"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}
func TestPoke(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("gotta catch em all"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}