test_helpers.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package handler
  2. import (
  3. "time"
  4. "github.com/mk6i/retro-aim-server/oscar"
  5. "github.com/mk6i/retro-aim-server/state"
  6. )
  7. // mockParams is a helper struct that centralizes mock function call parameters
  8. // in one place for a table test
  9. type mockParams struct {
  10. chatMessageRelayerParams
  11. chatRegistryParams
  12. feedbagManagerParams
  13. messageRelayerParams
  14. profileManagerParams
  15. sessionManagerParams
  16. userManagerParams
  17. }
  18. type chatRegistryParams struct {
  19. chatRegistryRetrieveParams
  20. }
  21. type chatRegistryRetrieveParams struct {
  22. chatID string
  23. retChatRoom state.ChatRoom
  24. retChatSessMgr any
  25. err error
  26. }
  27. // userManagerParams is a helper struct that contains mock parameters for
  28. // UserManager methods
  29. type userManagerParams struct {
  30. getUserParams
  31. upsertUserParams
  32. }
  33. // getUserParams is the list of parameters passed at the mock
  34. // UserManager.GetUser call site
  35. type getUserParams []struct {
  36. screenName string
  37. result *state.User
  38. err error
  39. }
  40. // upsertUserParams is the list of parameters passed at the mock
  41. // UserManager.UpsertUser call site
  42. type upsertUserParams []struct {
  43. user state.User
  44. err error
  45. }
  46. // sessionManagerParams is a helper struct that contains mock parameters for
  47. // SessionManager methods
  48. type sessionManagerParams struct {
  49. emptyParams
  50. addSessionParams
  51. removeSessionParams
  52. }
  53. // addSessionParams is the list of parameters passed at the mock
  54. // SessionManager.AddSession call site
  55. type addSessionParams []struct {
  56. sessID string
  57. screenName string
  58. result *state.Session
  59. }
  60. // removeSessionParams is the list of parameters passed at the mock
  61. // SessionManager.RemoveSession call site
  62. type removeSessionParams []struct {
  63. sess *state.Session
  64. }
  65. // emptyParams is the list of parameters passed at the mock
  66. // SessionManager.Empty call site
  67. type emptyParams []struct {
  68. result bool
  69. }
  70. // feedbagManagerParams is a helper struct that contains mock parameters for
  71. // FeedbagManager methods
  72. type feedbagManagerParams struct {
  73. blockedStateParams
  74. interestedUsersParams
  75. feedbagUpsertParams
  76. buddiesParams
  77. feedbagParams
  78. feedbagLastModifiedParams
  79. feedbagDeleteParams
  80. }
  81. // blockedStateParams is the list of parameters passed at the mock
  82. // FeedbagManager.BlockedState call site
  83. type blockedStateParams []struct {
  84. screenName1 string
  85. screenName2 string
  86. result state.BlockedState
  87. err error
  88. }
  89. // interestedUsersParams is the list of parameters passed at the mock
  90. // FeedbagManager.AdjacentUsers call site
  91. type interestedUsersParams []struct {
  92. screenName string
  93. users []string
  94. err error
  95. }
  96. // feedbagUpsertParams is the list of parameters passed at the mock
  97. // FeedbagManager.FeedbagUpsert call site
  98. type feedbagUpsertParams []struct {
  99. screenName string
  100. items []oscar.FeedbagItem
  101. }
  102. // buddiesParams is the list of parameters passed at the mock
  103. // FeedbagManager.Buddies call site
  104. type buddiesParams []struct {
  105. screenName string
  106. results []string
  107. }
  108. // feedbagParams is the list of parameters passed at the mock
  109. // FeedbagManager.Feedbag call site
  110. type feedbagParams []struct {
  111. screenName string
  112. results []oscar.FeedbagItem
  113. }
  114. // feedbagLastModifiedParams is the list of parameters passed at the mock
  115. // FeedbagManager.FeedbagLastModified call site
  116. type feedbagLastModifiedParams []struct {
  117. screenName string
  118. result time.Time
  119. }
  120. // feedbagDeleteParams is the list of parameters passed at the mock
  121. // FeedbagManager.FeedbagDelete call site
  122. type feedbagDeleteParams []struct {
  123. screenName string
  124. items []oscar.FeedbagItem
  125. }
  126. // messageRelayerParams is a helper struct that contains mock parameters for
  127. // MessageRelayer methods
  128. type messageRelayerParams struct {
  129. retrieveByScreenNameParams
  130. broadcastToScreenNamesParams
  131. sendToScreenNameParams
  132. }
  133. // retrieveByScreenNameParams is the list of parameters passed at the mock
  134. // MessageRelayer.RetrieveByScreenName call site
  135. type retrieveByScreenNameParams []struct {
  136. screenName string
  137. sess *state.Session
  138. }
  139. // broadcastToScreenNamesParams is the list of parameters passed at the mock
  140. // MessageRelayer.RelayToScreenNames call site
  141. type broadcastToScreenNamesParams []struct {
  142. screenNames []string
  143. message oscar.SNACMessage
  144. }
  145. // sendToScreenNameParams is the list of parameters passed at the mock
  146. // MessageRelayer.RelayToScreenName call site
  147. type sendToScreenNameParams []struct {
  148. screenName string
  149. message oscar.SNACMessage
  150. }
  151. // profileManagerParams is a helper struct that contains mock parameters for
  152. // ProfileManager methods
  153. type profileManagerParams struct {
  154. retrieveProfileParams
  155. upsertProfileParams
  156. }
  157. // retrieveByScreenNameParams is the list of parameters passed at the mock
  158. // ProfileManager.RetrieveProfile call site
  159. type retrieveProfileParams []struct {
  160. screenName string
  161. result string
  162. err error
  163. }
  164. // upsertProfileParams is the list of parameters passed at the mock
  165. // ProfileManager.UpsertProfile call site
  166. type upsertProfileParams []struct {
  167. screenName string
  168. body any
  169. }
  170. // chatMessageRelayerParams is a helper struct that contains mock parameters
  171. // for ChatMessageRelayer methods
  172. type chatMessageRelayerParams struct {
  173. broadcastExceptParams
  174. }
  175. // broadcastExceptParams is the list of parameters passed at the mock
  176. // ChatMessageRelayer.RelayToAllExcept call site
  177. type broadcastExceptParams []struct {
  178. except *state.Session
  179. message oscar.SNACMessage
  180. }
  181. // sessOptWarning sets a warning level on the session object
  182. func sessOptWarning(level uint16) func(session *state.Session) {
  183. return func(session *state.Session) {
  184. session.IncrementWarning(level)
  185. }
  186. }
  187. // sessOptCannedID sets a canned session ID ("user-userSession-id") on the session
  188. // object
  189. func sessOptCannedID(session *state.Session) {
  190. session.SetID("user-userSession-id")
  191. }
  192. // sessOptCannedID sets a canned session ID ("user-userSession-id") on the session
  193. // object
  194. func sessOptID(ID string) func(session *state.Session) {
  195. return func(session *state.Session) {
  196. session.SetID(ID)
  197. }
  198. }
  199. // sessOptAwayMessage sets away message on the session object
  200. func sessOptAwayMessage(awayMessage string) func(session *state.Session) {
  201. return func(session *state.Session) {
  202. session.SetAwayMessage(awayMessage)
  203. }
  204. }
  205. // sessOptCannedAwayMessage sets a canned away message ("this is my away
  206. // message!") on the session object
  207. func sessOptCannedAwayMessage(session *state.Session) {
  208. session.SetAwayMessage("this is my away message!")
  209. }
  210. // sessOptCannedSignonTime sets a canned sign-on time (1696790127565) on the
  211. // session object
  212. func sessOptCannedSignonTime(session *state.Session) {
  213. session.SetSignonTime(time.UnixMilli(1696790127565))
  214. }
  215. // sessOptCannedSignonTime sets the invisible flag to true on the session
  216. // object
  217. func sessOptInvisible(session *state.Session) {
  218. session.SetInvisible(true)
  219. }
  220. // sessOptIdle sets the idle flag to dur on the session object
  221. func sessOptIdle(dur time.Duration) func(session *state.Session) {
  222. return func(session *state.Session) {
  223. session.SetIdle(dur)
  224. }
  225. }
  226. // newTestSession creates a session object with 0 or more functional options
  227. // applied
  228. func newTestSession(screenName string, options ...func(session *state.Session)) *state.Session {
  229. s := state.NewSession()
  230. s.SetScreenName(screenName)
  231. for _, op := range options {
  232. op(s)
  233. }
  234. return s
  235. }