1234import {IKeyringPair} from '@polkadot/types/types';5import chai from 'chai';6import chaiAsPromised from 'chai-as-promised';7import {Context} from 'mocha';8import config from '../../config';9import '../../interfaces/augment-api-events';10import {DevUniqueHelper, SilentLogger, SilentConsole} from './unique.dev';1112chai.use(chaiAsPromised);13export const expect = chai.expect;1415export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>, url: string = config.substrateUrl) => {16 const silentConsole = new SilentConsole();17 silentConsole.enable();1819 const helper = new DevUniqueHelper(new SilentLogger());2021 try {22 await helper.connect(url);23 const ss58Format = helper.chain.getChainProperties().ss58Format;24 const privateKey = (seed: string) => helper.util.fromSeed(seed, ss58Format);25 await code(helper, privateKey);26 }27 finally {28 await helper.disconnect();29 silentConsole.disable();30 }31};3233export enum Pallets {34 Inflation = 'inflation',35 RmrkCore = 'rmrkcore',36 RmrkEquip = 'rmrkequip',37 ReFungible = 'refungible',38 Fungible = 'fungible',39 NFT = 'nonfungible',40 Scheduler = 'scheduler',41 AppPromotion = 'apppromotion',42}4344export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {45 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);46 47 if (missingPallets.length > 0) {48 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;49 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);50 test.skip();51 }52}5354export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {55 (opts.only ? it.only : 56 opts.skip ? it.skip : it)(name, async function () {57 await usingPlaygrounds(async (helper, privateKey) => {58 if (opts.requiredPallets) {59 requirePalletsOrSkip(this, helper, opts.requiredPallets);60 }61 62 await cb({helper, privateKey});63 });64 });65}66export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {67 return itSub(name, cb, {requiredPallets: required, ...opts});68}69itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {only: true});70itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {skip: true});7172itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {only: true});73itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});74itSub.ifWithPallets = itSubIfWithPallet;