git.delta.rocks / unique-network / refs/commits / 535a8e928ce8

difftreelog

source

tests/src/util/playgrounds/index.ts4.2 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 './unique.dev';1314chai.use(chaiAsPromised);15export const expect = chai.expect;1617const getTestHash = (filename: string) => {18  return crypto.createHash('md5').update(path.basename(filename)).digest('hex');19};2021export const getTestSeed = (filename: string) => {22  return `//Alice+${getTestHash(filename)}`;23};2425// todo:playgrounds normalize to seed and filename26export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {27  const silentConsole = new SilentConsole();28  silentConsole.enable();2930  const helper = new DevUniqueHelper(new SilentLogger());3132  try {33    await helper.connect(url);34    const ss58Format = helper.chain.getChainProperties().ss58Format;35    const privateKey = async (seed: string | {filename: string}) => {36      if (typeof seed === 'string') {37        return helper.util.fromSeed(seed, ss58Format);38      }39      else {40        const actualSeed = getTestSeed(seed.filename);41        let account = helper.util.fromSeed(actualSeed, ss58Format);42        if (await helper.balance.getSubstrate(account.address) == 0n) {43          console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);44          account = helper.util.fromSeed('//Alice', ss58Format);45        }46        return account;47      }48    };49    await code(helper, privateKey);50  }51  finally {52    await helper.disconnect();53    silentConsole.disable();54  }55};5657export enum Pallets {58  Inflation = 'inflation',59  RmrkCore = 'rmrkcore',60  RmrkEquip = 'rmrkequip',61  ReFungible = 'refungible',62  Fungible = 'fungible',63  NFT = 'nonfungible',64  Scheduler = 'scheduler',65}6667export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {68  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);69    70  if (missingPallets.length > 0) {71    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;72    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);73    test.skip();74  }75}7677export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {78  (opts.only ? it.only : 79    opts.skip ? it.skip : it)(name, async function () {80    await usingPlaygrounds(async (helper, privateKey) => {81      if (opts.requiredPallets) {82        requirePalletsOrSkip(this, helper, opts.requiredPallets);83      }84      85      await cb({helper, privateKey});86    });87  });88}89export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {90  return itSub(name, cb, {requiredPallets: required, ...opts});91}92itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});93itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});9495itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});96itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});97itSub.ifWithPallets = itSubIfWithPallet;