Skip to content

Commit

Permalink
Add "list all available commands" command
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemeng Qin committed Oct 31, 2018
1 parent 9692d58 commit 16f11e9
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 58 deletions.
18 changes: 18 additions & 0 deletions domain/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package domain

func AllCommands() []string {
return []string{
"start_roll_call",
"end_roll_call",
"set_title",
"in",
"out",
"maybe",
"set_in_for",
"set_out_for",
"set_maybe_for",
"whos_in",
"shh",
"louder",
}
}
39 changes: 17 additions & 22 deletions hangout/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,10 @@ import (
)

var (
allCommands []string = []string{
"start_roll_call",
"end_roll_call",
"set_title",
"in",
"out",
"maybe",
"set_in_for",
"set_out_for",
"set_maybe_for",
"whos_in",
"shh",
"louder",
}
startRollCallAlias string = "start"
endRollCallAlias string = "end"
whosInAlias string = "ls"
setTitleAlias string = "title"
startRollCallAlias = "start"
endRollCallAlias = "end"
whosInAlias = "ls"
setTitleAlias = "title"
)

func ParseDeprecatedEvent(requestBody []byte) (domain.Command, error) {
Expand All @@ -41,14 +27,23 @@ func ParseDeprecatedEvent(requestBody []byte) (domain.Command, error) {

// TODO: split this into a mapping function and write some tests around it
threadName := deprecatedEvent.Message.Thread.Name
avaliableCommands := domain.Command{
ChatID: threadName,
Name: "available_commands",
Params: []string{},
From: domain.User{
UserID: deprecatedEvent.Message.Sender.Name,
Name: deprecatedEvent.Message.Sender.DisplayName,
},
}
arguments := strings.Fields(deprecatedEvent.Message.ArgumentText)
if len(arguments) <= 0 {
return domain.EmptyCommand(), errors.New("no argument provided")
return avaliableCommands, nil
}

name, err := parseCommandName(arguments[0])
if err != nil {
return domain.EmptyCommand(), err
return avaliableCommands, nil
}

command := domain.Command{
Expand All @@ -73,7 +68,7 @@ func contains(set []string, element string) bool {
}

func parseCommandName(commandNameArgument string) (string, error) {
if contains(allCommands, commandNameArgument){
if contains(domain.AllCommands(), commandNameArgument){
return commandNameArgument, nil
}

Expand Down
83 changes: 47 additions & 36 deletions hangout/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,23 @@ var _ = Describe("helper", func() {

Describe("#ParseDeprecatedEvent", func() {
When("successful", func() {
It("parses request body to DeprecatedEvent", func() {
deprecatedEvent := chat.DeprecatedEvent{
Action: nil,
ConfigCompleteRedirectUrl: "",
EventTime: "",
Message: &chat.Message{
ArgumentText: " in argument1 argument2",
Sender: &chat.User{
DisplayName: "Ryuichi Sakamoto",
Name: "users/1234567",
},
Thread: &chat.Thread{
Name: "thread1",
},
deprecatedEvent := chat.DeprecatedEvent{
Action: nil,
ConfigCompleteRedirectUrl: "",
EventTime: "",
Message: &chat.Message{
ArgumentText: " in argument1 argument2",
Sender: &chat.User{
DisplayName: "Ryuichi Sakamoto",
Name: "users/1234567",
},
Thread: &chat.Thread{
Name: "thread1",
},
}
},
}

It("parses request body to DeprecatedEvent", func() {
requestBody, _ := json.Marshal(deprecatedEvent)
command, _ := ParseDeprecatedEvent(requestBody)

Expand All @@ -123,7 +124,7 @@ var _ = Describe("helper", func() {
}))
})

Context("parses alias to valid cammand name", func() {
Context("parses alias to valid command name", func() {
It("replaces 'start' as start_roll_call command", func() {
deprecatedEvent := mockEvent()
deprecatedEvent.Message.ArgumentText = "start foo bar"
Expand All @@ -135,57 +136,67 @@ var _ = Describe("helper", func() {
Expect(command.Params).To(Equal([]string{"foo", "bar"}))
})
})
})

When("failed", func() {
When("there is no arguments provided", func() {
deprecatedEvent := getEventWithNoArguments()

It("returns empty command", func() {
It("returns list all command", func() {
requestBody, _ := json.Marshal(deprecatedEvent)
command, _ := ParseDeprecatedEvent(requestBody)

Expect(command).To(BeEquivalentTo(domain.EmptyCommand()))
Expect(command.ChatID).To(Equal("thread1"))
Expect(command.Name).To(Equal("available_commands"))
Expect(command.From).To(BeEquivalentTo(domain.User{
UserID: "users/1234567",
Name: "Ryuichi Sakamoto",
}))
})

It("returns error", func() {
It("don't returns error", func() {
requestBody, _ := json.Marshal(deprecatedEvent)
_, err := ParseDeprecatedEvent(requestBody)

Expect(err).To(BeEquivalentTo(errors.New("no argument provided")))
Expect(err).NotTo(HaveOccurred())
})
})

When("can't be unmarshaled to event", func() {
requestBody, _ := json.Marshal([]byte("something"))
When("fails to get the command name", func() {
deprecatedEvent := mockEvent()
deprecatedEvent.Message.ArgumentText = "blah"

It("returns empty command", func() {
It("returns list all command", func() {
requestBody, _ := json.Marshal(deprecatedEvent)
command, _ := ParseDeprecatedEvent(requestBody)

Expect(command).To(BeEquivalentTo(domain.EmptyCommand()))
Expect(command.Name).To(Equal("available_commands"))
})

It("returns error", func() {
It("don't returns error", func() {
requestBody, _ := json.Marshal(deprecatedEvent)
_, err := ParseDeprecatedEvent(requestBody)

Expect(err).To(HaveOccurred())
Expect(err).NotTo(HaveOccurred())
})

})
})

When("fails to get the command name", func() {
deprecatedEvent := mockEvent()
deprecatedEvent.Message.ArgumentText = "blah"
When("failed", func() {
When("can't be unmarshaled to event", func() {
requestBody, _ := json.Marshal([]byte("something"))

It("return empty command and error", func() {
requestBody, _ := json.Marshal(deprecatedEvent)
command, err := ParseDeprecatedEvent(requestBody)
It("returns empty command", func() {
command, _ := ParseDeprecatedEvent(requestBody)

Expect(command).To(BeEquivalentTo(domain.EmptyCommand()))
Expect(err).To(HaveOccurred())
})

})
It("returns error", func() {
_, err := ParseDeprecatedEvent(requestBody)

Expect(err).To(HaveOccurred())
})
})
})
})
})
Expand Down
15 changes: 15 additions & 0 deletions whosinbot/whos_in_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (b *WhosInBot) HandleCommand(command domain.Command) (*domain.Response, err
return b.handleSetQuiet(command, true)
case "louder":
return b.handleSetQuiet(command, false)
case "available_commands":
return b.handleAvailableCommands(command)
default:
log.Printf("Not a bot command: %+v\n", command)
return nil, nil
Expand Down Expand Up @@ -129,6 +131,19 @@ func (b *WhosInBot) handleWhosIn(command domain.Command) (*domain.Response, erro
return &domain.Response{ChatID: command.ChatID, Text: responseList(rollCall)}, nil
}

func (b *WhosInBot) handleAvailableCommands(command domain.Command) (*domain.Response, error) {
formattedAvailableCommands := formatAvailableCommands(domain.AllCommands())
return &domain.Response{ChatID: command.ChatID, Text: formattedAvailableCommands}, nil
}

func formatAvailableCommands(allCommands []string) string {
formatted := ""
for _, command := range allCommands {
formatted += fmt.Sprintf("%s\n", command)
}
return formatted
}

func (b *WhosInBot) handleResponse(command domain.Command, status string) (*domain.Response, error) {
return b.setAttendanceFor(command, command.From.Name, status, command.ParamsString(), false)
}
Expand Down
43 changes: 43 additions & 0 deletions whosinbot/whos_in_bot_ginkgo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package whosinbot

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"whosinbot/domain"
"fmt"
)

var _ = Describe("WhosInBot", func() {
Describe("#handleAvailableCommands", func() {
It("creates response with available commands besides itself", func() {
command := domain.Command{
ChatID: "123",
Name: "available_commands",
Params: []string{},
From: domain.User{
UserID: "abc",
Name: "Sakamoto",
},
}

formattedAvailableCommands := fmt.Sprint("start_roll_call\n" +
"end_roll_call\n" +
"set_title\n" +
"in\n" +
"out\n" +
"maybe\n" +
"set_in_for\n" +
"set_out_for\n" +
"set_maybe_for\n" +
"whos_in\n" +
"shh\n" +
"louder\n")
expectedResponse := &domain.Response{ChatID: command.ChatID,
Text: formattedAvailableCommands}

response, err := bot.handleAvailableCommands(command)
Expect(response).To(BeEquivalentTo(expectedResponse))
Expect(err).ToNot(HaveOccurred())
})
})
})
13 changes: 13 additions & 0 deletions whosinbot/whosinbot_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package whosinbot_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestWhosinbot(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Whosinbot Suite")
}

0 comments on commit 16f11e9

Please sign in to comment.