follows.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { ApolloServer } from 'apollo-server-express'
  2. import { Mongoose } from 'mongoose'
  3. import { Aggregates } from '../src/types'
  4. import { ApolloServerTestClient } from 'apollo-server-testing/dist/createTestClient'
  5. import { buildAggregates, connectMongoose, createServer } from '../src/server'
  6. import { createTestClient } from 'apollo-server-testing'
  7. import {
  8. FOLLOW_CHANNEL,
  9. FollowChannel,
  10. FollowChannelArgs,
  11. GET_CHANNEL_FOLLOWS,
  12. GetChannelFollows,
  13. GetChannelFollowsArgs,
  14. UNFOLLOW_CHANNEL,
  15. UnfollowChannel,
  16. UnfollowChannelArgs,
  17. GET_MOST_FOLLOWED_CHANNELS,
  18. GetMostFollowedChannels,
  19. GetMostFollowedChannelsArgs,
  20. GET_MOST_FOLLOWED_CHANNELS_ALL_TIME,
  21. GetMostFollowedChannelsAllTime,
  22. GetMostFollowedChannelsAllTimeArgs,
  23. } from './queries/follows'
  24. import { ChannelFollowsInfo } from '../src/entities/ChannelFollowsInfo'
  25. import { ChannelEventsBucketModel } from '../src/models/ChannelEvent'
  26. import { TEST_BUCKET_SIZE } from './setup'
  27. const FIRST_CHANNEL_ID = '22'
  28. const SECOND_CHANNEL_ID = '23'
  29. describe('Channel follows resolver', () => {
  30. let server: ApolloServer
  31. let mongoose: Mongoose
  32. let aggregates: Aggregates
  33. let query: ApolloServerTestClient['query']
  34. let mutate: ApolloServerTestClient['mutate']
  35. beforeEach(async () => {
  36. mongoose = await connectMongoose(process.env.MONGO_URL!)
  37. aggregates = await buildAggregates()
  38. server = await createServer(mongoose, aggregates)
  39. const testClient = createTestClient(server)
  40. query = testClient.query
  41. mutate = testClient.mutate
  42. })
  43. afterEach(async () => {
  44. await server.stop()
  45. await ChannelEventsBucketModel.deleteMany({})
  46. await mongoose.disconnect()
  47. })
  48. const followChannel = async (channelId: string) => {
  49. const followChannelResponse = await mutate<FollowChannel, FollowChannelArgs>({
  50. mutation: FOLLOW_CHANNEL,
  51. variables: { channelId },
  52. })
  53. expect(followChannelResponse.errors).toBeUndefined()
  54. return followChannelResponse.data?.followChannel
  55. }
  56. const unfollowChannel = async (channelId: string) => {
  57. const unfollowChannelResponse = await mutate<UnfollowChannel, UnfollowChannelArgs>({
  58. mutation: UNFOLLOW_CHANNEL,
  59. variables: { channelId },
  60. })
  61. expect(unfollowChannelResponse.errors).toBeUndefined()
  62. return unfollowChannelResponse.data?.unfollowChannel
  63. }
  64. const getChannelFollows = async (channelId: string) => {
  65. const channelFollowsResponse = await query<GetChannelFollows, GetChannelFollowsArgs>({
  66. query: GET_CHANNEL_FOLLOWS,
  67. variables: { channelId },
  68. })
  69. expect(channelFollowsResponse.errors).toBeUndefined()
  70. return channelFollowsResponse.data?.channelFollows
  71. }
  72. const getMostFollowedChannels = async (timePeriodDays: number) => {
  73. const mostFollowedChannelsResponse = await query<GetMostFollowedChannels, GetMostFollowedChannelsArgs>({
  74. query: GET_MOST_FOLLOWED_CHANNELS,
  75. variables: { timePeriodDays },
  76. })
  77. expect(mostFollowedChannelsResponse.errors).toBeUndefined()
  78. return mostFollowedChannelsResponse.data?.mostFollowedChannels
  79. }
  80. const getMostFollowedChannelsAllTime = async (limit: number) => {
  81. const mostFollowedChannelsAllTimeResponse = await query<
  82. GetMostFollowedChannelsAllTime,
  83. GetMostFollowedChannelsAllTimeArgs
  84. >({
  85. query: GET_MOST_FOLLOWED_CHANNELS_ALL_TIME,
  86. variables: { limit },
  87. })
  88. expect(mostFollowedChannelsAllTimeResponse.errors).toBeUndefined()
  89. return mostFollowedChannelsAllTimeResponse.data?.mostFollowedChannelsAllTime
  90. }
  91. it('should return null for unknown channel follows', async () => {
  92. const channelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  93. const mostFollowedChannels = await getMostFollowedChannels(30)
  94. const mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  95. expect(channelFollows).toBeNull()
  96. expect(mostFollowedChannels).toHaveLength(0)
  97. expect(mostFollowedChannelsAllTime).toHaveLength(0)
  98. })
  99. it('should properly handle channel follow', async () => {
  100. const expectedChannelFollows: ChannelFollowsInfo = {
  101. id: FIRST_CHANNEL_ID,
  102. follows: 1,
  103. }
  104. let addChannelFollowData = await followChannel(FIRST_CHANNEL_ID)
  105. expect(addChannelFollowData).toEqual(expectedChannelFollows)
  106. let channelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  107. let mostFollowedChannels = await getMostFollowedChannels(30)
  108. let mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  109. expect(channelFollows).toEqual(expectedChannelFollows)
  110. expect(mostFollowedChannels).toEqual([expectedChannelFollows])
  111. expect(mostFollowedChannelsAllTime).toEqual([expectedChannelFollows])
  112. expectedChannelFollows.follows++
  113. addChannelFollowData = await followChannel(FIRST_CHANNEL_ID)
  114. expect(addChannelFollowData).toEqual(expectedChannelFollows)
  115. channelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  116. mostFollowedChannels = await getMostFollowedChannels(30)
  117. mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  118. expect(channelFollows).toEqual(expectedChannelFollows)
  119. expect(mostFollowedChannels).toEqual([expectedChannelFollows])
  120. expect(mostFollowedChannelsAllTime).toEqual([expectedChannelFollows])
  121. })
  122. it('should properly handle channel unfollow', async () => {
  123. const expectedChannelFollows: ChannelFollowsInfo = {
  124. id: FIRST_CHANNEL_ID,
  125. follows: 5,
  126. }
  127. await followChannel(FIRST_CHANNEL_ID)
  128. await followChannel(FIRST_CHANNEL_ID)
  129. await followChannel(FIRST_CHANNEL_ID)
  130. await followChannel(FIRST_CHANNEL_ID)
  131. await followChannel(FIRST_CHANNEL_ID)
  132. let channelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  133. let mostFollowedChannels = await getMostFollowedChannels(30)
  134. let mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  135. expect(channelFollows).toEqual(expectedChannelFollows)
  136. expect(mostFollowedChannels).toEqual([expectedChannelFollows])
  137. expect(mostFollowedChannelsAllTime).toEqual([expectedChannelFollows])
  138. expectedChannelFollows.follows--
  139. const unfollowChannelData = await unfollowChannel(FIRST_CHANNEL_ID)
  140. expect(unfollowChannelData).toEqual(expectedChannelFollows)
  141. channelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  142. mostFollowedChannels = await getMostFollowedChannels(30)
  143. mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  144. expect(channelFollows).toEqual(expectedChannelFollows)
  145. expect(mostFollowedChannels).toEqual([expectedChannelFollows])
  146. expect(mostFollowedChannelsAllTime).toEqual([expectedChannelFollows])
  147. })
  148. it('should keep the channel follows non-negative', async () => {
  149. const expectedChannelFollows: ChannelFollowsInfo = {
  150. id: FIRST_CHANNEL_ID,
  151. follows: 0,
  152. }
  153. await followChannel(FIRST_CHANNEL_ID)
  154. await followChannel(FIRST_CHANNEL_ID)
  155. await unfollowChannel(FIRST_CHANNEL_ID)
  156. await unfollowChannel(FIRST_CHANNEL_ID)
  157. await unfollowChannel(FIRST_CHANNEL_ID)
  158. const channelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  159. const mostFollowedChannels = await getMostFollowedChannels(30)
  160. const mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  161. expect(channelFollows).toEqual(expectedChannelFollows)
  162. expect(mostFollowedChannels).toEqual([expectedChannelFollows])
  163. expect(mostFollowedChannelsAllTime).toEqual([expectedChannelFollows])
  164. })
  165. it('should distinct follows of separate channels', async () => {
  166. const expectedFirstChannelFollows: ChannelFollowsInfo = {
  167. id: FIRST_CHANNEL_ID,
  168. follows: 1,
  169. }
  170. const expectedSecondChannelFollows: ChannelFollowsInfo = {
  171. id: SECOND_CHANNEL_ID,
  172. follows: 1,
  173. }
  174. const firstChannelFollowData = await followChannel(FIRST_CHANNEL_ID)
  175. const secondChannelFollowData = await followChannel(SECOND_CHANNEL_ID)
  176. expect(firstChannelFollowData).toEqual(expectedFirstChannelFollows)
  177. expect(secondChannelFollowData).toEqual(expectedSecondChannelFollows)
  178. expectedFirstChannelFollows.follows++
  179. await followChannel(FIRST_CHANNEL_ID)
  180. const firstChannelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  181. const secondChannelFollows = await getChannelFollows(SECOND_CHANNEL_ID)
  182. const mostFollowedChannels = await getMostFollowedChannels(30)
  183. const mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  184. expect(firstChannelFollows).toEqual(expectedFirstChannelFollows)
  185. expect(secondChannelFollows).toEqual(expectedSecondChannelFollows)
  186. expect(mostFollowedChannels).toEqual([expectedFirstChannelFollows, expectedSecondChannelFollows])
  187. expect(mostFollowedChannelsAllTime).toEqual([expectedFirstChannelFollows, expectedSecondChannelFollows])
  188. })
  189. it('should properly rebuild the aggregate', async () => {
  190. const expectedFirstChannelFollows: ChannelFollowsInfo = {
  191. id: FIRST_CHANNEL_ID,
  192. follows: 3,
  193. }
  194. const expectedSecondChannelFollows: ChannelFollowsInfo = {
  195. id: SECOND_CHANNEL_ID,
  196. follows: 4,
  197. }
  198. const checkFollows = async () => {
  199. const firstChannelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  200. const secondChannelFollows = await getChannelFollows(SECOND_CHANNEL_ID)
  201. const mostFollowedChannels = await getMostFollowedChannels(30)
  202. const mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  203. expect(firstChannelFollows).toEqual(expectedFirstChannelFollows)
  204. expect(secondChannelFollows).toEqual(expectedSecondChannelFollows)
  205. expect(mostFollowedChannels).toEqual([expectedSecondChannelFollows, expectedFirstChannelFollows])
  206. expect(mostFollowedChannelsAllTime).toEqual([expectedSecondChannelFollows, expectedFirstChannelFollows])
  207. }
  208. await followChannel(FIRST_CHANNEL_ID)
  209. await followChannel(FIRST_CHANNEL_ID)
  210. await followChannel(FIRST_CHANNEL_ID)
  211. await followChannel(SECOND_CHANNEL_ID)
  212. await followChannel(SECOND_CHANNEL_ID)
  213. await followChannel(SECOND_CHANNEL_ID)
  214. await followChannel(SECOND_CHANNEL_ID)
  215. await followChannel(SECOND_CHANNEL_ID)
  216. await unfollowChannel(SECOND_CHANNEL_ID)
  217. await checkFollows()
  218. await server.stop()
  219. aggregates = await buildAggregates()
  220. server = await createServer(mongoose, aggregates)
  221. const testClient = createTestClient(server)
  222. query = testClient.query
  223. mutate = testClient.mutate
  224. await checkFollows()
  225. })
  226. it('should properly handle saving events across buckets', async () => {
  227. const eventsCount = TEST_BUCKET_SIZE * 2 + 1
  228. const expectedChannelFollows: ChannelFollowsInfo = {
  229. id: FIRST_CHANNEL_ID,
  230. follows: eventsCount,
  231. }
  232. for (let i = 0; i < eventsCount; i++) {
  233. await followChannel(FIRST_CHANNEL_ID)
  234. }
  235. const channelFollows = await getChannelFollows(FIRST_CHANNEL_ID)
  236. const mostFollowedChannels = await getMostFollowedChannels(30)
  237. const mostFollowedChannelsAllTime = await getMostFollowedChannelsAllTime(10)
  238. expect(channelFollows).toEqual(expectedChannelFollows)
  239. expect(mostFollowedChannels).toEqual([expectedChannelFollows])
  240. expect(mostFollowedChannelsAllTime).toEqual([expectedChannelFollows])
  241. })
  242. })