From 1dbc0313421341c6e0fedeeb8d693af57051b2ec Mon Sep 17 00:00:00 2001 From: Max Andreev Date: Thu, 08 Sep 2022 13:30:37 +0000 Subject: [PATCH] Fix tests Add waiter function Remove global before script Remove useless logs Fix refungible tests --- --- a/tests/src/app-promotion.test.ts +++ b/tests/src/app-promotion.test.ts @@ -28,6 +28,7 @@ import {encodeAddress} from '@polkadot/util-crypto'; import {stringToU8a} from '@polkadot/util'; import {SponsoringMode, contractHelpers, createEthAccountWithBalance, deployFlipper, itWeb3, transferBalanceToEth} from './eth/util/helpers'; +import {DevUniqueHelper} from './util/playgrounds/unique.dev'; chai.use(chaiAsPromised); const expect = chai.expect; @@ -40,9 +41,9 @@ const UNLOCKING_PERIOD = 10n; // 20 blocks of parachain const rewardAvailableInBlock = (stakedInBlock: bigint) => (stakedInBlock - stakedInBlock % LOCKING_PERIOD) + (LOCKING_PERIOD * 2n); -before(async function () { +const beforeEach = async (context: Mocha.Context) => { await usingPlaygrounds(async (helper, privateKey) => { - if (!getModuleNames(helper.api!).includes(Pallets.AppPromotion)) this.skip(); + if (!getModuleNames(helper.api!).includes(Pallets.AppPromotion)) context.skip(); alice = privateKey('//Alice'); palletAdmin = privateKey('//Charlie'); // TODO use custom address await helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address}))); @@ -51,9 +52,13 @@ await helper.balance.transferToSubstrate(alice, palletAddress, 1000n * nominal); accounts = await helper.arrange.createCrowd(100, 1000n, alice); // create accounts-pool to speed up tests }); -}); +}; describe('app-promotions.stake extrinsic', () => { + before(async function () { + await beforeEach(this); + }); + it('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async () => { await usingPlaygrounds(async (helper) => { const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; @@ -128,6 +133,10 @@ }); describe('unstake balance extrinsic', () => { + before(async function () { + await beforeEach(this); + }); + it('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => { await usingPlaygrounds(async helper => { const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; @@ -247,6 +256,10 @@ }); describe('Admin adress', () => { + before(async function () { + await beforeEach(this); + }); + it('can be set by sudo only', async () => { await usingPlaygrounds(async (helper) => { const nonAdmin = accounts.pop()!; @@ -291,6 +304,7 @@ describe('App-promotion collection sponsoring', () => { before(async function () { + await beforeEach(this); await usingPlaygrounds(async (helper) => { const tx = helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})); await helper.signTransaction(alice, tx); @@ -394,6 +408,10 @@ }); describe('app-promotion stopSponsoringCollection', () => { + before(async function () { + await beforeEach(this); + }); + it('can not be called by non-admin', async () => { await usingPlaygrounds(async (helper) => { const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!]; @@ -450,6 +468,10 @@ }); describe('app-promotion contract sponsoring', () => { + before(async function () { + await beforeEach(this); + }); + itWeb3('should set palletes address as a sponsor', async ({api, web3, privateKeyWrapper}) => { await usingPlaygrounds(async (helper) => { const contractOwner = (await createEthAccountWithBalance(api, web3, privateKeyWrapper)).toLowerCase(); @@ -570,6 +592,10 @@ }); describe('app-promotion stopSponsoringContract', () => { + before(async function () { + await beforeEach(this); + }); + itWeb3('should remove pallet address from contract sponsors', async ({api, web3, privateKeyWrapper}) => { await usingPlaygrounds(async (helper) => { const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper); @@ -624,6 +650,10 @@ }); describe('app-promotion rewards', () => { + before(async function () { + await beforeEach(this); + }); + it('can not be called by non admin', async () => { await usingPlaygrounds(async (helper) => { const nonAdmin = accounts.pop()!; @@ -634,11 +664,13 @@ it('should credit 0.05% for staking period', async () => { await usingPlaygrounds(async helper => { const staker = accounts.pop()!; + + await waitPromotionPeriodDoesntEnd(helper); await helper.staking.stake(staker, 100n * nominal); await helper.staking.stake(staker, 200n * nominal); - // wair rewards are available: + // wait rewards are available: const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[1][0]; await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock)); @@ -688,7 +720,6 @@ }); it('should bring compound interest', async () => { - // TODO flaky test await usingPlaygrounds(async helper => { const staker = accounts.pop()!; @@ -756,3 +787,14 @@ return calculateIncome(income, calcPeriod, iter - 1); } else return income; } + +// Wait while promotion period less than specified block, to avoid boundary cases +// 0 if this should be the beginning of the period. +async function waitPromotionPeriodDoesntEnd(helper: DevUniqueHelper, waitBlockLessThan = LOCKING_PERIOD / 3n) { + const relayBlockNumber = (await helper.api!.query.parachainSystem.validationData()).value.relayParentNumber.toNumber(); // await helper.chain.getLatestBlockNumber(); + const currentPeriodBlock = BigInt(relayBlockNumber) % LOCKING_PERIOD; + + if (currentPeriodBlock > waitBlockLessThan) { + await helper.wait.forRelayBlockNumber(BigInt(relayBlockNumber) + LOCKING_PERIOD - currentPeriodBlock); + } +} --- a/tests/src/refungible.test.ts +++ b/tests/src/refungible.test.ts @@ -245,8 +245,8 @@ section: 'common', index: '0x4202', data: [ - collection.collectionId.toString(), - token.tokenId.toString(), + helper.api!.createType('u32', collection.collectionId).toHuman(), + helper.api!.createType('u32', token.tokenId).toHuman(), {Substrate: alice.address}, '100', ], @@ -265,8 +265,8 @@ section: 'common', index: '0x4203', data: [ - collection.collectionId.toString(), - token.tokenId.toString(), + helper.api!.createType('u32', collection.collectionId).toHuman(), + helper.api!.createType('u32', token.tokenId).toHuman(), {Substrate: alice.address}, '50', ], --- a/tests/src/util/playgrounds/unique.dev.ts +++ b/tests/src/util/playgrounds/unique.dev.ts @@ -35,7 +35,7 @@ for (const arg of args) { if (typeof arg !== 'string') continue; - if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis: UniqueApi/2, RmrkApi/1') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure') + if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis:') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure') return; } printer(...args); -- gitstuff