git.delta.rocks / unique-network / refs/commits / 3ce5f80a2a4f

difftreelog

source

tests/src/util/index.ts8.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import * as path from 'path';5import * as crypto from 'crypto';6import {IKeyringPair} from '@polkadot/types/types/interfaces';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import chaiSubset from 'chai-subset';10import {Context} from 'mocha';11import config from '../config';12import {ChainHelperBase} from './playgrounds/unique';13import {ILogger} from './playgrounds/types';14import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper, DevWestmintHelper, DevStatemineHelper, DevStatemintHelper} from './playgrounds/unique.dev';1516chai.use(chaiAsPromised);17chai.use(chaiSubset);18export const expect = chai.expect;1920const getTestHash = (filename: string) => {21  return crypto.createHash('md5').update(filename).digest('hex');22};2324export const getTestSeed = (filename: string) => {25  return `//Alice+${getTestHash(filename)}`;26};2728async function usingPlaygroundsGeneral<T extends ChainHelperBase>(helperType: new(logger: ILogger) => T, url: string, code: (helper: T, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>) {29  const silentConsole = new SilentConsole();30  silentConsole.enable();3132  const helper = new helperType(new SilentLogger());3334  try {35    await helper.connect(url);36    const ss58Format = helper.chain.getChainProperties().ss58Format;37    const privateKey = async (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => {38      if (typeof seed === 'string') {39        return helper.util.fromSeed(seed, ss58Format);40      }41      else {42        const actualSeed = getTestSeed(seed.filename);43        let account = helper.util.fromSeed(actualSeed, ss58Format);44        // here's to hoping that no45        if (!seed.ignoreFundsPresence && ((helper as any)['balance'] == undefined || await (helper as any).balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND)) {46          console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);47          account = helper.util.fromSeed('//Alice', ss58Format);48        }49        return account;50      }51    };52    await code(helper, privateKey);53  }54  finally {55    await helper.disconnect();56    silentConsole.disable();57  }58}5960export const usingPlaygrounds = (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {61  return usingPlaygroundsGeneral<DevUniqueHelper>(DevUniqueHelper, url, code);62};6364export const usingWestmintPlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {65  return usingPlaygroundsGeneral<DevWestmintHelper>(DevWestmintHelper, url, code);66};6768export const usingStateminePlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {69  return usingPlaygroundsGeneral<DevStatemineHelper>(DevWestmintHelper, url, code);70};7172export const usingStatemintPlaygrounds = (url: string, code: (helper: DevWestmintHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {73  return usingPlaygroundsGeneral<DevStatemintHelper>(DevWestmintHelper, url, code);74};7576export const usingRelayPlaygrounds = (url: string, code: (helper: DevRelayHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {77  return usingPlaygroundsGeneral<DevRelayHelper>(DevRelayHelper, url, code);78};7980export const usingAcalaPlaygrounds = (url: string, code: (helper: DevAcalaHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {81  return usingPlaygroundsGeneral<DevAcalaHelper>(DevAcalaHelper, url, code);82};8384export const usingKaruraPlaygrounds = (url: string, code: (helper: DevKaruraHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {85  return usingPlaygroundsGeneral<DevKaruraHelper>(DevAcalaHelper, url, code);86};8788export const usingMoonbeamPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {89  return usingPlaygroundsGeneral<DevMoonbeamHelper>(DevMoonbeamHelper, url, code);90};9192export const usingMoonriverPlaygrounds = (url: string, code: (helper: DevMoonbeamHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {93  return usingPlaygroundsGeneral<DevMoonriverHelper>(DevMoonriverHelper, url, code);94};9596export const MINIMUM_DONOR_FUND = 100_000n;97export const DONOR_FUNDING = 1_000_000n;9899// App-promotion periods:100export const LOCKING_PERIOD = 12n; // 12 blocks of relay101export const UNLOCKING_PERIOD = 6n; // 6 blocks of parachain102103104export enum Pallets {105  Inflation = 'inflation',106  RmrkCore = 'rmrkcore',107  RmrkEquip = 'rmrkequip',108  ReFungible = 'refungible',109  Fungible = 'fungible',110  NFT = 'nonfungible',111  Scheduler = 'scheduler',112  AppPromotion = 'apppromotion',113  CollatorSelection = 'collatorselection',114  TestUtils = 'testutils',115}116117export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {118  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);119120  if (missingPallets.length > 0) {121    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;122    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);123    test.skip();124  }125}126127export function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {128  (opts.only ? it.only :129    opts.skip ? it.skip : it)(name, async function () {130    await usingPlaygrounds(async (helper, privateKey) => {131      if (opts.requiredPallets) {132        requirePalletsOrSkip(this, helper, opts.requiredPallets);133      }134135      await cb({helper, privateKey});136    });137  });138}139export function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {140  return itSub(name, cb, {requiredPallets: required, ...opts});141}142itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});143itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});144145itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});146itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});147itSub.ifWithPallets = itSubIfWithPallet;148149export type SchedKind = 'anon' | 'named';150151export function itSched(152  name: string,153  cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any,154  opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},155) {156  itSub(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);157  itSub(name + ' (named scheduling)', (apis) => cb('named', apis), opts);158}159itSched.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {only: true});160itSched.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {skip: true});161itSched.ifWithPallets = itSchedIfWithPallets;162163function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {164  return itSched(name, cb, {requiredPallets: required, ...opts});165}166167export function describeXCM(title: string, fn: (this: Mocha.Suite) => void, opts: {skip?: boolean} = {}) {168  (process.env.RUN_XCM_TESTS && !opts.skip169    ? describe170    : describe.skip)(title, fn);171}172173describeXCM.skip = (name: string, fn: (this: Mocha.Suite) => void) => describeXCM(name, fn, {skip: true});