laconic-registry-cli/test/cli.test.ts

395 lines
13 KiB
TypeScript
Raw Normal View History

import fs from 'fs';
import yaml from 'js-yaml';
2024-01-24 13:26:35 +00:00
import assert from 'assert';
2024-01-24 06:51:43 +00:00
import { spawnSync } from 'child_process';
const TOKEN_TYPE = 'aphoton';
2024-01-24 06:51:43 +00:00
describe('Test laconic CLI commands', () => {
test('laconic', async () => {
const result = spawnSync('laconic');
2024-01-25 06:56:49 +00:00
expect(result.status).toBe(1);
2024-01-24 06:51:43 +00:00
const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim();
// Expect error with usage string
expect(output).toBe('');
expect(errorOutput).toContain('laconic <command>');
});
test('laconic cns', async () => {
const result = spawnSync('laconic', ['cns']);
2024-01-25 06:56:49 +00:00
expect(result.status).toBe(1);
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:');
});
describe('laconic CNS commands', () => {
const initialAccountBalance = Number('100000000000000000000000000');
2024-01-25 06:56:49 +00:00
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']);
2024-01-24 07:17:38 +00:00
const output = result.stdout.toString().trim();
const outputObj = JSON.parse(output);
2024-01-24 07:17:38 +00:00
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);
2024-01-24 07:17:38 +00:00
});
describe('Bond operations', () => {
const bondBalance = 1000000000;
2024-01-25 06:56:49 +00:00
const bondOwner = testAccount;
let bondId: string;
2024-01-24 13:26:35 +00:00
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}`]);
2024-01-24 07:17:38 +00:00
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(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<any>(JSON.parse(output));
2024-01-24 12:52:03 +00:00
// Expected bond
2024-01-25 06:56:49 +00:00
const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance });
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond);
});
test('laconic cns bond list --owner <owner_address>', 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<any>(JSON.parse(output));
2024-01-24 12:52:03 +00:00
// Expected bond
2024-01-25 06:56:49 +00:00
const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance });
2024-01-24 12:52:03 +00:00
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond);
});
2024-01-24 12:52:03 +00:00
test('laconic cns bond get --id <bond_id>', async () => {
const result = spawnSync('laconic', ['cns', 'bond', 'get', '--id', bondId]);
2024-01-24 12:52:03 +00:00
const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim();
2024-01-24 12:52:03 +00:00
expect(errorOutput).toBe('');
expect(result.status).toBe(0);
2024-01-24 12:52:03 +00:00
expect(output.length).toBeGreaterThan(0);
const outputObj = Array.from<any>(JSON.parse(output));
2024-01-24 12:52:03 +00:00
// Expected bond
2024-01-25 06:56:49 +00:00
const expectedBond = getBondObj({ id: bondId, owner: bondOwner, balance: bondBalance });
2024-01-24 12:52:03 +00:00
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond);
});
});
2024-01-24 12:52:03 +00:00
describe('Account and tokens operations', () => {
let balanceBeforeSend: number;
2024-01-24 12:52:03 +00:00
test('laconic cns account get --address <account_address>', async () => {
2024-01-25 06:56:49 +00:00
const result = spawnSync('laconic', ['cns', 'account', 'get', '--address', testAccount]);
2024-01-24 12:52:03 +00:00
const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim();
2024-01-24 12:52:03 +00:00
expect(errorOutput).toBe('');
expect(result.status).toBe(0);
2024-01-24 12:52:03 +00:00
expect(output.length).toBeGreaterThan(0);
const outputObj = Array.from<any>(JSON.parse(output));
// Expected account
2024-01-25 06:56:49 +00:00
const expectedAccount = getAccountObj({ address: testAccount })
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedAccount);
2024-01-25 06:56:49 +00:00
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);
});
test('laconic cns tokens send --address <account_address> --type <token_type> --quantity <quantity>', async () => {
const sendAmount = 1000000000;
const balanceAfterSend = balanceBeforeSend - sendAmount;
2024-01-25 06:56:49 +00:00
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();
expect(errorOutput).toBe('');
expect(result.status).toBe(0);
expect(output.length).toBeGreaterThan(0);
const outputObj = Array.from<any>(JSON.parse(output));
// Expected acconts
const expectedAccounts = [
2024-01-25 06:56:49 +00:00
getAccountObj({ address: testAccount, balance: balanceAfterSend }),
getAccountObj({ address: testAccount2, balance: sendAmount }),
];
expect(outputObj.length).toEqual(2);
expect(outputObj).toMatchObject(expectedAccounts);
});
2024-01-24 07:17:38 +00:00
});
describe('Record operations', () => {
const recordFilePath = 'test/data/watcher-record.yml';
const gas = 250000;
const bondBalance = 1000000000;
2024-01-25 06:56:49 +00:00
let bondId: string;
let recordId: string;
test('laconic cns record publish --filename <record_file> --bond-id <bond_id> --gas <gas>', async () => {
2024-01-25 06:56:49 +00:00
// 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();
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(outputObj).toHaveProperty('id');
recordId = outputObj.id;
});
test('laconic cns record list', async () => {
const result = spawnSync('laconic', ['cns', 'record', '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<any>(JSON.parse(output));
// Expected record
2024-01-25 06:56:49 +00:00
const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId });
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedRecord);
expect(outputObj[0]).toHaveProperty('createTime');
expect(outputObj[0]).toHaveProperty('expiryTime');
expect(outputObj[0]).toHaveProperty('owners');
expect(outputObj[0].owners.length).toEqual(1);
});
test('laconic cns record get --id <record_id>', async () => {
const result = spawnSync('laconic', ['cns', 'record', 'get', '--id', recordId]);
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
2024-01-25 06:56:49 +00:00
const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId });
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedRecord);
});
});
describe('Name authority operations', () => {
const authorityName = 'laconic';
test('laconic cns authority reserve <authority_name>', async () => {
const result = spawnSync('laconic', ['cns', 'authority', 'reserve', authorityName]);
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(outputObj).toEqual({ success: true });
});
test('laconic cns authority whois <authority_name>', async () => {
const result = spawnSync('laconic', ['cns', 'authority', 'whois', authorityName]);
2024-01-25 06:56:49 +00:00
// 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)
2024-01-25 06:56:49 +00:00
const expectedAuthority = getAuthorityObj({ status: 'auction', auction: getAuctionObj({ owner: testAccount }) });
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toMatchObject(expectedAuthority);
expect(outputObj[0]).toHaveProperty('expiryTime');
expect(outputObj[0].height).toBeGreaterThan(0);
});
});
2024-01-24 06:51:43 +00:00
});
});
2024-01-24 12:52:03 +00:00
// Helper methods
2024-01-25 06:56:49 +00:00
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 {
2024-01-24 13:26:35 +00:00
return {
2024-01-24 12:52:03 +00:00
id: params.id,
2024-01-24 13:26:35 +00:00
owner: params.owner,
2024-01-24 12:52:03 +00:00
balance: [
{
type: TOKEN_TYPE,
2024-01-24 12:52:03 +00:00
quantity: params.balance
}
]
};
}
2024-01-25 06:56:49 +00:00
function getAccountObj(params: { address: string, balance?: number }): any {
const balanceObj: any = { type: TOKEN_TYPE };
if (params.balance) {
balanceObj.quantity = params.balance;
}
2024-01-25 06:56:49 +00:00
return {
address: params.address,
balance: [balanceObj]
};
}
2024-01-25 06:56:49 +00:00
function getRecordObj(recordFilePath: string, params: { bondId: string, recordId: string }): any {
const recordContent = yaml.load(fs.readFileSync(recordFilePath, 'utf8')) as any;
return {
id: params.recordId,
names: null,
bondId: params.bondId,
attributes: recordContent.record
};
}
2024-01-25 06:56:49 +00:00
function getAuthorityObj(params: { status: string, auction: any }): any {
return {
ownerAddress: '',
ownerPublicKey: '',
status: params.status,
bondId: '',
auction: params.auction || null
};
}
2024-01-25 06:56:49 +00:00
function getAuctionObj(params: { owner: string, status?: string, bids?: any[] }): any {
const auctionFees = {
commit: 1000000,
reveal: 1000000,
minimumBid: 5000000
};
return {
status: params.status || 'commit',
ownerAddress: params.owner,
commitFee: {
type: TOKEN_TYPE,
quantity: auctionFees.commit
},
revealFee: {
type: TOKEN_TYPE,
quantity: auctionFees.reveal
},
minimumBid: {
type: TOKEN_TYPE,
quantity: auctionFees.minimumBid
},
winnerAddress: '',
bids: params.bids || []
};
}