chat_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package handler
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/mkaminski/goaim/oscar"
  6. "github.com/mkaminski/goaim/state"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/mock"
  9. )
  10. func TestSendAndReceiveChatChannelMsgToHost(t *testing.T) {
  11. cases := []struct {
  12. // name is the unit test name
  13. name string
  14. // userSession is the session of the user sending the chat message
  15. userSession *state.Session
  16. // inputSNAC is the SNAC sent by the sender client
  17. inputSNAC oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost
  18. // expectSNACToParticipants is the message the server broadcast to chat
  19. // room participants (except the sender)
  20. expectSNACToParticipants oscar.SNACMessage
  21. expectOutput *oscar.SNACMessage
  22. }{
  23. {
  24. name: "send chat room message, expect acknowledgement to sender client",
  25. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
  26. inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  27. Cookie: 1234,
  28. Channel: 14,
  29. TLVRestBlock: oscar.TLVRestBlock{
  30. TLVList: oscar.TLVList{
  31. {
  32. TType: oscar.ChatTLVPublicWhisperFlag,
  33. Val: []byte{},
  34. },
  35. {
  36. TType: oscar.ChatTLVEnableReflectionFlag,
  37. Val: []byte{},
  38. },
  39. },
  40. },
  41. },
  42. expectSNACToParticipants: oscar.SNACMessage{
  43. Frame: oscar.SNACFrame{
  44. FoodGroup: oscar.Chat,
  45. SubGroup: oscar.ChatChannelMsgToClient,
  46. },
  47. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  48. Cookie: 1234,
  49. Channel: 14,
  50. TLVRestBlock: oscar.TLVRestBlock{
  51. TLVList: oscar.TLVList{
  52. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  53. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  54. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  55. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  56. },
  57. },
  58. },
  59. },
  60. expectOutput: &oscar.SNACMessage{
  61. Frame: oscar.SNACFrame{
  62. FoodGroup: oscar.Chat,
  63. SubGroup: oscar.ChatChannelMsgToClient,
  64. },
  65. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  66. Cookie: 1234,
  67. Channel: 14,
  68. TLVRestBlock: oscar.TLVRestBlock{
  69. TLVList: oscar.TLVList{
  70. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  71. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  72. oscar.NewTLV(oscar.ChatTLVSenderInformation, newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  73. },
  74. },
  75. },
  76. },
  77. },
  78. {
  79. name: "send chat room message, don't expect acknowledgement to sender client",
  80. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
  81. inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  82. Cookie: 1234,
  83. Channel: 14,
  84. TLVRestBlock: oscar.TLVRestBlock{
  85. TLVList: oscar.TLVList{
  86. {
  87. TType: oscar.ChatTLVPublicWhisperFlag,
  88. Val: []byte{},
  89. },
  90. },
  91. },
  92. },
  93. expectSNACToParticipants: oscar.SNACMessage{
  94. Frame: oscar.SNACFrame{
  95. FoodGroup: oscar.Chat,
  96. SubGroup: oscar.ChatChannelMsgToClient,
  97. },
  98. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  99. Cookie: 1234,
  100. Channel: 14,
  101. TLVRestBlock: oscar.TLVRestBlock{
  102. TLVList: oscar.TLVList{
  103. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  104. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  105. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  106. },
  107. },
  108. },
  109. },
  110. expectOutput: &oscar.SNACMessage{},
  111. },
  112. }
  113. for _, tc := range cases {
  114. t.Run(tc.name, func(t *testing.T) {
  115. chatID := "the-chat-id"
  116. chatSessMgr := newMockChatSessionManager(t)
  117. chatSessMgr.EXPECT().
  118. BroadcastExcept(mock.Anything, tc.userSession, tc.expectSNACToParticipants)
  119. svc := ChatService{
  120. chatRegistry: state.NewChatRegistry(),
  121. }
  122. svc.chatRegistry.Register(state.ChatRoom{Cookie: chatID}, chatSessMgr)
  123. outputSNAC, err := svc.ChannelMsgToHostHandler(context.Background(), tc.userSession, chatID, tc.inputSNAC)
  124. assert.NoError(t, err)
  125. if tc.expectOutput.Frame == (oscar.SNACFrame{}) {
  126. return // handler doesn't return response
  127. }
  128. assert.Equal(t, tc.expectOutput, outputSNAC)
  129. })
  130. }
  131. }