From d51f9618e083ad27f2c8a3027c7389a3ec2ae045 Mon Sep 17 00:00:00 2001 From: nabarun Date: Mon, 11 Apr 2022 18:13:08 +0530 Subject: [PATCH] Implement tests for auction module --- package.json | 1 + src/auction.test.ts | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/auction.test.ts diff --git a/package.json b/package.json index 8914d18..52d8b50 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ }, "scripts": { "test": "jest --runInBand --verbose", + "test:auctions": "AUCTIONS_ENABLED=1 jest --runInBand --verbose", "build": "tsc" } } diff --git a/src/auction.test.ts b/src/auction.test.ts new file mode 100644 index 0000000..999a911 --- /dev/null +++ b/src/auction.test.ts @@ -0,0 +1,75 @@ +import assert from 'assert'; + +import { Registry, Account } from './index'; +import { getConfig } from './testing/helper'; + +jest.setTimeout(30 * 60 * 1000); + +const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig(); + +const auctionTests = (numBidders = 3) => { + let registry: Registry; + + const accounts: { address: string, privateKey: string }[] = []; + + let auctionId: string; + let authorityName: string; + + beforeAll(async () => { + console.log('Running auction tests with num bidders', numBidders); + + registry = new Registry(restEndpoint, gqlEndpoint, chainId); + }); + + test('Setup bidder accounts', async () => { + for (let i = 0; i < numBidders; i++) { + const mnenonic = Account.generateMnemonic(); + const account = await Account.generateFromMnemonic(mnenonic); + await account.init(); + const bidderAddress = account.formattedCosmosAddress; + assert(bidderAddress) + await registry.sendCoins({ denom: 'uwire', amount: '1000000000', destinationAddress: bidderAddress }, accountAddress, privateKey, fee); + accounts.push({ address: bidderAddress, privateKey: account.privateKey.toString('hex') }); + } + + accounts.unshift({ address: accountAddress, privateKey }); + }); + + test('Reserve authority.', async () => { + authorityName = `dxos-${Date.now()}`; + await registry.reserveAuthority({ name: authorityName, owner: accounts[0].address }, accounts[0].address, accounts[0].privateKey, fee); + }); + + test('Authority should be under auction.', async () => { + const [record] = await registry.lookupAuthorities([authorityName], true); + expect(record.ownerAddress).toEqual(''); + expect(record.height).toBeDefined(); + expect(record.status).toEqual('auction'); + + expect(record.auction.id).toBeDefined(); + expect(record.auction.status).toEqual('commit'); + + auctionId = record.auction.id; + }); +}; + +const withNumBidders = (numBidders: number) => () => auctionTests(numBidders); + +if (!process.env.AUCTIONS_ENABLED) { + // Required as jest complains if file has no tests. + test('skipping auction tests', () => {}); +} else { + /** + Running these tests requires name auctions enabled. In chiba-clonk repo run: + + AUCTION_ENABLED=true ./init.sh + + + Run tests: + + yarn test:auctions + */ + describe('Auction (1 bidder)', withNumBidders(1)); + describe('Auction (2 bidders)', withNumBidders(2)); + describe('Auction (4 bidders)', withNumBidders(4)); +}