Skip to content

Commit

Permalink
add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed Apr 29, 2024
1 parent bcf37c1 commit 83d44c2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/connectors/matrix/actions/message/list_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (action *ListCommandsAction) listMessageCommands(event *matrix.MessageEvent
}

message, messageFormatted := msg.Build()
action.storer.SendAndStoreMessage(
go action.storer.SendAndStoreMessage(
message,
messageFormatted,
matrixdb.MessageTypeListCommands,
Expand Down Expand Up @@ -117,7 +117,7 @@ func (action *ListCommandsAction) listReplyCommands(event *matrix.MessageEvent)
}

message, messageFormatted := msg.Build()
action.storer.SendAndStoreMessage(
go action.storer.SendAndStoreMessage(
message,
messageFormatted,
matrixdb.MessageTypeListCommands,
Expand All @@ -143,7 +143,7 @@ func (action *ListCommandsAction) listReactions(event *matrix.MessageEvent) {
}

message, messageFormatted := msg.Build()
action.storer.SendAndStoreMessage(
go action.storer.SendAndStoreMessage(
message,
messageFormatted,
matrixdb.MessageTypeListCommands,
Expand Down
75 changes: 75 additions & 0 deletions internal/connectors/matrix/actions/message/list_commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package message_test

import (
"testing"
"time"

"github.com/CubicrootXYZ/gologger"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/actions/message"
matrixdb "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/database"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/mautrixcl"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/messenger"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/tests"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/database"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestListCommandsAction(t *testing.T) {
action := &message.ListCommandsAction{}

assert.NotEmpty(t, action.Name())

title, desc, examples := action.GetDocu()
assert.NotEmpty(t, title)
assert.NotEmpty(t, desc)
assert.NotEmpty(t, examples)

assert.NotNil(t, action.Selector())
}

func TestListCommandsAction_Selector(t *testing.T) {
action := &message.ListCommandsAction{}
r := action.Selector()

_, _, examples := action.GetDocu()
for _, example := range examples {
assert.True(t, r.MatchString(example))
}
}

func TestListCommandsAction_HandleEvent(t *testing.T) {
// Setup
ctrl := gomock.NewController(t)
defer ctrl.Finish()
db := database.NewMockService(ctrl)
matrixDB := matrixdb.NewMockService(ctrl)
client := mautrixcl.NewMockClient(ctrl)
msngr := messenger.NewMockMessenger(ctrl)

action := &message.ListCommandsAction{}
action.Configure(
gologger.New(gologger.LogLevelDebug, 0),
client,
msngr,
matrixDB,
db,
nil,
)

// Expectations
matrixDB.EXPECT().NewMessage(gomock.Any()).Times(3).Return(nil, nil)

msngr.EXPECT().SendMessage(gomock.Any()).Times(3).Return(&messenger.MessageResponse{
ExternalIdentifier: "ext1",
}, nil)

// Execute
action.HandleEvent(tests.TestEvent(
tests.MessageWithBody(
"list my commands",
"list my commands",
),
))
time.Sleep(time.Millisecond * 50) // wait for goroutine to finish
}

0 comments on commit 83d44c2

Please sign in to comment.