git.delta.rocks / unique-network / refs/commits / 50109b665ea6

difftreelog

source

tests/src/util/playgrounds/index.ts5.3 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {IKeyringPair} from '@polkadot/types/types';5import chai from 'chai';6import chaiAsPromised from 'chai-as-promised';7import {Context} from 'mocha';8import config from '../../config';9import '../../interfaces/augment-api-events';10import {ChainHelperBase} from './unique';11import {ILogger} from './types';12import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper, DevWestmintHelper} from './unique.dev';1314chai.use(chaiAsPromised);15export const expect = chai.expect;1617async function usingPlaygroundsGeneral<T extends ChainHelperBase>(helperType: new(logger: ILogger) => T, url: string, code: (helper: T, privateKey: (seed: string) => IKeyringPair) => Promise<void>) {18  const silentConsole = new SilentConsole();19  silentConsole.enable();2021  const helper = new helperType(new SilentLogger());2223  try {24    await helper.connect(url);25    const ss58Format = helper.chain.getChainProperties().ss58Format;26    const privateKey = (seed: string) => helper.util.fromSeed(seed, ss58Format);27    await code(helper, privateKey);28  }29  finally {30    await helper.disconnect();31    silentConsole.disable();32  }33}3435export const usingPlaygrounds = (code: (helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>, url: string = config.substrateUrl) => {36  return usingPlaygroundsGeneral<DevUniqueHelper>(DevUniqueHelper, url, code);37};3839export const usingWestmintPlaygrounds = async (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {40  return usingPlaygroundsGeneral<DevWestmintHelper>(DevWestmintHelper, url, code);41};4243export const usingRelayPlaygrounds = async (url: string, code: (helper: DevRelayHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {44  return usingPlaygroundsGeneral<DevRelayHelper>(DevRelayHelper, url, code);45};4647export const usingAcalaPlaygrounds = async (url: string, code: (helper: DevAcalaHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {48  return usingPlaygroundsGeneral<DevAcalaHelper>(DevAcalaHelper, url, code);49};5051export const usingKaruraPlaygrounds = async (url: string, code: (helper: DevKaruraHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {52  return usingPlaygroundsGeneral<DevKaruraHelper>(DevAcalaHelper, url, code);53};5455export const usingMoonbeamPlaygrounds = async (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {56  return usingPlaygroundsGeneral<DevMoonbeamHelper>(DevMoonbeamHelper, url, code);57};5859export const usingMoonriverPlaygrounds = async (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {60  return usingPlaygroundsGeneral<DevMoonriverHelper>(DevMoonriverHelper, url, code);61};6263export enum Pallets {64  Inflation = 'inflation',65  RmrkCore = 'rmrkcore',66  RmrkEquip = 'rmrkequip',67  ReFungible = 'refungible',68  Fungible = 'fungible',69  NFT = 'nonfungible',70  Scheduler = 'scheduler',71  AppPromotion = 'apppromotion',72}7374export async function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {75  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);76    77  if (missingPallets.length > 0) {78    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;79    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);8081    await helper.disconnect();82    test.skip();83  }84}8586export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {87  (opts.only ? it.only : 88    opts.skip ? it.skip : it)(name, async function () {89    await usingPlaygrounds(async (helper, privateKey) => {90      if (opts.requiredPallets) {91        await requirePalletsOrSkip(this, helper, opts.requiredPallets);92      }93      94      await cb({helper, privateKey});95    });96  });97}98export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {99  return itSub(name, cb, {requiredPallets: required, ...opts});100}101itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {only: true});102itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {skip: true});103104itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {only: true});105itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});106itSub.ifWithPallets = itSubIfWithPallet;107108export const describeXcm = (109  process.env.RUN_XCM_TESTS110    ? describe111    : describe.skip112);