git.delta.rocks / unique-network / refs/commits / 415e2e02d914

difftreelog

source

tests/src/util/playgrounds/index.ts3.4 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {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 U128_MAX = (1n << 128n) - 1n;1617const MICROUNIQUE = 1_000_000_000_000n;18const MILLIUNIQUE = 1_000n * MICROUNIQUE;19const CENTIUNIQUE = 10n * MILLIUNIQUE;20export const UNIQUE = 100n * CENTIUNIQUE;2122export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>, url: string = config.substrateUrl) => {23  const silentConsole = new SilentConsole();24  silentConsole.enable();2526  const helper = new DevUniqueHelper(new SilentLogger());2728  try {29    await helper.connect(url);30    const ss58Format = helper.chain.getChainProperties().ss58Format;31    const privateKey = (seed: string) => helper.util.fromSeed(seed, ss58Format);32    await code(helper, privateKey);33  }34  finally {35    await helper.disconnect();36    silentConsole.disable();37  }38};3940export enum Pallets {41  Inflation = 'inflation',42  RmrkCore = 'rmrkcore',43  RmrkEquip = 'rmrkequip',44  ReFungible = 'refungible',45  Fungible = 'fungible',46  NFT = 'nonfungible',47  Scheduler = 'scheduler',48}4950export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {51  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);52    53  if (missingPallets.length > 0) {54    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;55    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);56    test.skip();57  }58}5960export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {61  (opts.only ? it.only : 62    opts.skip ? it.skip : it)(name, async function () {63    await usingPlaygrounds(async (helper, privateKey) => {64      if (opts.requiredPallets) {65        requirePalletsOrSkip(this, helper, opts.requiredPallets);66      }67      68      await cb({helper, privateKey});69    });70  });71}72export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {73  return itSub(name, cb, {requiredPallets: required, ...opts});74}75itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {only: true});76itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSub(name, cb, {skip: true});7778itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {only: true});79itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});80itSub.ifWithPallets = itSubIfWithPallet;