Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: mock websocket for future tests #22

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
21 changes: 19 additions & 2 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var (

type Bot struct {
logger *zap.SugaredLogger
conn *websocket.Conn
conn WebsocketConn
dialer Dialer
reconnect bool
lastSentMsg string
url string
Expand All @@ -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,
Expand All @@ -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)},
Expand Down
26 changes: 26 additions & 0 deletions internal/bot/bot_mock.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 0 additions & 9 deletions internal/bot/bot_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/triviabot/triviabot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down