git.delta.rocks / unique-network / refs/commits / fa01f9a87ad6

difftreelog

source

tests/src/util/index.ts8.9 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 parachain102103// Native contracts104export const COLLECTION_HELPER = '0x6c4e9fe1ae37a41e93cee429e8e1881abdcbb54f';105export const CONTRACT_HELPER = '0x842899ECF380553E8a4de75bF534cdf6fBF64049';106107export enum Pallets {108  Inflation = 'inflation',109  RmrkCore = 'rmrkcore',110  RmrkEquip = 'rmrkequip',111  ReFungible = 'refungible',112  Fungible = 'fungible',113  NFT = 'nonfungible',114  Scheduler = 'scheduler',115  AppPromotion = 'apppromotion',116  CollatorSelection = 'collatorselection',117  Session = 'session',118  Identity = 'identity',119  TestUtils = 'testutils',120}121122export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {123  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);124125  if (missingPallets.length > 0) {126    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;127    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);128    test.skip();129  }130}131132export function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {133  (opts.only ? it.only :134    opts.skip ? it.skip : it)(name, async function () {135    await usingPlaygrounds(async (helper, privateKey) => {136      if (opts.requiredPallets) {137        requirePalletsOrSkip(this, helper, opts.requiredPallets);138      }139140      await cb({helper, privateKey});141    });142  });143}144export function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {145  return itSub(name, cb, {requiredPallets: required, ...opts});146}147itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});148itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});149150itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});151itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});152itSub.ifWithPallets = itSubIfWithPallet;153154export type SchedKind = 'anon' | 'named';155156export function itSched(157  name: string,158  cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any,159  opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {},160) {161  itSub(name + ' (anonymous scheduling)', (apis) => cb('anon', apis), opts);162  itSub(name + ' (named scheduling)', (apis) => cb('named', apis), opts);163}164itSched.only = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {only: true});165itSched.skip = (name: string, cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSched(name, cb, {skip: true});166itSched.ifWithPallets = itSchedIfWithPallets;167168function itSchedIfWithPallets(name: string, required: string[], cb: (schedKind: SchedKind, apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {169  return itSched(name, cb, {requiredPallets: required, ...opts});170}171172export function describeXCM(title: string, fn: (this: Mocha.Suite) => void, opts: {skip?: boolean} = {}) {173  (process.env.RUN_XCM_TESTS && !opts.skip174    ? describe175    : describe.skip)(title, fn);176}177178describeXCM.skip = (name: string, fn: (this: Mocha.Suite) => void) => describeXCM(name, fn, {skip: true});