From aae5bc8d2e08ad9a63cb176f691551af2d0d2508 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 24 Apr 2021 07:54:50 -0700 Subject: [PATCH 1/2] Added /back and tests for all away commands --- chat/command.go | 13 ++++++++++ chat/command_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 chat/command_test.go diff --git a/chat/command.go b/chat/command.go index 63423c0..f3ab015 100644 --- a/chat/command.go +++ b/chat/command.go @@ -478,6 +478,19 @@ func InitCommands(c *Commands) { }, }) + c.Add(Command{ + Prefix: "/back", + Help: "Set yourself as back, clear away status", + Handler: func(room *Room, msg message.CommandMsg) error { + isAway, _, _ := msg.From().GetAway() + if isAway { + msg.From().SetAway("") + room.Send(message.NewEmoteMsg("is back.", msg.From())) + } + return nil + }, + }) + c.Add(Command{ Op: true, Prefix: "/mute", diff --git a/chat/command_test.go b/chat/command_test.go new file mode 100644 index 0000000..839d34c --- /dev/null +++ b/chat/command_test.go @@ -0,0 +1,58 @@ +package chat + +import ( + "fmt" + "testing" + + "github.com/shazow/ssh-chat/chat/message" +) + +func TestAwayCommands(t *testing.T) { + cmds := &Commands{} + InitCommands(cmds) + + room := NewRoom() + go room.Serve() + defer room.Close() + + // steps are order dependent + // User can be "away" or "not away" using 3 commands "/away [msg]", "/away", "/back" + // 2^3 possible cases, run all and verify state at the end + type step struct { + // input + Msg string + + // expected output + IsUserAway bool + AwayMessage string + } + awayStep := step{"/away snorkling", true, "snorkling"} + notAwayStep := step{"/away", false, ""} + backStep := step{"/back", false, ""} + + steps := []step{awayStep, notAwayStep, backStep} + cases := [][]int{ + {0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}, + } + for _, c := range cases { + t.Run(fmt.Sprintf("Case: %d, %d, %d", c[0], c[1], c[2]), func(t *testing.T) { + + u := message.NewUser(message.SimpleID("shark")) + + for _, s := range []step{steps[c[0]], steps[c[1]], steps[c[2]]} { + msg, _ := message.NewPublicMsg(s.Msg, u).ParseCommand() + + cmds.Run(room, *msg) + + isAway, _, awayMsg := u.GetAway() + if isAway != s.IsUserAway { + t.Fatalf("expected user away state '%t' not equals to actual '%t' after message '%s'", s.IsUserAway, isAway, s.Msg) + } + if awayMsg != s.AwayMessage { + t.Fatalf("expected user away message '%s' not equal to actual '%s' after message '%s'", s.AwayMessage, awayMsg, s.Msg) + } + } + + }) + } +} From c3dccfd3eb1a9c9b507ccabdd7a359f3da7f123b Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Sat, 24 Apr 2021 12:14:24 -0400 Subject: [PATCH 2/2] chat: /back help formatting. --- chat/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chat/command.go b/chat/command.go index f3ab015..6420324 100644 --- a/chat/command.go +++ b/chat/command.go @@ -480,7 +480,7 @@ func InitCommands(c *Commands) { c.Add(Command{ Prefix: "/back", - Help: "Set yourself as back, clear away status", + Help: "Clear away status.", Handler: func(room *Room, msg message.CommandMsg) error { isAway, _, _ := msg.From().GetAway() if isAway {