From 0221d75b866fcb29d835da3de88440f8ca30ec88 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Wed, 24 Jan 2024 18:22:03 +0530 Subject: [PATCH] Add tests for commands to fetch bond --- test/cli.test.ts | 88 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 16 deletions(-) diff --git a/test/cli.test.ts b/test/cli.test.ts index 6ed0af6..095c46a 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -1,16 +1,6 @@ import { spawnSync } from 'child_process'; describe('Test laconic CLI commands', () => { - beforeAll(async () => { - // TODO: Install laconic binary (create symlink in path) - // TODO: Create a temp directory to run the tests in and cd into that - // TODO: Create the required config - }); - - afterAll(async () => { - // TODO: Cleanup temp dir - }); - test('laconic', async () => { const result = spawnSync('laconic'); @@ -52,6 +42,7 @@ describe('Test laconic CLI commands', () => { expect(outputObj).toHaveProperty('node'); expect(outputObj).toHaveProperty('node.network', 'laconic_9000-1'); expect(outputObj).toHaveProperty('sync'); + expect(Number(outputObj.sync.latest_block_height)).toBeGreaterThan(0); expect(outputObj).toHaveProperty('validator'); expect(outputObj).toHaveProperty('validators'); expect(outputObj).toHaveProperty('num_peers'); @@ -63,10 +54,12 @@ describe('Test laconic CLI commands', () => { }); describe('laconic cns bond ', () => { - let createdBondId: string; + const bondBalance = 1000000000; + let bondId: string; + let bondOwner: string; test('laconic cns bond create', async () => { - const result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', 'aphoton', '--quantity', '1000000000', '--gas', '200000', '--fees', '200000aphoton']); + const result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', 'aphoton', '--quantity', bondBalance.toString(), '--gas', '200000', '--fees', '200000aphoton']); const output = result.stdout.toString().trim(); const errorOutput = result.stderr.toString().trim(); @@ -79,7 +72,8 @@ describe('Test laconic CLI commands', () => { // Expect output object to resultant bond id expect(outputObj).toHaveProperty('bondId'); - createdBondId = outputObj.bondId; + + bondId = outputObj.bondId; }); test('laconic cns bond list', async () => { @@ -94,13 +88,75 @@ describe('Test laconic CLI commands', () => { expect(output.length).toBeGreaterThan(0); const outputObj = Array.from(JSON.parse(output)); - // Expect output object to an array of bonds + // Expected bond + const expectedBond = getExpectedBond({ id: bondId, balance: bondBalance }); + expect(outputObj.length).toEqual(1); - expect(outputObj[0]).toHaveProperty('id', createdBondId); expect(outputObj[0]).toHaveProperty('owner'); - expect(outputObj[0]).toHaveProperty('balance'); + expect(outputObj[0]).toMatchObject(expectedBond); + + bondOwner = outputObj[0].owner; + }); + + 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)); + + // Expected bond + const expectedBond = getExpectedBond({ id: bondId, owner: bondOwner, balance: bondBalance }); + + expect(outputObj.length).toEqual(1); + expect(outputObj[0]).toEqual(expectedBond); + }); + + 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)); + + // Expected bond + const expectedBond = getExpectedBond({ id: bondId, owner: bondOwner, balance: bondBalance }); + + expect(outputObj.length).toEqual(1); + expect(outputObj[0]).toEqual(expectedBond); }); }); }); }); }); + +// Helper methods + +function getExpectedBond(params: { id: string, balance: number, owner?: string }): any { + const bond: any = { + id: params.id, + balance: [ + { + type: "aphoton", + quantity: params.balance + } + ] + }; + + // Add owner only if passed + if (params.owner) { + bond.owner = params.owner; + } + + return bond; +}