2019-01-19 00:48:12 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package nerdepedia
import (
2020-10-24 18:09:20 +00:00
"bytes"
"io"
"io/ioutil"
"net/http"
2019-01-19 00:48:12 +00:00
"strings"
"testing"
2020-10-24 18:09:20 +00:00
"github.com/rs/zerolog/log"
"github.com/velour/catbase/plugins/cli"
2019-01-19 00:48:12 +00:00
"github.com/stretchr/testify/assert"
2020-10-24 18:09:20 +00:00
2019-01-19 00:48:12 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
)
2020-10-24 18:09:20 +00:00
var body = [ ] byte ( `
< meta name = "description" content = "Refresher Reading was a recurring feature appearing in Star Wars Insider. 20 Things You Didn't Know About the Tantive IV 20 Things You Didn't Know About the Mos Eisley Cantina 20 Things You Didn'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
}
2019-05-27 23:21:53 +00:00
func makeMessage ( payload string ) ( bot . Connector , bot . Kind , msg . Message ) {
2019-01-19 00:48:12 +00:00
isCmd := strings . HasPrefix ( payload , "!" )
if isCmd {
payload = payload [ 1 : ]
}
2019-05-27 23:21:53 +00:00
return & cli . CliPlugin { } , bot . Message , msg . Message {
2019-01-19 00:48:12 +00:00
User : & user . User { Name : "tester" } ,
Channel : "test" ,
Body : payload ,
Command : isCmd ,
}
}
2019-01-19 16:06:13 +00:00
func TestWars ( t * testing . T ) {
2019-01-19 00:48:12 +00:00
mb := bot . NewMockBot ( )
c := New ( mb )
assert . NotNil ( t , c )
2020-10-24 18:09:20 +00:00
client = MockClient {
Status : http . StatusOK ,
Body : ioutil . NopCloser ( bytes . NewReader ( body ) ) ,
Err : nil ,
}
2019-02-05 20:02:15 +00:00
res := c . message ( makeMessage ( "help me obi-wan" ) )
2019-01-19 00:48:12 +00:00
assert . Len ( t , mb . Messages , 1 )
assert . True ( t , res )
}