chat_registry_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package state
  2. import (
  3. "testing"
  4. "github.com/mk6i/retro-aim-server/wire"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestChatRegistry_RegisterAndReceive(t *testing.T) {
  8. type registration struct {
  9. room ChatRoom
  10. value any
  11. }
  12. tests := []struct {
  13. name string
  14. givenRegistered []registration
  15. lookupCookie string
  16. wantRegistered registration
  17. wantErr error
  18. }{
  19. {
  20. name: "chat room and value found",
  21. givenRegistered: []registration{
  22. {
  23. room: ChatRoom{Cookie: "cookie1"},
  24. value: "value1",
  25. },
  26. {
  27. room: ChatRoom{Cookie: "cookie2"},
  28. value: "value2",
  29. },
  30. },
  31. lookupCookie: "cookie2",
  32. wantRegistered: registration{
  33. room: ChatRoom{Cookie: "cookie2"},
  34. value: "value2",
  35. },
  36. },
  37. {
  38. name: "chat room and value not found",
  39. givenRegistered: []registration{
  40. {
  41. room: ChatRoom{Cookie: "cookie1"},
  42. value: "value1",
  43. },
  44. {
  45. room: ChatRoom{Cookie: "cookie2"},
  46. value: "value2",
  47. },
  48. },
  49. lookupCookie: "cookie3",
  50. wantErr: ErrChatRoomNotFound,
  51. },
  52. }
  53. for _, tt := range tests {
  54. t.Run(tt.name, func(t *testing.T) {
  55. chatRegistry := NewChatRegistry()
  56. for _, r := range tt.givenRegistered {
  57. chatRegistry.Register(r.room, r.value)
  58. }
  59. room, value, err := chatRegistry.Retrieve(tt.lookupCookie)
  60. assert.Equal(t, tt.wantRegistered.room, room)
  61. assert.Equal(t, tt.wantRegistered.value, value)
  62. assert.ErrorIs(t, err, tt.wantErr)
  63. })
  64. }
  65. }
  66. func TestChatRegistry_RegisterAndRemove(t *testing.T) {
  67. type registration struct {
  68. room ChatRoom
  69. value any
  70. }
  71. tests := []struct {
  72. name string
  73. givenRegistered []registration
  74. removeCookie string
  75. wantRegistered []registration
  76. wantErr error
  77. }{
  78. {
  79. name: "chat room and value removed",
  80. givenRegistered: []registration{
  81. {
  82. room: ChatRoom{Cookie: "cookie1"},
  83. value: "value1",
  84. },
  85. {
  86. room: ChatRoom{Cookie: "cookie2"},
  87. value: "value2",
  88. },
  89. },
  90. removeCookie: "cookie2",
  91. wantRegistered: []registration{
  92. {
  93. room: ChatRoom{Cookie: "cookie1"},
  94. value: "value1",
  95. },
  96. },
  97. },
  98. {
  99. name: "no chat room and value removed",
  100. givenRegistered: []registration{
  101. {
  102. room: ChatRoom{Cookie: "cookie1"},
  103. value: "value1",
  104. },
  105. {
  106. room: ChatRoom{Cookie: "cookie2"},
  107. value: "value2",
  108. },
  109. },
  110. removeCookie: "cookie3",
  111. wantRegistered: []registration{
  112. {
  113. room: ChatRoom{Cookie: "cookie1"},
  114. value: "value1",
  115. },
  116. {
  117. room: ChatRoom{Cookie: "cookie2"},
  118. value: "value2",
  119. },
  120. },
  121. },
  122. }
  123. for _, tt := range tests {
  124. t.Run(tt.name, func(t *testing.T) {
  125. chatRegistry := NewChatRegistry()
  126. for _, r := range tt.givenRegistered {
  127. chatRegistry.Register(r.room, r.value)
  128. }
  129. chatRegistry.Remove(tt.removeCookie)
  130. for _, r := range tt.wantRegistered {
  131. room, value, err := chatRegistry.Retrieve(r.room.Cookie)
  132. assert.Equal(t, r.room, room)
  133. assert.Equal(t, r.value, value)
  134. assert.NoError(t, err)
  135. }
  136. })
  137. }
  138. }
  139. func TestChatRoom_TLVList(t *testing.T) {
  140. room := NewChatRoom()
  141. room.Name = "chat-room-name"
  142. have := room.TLVList()
  143. want := []wire.TLV{
  144. wire.NewTLV(wire.ChatRoomTLVFlags, uint16(15)),
  145. wire.NewTLV(wire.ChatRoomTLVCreateTime, uint32(room.CreateTime.Unix())),
  146. wire.NewTLV(wire.ChatRoomTLVMaxMsgLen, uint16(1024)),
  147. wire.NewTLV(wire.ChatRoomTLVMaxOccupancy, uint16(100)),
  148. wire.NewTLV(wire.ChatRoomTLVNavCreatePerms, uint8(2)),
  149. wire.NewTLV(wire.ChatRoomTLVFullyQualifiedName, room.Name),
  150. wire.NewTLV(wire.ChatRoomTLVRoomName, room.Name),
  151. }
  152. assert.Equal(t, want, have)
  153. }