From 4036d33d7a4320f615097c739acb6677eda38d86 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 25 Jan 2024 09:56:33 +0530 Subject: [PATCH] Refactor and add test for command to send balance --- .gitea/workflows/test.yml | 2 - test/cli.test.ts | 300 ++++++++++++++++++++++---------------- test/run-tests.sh | 2 + 3 files changed, 179 insertions(+), 125 deletions(-) diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index 352a909..e4cdb02 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -34,8 +34,6 @@ jobs: run: yarn && yarn build - name: Install registry-cli run: yarn global add file:$PWD - - name: Log CLI version - run: laconic --version - name: Checkout laconicd uses: actions/checkout@v3 diff --git a/test/cli.test.ts b/test/cli.test.ts index dda9afa..d9b68b3 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -1,6 +1,8 @@ import assert from 'assert'; import { spawnSync } from 'child_process'; +const TOKEN_TYPE = 'aphoton'; + describe('Test laconic CLI commands', () => { test('laconic', async () => { const result = spawnSync('laconic'); @@ -14,161 +16,213 @@ describe('Test laconic CLI commands', () => { expect(result.status).toBe(1); }); - // TODO: Test subcommands - describe('laconic ', () => { - test('laconic cns', async () => { - const result = spawnSync('laconic', ['cns']); + test('laconic cns', async () => { + const result = spawnSync('laconic', ['cns']); + + const output = result.stdout.toString().trim(); + const errorOutput = result.stderr.toString().trim(); + + // Expect error with usage string + expect(output).toBe(''); + expect(errorOutput).toContain('laconic cns'); + expect(errorOutput).toContain('CNS tools'); + expect(errorOutput).toContain('Commands:'); + expect(result.status).toBe(1); + }); + + describe('laconic CNS commands', () => { + const initialAccountBalance = Number('100000000000000000000000000'); + // let accountSequence = 0; + const existingAccount = process.env.EXISTING_ACCOUNT; + assert(existingAccount, 'EXISTING_ACCOUNT not set in env'); + + const secondAccount = 'ethm1vc62ysqu504at932jjq8pwrqgjt67rx6ggn5yu'; + + + 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(); - // Expect error with usage string - expect(output).toBe(''); - expect(errorOutput).toContain('laconic cns'); - expect(errorOutput).toContain('CNS tools'); - expect(errorOutput).toContain('Commands:'); - expect(result.status).toBe(1); + // Expect output object to have CNS status props + expect(outputObj).toHaveProperty('version'); + 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'); + expect(outputObj).toHaveProperty('peers'); + expect(outputObj).toHaveProperty('disk_usage'); + + expect(errorOutput).toBe(''); + expect(result.status).toBe(0); }); - describe('laconic cns ', () => { - const initialAccountBalance = Number('100000000000000000000000000'); - const existingAccount = process.env.EXISTING_ACCOUNT; - assert(existingAccount, 'EXISTING_ACCOUNT not set in env'); + describe('Bond operations', () => { + const bondBalance = 1000000000; + const bondOwner = existingAccount; + let bondId = 'de88ffab68af143da5d3ba2cf86f468ac4ac2efaa005e4d2fbce5fde85cbaa3e'; + // let bondId: string; - test('laconic cns status', async () => { - const result = spawnSync('laconic', ['cns', 'status']); + 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}`]); + // accountSequence++; 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(outputObj).toHaveProperty('version'); - 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'); - expect(outputObj).toHaveProperty('peers'); - expect(outputObj).toHaveProperty('disk_usage'); - 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(outputObj).toHaveProperty('bondId'); + + bondId = outputObj.bondId; }); - describe('laconic cns bond ', () => { - const bondBalance = 1000000000; - const bondOwner = existingAccount; - let bondId: string; + test('laconic cns bond list', async () => { + const result = spawnSync('laconic', ['cns', 'bond', 'list']); - test('laconic cns bond create', async () => { - 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(); - const output = result.stdout.toString().trim(); - const errorOutput = result.stderr.toString().trim(); + expect(errorOutput).toBe(''); + expect(result.status).toBe(0); - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); + expect(output.length).toBeGreaterThan(0); + const outputObj = Array.from(JSON.parse(output)); - expect(output.length).toBeGreaterThan(0); - const outputObj = JSON.parse(output); + // Expected bond + const expectedBond = getExpectedBond({ id: bondId, owner: bondOwner, balance: bondBalance }); - // Expect output object to resultant bond id - expect(outputObj).toHaveProperty('bondId'); - - bondId = outputObj.bondId; - }); - - 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)); - - // 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 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); - }); + expect(outputObj.length).toEqual(1); + expect(outputObj[0]).toEqual(expectedBond); }); - describe('laconic cns account ', () => { - test('laconic cns account get --address ', async () => { - const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', existingAccount]); + 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(); + const output = result.stdout.toString().trim(); + const errorOutput = result.stderr.toString().trim(); - expect(errorOutput).toBe(''); - expect(result.status).toBe(0); + expect(errorOutput).toBe(''); + expect(result.status).toBe(0); - expect(output.length).toBeGreaterThan(0); - const outputObj = Array.from(JSON.parse(output)); + expect(output.length).toBeGreaterThan(0); + const outputObj = Array.from(JSON.parse(output)); - // Expected account - const expectedAccount = { + // 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); + }); + }); + + describe('Account and tokens operations', () => { + let balanceBeforeSend: number; + + test('laconic cns account get --address ', async () => { + const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', existingAccount]); + + 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 account + const expectedAccount = { + address: existingAccount, + number: 0, + sequence: 2, + balance: [ + { + type: TOKEN_TYPE + } + ] + }; + balanceBeforeSend = Number(outputObj[0].balance[0].quantity); + + expect(outputObj.length).toEqual(1); + expect(outputObj[0]).toMatchObject(expectedAccount); + expect(balanceBeforeSend).toBeGreaterThan(0); + expect(balanceBeforeSend).toBeLessThan(initialAccountBalance); + }); + + test('laconic cns tokens send --address --type --quantity ', async () => { + const sendAmount = 1000000000; + const balanceAfterSend = balanceBeforeSend - sendAmount; + + const result = spawnSync('laconic', ['cns', 'tokens', 'send', '--address', secondAccount, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]); + // accountSequence++; + + 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 acconts + const expectedAccounts = [ + { address: existingAccount, number: 0, - sequence: 2, + sequence: 3, balance: [ { - type: "aphoton" + type: TOKEN_TYPE, + quantity: balanceAfterSend } ] - }; + }, + { + address: secondAccount, + pubKey: null, + sequence: 0, + balance: [ + { + type: TOKEN_TYPE, + quantity: sendAmount + } + ] + }, + ]; - expect(outputObj.length).toEqual(1); - expect(outputObj[0]).toMatchObject(expectedAccount); - expect(Number(outputObj[0].balance[0].quantity)).toBeGreaterThan(0); - expect(Number(outputObj[0].balance[0].quantity)).toBeLessThan(initialAccountBalance); - }); + expect(outputObj.length).toEqual(2); + expect(outputObj).toMatchObject(expectedAccounts); }); }); }); @@ -182,7 +236,7 @@ function getExpectedBond(params: { id: string, owner: string, balance: number}): owner: params.owner, balance: [ { - type: "aphoton", + type: TOKEN_TYPE, quantity: params.balance } ] diff --git a/test/run-tests.sh b/test/run-tests.sh index 1ec0755..87ef794 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -21,6 +21,8 @@ services: userKey: $laconicd_key bondId: chainId: $cosmos_chain_id + gas: 200000 + fees: 200000aphoton EOL ) echo "$config" > "$config_file"