mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-15 00:20:37 +03:00
Adding impl for quiet mode with tests.
This commit is contained in:
parent
362d0fdffd
commit
a2ee2000bb
@ -85,6 +85,12 @@ func (ch *Channel) HandleMsg(m Message) {
|
|||||||
// Skip
|
// Skip
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if _, ok := m.(*AnnounceMsg); ok {
|
||||||
|
if user.Config.Quiet {
|
||||||
|
// Skip
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
err := user.Send(m)
|
err := user.Send(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch.Leave(user)
|
ch.Leave(user)
|
||||||
|
@ -57,6 +57,113 @@ func TestChannelJoin(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChannelDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
|
||||||
|
u := NewUser("foo")
|
||||||
|
u.Config = UserConfig{
|
||||||
|
Quiet: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := NewChannel()
|
||||||
|
defer ch.Close()
|
||||||
|
|
||||||
|
err := ch.Join(u)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drain the initial Join message
|
||||||
|
<-ch.broadcast
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for msg := range u.msg {
|
||||||
|
if _, ok := msg.(*AnnounceMsg); ok {
|
||||||
|
t.Errorf("Got unexpected `%T`", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Call with an AnnounceMsg and all the other types
|
||||||
|
// and assert we received only non-announce messages
|
||||||
|
ch.HandleMsg(NewAnnounceMsg("Ignored"))
|
||||||
|
// Assert we still get all other types of messages
|
||||||
|
ch.HandleMsg(NewEmoteMsg("hello", u))
|
||||||
|
ch.HandleMsg(NewSystemMsg("hello", u))
|
||||||
|
ch.HandleMsg(NewPrivateMsg("hello", u, u))
|
||||||
|
ch.HandleMsg(NewPublicMsg("hello", u))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChannelQuietToggleBroadcasts(t *testing.T) {
|
||||||
|
u := NewUser("foo")
|
||||||
|
u.Config = UserConfig{
|
||||||
|
Quiet: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := NewChannel()
|
||||||
|
defer ch.Close()
|
||||||
|
|
||||||
|
err := ch.Join(u)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drain the initial Join message
|
||||||
|
<-ch.broadcast
|
||||||
|
|
||||||
|
u.ToggleQuietMode()
|
||||||
|
|
||||||
|
expectedMsg := NewAnnounceMsg("Ignored")
|
||||||
|
ch.HandleMsg(expectedMsg)
|
||||||
|
msg := <-u.msg
|
||||||
|
if _, ok := msg.(*AnnounceMsg); !ok {
|
||||||
|
t.Errorf("Got: `%T`; Expected: `%T`", msg, expectedMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
u.ToggleQuietMode()
|
||||||
|
|
||||||
|
ch.HandleMsg(NewAnnounceMsg("Ignored"))
|
||||||
|
ch.HandleMsg(NewSystemMsg("hello", u))
|
||||||
|
msg = <-u.msg
|
||||||
|
if _, ok := msg.(*AnnounceMsg); ok {
|
||||||
|
t.Errorf("Got unexpected `%T`", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuietToggleDisplayState(t *testing.T) {
|
||||||
|
var expected, actual []byte
|
||||||
|
|
||||||
|
s := &MockScreen{}
|
||||||
|
u := NewUser("foo")
|
||||||
|
|
||||||
|
ch := NewChannel()
|
||||||
|
go ch.Serve()
|
||||||
|
defer ch.Close()
|
||||||
|
|
||||||
|
err := ch.Join(u)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drain the initial Join message
|
||||||
|
<-ch.broadcast
|
||||||
|
|
||||||
|
ch.Send(ParseInput("/quiet", u))
|
||||||
|
u.ConsumeOne(s)
|
||||||
|
expected = []byte("-> Quiet mode is toggled ON" + Newline)
|
||||||
|
s.Read(&actual)
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
ch.Send(ParseInput("/quiet", u))
|
||||||
|
u.ConsumeOne(s)
|
||||||
|
expected = []byte("-> Quiet mode is toggled OFF" + Newline)
|
||||||
|
|
||||||
|
s.Read(&actual)
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestChannelNames(t *testing.T) {
|
func TestChannelNames(t *testing.T) {
|
||||||
var expected, actual []byte
|
var expected, actual []byte
|
||||||
|
|
||||||
@ -72,6 +179,7 @@ func TestChannelNames(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Drain the initial Join message
|
||||||
<-ch.broadcast
|
<-ch.broadcast
|
||||||
|
|
||||||
ch.Send(ParseInput("/names", u))
|
ch.Send(ParseInput("/names", u))
|
||||||
|
@ -196,6 +196,24 @@ func InitCommands(c *Commands) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
c.Add(Command{
|
||||||
|
Prefix: "/quiet",
|
||||||
|
Help: "Silence announcement-type messages (join, part, rename, etc).",
|
||||||
|
Handler: func(channel *Channel, msg CommandMsg) error {
|
||||||
|
u := msg.From()
|
||||||
|
u.ToggleQuietMode()
|
||||||
|
|
||||||
|
var body string
|
||||||
|
if u.Config.Quiet {
|
||||||
|
body = "Quiet mode is toggled ON"
|
||||||
|
} else {
|
||||||
|
body = "Quiet mode is toggled OFF"
|
||||||
|
}
|
||||||
|
channel.Send(NewSystemMsg(body, u))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
c.Add(Command{
|
c.Add(Command{
|
||||||
Op: true,
|
Op: true,
|
||||||
Prefix: "/op",
|
Prefix: "/op",
|
||||||
|
@ -59,6 +59,11 @@ func (u *User) SetName(name string) {
|
|||||||
u.SetColorIdx(rand.Int())
|
u.SetColorIdx(rand.Int())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToggleQuietMode will toggle whether or not quiet mode is enabled
|
||||||
|
func (u *User) ToggleQuietMode() {
|
||||||
|
u.Config.Quiet = !u.Config.Quiet
|
||||||
|
}
|
||||||
|
|
||||||
// SetColorIdx will set the colorIdx to a specific value, primarily used for
|
// SetColorIdx will set the colorIdx to a specific value, primarily used for
|
||||||
// testing.
|
// testing.
|
||||||
func (u *User) SetColorIdx(idx int) {
|
func (u *User) SetColorIdx(idx int) {
|
||||||
@ -122,6 +127,7 @@ func (u *User) Send(m Message) error {
|
|||||||
type UserConfig struct {
|
type UserConfig struct {
|
||||||
Highlight bool
|
Highlight bool
|
||||||
Bell bool
|
Bell bool
|
||||||
|
Quiet bool
|
||||||
Theme *Theme
|
Theme *Theme
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,6 +138,7 @@ func init() {
|
|||||||
DefaultUserConfig = &UserConfig{
|
DefaultUserConfig = &UserConfig{
|
||||||
Highlight: true,
|
Highlight: true,
|
||||||
Bell: false,
|
Bell: false,
|
||||||
|
Quiet: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Seed random?
|
// TODO: Seed random?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user