test_helpers.go 7.1 KB

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