Skip to content

Commit

Permalink
Merge pull request #23 from theothertomelliott/theothertomelliott/tes…
Browse files Browse the repository at this point in the history
…t_client_improvements

Create a single test client
  • Loading branch information
theothertomelliott authored Jan 3, 2024
2 parents 288d79c + 87ce2de commit e6a3bcc
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 178 deletions.
84 changes: 3 additions & 81 deletions slack/app_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
package slack

import (
"context"
"sync"
"testing"
"time"

"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"github.com/theothertomelliott/spanner"
)

func TestHandlerIsCalledForEachEvent(t *testing.T) {
slackEvents := make(chan socketmode.Event, 10)
client := newRunSocketClient()

testApp := newAppWithClient(
client,
slackEvents,
)
client := newTestClient()
testApp := client.CreateApp()

results := make(chan struct{}, 2)

Expand All @@ -33,7 +24,7 @@ func TestHandlerIsCalledForEachEvent(t *testing.T) {
for i := 0; i < 10; i++ {
var eventType string
if i%2 == 0 {
slackEvents <- socketmode.Event{}
client.SendEventToApp(socketmode.Event{})
eventType = "slack"
} else {
testApp.SendCustom(NewCustomEvent(map[string]interface{}{}))
Expand All @@ -50,72 +41,3 @@ func TestHandlerIsCalledForEachEvent(t *testing.T) {
t.Errorf("expected run to be called exactly once")
}
}

func newRunSocketClient() *runSocketClient {
return &runSocketClient{
stop: make(chan struct{}),
}
}

type runSocketClient struct {
nilSocketClient

runMtx sync.Mutex
runCount int

stop chan struct{}
}

func (r *runSocketClient) RunContext(context.Context) error {
r.runMtx.Lock()
r.runCount++
r.runMtx.Unlock()
<-r.stop // Must block
return nil
}

func (*runSocketClient) Ack(req socketmode.Request, payload ...interface{}) {}

func messageEvent(messageEvent slackevents.MessageEvent) socketmode.Event {
return socketmode.Event{
Type: socketmode.EventTypeEventsAPI,
Data: slackevents.EventsAPIEvent{
Type: slackevents.CallbackEvent,
InnerEvent: slackevents.EventsAPIInnerEvent{
Data: &messageEvent,
},
},
}
}

func slashCommandEvent(data slack.SlashCommand) socketmode.Event {
return socketmode.Event{
Type: socketmode.EventTypeSlashCommand,
Data: data,
}
}

func messageInteractionEvent(
hash string,
timestamp string,
metadata slack.SlackMetadata,
actionCallbacks slack.ActionCallbacks,
blockActionState *slack.BlockActionStates,
) socketmode.Event {
return socketmode.Event{
Type: socketmode.EventTypeInteractive,
Data: slack.InteractionCallback{
ViewSubmissionCallback: slack.ViewSubmissionCallback{
Hash: hash,
},
Message: slack.Message{
Msg: slack.Msg{
Metadata: metadata,
Timestamp: timestamp,
},
},
ActionCallback: actionCallbacks,
BlockActionState: blockActionState,
},
}
}
84 changes: 6 additions & 78 deletions slack/examples_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
package slack

import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"

"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"github.com/theothertomelliott/spanner"
)

// TestGettingStarted verifies that the code in examples/gettingstarted
// interacts with Slack in the expected way
func TestGettingStarted(t *testing.T) {
slackEvents := make(chan socketmode.Event)
client := newExampleTestClient()

testApp := newAppWithClient(
client,
slackEvents,
)

postEvent := make(chan interface{}, 10)
testApp.SetPostEventFunc(func() {
postEvent <- struct{}{}
})
client := newTestClient()
testApp := client.CreateApp()

go func() {
err := testApp.Run(handler)
Expand All @@ -37,16 +25,13 @@ func TestGettingStarted(t *testing.T) {
}()

// Send hello message
slackEvents <- messageEvent(
client.SendEventToApp(messageEvent(
slackevents.MessageEvent{
Text: "hello",
Channel: "ABC123",
User: "DEF456",
},
)

// Wait for event to be handled
<-postEvent
))

// Expect a single message and clear the message list
if len(client.messagesSent) != 1 {
Expand All @@ -61,7 +46,7 @@ func TestGettingStarted(t *testing.T) {
}

// Select an option value
slackEvents <- messageInteractionEvent(
client.SendEventToApp(messageInteractionEvent(
"hash",
"timestamp",
msg.metadata,
Expand All @@ -77,10 +62,7 @@ func TestGettingStarted(t *testing.T) {
},
},
},
)

// Wait for event to be handled
<-postEvent
))

if len(client.messagesSent) != 1 {
t.Errorf("expected one message to be sent, got %d", len(client.messagesSent))
Expand All @@ -107,57 +89,3 @@ func handler(ev spanner.Event) error {
}
return nil
}

func newExampleTestClient() *exampleTestClient {
return &exampleTestClient{
stop: make(chan struct{}),
}
}

type exampleTestClient struct {
nilSocketClient

messagesSent []sentMessage
messagesUpdated []updatedMessage

stop chan struct{}
}

type sentMessage struct {
channelID string
blocks []slack.Block
metadata slack.SlackMetadata
}

type updatedMessage struct {
sentMessage
timestamp string
}

func (r *exampleTestClient) RunContext(context.Context) error {
<-r.stop // Must block
return nil
}

func (*exampleTestClient) Ack(req socketmode.Request, payload ...interface{}) {}

func (c *exampleTestClient) SendMessageWithMetadata(ctx context.Context, channelID string, blocks []slack.Block, metadata slack.SlackMetadata) (string, string, string, error) {
c.messagesSent = append(c.messagesSent, sentMessage{
channelID: channelID,
blocks: blocks,
metadata: metadata,
})
return "", "", "", nil
}

func (c *exampleTestClient) UpdateMessageWithMetadata(ctx context.Context, channelID string, timestamp string, blocks []slack.Block, metadata slack.SlackMetadata) (string, string, string, error) {
c.messagesUpdated = append(c.messagesUpdated, updatedMessage{
sentMessage: sentMessage{
channelID: channelID,
blocks: blocks,
metadata: metadata,
},
timestamp: timestamp,
})
return "", timestamp, "", nil
}
12 changes: 3 additions & 9 deletions slack/receivemessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@ import (
"testing"

"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"github.com/theothertomelliott/spanner"
)

func TestReceiveMessageContent(t *testing.T) {
slackEvents := make(chan socketmode.Event, 10)
client := newRunSocketClient()

testApp := newAppWithClient(
client,
slackEvents,
)
client := newTestClient()
testApp := client.CreateApp()

message := slackevents.MessageEvent{
Channel: "ABC123",
User: "DEF456",
Text: "hello, there",
}
slackEvents <- messageEvent(message)
client.SendEventToAppAsync(messageEvent(message))

testApp.Run(func(evt spanner.Event) error {
defer func() {
Expand Down
14 changes: 4 additions & 10 deletions slack/receiveslashcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@ import (
"testing"

"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
"github.com/theothertomelliott/spanner"
)

func TestReceiveSlashCommand(t *testing.T) {
slackEvents := make(chan socketmode.Event, 10)
client := newRunSocketClient()

testApp := newAppWithClient(
client,
slackEvents,
)
client := newTestClient()
testApp := client.CreateApp()

slashCommand := slack.SlashCommand{
ChannelID: "ABC123",
UserID: "DEF456",
TriggerID: "trigger",
Command: "/mycommand",
}
slackEvents <- slashCommandEvent(
client.SendEventToAppAsync(slashCommandEvent(
slashCommand,
)
))

testApp.Run(func(evt spanner.Event) error {
defer func() {
Expand Down
Loading

0 comments on commit e6a3bcc

Please sign in to comment.