From 56f958b9d34e42e5fc76cee401c22bdc3c6f758b Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Wed, 28 Aug 2024 14:47:32 +0530 Subject: [PATCH] Add tests for gas and fees config combinations --- test/cli.test.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++- test/helpers.ts | 33 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/test/cli.test.ts b/test/cli.test.ts index 5d84875..4f0168a 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -15,7 +15,8 @@ import { getRecordObj, getAuthorityObj, getAuctionObj, - getBidObj + getBidObj, + updateGasAndFeesConfig } from './helpers'; describe('Test laconic CLI commands', () => { @@ -585,5 +586,53 @@ describe('Test laconic CLI commands', () => { expect(resolveOutputObj.length).toEqual(0); }); }); + + describe('Gas and fees config', () => { + const bondAmount = 1000; + + test('gas set, fees set to Xalnt', async () => { + // gasPrice not set + const result = spawnSync('laconic', ['registry', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', bondAmount.toString()]); + + const outputObj = checkResultAndRetrieveOutput(result); + expect(outputObj.bondId).toBeDefined(); + + // gasPrice set (lower than min gas price) + updateGasAndFeesConfig(undefined, undefined, '0.00001alnt'); + const result1 = spawnSync('laconic', ['registry', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', bondAmount.toString()]); + + const outputObj1 = checkResultAndRetrieveOutput(result1); + expect(outputObj1.bondId).toBeDefined(); + }); + + test('gas not set, fees not set, gasPrice set', async () => { + updateGasAndFeesConfig(null, null, '1alnt'); + const result = spawnSync('laconic', ['registry', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', bondAmount.toString()]); + + const outputObj = checkResultAndRetrieveOutput(result); + expect(outputObj.bondId).toBeDefined(); + }); + + test('gas not set, fees set without token suffix, gasPrice set', async () => { + updateGasAndFeesConfig(null, '1.8', '1alnt'); + const result = spawnSync('laconic', ['registry', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', bondAmount.toString()]); + + const outputObj = checkResultAndRetrieveOutput(result); + expect(outputObj.bondId).toBeDefined(); + }); + + test('gas not set, fees not set, gasPrice not set', async () => { + updateGasAndFeesConfig(null, null, null); + const result = spawnSync('laconic', ['registry', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', bondAmount.toString()]); + + expect(result.status).toBe(1); + + const output = result.stdout.toString().trim(); + const errorOutput = result.stderr.toString().trim(); + + expect(output).toBe(''); + expect(errorOutput).toContain('Gas price must be set in the client options when auto gas is used.'); + }); + }); }); }); diff --git a/test/helpers.ts b/test/helpers.ts index 0a1e733..b024e4e 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -1,7 +1,10 @@ import fs from 'fs'; +import path from 'path'; import yaml from 'js-yaml'; import { SpawnSyncReturns, spawnSync } from 'child_process'; +import { getConfig } from '../src/util'; + export const CHAIN_ID = 'laconic_9000-1'; export const TOKEN_TYPE = 'alnt'; @@ -118,3 +121,33 @@ export function getBidObj (params: { bidder: string, status?: string }): any { export async function delay (ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } + +export function updateGasAndFeesConfig (gas?: string | null, fees?: string | null, gasPrice?: string | null): void { + const configFilePath = './config.yml'; + const config = getConfig(path.resolve(configFilePath)); + + if (gas != null) { + config.services.registry.gas = gas; + } else if (gas === null) { + delete config.services.registry.gas; + } + + if (fees != null) { + config.services.registry.fees = fees; + } else if (fees === null) { + delete config.services.registry.fees; + } + + if (gasPrice != null) { + config.services.registry.gasPrice = gasPrice; + } else if (gasPrice === null) { + delete config.services.registry.gasPrice; + } + + try { + fs.writeFileSync(configFilePath, yaml.dump(config), 'utf8'); + } catch (e) { + console.error('Error writing config file:', e); + throw e; + } +}