From 12dd420a13dbb3b527c230d2ae0b42ba3bfbe7b7 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 25 Jan 2024 12:45:30 +0530 Subject: [PATCH] Refactor basic command result checks --- test/cli.test.ts | 133 +++++++++-------------------------------------- 1 file changed, 25 insertions(+), 108 deletions(-) diff --git a/test/cli.test.ts b/test/cli.test.ts index 3fc4338..7743562 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import yaml from 'js-yaml'; import assert from 'assert'; -import { spawnSync } from 'child_process'; +import { SpawnSyncReturns, spawnSync } from 'child_process'; const TOKEN_TYPE = 'aphoton'; @@ -40,10 +40,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns status', async () => { const result = spawnSync('laconic', ['cns', 'status']); - - const output = result.stdout.toString().trim(); - const outputObj = JSON.parse(output); - const errorOutput = result.stderr.toString().trim(); + const outputObj = checkResultAndRetrieveOutput(result); // Expect output object to have CNS status props expect(outputObj).toHaveProperty('version'); @@ -56,9 +53,6 @@ describe('Test laconic CLI commands', () => { expect(outputObj).toHaveProperty('num_peers'); expect(outputObj).toHaveProperty('peers'); expect(outputObj).toHaveProperty('disk_usage'); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); }); describe('Bond operations', () => { @@ -68,15 +62,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns bond create', async () => { const result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', bondBalance.toString(), '--gas', '200000', '--fees', `200000${TOKEN_TYPE}`]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = JSON.parse(output); + const outputObj = checkResultAndRetrieveOutput(result); // Expect output object to have resultant bond id expect(outputObj).toHaveProperty('bondId'); @@ -86,15 +72,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns bond list', async () => { const result = spawnSync('laconic', ['cns', 'bond', 'list']); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected bond const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); @@ -105,15 +83,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns bond list --owner ', async () => { const result = spawnSync('laconic', ['cns', 'bond', 'list', '--owner', bondOwner]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected bond const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); @@ -124,15 +94,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns bond get --id ', async () => { const result = spawnSync('laconic', ['cns', 'bond', 'get', '--id', bondId]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected bond const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); @@ -147,15 +109,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns account get --address ', async () => { const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', testAccount]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected account const expectedAccount = getAccountObj({ address: testAccount }) @@ -175,15 +129,7 @@ describe('Test laconic CLI commands', () => { const balanceAfterSend = balanceBeforeSend - sendAmount; const result = spawnSync('laconic', ['cns', 'tokens', 'send', '--address', testAccount2, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected acconts const expectedAccounts = [ @@ -208,15 +154,7 @@ describe('Test laconic CLI commands', () => { ({ bondId } = createBond(bondBalance)); const result = spawnSync('laconic', ['cns', 'record', 'publish', '--filename', recordFilePath, '--bond-id', bondId, '--gas', gas.toString()]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = JSON.parse(output); + const outputObj = checkResultAndRetrieveOutput(result); // Expect output object to resultant bond id expect(outputObj).toHaveProperty('id'); @@ -226,15 +164,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns record list', async () => { const result = spawnSync('laconic', ['cns', 'record', 'list']); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected record const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); @@ -249,15 +179,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns record get --id ', async () => { const result = spawnSync('laconic', ['cns', 'record', 'get', '--id', recordId]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected record const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); @@ -272,15 +194,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns authority reserve ', async () => { const result = spawnSync('laconic', ['cns', 'authority', 'reserve', authorityName]); - - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = JSON.parse(output); + const outputObj = checkResultAndRetrieveOutput(result); // Expect result expect(outputObj).toEqual({ success: true }); @@ -288,16 +202,7 @@ describe('Test laconic CLI commands', () => { test('laconic cns authority whois ', async () => { const result = spawnSync('laconic', ['cns', 'authority', 'whois', authorityName]); - - // TODO: Refactor these basic checks - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); - - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); - - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + const outputObj = checkResultAndRetrieveOutput(result); // Expected authority (still in auction) const expectedAuthority = getAuthorityObj({ status: 'auction', auction: getAuctionObj({ owner: testAccount }) }); @@ -313,6 +218,18 @@ describe('Test laconic CLI commands', () => { // Helper methods +function checkResultAndRetrieveOutput(result: SpawnSyncReturns): any { + expect(result.status).toBe(0); + + const errorOutput = result.stderr.toString().trim(); + expect(errorOutput).toBe(''); + + const output = result.stdout.toString().trim(); + expect(output.length).toBeGreaterThan(0); + + return JSON.parse(output); +} + function createBond(quantity: number): { bondId: string } { const result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', quantity.toString(), '--gas', '200000', '--fees', `200000${TOKEN_TYPE}`]); const output = result.stdout.toString().trim();