From 3a0866ea969e69cef6140a191a4f40059b92f855 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 25 Jan 2024 12:26:49 +0530 Subject: [PATCH] Refactor and fix failing test --- .gitea/workflows/test.yml | 2 - test/cli.test.ts | 107 ++++++++++++++++---------------------- test/run-tests.sh | 2 +- 3 files changed, 47 insertions(+), 64 deletions(-) diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index e4cdb02..295669b 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -42,8 +42,6 @@ jobs: repository: cerc-io/laconicd fetch-depth: 0 ref: main - - name: Environment - run: ls -tlh && env - name: Build laconicd container working-directory: ./laconicd/tests/sdk_tests diff --git a/test/cli.test.ts b/test/cli.test.ts index f299a81..3fc4338 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -8,6 +8,7 @@ const TOKEN_TYPE = 'aphoton'; describe('Test laconic CLI commands', () => { test('laconic', async () => { const result = spawnSync('laconic'); + expect(result.status).toBe(1); const output = result.stdout.toString().trim(); const errorOutput = result.stderr.toString().trim(); @@ -15,11 +16,11 @@ describe('Test laconic CLI commands', () => { // Expect error with usage string expect(output).toBe(''); expect(errorOutput).toContain('laconic '); - expect(result.status).toBe(1); }); test('laconic cns', async () => { const result = spawnSync('laconic', ['cns']); + expect(result.status).toBe(1); const output = result.stdout.toString().trim(); const errorOutput = result.stderr.toString().trim(); @@ -29,17 +30,13 @@ describe('Test laconic CLI commands', () => { 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'; - + const testAccount = process.env.TEST_ACCOUNT; + const testAccount2 = 'ethm1vc62ysqu504at932jjq8pwrqgjt67rx6ggn5yu'; + assert(testAccount, 'TEST_ACCOUNT not set in env'); test('laconic cns status', async () => { const result = spawnSync('laconic', ['cns', 'status']); @@ -66,12 +63,11 @@ describe('Test laconic CLI commands', () => { describe('Bond operations', () => { const bondBalance = 1000000000; - const bondOwner = existingAccount; + const bondOwner = testAccount; let bondId: string; 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 errorOutput = result.stderr.toString().trim(); @@ -101,7 +97,7 @@ describe('Test laconic CLI commands', () => { const outputObj = Array.from(JSON.parse(output)); // Expected bond - const expectedBond = getBond({ id: bondId, owner: bondOwner, balance: bondBalance }); + const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toEqual(expectedBond); @@ -120,7 +116,7 @@ describe('Test laconic CLI commands', () => { const outputObj = Array.from(JSON.parse(output)); // Expected bond - const expectedBond = getBond({ id: bondId, owner: bondOwner, balance: bondBalance }); + const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toEqual(expectedBond); @@ -139,7 +135,7 @@ describe('Test laconic CLI commands', () => { const outputObj = Array.from(JSON.parse(output)); // Expected bond - const expectedBond = getBond({ id: bondId, owner: bondOwner, balance: bondBalance }); + const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toEqual(expectedBond); @@ -150,7 +146,7 @@ describe('Test laconic CLI commands', () => { let balanceBeforeSend: number; test('laconic cns account get --address ', async () => { - const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', existingAccount]); + const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', testAccount]); const output = result.stdout.toString().trim(); const errorOutput = result.stderr.toString().trim(); @@ -162,20 +158,14 @@ describe('Test laconic CLI commands', () => { 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); + const expectedAccount = getAccountObj({ address: testAccount }) expect(outputObj.length).toEqual(1); expect(outputObj[0]).toMatchObject(expectedAccount); + expect(outputObj[0]).toHaveProperty('number'); + expect(outputObj[0]).toHaveProperty('sequence'); + + balanceBeforeSend = Number(outputObj[0].balance[0].quantity); expect(balanceBeforeSend).toBeGreaterThan(0); expect(balanceBeforeSend).toBeLessThan(initialAccountBalance); }); @@ -184,8 +174,7 @@ describe('Test laconic CLI commands', () => { const sendAmount = 1000000000; const balanceAfterSend = balanceBeforeSend - sendAmount; - const result = spawnSync('laconic', ['cns', 'tokens', 'send', '--address', secondAccount, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]); - // accountSequence++; + 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(); @@ -198,28 +187,8 @@ describe('Test laconic CLI commands', () => { // Expected acconts const expectedAccounts = [ - { - address: existingAccount, - number: 0, - sequence: 3, - balance: [ - { - type: TOKEN_TYPE, - quantity: balanceAfterSend - } - ] - }, - { - address: secondAccount, - pubKey: null, - sequence: 0, - balance: [ - { - type: TOKEN_TYPE, - quantity: sendAmount - } - ] - }, + getAccountObj({ address: testAccount, balance: balanceAfterSend }), + getAccountObj({ address: testAccount2, balance: sendAmount }), ]; expect(outputObj.length).toEqual(2); @@ -231,10 +200,13 @@ describe('Test laconic CLI commands', () => { const recordFilePath = 'test/data/watcher-record.yml'; const gas = 250000; const bondBalance = 1000000000; - const { bondId } = createBond(bondBalance); + let bondId: string; let recordId: string; test('laconic cns record publish --filename --bond-id --gas ', async () => { + // Create a new bond to be associated with the record + ({ bondId } = createBond(bondBalance)); + const result = spawnSync('laconic', ['cns', 'record', 'publish', '--filename', recordFilePath, '--bond-id', bondId, '--gas', gas.toString()]); const output = result.stdout.toString().trim(); @@ -265,7 +237,7 @@ describe('Test laconic CLI commands', () => { const outputObj = Array.from(JSON.parse(output)); // Expected record - const expectedRecord = getRecord(recordFilePath, { bondId, recordId }); + const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toMatchObject(expectedRecord); @@ -288,7 +260,7 @@ describe('Test laconic CLI commands', () => { const outputObj = Array.from(JSON.parse(output)); // Expected record - const expectedRecord = getRecord(recordFilePath, { bondId, recordId }); + const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toMatchObject(expectedRecord); @@ -317,6 +289,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(); @@ -327,7 +300,7 @@ describe('Test laconic CLI commands', () => { const outputObj = Array.from(JSON.parse(output)); // Expected authority (still in auction) - const expectedAuthority = getAuthority({ status: 'auction', auction: getAuction({ owner: existingAccount }) }); + const expectedAuthority = getAuthorityObj({ status: 'auction', auction: getAuctionObj({ owner: testAccount }) }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toMatchObject(expectedAuthority); @@ -340,7 +313,14 @@ describe('Test laconic CLI commands', () => { // Helper methods -function getBond(params: { id: string, owner: string, balance: number}): any { +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(); + + return JSON.parse(output); +} + +function getBondObj(params: { id: string, owner: string, balance: number}): any { return { id: params.id, owner: params.owner, @@ -353,14 +333,19 @@ function getBond(params: { id: string, owner: string, balance: number}): any { }; } -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(); +function getAccountObj(params: { address: string, balance?: number }): any { + const balanceObj: any = { type: TOKEN_TYPE }; + if (params.balance) { + balanceObj.quantity = params.balance; + } - return JSON.parse(output); + return { + address: params.address, + balance: [balanceObj] + }; } -function getRecord(recordFilePath: string, params: { bondId: string, recordId: string }): any { +function getRecordObj(recordFilePath: string, params: { bondId: string, recordId: string }): any { const recordContent = yaml.load(fs.readFileSync(recordFilePath, 'utf8')) as any; return { @@ -371,7 +356,7 @@ function getRecord(recordFilePath: string, params: { bondId: string, recordId: s }; } -function getAuthority(params: { status: string, auction: any }): any { +function getAuthorityObj(params: { status: string, auction: any }): any { return { ownerAddress: '', ownerPublicKey: '', @@ -381,7 +366,7 @@ function getAuthority(params: { status: string, auction: any }): any { }; } -function getAuction(params: { owner: string, status?: string, bids?: any[] }): any { +function getAuctionObj(params: { owner: string, status?: string, bids?: any[] }): any { const auctionFees = { commit: 1000000, reveal: 1000000, diff --git a/test/run-tests.sh b/test/run-tests.sh index 87ef794..9928477 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -31,4 +31,4 @@ echo "$config" > "$config_file" docker compose exec laconicd sh -c "curl --retry 10 --retry-delay 3 --retry-connrefused http://127.0.0.1:9473/api" # Run tests -EXISTING_ACCOUNT=$laconicd_account_address yarn test +TEST_ACCOUNT=$laconicd_account_address yarn test