follows.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { ApolloServer } from 'apollo-server-express'
  2. import { Mongoose } from 'mongoose'
  3. import { Aggregates } from '../src/types'
  4. import { buildAggregates, connectMongoose, createServer } from '../src/server'
  5. import {
  6. FOLLOW_CHANNEL,
  7. FollowChannel,
  8. FollowChannelArgs,
  9. UNFOLLOW_CHANNEL,
  10. UnfollowChannel,
  11. UnfollowChannelArgs,
  12. GET_MOST_FOLLOWED_CHANNELS_CONNECTION,
  13. GetMostFollowedChannelsConnection,
  14. GetMostFollowedChannelsConnectionArgs,
  15. } from './queries/follows'
  16. import { ChannelFollowsInfo } from '../src/entities/ChannelFollowsInfo'
  17. import { createMutationFn, createQueryFn, MutationFn, QueryFn } from './helpers'
  18. import { ChannelEventModel } from '../src/models/ChannelEvent'
  19. const FIRST_CHANNEL_ID = '6'
  20. const SECOND_CHANNEL_ID = '7'
  21. describe('Channel follows resolver', () => {
  22. let server: ApolloServer
  23. let mongoose: Mongoose
  24. let aggregates: Aggregates
  25. let query: QueryFn
  26. let mutate: MutationFn
  27. beforeEach(async () => {
  28. mongoose = await connectMongoose(process.env.MONGO_URL!)
  29. aggregates = await buildAggregates()
  30. server = await createServer(mongoose, aggregates, process.env.ORION_QUERY_NODE_URL!)
  31. await server.start()
  32. query = createQueryFn(server)
  33. mutate = createMutationFn(server)
  34. })
  35. afterEach(async () => {
  36. await server.stop()
  37. await ChannelEventModel.deleteMany({})
  38. await mongoose.disconnect()
  39. })
  40. const followChannel = async (channelId: string) => {
  41. const followChannelResponse = await mutate<FollowChannel, FollowChannelArgs>({
  42. mutation: FOLLOW_CHANNEL,
  43. variables: { channelId },
  44. })
  45. expect(followChannelResponse.errors).toBeUndefined()
  46. return followChannelResponse.data?.followChannel
  47. }
  48. const unfollowChannel = async (channelId: string) => {
  49. const unfollowChannelResponse = await mutate<UnfollowChannel, UnfollowChannelArgs>({
  50. mutation: UNFOLLOW_CHANNEL,
  51. variables: { channelId },
  52. })
  53. expect(unfollowChannelResponse.errors).toBeUndefined()
  54. return unfollowChannelResponse.data?.unfollowChannel
  55. }
  56. const getMostFollowedChannels = async (periodDays: 7 | 30 | null) => {
  57. const mostFollowedChannelsResponse = await query<
  58. GetMostFollowedChannelsConnection,
  59. GetMostFollowedChannelsConnectionArgs
  60. >({
  61. query: GET_MOST_FOLLOWED_CHANNELS_CONNECTION,
  62. variables: { periodDays, limit: 10 },
  63. })
  64. expect(mostFollowedChannelsResponse.errors).toBeUndefined()
  65. return mostFollowedChannelsResponse.data?.mostFollowedChannelsConnection
  66. }
  67. it('should return null for unknown channel follows', async () => {
  68. const mostFollowedChannels = await getMostFollowedChannels(30)
  69. const mostFollowedChannelsAllTime = await getMostFollowedChannels(null)
  70. expect(mostFollowedChannels?.edges).toHaveLength(0)
  71. expect(mostFollowedChannelsAllTime?.edges).toHaveLength(0)
  72. })
  73. it('should properly handle channel follow', async () => {
  74. const expectedChannelFollows: ChannelFollowsInfo = {
  75. id: FIRST_CHANNEL_ID,
  76. follows: 1,
  77. }
  78. const expectedMostFollowedChannels = {
  79. edges: [expectedChannelFollows].map((follow) => ({ node: follow })),
  80. }
  81. let addChannelFollowData = await followChannel(FIRST_CHANNEL_ID)
  82. expect(addChannelFollowData).toEqual(expectedChannelFollows)
  83. let mostFollowedChannels = await getMostFollowedChannels(30)
  84. let mostFollowedChannelsAllTime = await getMostFollowedChannels(null)
  85. expect(mostFollowedChannels).toEqual(expectedMostFollowedChannels)
  86. expect(mostFollowedChannelsAllTime).toEqual(expectedMostFollowedChannels)
  87. expectedChannelFollows.follows++
  88. addChannelFollowData = await followChannel(FIRST_CHANNEL_ID)
  89. expect(addChannelFollowData).toEqual(expectedChannelFollows)
  90. mostFollowedChannels = await getMostFollowedChannels(30)
  91. mostFollowedChannelsAllTime = await getMostFollowedChannels(null)
  92. expect(mostFollowedChannels).toEqual(expectedMostFollowedChannels)
  93. expect(mostFollowedChannelsAllTime).toEqual(expectedMostFollowedChannels)
  94. })
  95. it('should properly handle channel unfollow', async () => {
  96. const expectedChannelFollows: ChannelFollowsInfo = {
  97. id: FIRST_CHANNEL_ID,
  98. follows: 5,
  99. }
  100. const expectedMostFollowedChannels = {
  101. edges: [expectedChannelFollows].map((follow) => ({ node: follow })),
  102. }
  103. await followChannel(FIRST_CHANNEL_ID)
  104. await followChannel(FIRST_CHANNEL_ID)
  105. await followChannel(FIRST_CHANNEL_ID)
  106. await followChannel(FIRST_CHANNEL_ID)
  107. await followChannel(FIRST_CHANNEL_ID)
  108. let mostFollowedChannels = await getMostFollowedChannels(30)
  109. let mostFollowedChannelsAllTime = await getMostFollowedChannels(null)
  110. expect(mostFollowedChannels).toEqual(expectedMostFollowedChannels)
  111. expect(mostFollowedChannelsAllTime).toEqual(expectedMostFollowedChannels)
  112. expectedChannelFollows.follows--
  113. const unfollowChannelData = await unfollowChannel(FIRST_CHANNEL_ID)
  114. expect(unfollowChannelData).toEqual(expectedChannelFollows)
  115. mostFollowedChannels = await getMostFollowedChannels(30)
  116. mostFollowedChannelsAllTime = await getMostFollowedChannels(null)
  117. expect(mostFollowedChannels).toEqual(expectedMostFollowedChannels)
  118. expect(mostFollowedChannelsAllTime).toEqual(expectedMostFollowedChannels)
  119. })
  120. it('should distinct follows of separate channels', async () => {
  121. const expectedFirstChannelFollows: ChannelFollowsInfo = {
  122. id: FIRST_CHANNEL_ID,
  123. follows: 1,
  124. }
  125. const expectedSecondChannelFollows: ChannelFollowsInfo = {
  126. id: SECOND_CHANNEL_ID,
  127. follows: 1,
  128. }
  129. const expectedMostFollowedChannels = {
  130. edges: [expectedFirstChannelFollows, expectedSecondChannelFollows].map((follow) => ({ node: follow })),
  131. }
  132. const firstChannelFollowData = await followChannel(FIRST_CHANNEL_ID)
  133. const secondChannelFollowData = await followChannel(SECOND_CHANNEL_ID)
  134. expect(firstChannelFollowData).toEqual(expectedFirstChannelFollows)
  135. expect(secondChannelFollowData).toEqual(expectedSecondChannelFollows)
  136. expectedFirstChannelFollows.follows++
  137. await followChannel(FIRST_CHANNEL_ID)
  138. const mostFollowedChannels = await getMostFollowedChannels(30)
  139. const mostFollowedChannelsAllTime = await getMostFollowedChannels(null)
  140. expect(mostFollowedChannels).toEqual(expectedMostFollowedChannels)
  141. expect(mostFollowedChannelsAllTime).toEqual(expectedMostFollowedChannels)
  142. })
  143. it('should properly rebuild the aggregate', async () => {
  144. const expectedFirstChannelFollows: ChannelFollowsInfo = {
  145. id: FIRST_CHANNEL_ID,
  146. follows: 3,
  147. }
  148. const expectedSecondChannelFollows: ChannelFollowsInfo = {
  149. id: SECOND_CHANNEL_ID,
  150. follows: 4,
  151. }
  152. const expectedMostFollowedChannels = {
  153. edges: [expectedFirstChannelFollows, expectedSecondChannelFollows].map((follow) => ({ node: follow })),
  154. }
  155. const checkFollows = async () => {
  156. const mostFollowedChannels = await getMostFollowedChannels(30)
  157. const mostFollowedChannelsAllTime = await getMostFollowedChannels(null)
  158. expect(mostFollowedChannels).toEqual(expectedMostFollowedChannels)
  159. expect(mostFollowedChannelsAllTime).toEqual(expectedMostFollowedChannels)
  160. }
  161. await followChannel(FIRST_CHANNEL_ID)
  162. await followChannel(FIRST_CHANNEL_ID)
  163. await followChannel(FIRST_CHANNEL_ID)
  164. await followChannel(SECOND_CHANNEL_ID)
  165. await followChannel(SECOND_CHANNEL_ID)
  166. await followChannel(SECOND_CHANNEL_ID)
  167. await followChannel(SECOND_CHANNEL_ID)
  168. await followChannel(SECOND_CHANNEL_ID)
  169. await unfollowChannel(SECOND_CHANNEL_ID)
  170. await checkFollows()
  171. await server.stop()
  172. aggregates = await buildAggregates()
  173. server = await createServer(mongoose, aggregates, process.env.ORION_QUERY_NODE_URL!)
  174. query = createQueryFn(server)
  175. mutate = createMutationFn(server)
  176. await checkFollows()
  177. })
  178. })