From 1fd56b304b3f9838d226f5cd22f931dff28b0817 Mon Sep 17 00:00:00 2001 From: Christoffer Lundell Date: Tue, 18 Jun 2024 12:05:21 +0200 Subject: [PATCH] testing: mock websocket for future tests --- go.mod | 5 +++++ go.sum | 7 +++++-- internal/bot/bot.go | 21 +++++++++++++++++++-- internal/bot/bot_mock.go | 26 ++++++++++++++++++++++++++ internal/bot/bot_test.go | 9 --------- internal/triviabot/triviabot.go | 2 +- 6 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 internal/bot/bot_mock.go delete mode 100644 internal/bot/bot_test.go diff --git a/go.mod b/go.mod index 84d3485..52b037a 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/dustin/go-humanize v1.0.1 github.com/friendsofgo/errors v0.9.2 github.com/mattn/go-sqlite3 v1.14.22 + github.com/stretchr/testify v1.9.0 github.com/volatiletech/null/v8 v8.1.2 github.com/volatiletech/sqlboiler/v4 v4.16.2 github.com/volatiletech/strmangle v0.0.6 @@ -15,10 +16,14 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gofrs/uuid v4.2.0+incompatible // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/cast v1.5.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/volatiletech/inflect v0.0.1 // indirect github.com/volatiletech/randomize v0.0.1 // indirect go.uber.org/multierr v1.10.0 // indirect golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 18063ee..9e21ce2 100644 --- a/go.sum +++ b/go.sum @@ -406,6 +406,8 @@ github.com/spf13/viper v1.12.0/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiu github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -415,8 +417,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs= github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= @@ -926,6 +928,7 @@ google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 41907f6..fdd8677 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -29,7 +29,8 @@ var ( type Bot struct { logger *zap.SugaredLogger - conn *websocket.Conn + conn WebsocketConn + dialer Dialer reconnect bool lastSentMsg string url string @@ -47,14 +48,28 @@ type Msg struct { Features []string `json:"features,omitempty"` } +type WebsocketConn interface { + Read(ctx context.Context) (websocket.MessageType, []byte, error) + Write(ctx context.Context, messageType websocket.MessageType, data []byte) error + Close(code websocket.StatusCode, reason string) error +} + +type Dialer func(ctx context.Context, url string, opts *websocket.DialOptions) (WebsocketConn, *http.Response, error) + +func WebSocketDialer(ctx context.Context, url string, opts *websocket.DialOptions) (WebsocketConn, *http.Response, error) { + return websocket.Dial(ctx, url, opts) +} + func New( logger *zap.SugaredLogger, + dialer Dialer, url, jwt string, reconnect bool, filters ...MsgTypeFilter, ) (*Bot, error) { b := &Bot{ logger: logger, + dialer: dialer, reconnect: reconnect, filters: filters, url: url, @@ -67,7 +82,9 @@ func New( } func (b *Bot) dial(url, jwt string) error { - c, _, err := websocket.Dial(context.Background(), url, + c, _, err := b.dialer( + context.Background(), + url, &websocket.DialOptions{ HTTPHeader: http.Header{ "Cookie": []string{fmt.Sprintf("jwt=%s", jwt)}, diff --git a/internal/bot/bot_mock.go b/internal/bot/bot_mock.go new file mode 100644 index 0000000..b503f4e --- /dev/null +++ b/internal/bot/bot_mock.go @@ -0,0 +1,26 @@ +package bot + +import ( + "context" + "github.com/stretchr/testify/mock" + "nhooyr.io/websocket" +) + +type MockWebSocketConn struct { + mock.Mock +} + +func (m *MockWebSocketConn) Read(ctx context.Context) (websocket.MessageType, []byte, error) { + args := m.Called(ctx) + return args.Get(0).(websocket.MessageType), args.Get(1).([]byte), args.Error(2) +} + +func (m *MockWebSocketConn) Write(ctx context.Context, messageType websocket.MessageType, data []byte) error { + args := m.Called(ctx, messageType, data) + return args.Error(0) +} + +func (m *MockWebSocketConn) Close(code websocket.StatusCode, reason string) error { + args := m.Called(code, reason) + return args.Error(0) +} diff --git a/internal/bot/bot_test.go b/internal/bot/bot_test.go deleted file mode 100644 index 43da65e..0000000 --- a/internal/bot/bot_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package bot_test - -import "testing" - -func TestAdd(t *testing.T) { - if 1+1 != 2 { - t.Error("failed horribly") - } -} diff --git a/internal/triviabot/triviabot.go b/internal/triviabot/triviabot.go index a12d2b6..9765fd9 100644 --- a/internal/triviabot/triviabot.go +++ b/internal/triviabot/triviabot.go @@ -44,7 +44,7 @@ func New( bot.PrivMsgSentFilter, } - bot, err := bot.New(logger, url, jwt, true, filters...) + bot, err := bot.New(logger, bot.WebSocketDialer, url, jwt, true, filters...) if err != nil { return nil, fmt.Errorf("error creating bot: %w", err) }