WithdrawApplicationsFixture.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { assert } from 'chai'
  2. import { Api } from '../../Api'
  3. import { QueryNodeApi } from '../../QueryNodeApi'
  4. import { EventDetails, WorkingGroupModuleName } from '../../types'
  5. import { BaseWorkingGroupFixture } from './BaseWorkingGroupFixture'
  6. import { ApplicationId } from '@joystream/types/working-group'
  7. import { SubmittableExtrinsic } from '@polkadot/api/types'
  8. import { ISubmittableResult } from '@polkadot/types/types/'
  9. import { Utils } from '../../utils'
  10. import { EventType } from '../../graphql/generated/schema'
  11. import { ApplicationFieldsFragment, ApplicationWithdrawnEventFieldsFragment } from '../../graphql/generated/queries'
  12. export class WithdrawApplicationsFixture extends BaseWorkingGroupFixture {
  13. protected applicationIds: ApplicationId[]
  14. protected accounts: string[]
  15. public constructor(
  16. api: Api,
  17. query: QueryNodeApi,
  18. group: WorkingGroupModuleName,
  19. accounts: string[],
  20. applicationIds: ApplicationId[]
  21. ) {
  22. super(api, query, group)
  23. this.accounts = accounts
  24. this.applicationIds = applicationIds
  25. }
  26. protected async getSignerAccountOrAccounts(): Promise<string[]> {
  27. return this.accounts
  28. }
  29. protected async getExtrinsics(): Promise<SubmittableExtrinsic<'promise'>[]> {
  30. return this.applicationIds.map((id) => this.api.tx[this.group].withdrawApplication(id))
  31. }
  32. protected async getEventFromResult(result: ISubmittableResult): Promise<EventDetails> {
  33. return this.api.retrieveWorkingGroupsEventDetails(result, this.group, 'ApplicationWithdrawn')
  34. }
  35. protected assertQueryNodeEventIsValid(qEvent: ApplicationWithdrawnEventFieldsFragment, i: number): void {
  36. assert.equal(qEvent.event.type, EventType.ApplicationWithdrawn)
  37. assert.equal(qEvent.application.runtimeId, this.applicationIds[i].toNumber())
  38. assert.equal(qEvent.group.name, this.group)
  39. }
  40. protected assertApplicationStatusesAreValid(
  41. qEvents: ApplicationWithdrawnEventFieldsFragment[],
  42. qApplications: ApplicationFieldsFragment[]
  43. ): void {
  44. this.events.map((e, i) => {
  45. const qEvent = this.findMatchingQueryNodeEvent(e, qEvents)
  46. const qApplication = qApplications.find((a) => a.runtimeId === this.applicationIds[i].toNumber())
  47. Utils.assert(qApplication, 'Query node: Application not found!')
  48. Utils.assert(
  49. qApplication.status.__typename === 'ApplicationStatusWithdrawn',
  50. 'Query node: Invalid application status!'
  51. )
  52. Utils.assert(
  53. qApplication.status.applicationWithdrawnEvent,
  54. 'Query node: Missing applicationWithdrawnEvent relation'
  55. )
  56. assert.equal(qApplication.status.applicationWithdrawnEvent.id, qEvent.id)
  57. })
  58. }
  59. async runQueryNodeChecks(): Promise<void> {
  60. await super.runQueryNodeChecks()
  61. // Query the evens
  62. const qEvents = await this.query.tryQueryWithTimeout(
  63. () => this.query.getApplicationWithdrawnEvents(this.events),
  64. (qEvents) => this.assertQueryNodeEventsAreValid(qEvents)
  65. )
  66. // Check application statuses
  67. const qApplciations = await this.query.getApplicationsByIds(this.applicationIds, this.group)
  68. this.assertApplicationStatusesAreValid(qEvents, qApplciations)
  69. }
  70. }