Skip to content

Commit

Permalink
/back, /away: Change no-op to return err
Browse files Browse the repository at this point in the history
Fixes #402

When the user is not set as away, using the
`/back` or `/away` command should return error.
The previous behaviour was inconsistent,
`/away` sent a message and `/back` ignored it.
New behaviour is error for both cases.

Co-authored-by: Akshay <akshay.shekher@gmail.com>
  • Loading branch information
voldyman and Akshay authored Oct 13, 2021
1 parent 0eebb64 commit d256300
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 7 additions & 5 deletions chat/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,13 @@ func InitCommands(c *Commands) {
msg.From().SetAway(awayMsg)
if awayMsg != "" {
room.Send(message.NewEmoteMsg("has gone away: "+awayMsg, msg.From()))
} else if !isAway {
room.Send(message.NewSystemMsg("Not away. Append a reason message to set away.", msg.From()))
} else {
return nil
}
if isAway {
room.Send(message.NewEmoteMsg("is back.", msg.From()))
return nil
}
return nil
return errors.New("not away. Append a reason message to set away")
},
})

Expand All @@ -486,8 +487,9 @@ func InitCommands(c *Commands) {
if isAway {
msg.From().SetAway("")
room.Send(message.NewEmoteMsg("is back.", msg.From()))
return nil
}
return nil
return errors.New("must be away to be back")
},
})

Expand Down
20 changes: 16 additions & 4 deletions chat/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ func TestAwayCommands(t *testing.T) {
// expected output
IsUserAway bool
AwayMessage string

// expected state change
ExpectsError func(awayBefore bool) bool
}
awayStep := step{"/away snorkling", true, "snorkling"}
notAwayStep := step{"/away", false, ""}
backStep := step{"/back", false, ""}
neverError := func(_ bool) bool { return false }
// if the user was away before, then the error is expected
errorIfAwayBefore := func(awayBefore bool) bool { return awayBefore }

awayStep := step{"/away snorkling", true, "snorkling", neverError}
notAwayStep := step{"/away", false, "", errorIfAwayBefore}
backStep := step{"/back", false, "", errorIfAwayBefore}

steps := []step{awayStep, notAwayStep, backStep}
cases := [][]int{
Expand All @@ -42,7 +49,12 @@ func TestAwayCommands(t *testing.T) {
for _, s := range []step{steps[c[0]], steps[c[1]], steps[c[2]]} {
msg, _ := message.NewPublicMsg(s.Msg, u).ParseCommand()

cmds.Run(room, *msg)
awayBeforeCommand, _, _ := u.GetAway()

err := cmds.Run(room, *msg)
if err != nil && s.ExpectsError(awayBeforeCommand) {
t.Fatalf("unexpected error running the command: %+v", err)
}

isAway, _, awayMsg := u.GetAway()
if isAway != s.IsUserAway {
Expand Down

0 comments on commit d256300

Please sign in to comment.