test_helpers.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. feedbagManagerParams
  11. messageRelayerParams
  12. profileManagerParams
  13. }
  14. // feedbagManagerParams is a helper struct that contains mock parameters for
  15. // FeedbagManager methods
  16. type feedbagManagerParams struct {
  17. blockedParams
  18. interestedUsersParams
  19. }
  20. // blockedParams is the list of parameters passed at the mock
  21. // FeedbagManager.Blocked call site
  22. type blockedParams []struct {
  23. screenName1 string
  24. screenName2 string
  25. result state.BlockedState
  26. err error
  27. }
  28. // interestedUsersParams is the list of parameters passed at the mock
  29. // FeedbagManager.InterestedUsers call site
  30. type interestedUsersParams []struct {
  31. screenName string
  32. users []string
  33. }
  34. // messageRelayerParams is a helper struct that contains mock parameters for
  35. // MessageRelayer methods
  36. type messageRelayerParams struct {
  37. retrieveByScreenNameParams
  38. broadcastToScreenNamesParams
  39. }
  40. // retrieveByScreenNameParams is the list of parameters passed at the mock
  41. // MessageRelayer.RetrieveByScreenName call site
  42. type retrieveByScreenNameParams []struct {
  43. screenName string
  44. sess *state.Session
  45. }
  46. // broadcastToScreenNamesParams is the list of parameters passed at the mock
  47. // MessageRelayer.BroadcastToScreenNames call site
  48. type broadcastToScreenNamesParams []struct {
  49. screenNames []string
  50. message oscar.SNACMessage
  51. }
  52. // profileManagerParams is a helper struct that contains mock parameters for
  53. // ProfileManager methods
  54. type profileManagerParams struct {
  55. retrieveProfileParams
  56. upsertProfileParams
  57. }
  58. // retrieveByScreenNameParams is the list of parameters passed at the mock
  59. // ProfileManager.RetrieveProfile call site
  60. type retrieveProfileParams []struct {
  61. screenName string
  62. result string
  63. err error
  64. }
  65. // upsertProfileParams is the list of parameters passed at the mock
  66. // ProfileManager.UpsertProfile call site
  67. type upsertProfileParams []struct {
  68. screenName string
  69. body any
  70. }
  71. // sessOptWarning sets a warning level on the session object
  72. func sessOptWarning(level uint16) func(session *state.Session) {
  73. return func(session *state.Session) {
  74. session.IncreaseWarning(level)
  75. }
  76. }
  77. // sessOptCannedID sets a canned session ID ("user-sess-id") on the session
  78. // object
  79. func sessOptCannedID(session *state.Session) {
  80. session.SetID("user-sess-id")
  81. }
  82. // sessOptCannedID sets a canned session ID ("user-sess-id") on the session
  83. // object
  84. func sessOptID(ID string) func(session *state.Session) {
  85. return func(session *state.Session) {
  86. session.SetID(ID)
  87. }
  88. }
  89. // sessOptAwayMessage sets away message on the session object
  90. func sessOptAwayMessage(awayMessage string) func(session *state.Session) {
  91. return func(session *state.Session) {
  92. session.SetAwayMessage(awayMessage)
  93. }
  94. }
  95. // sessOptCannedAwayMessage sets a canned away message ("this is my away
  96. // message!") on the session object
  97. func sessOptCannedAwayMessage(session *state.Session) {
  98. session.SetAwayMessage("this is my away message!")
  99. }
  100. // sessOptCannedSignonTime sets a canned sign-on time (1696790127565) on the
  101. // session object
  102. func sessOptCannedSignonTime(session *state.Session) {
  103. session.SetSignonTime(time.UnixMilli(1696790127565))
  104. }
  105. // sessOptCannedSignonTime sets the invisible flag to true on the session
  106. // object
  107. func sessOptInvisible(session *state.Session) {
  108. session.SetInvisible(true)
  109. }
  110. // sessOptIdle sets the idle flag to dur on the session object
  111. func sessOptIdle(dur time.Duration) func(session *state.Session) {
  112. return func(session *state.Session) {
  113. session.SetIdle(dur)
  114. }
  115. }
  116. // newTestSession creates a session object with 0 or more functional options
  117. // applied
  118. func newTestSession(screenName string, options ...func(session *state.Session)) *state.Session {
  119. s := state.NewSession()
  120. s.SetScreenName(screenName)
  121. for _, op := range options {
  122. op(s)
  123. }
  124. return s
  125. }