git.delta.rocks / unique-network / refs/commits / 812b7162c5f0

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  Session = 'session',115  Identity = 'identity',116  TestUtils = 'testutils',117}118119export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {120  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);121122  if (missingPallets.length > 0) {123    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;124    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);125    test.skip();126  }127}128129export function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {130  (opts.only ? it.only :131    opts.skip ? it.skip : it)(name, async function () {132    await usingPlaygrounds(async (helper, privateKey) => {133      if (opts.requiredPallets) {134        requirePalletsOrSkip(this, helper, opts.requiredPallets);135      }136137      await cb({helper, privateKey});138    });139  });140}141export function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {142  return itSub(name, cb, {requiredPallets: required, ...opts});143}144itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});145itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});146147itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});148itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});149itSub.ifWithPallets = itSubIfWithPallet;150151export type SchedKind = 'anon' | 'named';152153export function itSched(154  name: string,155  cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any,156  opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},157) {158  itSub(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);159  itSub(name + ' (named scheduling)', (apis) => cb('named', apis), opts);160}161itSched.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {only: true});162itSched.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {skip: true});163itSched.ifWithPallets = itSchedIfWithPallets;164165function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {166  return itSched(name, cb, {requiredPallets: required, ...opts});167}168169export function describeXCM(title: string, fn: (this: Mocha.Suite) => void, opts: {skip?: boolean} = {}) {170  (process.env.RUN_XCM_TESTS && !opts.skip171    ? describe172    : describe.skip)(title, fn);173}174175describeXCM.skip = (name: string, fn: (this: Mocha.Suite) => void) => describeXCM(name, fn, {skip: true});