chat_nav_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package handler
  2. import (
  3. "bytes"
  4. "log/slog"
  5. "testing"
  6. "github.com/mk6i/retro-aim-server/state"
  7. "github.com/mk6i/retro-aim-server/wire"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/mock"
  10. )
  11. func TestChatNavHandler_CreateRoom(t *testing.T) {
  12. input := wire.SNACMessage{
  13. Frame: wire.SNACFrame{
  14. FoodGroup: wire.ChatNav,
  15. SubGroup: wire.ChatNavCreateRoom,
  16. },
  17. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  18. Exchange: 1,
  19. },
  20. }
  21. output := wire.SNACMessage{
  22. Frame: wire.SNACFrame{
  23. FoodGroup: wire.ChatNav,
  24. SubGroup: wire.ChatNavNavInfo,
  25. },
  26. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  27. }
  28. sess := state.NewSession()
  29. svc := newMockChatNavService(t)
  30. svc.EXPECT().
  31. CreateRoom(mock.Anything, sess, input.Frame, input.Body).
  32. Return(output, nil)
  33. h := NewChatNavHandler(svc, slog.Default())
  34. ss := newMockResponseWriter(t)
  35. ss.EXPECT().
  36. SendSNAC(output.Frame, output.Body).
  37. Return(nil)
  38. buf := &bytes.Buffer{}
  39. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  40. assert.NoError(t, h.CreateRoom(nil, sess, input.Frame, buf, ss))
  41. }
  42. func TestChatNavHandler_CreateRoom_ReadErr(t *testing.T) {
  43. input := wire.SNACMessage{
  44. Frame: wire.SNACFrame{
  45. FoodGroup: wire.ChatNav,
  46. SubGroup: wire.ChatNavCreateRoom,
  47. },
  48. Body: wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  49. Exchange: 1,
  50. },
  51. }
  52. output := wire.SNACMessage{
  53. Frame: wire.SNACFrame{
  54. FoodGroup: wire.ChatNav,
  55. SubGroup: wire.ChatNavNavInfo,
  56. },
  57. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  58. }
  59. sess := state.NewSession()
  60. svc := newMockChatNavService(t)
  61. svc.EXPECT().
  62. CreateRoom(mock.Anything, sess, input.Frame, input.Body).
  63. Return(output, nil)
  64. h := NewChatNavHandler(svc, slog.Default())
  65. ss := newMockResponseWriter(t)
  66. ss.EXPECT().
  67. SendSNAC(output.Frame, output.Body).
  68. Return(nil)
  69. buf := &bytes.Buffer{}
  70. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  71. assert.NoError(t, h.CreateRoom(nil, sess, input.Frame, buf, ss))
  72. }
  73. func TestChatNavHandler_RequestChatRights(t *testing.T) {
  74. input := wire.SNACMessage{
  75. Frame: wire.SNACFrame{
  76. FoodGroup: wire.ChatNav,
  77. SubGroup: wire.ChatNavRequestChatRights,
  78. },
  79. Body: struct{}{},
  80. }
  81. output := wire.SNACMessage{
  82. Frame: wire.SNACFrame{
  83. FoodGroup: wire.ChatNav,
  84. SubGroup: wire.ChatNavNavInfo,
  85. },
  86. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  87. }
  88. svc := newMockChatNavService(t)
  89. svc.EXPECT().
  90. RequestChatRights(mock.Anything, input.Frame).
  91. Return(output)
  92. h := NewChatNavHandler(svc, slog.Default())
  93. ss := newMockResponseWriter(t)
  94. ss.EXPECT().
  95. SendSNAC(output.Frame, output.Body).
  96. Return(nil)
  97. buf := &bytes.Buffer{}
  98. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  99. assert.NoError(t, h.RequestChatRights(nil, nil, input.Frame, buf, ss))
  100. }
  101. func TestChatNavHandler_RequestRoomInfo(t *testing.T) {
  102. input := wire.SNACMessage{
  103. Frame: wire.SNACFrame{
  104. FoodGroup: wire.ChatNav,
  105. SubGroup: wire.ChatNavRequestRoomInfo,
  106. },
  107. Body: wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  108. Exchange: 1,
  109. },
  110. }
  111. output := wire.SNACMessage{
  112. Frame: wire.SNACFrame{
  113. FoodGroup: wire.ChatNav,
  114. SubGroup: wire.ChatNavNavInfo,
  115. },
  116. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  117. }
  118. svc := newMockChatNavService(t)
  119. svc.EXPECT().
  120. RequestRoomInfo(mock.Anything, input.Frame, input.Body).
  121. Return(output, nil)
  122. h := NewChatNavHandler(svc, slog.Default())
  123. ss := newMockResponseWriter(t)
  124. ss.EXPECT().
  125. SendSNAC(output.Frame, output.Body).
  126. Return(nil)
  127. buf := &bytes.Buffer{}
  128. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  129. assert.NoError(t, h.RequestRoomInfo(nil, nil, input.Frame, buf, ss))
  130. }
  131. func TestChatNavHandler_RequestExchangeInfo(t *testing.T) {
  132. input := wire.SNACMessage{
  133. Frame: wire.SNACFrame{
  134. FoodGroup: wire.ChatNav,
  135. SubGroup: wire.ChatNavRequestExchangeInfo,
  136. },
  137. Body: wire.SNAC_0x0D_0x03_ChatNavRequestExchangeInfo{
  138. Exchange: 4,
  139. },
  140. }
  141. output := wire.SNACMessage{
  142. Frame: wire.SNACFrame{
  143. FoodGroup: wire.ChatNav,
  144. SubGroup: wire.ChatNavNavInfo,
  145. },
  146. Body: wire.SNAC_0x0D_0x09_ChatNavNavInfo{},
  147. }
  148. svc := newMockChatNavService(t)
  149. svc.EXPECT().
  150. ExchangeInfo(mock.Anything, input.Frame, input.Body).
  151. Return(output, nil)
  152. h := NewChatNavHandler(svc, slog.Default())
  153. ss := newMockResponseWriter(t)
  154. ss.EXPECT().
  155. SendSNAC(output.Frame, output.Body).
  156. Return(nil)
  157. buf := &bytes.Buffer{}
  158. assert.NoError(t, wire.MarshalBE(input.Body, buf))
  159. assert.NoError(t, h.RequestExchangeInfo(nil, nil, input.Frame, buf, ss))
  160. }