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

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  AppPromotion = 'apppromotion',66}6768export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {69  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);70    71  if (missingPallets.length > 0) {72    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;73    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);74    test.skip();75  }76}7778export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {79  (opts.only ? it.only : 80    opts.skip ? it.skip : it)(name, async function () {81    await usingPlaygrounds(async (helper, privateKey) => {82      if (opts.requiredPallets) {83        requirePalletsOrSkip(this, helper, opts.requiredPallets);84      }85      86      await cb({helper, privateKey});87    });88  });89}90export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {91  return itSub(name, cb, {requiredPallets: required, ...opts});92}93itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});94itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});9596itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});97itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});98itSub.ifWithPallets = itSubIfWithPallet;