4 "github.com/stretchr/testify/assert"
10 func TestHandleSetChatSubject(t *testing.T) {
22 name: "sends chat subject to private chat members",
25 UserName: &[]byte{0x00, 0x01},
27 PrivateChats: map[uint32]*PrivateChat{
30 ClientConn: map[uint16]*ClientConn{
33 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
39 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
46 Clients: map[uint16]*ClientConn{
49 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
55 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
65 Type: []byte{0, 0x6a},
66 ID: []byte{0, 0, 0, 1},
67 ErrorCode: []byte{0, 0, 0, 0},
69 NewField(fieldChatID, []byte{0, 0, 0, 1}),
70 NewField(fieldChatSubject, []byte("Test Subject")),
76 clientID: &[]byte{0, 1},
79 Type: []byte{0, 0x77},
80 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
81 ErrorCode: []byte{0, 0, 0, 0},
83 NewField(fieldChatID, []byte{0, 0, 0, 1}),
84 NewField(fieldChatSubject, []byte("Test Subject")),
88 clientID: &[]byte{0, 2},
91 Type: []byte{0, 0x77},
92 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
93 ErrorCode: []byte{0, 0, 0, 0},
95 NewField(fieldChatID, []byte{0, 0, 0, 1}),
96 NewField(fieldChatSubject, []byte("Test Subject")),
103 for _, tt := range tests {
104 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
106 t.Run(tt.name, func(t *testing.T) {
107 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
108 if (err != nil) != tt.wantErr {
109 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
112 if !assert.Equal(t, tt.want, got) {
113 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
119 func TestHandleLeaveChat(t *testing.T) {
131 name: "returns expected transactions",
136 PrivateChats: map[uint32]*PrivateChat{
138 ClientConn: map[uint16]*ClientConn{
141 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
147 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
154 Clients: map[uint16]*ClientConn{
157 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
163 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
170 t: NewTransaction(tranDeleteUser, nil, NewField(fieldChatID, []byte{0, 0, 0, 1})),
174 clientID: &[]byte{0, 1},
177 Type: []byte{0, 0x76},
178 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
179 ErrorCode: []byte{0, 0, 0, 0},
181 NewField(fieldChatID, []byte{0, 0, 0, 1}),
182 NewField(fieldUserID, []byte{0, 2}),
189 for _, tt := range tests {
191 t.Run(tt.name, func(t *testing.T) {
192 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
193 if (err != nil) != tt.wantErr {
194 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
197 if !assert.Equal(t, tt.want, got) {
198 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
204 func TestHandleGetUserNameList(t *testing.T) {
216 name: "replies with userlist transaction",
222 Clients: map[uint16]*ClientConn{
226 Flags: &[]byte{0, 3},
227 UserName: &[]byte{0, 4},
233 ID: []byte{0, 0, 0, 1},
239 clientID: &[]byte{1, 1},
243 ID: []byte{0, 0, 0, 1},
244 ErrorCode: []byte{0, 0, 0, 0},
247 fieldUsernameWithInfo,
248 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
256 for _, tt := range tests {
257 t.Run(tt.name, func(t *testing.T) {
258 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
259 if (err != nil) != tt.wantErr {
260 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
263 if !reflect.DeepEqual(got, tt.want) {
264 t.Errorf("HandleGetUserNameList() got = %v, want %v", got, tt.want)
270 func TestHandleChatSend(t *testing.T) {
282 name: "sends chat msg transaction to all clients",
285 UserName: &[]byte{0x00, 0x01},
287 Clients: map[uint16]*ClientConn{
290 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
296 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
305 NewField(fieldData, []byte("hai")),
311 clientID: &[]byte{0, 1},
314 Type: []byte{0, 0x6a},
315 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
316 ErrorCode: []byte{0, 0, 0, 0},
318 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
322 clientID: &[]byte{0, 2},
325 Type: []byte{0, 0x6a},
326 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
327 ErrorCode: []byte{0, 0, 0, 0},
329 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
336 name: "only sends chat msg to clients with accessReadChat permission",
339 UserName: &[]byte{0x00, 0x01},
341 Clients: map[uint16]*ClientConn{
344 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
350 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
359 NewField(fieldData, []byte("hai")),
365 clientID: &[]byte{0, 1},
368 Type: []byte{0, 0x6a},
369 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
370 ErrorCode: []byte{0, 0, 0, 0},
372 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
379 for _, tt := range tests {
380 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
381 t.Run(tt.name, func(t *testing.T) {
382 got, err := HandleChatSend(tt.args.cc, tt.args.t)
384 if (err != nil) != tt.wantErr {
385 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
388 if !assert.Equal(t, tt.want, got) {
389 t.Errorf("HandleChatSend() got = %v, want %v", got, tt.want)