activeVideoCounters.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { assert } from 'chai'
  2. import { ApolloQueryResult } from '@apollo/client'
  3. import { Api } from '../../Api'
  4. import { BaseQueryNodeFixture, FixtureRunner } from '../../Fixture'
  5. import { BuyMembershipHappyCaseFixture } from '../membership'
  6. import { KeyringPair } from '@polkadot/keyring/types'
  7. import { Bytes } from '@polkadot/types'
  8. import { QueryNodeApi } from '../../QueryNodeApi'
  9. import BN from 'bn.js'
  10. import { Worker, WorkerId } from '@joystream/types/working-group'
  11. import {
  12. getMemberDefaults,
  13. getChannelCategoryDefaults,
  14. getChannelDefaults,
  15. getVideoDefaults,
  16. getVideoCategoryDefaults,
  17. } from './contentTemplates'
  18. import { JoystreamCLI, ICreatedVideoData } from '../../cli/joystream'
  19. import * as path from 'path'
  20. /**
  21. Fixture that test Joystream content can be created, is reflected in query node,
  22. and channel and categories counts their active videos properly.
  23. Assuming all videos start in channel, video category, and channel category respectively
  24. `channelIds[0]`, `channelCategoryIds[0]`, and `videoCategoryIds[0]`.
  25. */
  26. export class ActiveVideoCountersFixture extends BaseQueryNodeFixture {
  27. private cli: JoystreamCLI
  28. private channelIds: number[]
  29. private videosData: ICreatedVideoData[]
  30. private channelCategoryIds: number[]
  31. private videoCategoryIds: number[]
  32. constructor(
  33. api: Api,
  34. query: QueryNodeApi,
  35. cli: JoystreamCLI,
  36. channelIds: number[],
  37. videosData: ICreatedVideoData[],
  38. channelCategoryIds: number[],
  39. videoCategoryIds: number[]
  40. ) {
  41. super(api, query)
  42. this.cli = cli
  43. this.channelIds = channelIds
  44. this.videosData = videosData
  45. this.channelCategoryIds = channelCategoryIds
  46. this.videoCategoryIds = videoCategoryIds
  47. }
  48. /*
  49. Execute this Fixture.
  50. */
  51. public async execute(): Promise<void> {
  52. const videoCount = this.videosData.length
  53. const videoCategoryCount = this.videoCategoryIds.length
  54. const channelCount = this.channelIds.length
  55. const channelCategoryCount = this.channelCategoryIds.length
  56. // check channel and categories con are counted as active
  57. this.debug('Checking channels active video counters')
  58. await this.assertCounterMatch('channels', this.channelIds[0], videoCount)
  59. this.debug('Checking channel categories active video counters')
  60. await this.assertCounterMatch('channelCategories', this.channelCategoryIds[0], videoCount)
  61. this.debug('Checking video categories active video counters')
  62. await this.assertCounterMatch('videoCategories', this.videoCategoryIds[0], videoCount)
  63. // move channel to different channel category and video to different videoCategory
  64. const oneMovedItemCount = 1
  65. this.debug('Move channel to different channel category')
  66. await this.cli.updateChannel(this.channelIds[0], {
  67. category: this.channelCategoryIds[1], // move from category 1 to category 2
  68. })
  69. this.debug('Move video to different video category')
  70. await this.cli.updateVideo(this.videosData[0].videoId, {
  71. category: this.videoCategoryIds[1], // move from category 1 to category 2
  72. })
  73. // check counters of channel category and video category with newly moved in video/channel
  74. this.debug('Checking channel categories active video counters (2)')
  75. await this.assertCounterMatch('channelCategories', this.channelCategoryIds[1], videoCount)
  76. this.debug('Checking video categories active video counters (2)')
  77. await this.assertCounterMatch('videoCategories', this.videoCategoryIds[1], oneMovedItemCount)
  78. /** Giza doesn't support changing channels - uncomment this on later releases where it's supported
  79. // move one video to another channel
  80. this.debug('Move video to different channel')
  81. await this.cli.updateVideo(videosData[0].videoId, {
  82. channel: channelIds[1], // move from channel 1 to channel 2
  83. })
  84. // check counter of channel with newly moved video
  85. this.debug('Checking channels active video counters (2)')
  86. await this.assertCounterMatch('channels', channelIds[0], videoCount - oneMovedItemCount)
  87. await this.assertCounterMatch('channels', channelIds[1], oneMovedItemCount)
  88. // end
  89. */
  90. this.debug('Done')
  91. }
  92. /**
  93. Asserts a channel, or a video/channel categories have their active videos counter set properly
  94. in Query node.
  95. */
  96. private async assertCounterMatch(
  97. entityName: 'channels' | 'channelCategories' | 'videoCategories',
  98. entityId: number,
  99. expectedCount: number
  100. ) {
  101. const getterName = `get${entityName[0].toUpperCase()}${entityName.slice(1)}VideoCounters` as
  102. | 'getChannelsVideoCounters'
  103. | 'getChannelCategoriesVideoCounters'
  104. | 'getVideoCategoriesVideoCounters'
  105. await this.query.tryQueryWithTimeout(
  106. () => this.query[getterName](),
  107. (entities) => {
  108. assert(entities.length > 0) // some entities were loaded
  109. const entity = entities.find((item: any) => item.id === entityId.toString())
  110. // all videos created in this fixture should be active and belong to first entity
  111. assert(entity && entity.activeVideosCounter === expectedCount)
  112. }
  113. )
  114. }
  115. }