git.delta.rocks / unique-network / refs/commits / 88cc507e215e

difftreelog

source

tests/src/util/index.ts4.4 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';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import {Context} from 'mocha';10import config from '../config';11import '../interfaces/augment-api-events';12import {DevUniqueHelper, SilentLogger, SilentConsole} from './playgrounds/unique.dev';1314chai.use(chaiAsPromised);15export const expect = chai.expect;1617const getTestHash = (filename: string) => {18  return crypto.createHash('md5').update(filename).digest('hex');19};2021export const getTestSeed = (filename: string) => {22  return `//Alice+${getTestHash(filename)}`;23};2425export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {26  const silentConsole = new SilentConsole();27  silentConsole.enable();2829  const helper = new DevUniqueHelper(new SilentLogger());3031  try {32    await helper.connect(url);33    const ss58Format = helper.chain.getChainProperties().ss58Format;34    const privateKey = async (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => {35      if (typeof seed === 'string') {36        return helper.util.fromSeed(seed, ss58Format);37      }38      else {39        const actualSeed = getTestSeed(seed.filename);40        let account = helper.util.fromSeed(actualSeed, ss58Format);41        if (!seed.ignoreFundsPresence && await helper.balance.getSubstrate(account.address) < MINIMUM_DONOR_FUND) {42          console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);43          account = helper.util.fromSeed('//Alice', ss58Format);44        }45        return account;46      }47    };48    await code(helper, privateKey);49  }50  finally {51    await helper.disconnect();52    silentConsole.disable();53  }54};5556export const MINIMUM_DONOR_FUND = 100_000n;57export const DONOR_FUNDING = 1_000_000n;5859export enum Pallets {60  Inflation = 'inflation',61  RmrkCore = 'rmrkcore',62  RmrkEquip = 'rmrkequip',63  ReFungible = 'refungible',64  Fungible = 'fungible',65  NFT = 'nonfungible',66  Scheduler = 'scheduler',67  AppPromotion = 'apppromotion',68}6970export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {71  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);72    73  if (missingPallets.length > 0) {74    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;75    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);76    test.skip();77  }78}7980export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {81  (opts.only ? it.only : 82    opts.skip ? it.skip : it)(name, async function () {83    await usingPlaygrounds(async (helper, privateKey) => {84      if (opts.requiredPallets) {85        requirePalletsOrSkip(this, helper, opts.requiredPallets);86      }87      88      await cb({helper, privateKey});89    });90  });91}92export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {93  return itSub(name, cb, {requiredPallets: required, ...opts});94}95itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});96itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});9798itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});99itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});100itSub.ifWithPallets = itSubIfWithPallet;