chat_nav_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package handler
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/mkaminski/goaim/oscar"
  7. "github.com/mkaminski/goaim/state"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestSendAndReceiveCreateRoom(t *testing.T) {
  11. //
  12. // build dependencies
  13. //
  14. userSess := newTestSession("user-screen-name", sessOptCannedID)
  15. cr := state.NewChatRegistry()
  16. sm := newMockChatSessionManager(t)
  17. sm.EXPECT().NewSessionWithSN(userSess.ID(), userSess.ScreenName()).
  18. Return(&state.Session{})
  19. //
  20. // send input SNAC
  21. //
  22. inputSNAC := oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  23. Exchange: 1,
  24. Cookie: "create", // actual canned value sent by AIM client
  25. InstanceNumber: 2,
  26. DetailLevel: 3,
  27. TLVBlock: oscar.TLVBlock{
  28. TLVList: oscar.TLVList{
  29. oscar.NewTLV(oscar.ChatTLVRoomName, "the-chat-room-name"),
  30. },
  31. },
  32. }
  33. svc := ChatNavService{
  34. chatRegistry: cr,
  35. newChatRoom: func() state.ChatRoom {
  36. return state.ChatRoom{
  37. Cookie: "dummy-cookie",
  38. CreateTime: time.UnixMilli(0),
  39. }
  40. },
  41. newChatSessMgr: func() ChatSessionManager {
  42. return sm
  43. },
  44. }
  45. outputSNAC, err := svc.CreateRoomHandler(context.Background(), userSess, inputSNAC)
  46. assert.NoError(t, err)
  47. //
  48. // verify chat room created by handler
  49. //
  50. expectChatRoom := state.ChatRoom{
  51. Cookie: "dummy-cookie",
  52. CreateTime: time.UnixMilli(0),
  53. DetailLevel: 3,
  54. Exchange: 1,
  55. InstanceNumber: 2,
  56. Name: "the-chat-room-name",
  57. }
  58. chatRoom, _, err := cr.Retrieve("dummy-cookie")
  59. assert.NoError(t, err)
  60. assert.Equal(t, expectChatRoom, chatRoom)
  61. //
  62. // send input SNAC
  63. //
  64. expectSNAC := oscar.SNACMessage{
  65. Frame: oscar.SNACFrame{
  66. FoodGroup: oscar.ChatNav,
  67. SubGroup: oscar.ChatNavNavInfo,
  68. },
  69. Body: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  70. TLVRestBlock: oscar.TLVRestBlock{
  71. TLVList: oscar.TLVList{
  72. oscar.NewTLV(
  73. oscar.ChatNavTLVRoomInfo,
  74. oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  75. Exchange: chatRoom.Exchange,
  76. Cookie: chatRoom.Cookie,
  77. InstanceNumber: chatRoom.InstanceNumber,
  78. DetailLevel: chatRoom.DetailLevel,
  79. TLVBlock: oscar.TLVBlock{
  80. TLVList: chatRoom.TLVList(),
  81. },
  82. },
  83. ),
  84. },
  85. },
  86. },
  87. }
  88. assert.Equal(t, expectSNAC, outputSNAC)
  89. }