Refactor and fix failing test

This commit is contained in:
Prathamesh Musale 2024-01-25 12:26:49 +05:30
parent 19ac893020
commit 3a0866ea96
3 changed files with 47 additions and 64 deletions

View File

@ -42,8 +42,6 @@ jobs:
repository: cerc-io/laconicd repository: cerc-io/laconicd
fetch-depth: 0 fetch-depth: 0
ref: main ref: main
- name: Environment
run: ls -tlh && env
- name: Build laconicd container - name: Build laconicd container
working-directory: ./laconicd/tests/sdk_tests working-directory: ./laconicd/tests/sdk_tests

View File

@ -8,6 +8,7 @@ const TOKEN_TYPE = 'aphoton';
describe('Test laconic CLI commands', () => { describe('Test laconic CLI commands', () => {
test('laconic', async () => { test('laconic', async () => {
const result = spawnSync('laconic'); const result = spawnSync('laconic');
expect(result.status).toBe(1);
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim(); const errorOutput = result.stderr.toString().trim();
@ -15,11 +16,11 @@ describe('Test laconic CLI commands', () => {
// Expect error with usage string // Expect error with usage string
expect(output).toBe(''); expect(output).toBe('');
expect(errorOutput).toContain('laconic <command>'); expect(errorOutput).toContain('laconic <command>');
expect(result.status).toBe(1);
}); });
test('laconic cns', async () => { test('laconic cns', async () => {
const result = spawnSync('laconic', ['cns']); const result = spawnSync('laconic', ['cns']);
expect(result.status).toBe(1);
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
const errorOutput = result.stderr.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('laconic cns');
expect(errorOutput).toContain('CNS tools'); expect(errorOutput).toContain('CNS tools');
expect(errorOutput).toContain('Commands:'); expect(errorOutput).toContain('Commands:');
expect(result.status).toBe(1);
}); });
describe('laconic CNS commands', () => { describe('laconic CNS commands', () => {
const initialAccountBalance = Number('100000000000000000000000000'); const initialAccountBalance = Number('100000000000000000000000000');
// let accountSequence = 0; const testAccount = process.env.TEST_ACCOUNT;
const existingAccount = process.env.EXISTING_ACCOUNT; const testAccount2 = 'ethm1vc62ysqu504at932jjq8pwrqgjt67rx6ggn5yu';
assert(existingAccount, 'EXISTING_ACCOUNT not set in env'); assert(testAccount, 'TEST_ACCOUNT not set in env');
const secondAccount = 'ethm1vc62ysqu504at932jjq8pwrqgjt67rx6ggn5yu';
test('laconic cns status', async () => { test('laconic cns status', async () => {
const result = spawnSync('laconic', ['cns', 'status']); const result = spawnSync('laconic', ['cns', 'status']);
@ -66,12 +63,11 @@ describe('Test laconic CLI commands', () => {
describe('Bond operations', () => { describe('Bond operations', () => {
const bondBalance = 1000000000; const bondBalance = 1000000000;
const bondOwner = existingAccount; const bondOwner = testAccount;
let bondId: string; let bondId: string;
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}`]);
// accountSequence++;
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim(); const errorOutput = result.stderr.toString().trim();
@ -101,7 +97,7 @@ describe('Test laconic CLI commands', () => {
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expected bond // 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.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond); expect(outputObj[0]).toEqual(expectedBond);
@ -120,7 +116,7 @@ describe('Test laconic CLI commands', () => {
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expected bond // 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.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond); expect(outputObj[0]).toEqual(expectedBond);
@ -139,7 +135,7 @@ describe('Test laconic CLI commands', () => {
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expected bond // 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.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond); expect(outputObj[0]).toEqual(expectedBond);
@ -150,7 +146,7 @@ describe('Test laconic CLI commands', () => {
let balanceBeforeSend: number; let balanceBeforeSend: number;
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', existingAccount]); const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', testAccount]);
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim(); const errorOutput = result.stderr.toString().trim();
@ -162,20 +158,14 @@ describe('Test laconic CLI commands', () => {
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expected account // Expected account
const expectedAccount = { const expectedAccount = getAccountObj({ address: testAccount })
address: existingAccount,
number: 0,
sequence: 2,
balance: [
{
type: TOKEN_TYPE
}
]
};
balanceBeforeSend = Number(outputObj[0].balance[0].quantity);
expect(outputObj.length).toEqual(1); expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedAccount); 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).toBeGreaterThan(0);
expect(balanceBeforeSend).toBeLessThan(initialAccountBalance); expect(balanceBeforeSend).toBeLessThan(initialAccountBalance);
}); });
@ -184,8 +174,7 @@ describe('Test laconic CLI commands', () => {
const sendAmount = 1000000000; const sendAmount = 1000000000;
const balanceAfterSend = balanceBeforeSend - sendAmount; const balanceAfterSend = balanceBeforeSend - sendAmount;
const result = spawnSync('laconic', ['cns', 'tokens', 'send', '--address', secondAccount, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]); const result = spawnSync('laconic', ['cns', 'tokens', 'send', '--address', testAccount2, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]);
// accountSequence++;
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim(); const errorOutput = result.stderr.toString().trim();
@ -198,28 +187,8 @@ describe('Test laconic CLI commands', () => {
// Expected acconts // Expected acconts
const expectedAccounts = [ const expectedAccounts = [
{ getAccountObj({ address: testAccount, balance: balanceAfterSend }),
address: existingAccount, getAccountObj({ address: testAccount2, balance: sendAmount }),
number: 0,
sequence: 3,
balance: [
{
type: TOKEN_TYPE,
quantity: balanceAfterSend
}
]
},
{
address: secondAccount,
pubKey: null,
sequence: 0,
balance: [
{
type: TOKEN_TYPE,
quantity: sendAmount
}
]
},
]; ];
expect(outputObj.length).toEqual(2); expect(outputObj.length).toEqual(2);
@ -231,10 +200,13 @@ describe('Test laconic CLI commands', () => {
const recordFilePath = 'test/data/watcher-record.yml'; const recordFilePath = 'test/data/watcher-record.yml';
const gas = 250000; const gas = 250000;
const bondBalance = 1000000000; const bondBalance = 1000000000;
const { bondId } = createBond(bondBalance); let bondId: string;
let recordId: string; let recordId: string;
test('laconic cns record publish --filename <record_file> --bond-id <bond_id> --gas <gas>', async () => { test('laconic cns record publish --filename <record_file> --bond-id <bond_id> --gas <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 result = spawnSync('laconic', ['cns', 'record', 'publish', '--filename', recordFilePath, '--bond-id', bondId, '--gas', gas.toString()]);
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
@ -265,7 +237,7 @@ describe('Test laconic CLI commands', () => {
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expected record // Expected record
const expectedRecord = getRecord(recordFilePath, { bondId, recordId }); const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId });
expect(outputObj.length).toEqual(1); expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedRecord); expect(outputObj[0]).toMatchObject(expectedRecord);
@ -288,7 +260,7 @@ describe('Test laconic CLI commands', () => {
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expected record // Expected record
const expectedRecord = getRecord(recordFilePath, { bondId, recordId }); const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId });
expect(outputObj.length).toEqual(1); expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedRecord); expect(outputObj[0]).toMatchObject(expectedRecord);
@ -317,6 +289,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]);
// TODO: Refactor these basic checks
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim(); const errorOutput = result.stderr.toString().trim();
@ -327,7 +300,7 @@ describe('Test laconic CLI commands', () => {
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expected authority (still in auction) // 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.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedAuthority); expect(outputObj[0]).toMatchObject(expectedAuthority);
@ -340,7 +313,14 @@ describe('Test laconic CLI commands', () => {
// Helper methods // 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 { return {
id: params.id, id: params.id,
owner: params.owner, owner: params.owner,
@ -353,14 +333,19 @@ function getBond(params: { id: string, owner: string, balance: number}): any {
}; };
} }
function createBond(quantity: number): { bondId: string } { function getAccountObj(params: { address: string, balance?: number }): any {
const result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', TOKEN_TYPE, '--quantity', quantity.toString(), '--gas', '200000', '--fees', `200000${TOKEN_TYPE}`]); const balanceObj: any = { type: TOKEN_TYPE };
const output = result.stdout.toString().trim(); if (params.balance) {
balanceObj.quantity = params.balance;
return JSON.parse(output);
} }
function getRecord(recordFilePath: string, params: { bondId: string, recordId: string }): any { return {
address: params.address,
balance: [balanceObj]
};
}
function getRecordObj(recordFilePath: string, params: { bondId: string, recordId: string }): any {
const recordContent = yaml.load(fs.readFileSync(recordFilePath, 'utf8')) as any; const recordContent = yaml.load(fs.readFileSync(recordFilePath, 'utf8')) as any;
return { 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 { return {
ownerAddress: '', ownerAddress: '',
ownerPublicKey: '', 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 = { const auctionFees = {
commit: 1000000, commit: 1000000,
reveal: 1000000, reveal: 1000000,

View File

@ -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" docker compose exec laconicd sh -c "curl --retry 10 --retry-delay 3 --retry-connrefused http://127.0.0.1:9473/api"
# Run tests # Run tests
EXISTING_ACCOUNT=$laconicd_account_address yarn test TEST_ACCOUNT=$laconicd_account_address yarn test