Refactor basic command result checks

This commit is contained in:
Prathamesh Musale 2024-01-25 12:45:30 +05:30
parent 3a0866ea96
commit 12dd420a13

View File

@ -1,7 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import yaml from 'js-yaml'; import yaml from 'js-yaml';
import assert from 'assert'; import assert from 'assert';
import { spawnSync } from 'child_process'; import { SpawnSyncReturns, spawnSync } from 'child_process';
const TOKEN_TYPE = 'aphoton'; const TOKEN_TYPE = 'aphoton';
@ -40,10 +40,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns status', async () => { test('laconic cns status', async () => {
const result = spawnSync('laconic', ['cns', 'status']); const result = spawnSync('laconic', ['cns', 'status']);
const outputObj = checkResultAndRetrieveOutput(result);
const output = result.stdout.toString().trim();
const outputObj = JSON.parse(output);
const errorOutput = result.stderr.toString().trim();
// Expect output object to have CNS status props // Expect output object to have CNS status props
expect(outputObj).toHaveProperty('version'); expect(outputObj).toHaveProperty('version');
@ -56,9 +53,6 @@ describe('Test laconic CLI commands', () => {
expect(outputObj).toHaveProperty('num_peers'); expect(outputObj).toHaveProperty('num_peers');
expect(outputObj).toHaveProperty('peers'); expect(outputObj).toHaveProperty('peers');
expect(outputObj).toHaveProperty('disk_usage'); expect(outputObj).toHaveProperty('disk_usage');
expect(errorOutput).toBe('');
expect(result.status).toBe(0);
}); });
describe('Bond operations', () => { describe('Bond operations', () => {
@ -68,15 +62,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns bond create', async () => { 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 result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', bondBalance.toString(), '--gas', '200000', '--fees', `200000${TOKEN_TYPE}`]);
const outputObj = checkResultAndRetrieveOutput(result);
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);
// Expect output object to have resultant bond id // Expect output object to have resultant bond id
expect(outputObj).toHaveProperty('bondId'); expect(outputObj).toHaveProperty('bondId');
@ -86,15 +72,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns bond list', async () => { test('laconic cns bond list', async () => {
const result = spawnSync('laconic', ['cns', 'bond', 'list']); const result = spawnSync('laconic', ['cns', 'bond', 'list']);
const outputObj = checkResultAndRetrieveOutput(result);
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<any>(JSON.parse(output));
// Expected bond // Expected bond
const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance });
@ -105,15 +83,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns bond list --owner <owner_address>', async () => { test('laconic cns bond list --owner <owner_address>', async () => {
const result = spawnSync('laconic', ['cns', 'bond', 'list', '--owner', bondOwner]); const result = spawnSync('laconic', ['cns', 'bond', 'list', '--owner', bondOwner]);
const outputObj = checkResultAndRetrieveOutput(result);
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<any>(JSON.parse(output));
// Expected bond // Expected bond
const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance });
@ -124,15 +94,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns bond get --id <bond_id>', async () => { test('laconic cns bond get --id <bond_id>', async () => {
const result = spawnSync('laconic', ['cns', 'bond', 'get', '--id', bondId]); const result = spawnSync('laconic', ['cns', 'bond', 'get', '--id', bondId]);
const outputObj = checkResultAndRetrieveOutput(result);
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<any>(JSON.parse(output));
// Expected bond // Expected bond
const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance });
@ -147,15 +109,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns account get --address <account_address>', async () => { test('laconic cns account get --address <account_address>', async () => {
const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', testAccount]); const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', testAccount]);
const outputObj = checkResultAndRetrieveOutput(result);
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<any>(JSON.parse(output));
// Expected account // Expected account
const expectedAccount = getAccountObj({ address: testAccount }) const expectedAccount = getAccountObj({ address: testAccount })
@ -175,15 +129,7 @@ describe('Test laconic CLI commands', () => {
const balanceAfterSend = balanceBeforeSend - sendAmount; const balanceAfterSend = balanceBeforeSend - sendAmount;
const result = spawnSync('laconic', ['cns', 'tokens', 'send', '--address', testAccount2, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]); const result = spawnSync('laconic', ['cns', 'tokens', 'send', '--address', testAccount2, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]);
const outputObj = checkResultAndRetrieveOutput(result);
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<any>(JSON.parse(output));
// Expected acconts // Expected acconts
const expectedAccounts = [ const expectedAccounts = [
@ -208,15 +154,7 @@ describe('Test laconic CLI commands', () => {
({ bondId } = createBond(bondBalance)); ({ bondId } = createBond(bondBalance));
const result = spawnSync('laconic', ['cns', 'record', 'publish', '--filename', recordFilePath, '--bond-id', bondId, '--gas', gas.toString()]); const result = spawnSync('laconic', ['cns', 'record', 'publish', '--filename', recordFilePath, '--bond-id', bondId, '--gas', gas.toString()]);
const outputObj = checkResultAndRetrieveOutput(result);
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);
// Expect output object to resultant bond id // Expect output object to resultant bond id
expect(outputObj).toHaveProperty('id'); expect(outputObj).toHaveProperty('id');
@ -226,15 +164,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns record list', async () => { test('laconic cns record list', async () => {
const result = spawnSync('laconic', ['cns', 'record', 'list']); const result = spawnSync('laconic', ['cns', 'record', 'list']);
const outputObj = checkResultAndRetrieveOutput(result);
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<any>(JSON.parse(output));
// Expected record // Expected record
const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId });
@ -249,15 +179,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns record get --id <record_id>', async () => { test('laconic cns record get --id <record_id>', async () => {
const result = spawnSync('laconic', ['cns', 'record', 'get', '--id', recordId]); const result = spawnSync('laconic', ['cns', 'record', 'get', '--id', recordId]);
const outputObj = checkResultAndRetrieveOutput(result);
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<any>(JSON.parse(output));
// Expected record // Expected record
const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId });
@ -272,15 +194,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns authority reserve <authority_name>', async () => { test('laconic cns authority reserve <authority_name>', async () => {
const result = spawnSync('laconic', ['cns', 'authority', 'reserve', authorityName]); const result = spawnSync('laconic', ['cns', 'authority', 'reserve', authorityName]);
const outputObj = checkResultAndRetrieveOutput(result);
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);
// Expect result // Expect result
expect(outputObj).toEqual({ success: true }); expect(outputObj).toEqual({ success: true });
@ -288,16 +202,7 @@ describe('Test laconic CLI commands', () => {
test('laconic cns authority whois <authority_name>', async () => { test('laconic cns authority whois <authority_name>', async () => {
const result = spawnSync('laconic', ['cns', 'authority', 'whois', authorityName]); const result = spawnSync('laconic', ['cns', 'authority', 'whois', authorityName]);
const outputObj = checkResultAndRetrieveOutput(result);
// 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<any>(JSON.parse(output));
// Expected authority (still in auction) // Expected authority (still in auction)
const expectedAuthority = getAuthorityObj({ status: 'auction', auction: getAuctionObj({ owner: testAccount }) }); const expectedAuthority = getAuthorityObj({ status: 'auction', auction: getAuctionObj({ owner: testAccount }) });
@ -313,6 +218,18 @@ describe('Test laconic CLI commands', () => {
// Helper methods // Helper methods
function checkResultAndRetrieveOutput(result: SpawnSyncReturns<Buffer>): 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 } { 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 result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', quantity.toString(), '--gas', '200000', '--fees', `200000${TOKEN_TYPE}`]);
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();