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}4243export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {44 const missingPallets = helper.fetchMissingPalletNames(requiredPallets);45 46 if (missingPallets.length > 0) {47 const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;48 console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);49 test.skip();50 }51}5253export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {54 (opts.only ? it.only : 55 opts.skip ? it.skip : it)(name, async function () {56 await usingPlaygrounds(async (helper, privateKey) => {57 if (opts.requiredPallets) {58 requirePalletsOrSkip(this, helper, opts.requiredPallets);59 }60 61 await cb({helper, privateKey});62 });63 });64}65export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {66 return itSub(name, cb, {requiredPallets: required, ...opts});67}68itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {only: true});69itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {skip: true});7071itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {only: true});72itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});73itSub.ifWithPallets = itSubIfWithPallet;