From ad91dfa4bffef607a7611a52d09649d0fcbe49c2 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 25 Jan 2024 16:18:19 +0530 Subject: [PATCH] Add tests for name operations --- test/cli.test.ts | 52 ++++++++++++++++++++++++++++++++++++------------ test/helpers.ts | 4 ++-- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/test/cli.test.ts b/test/cli.test.ts index c3726d6..c04dba8 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -55,6 +55,8 @@ describe('Test laconic CLI commands', () => { const testAuthorityName = 'laconic'; let testAuctionId: string; + const testRecordFilePath = 'test/data/watcher-record.yml'; + let testRecordId: string, testRecordBondId: string; test('laconic cns status', async () => { const result = spawnSync('laconic', ['cns', 'status']); @@ -173,23 +175,20 @@ describe('Test laconic CLI commands', () => { }); describe('Record operations', () => { - const recordFilePath = 'test/data/watcher-record.yml'; const gas = 250000; const bondBalance = 1000000000; - 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)); + ({ bondId: testRecordBondId } = 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', testRecordFilePath, '--bond-id', testRecordBondId, '--gas', gas.toString()]); const outputObj = checkResultAndRetrieveOutput(result); // Expect output object to resultant bond id expect(outputObj.id).toBeDefined(); - recordId = outputObj.id; + testRecordId = outputObj.id; }); test('laconic cns record list', async () => { @@ -197,7 +196,7 @@ describe('Test laconic CLI commands', () => { const outputObj = checkResultAndRetrieveOutput(result); // Expected record - const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); + const expectedRecord = getRecordObj(testRecordFilePath, { bondId: testRecordBondId, recordId: testRecordId, names: null }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toMatchObject(expectedRecord); @@ -208,11 +207,11 @@ describe('Test laconic CLI commands', () => { }); test('laconic cns record get --id ', async () => { - const result = spawnSync('laconic', ['cns', 'record', 'get', '--id', recordId]); + const result = spawnSync('laconic', ['cns', 'record', 'get', '--id', testRecordId]); const outputObj = checkResultAndRetrieveOutput(result); // Expected record - const expectedRecord = getRecordObj(recordFilePath, { bondId, recordId }); + const expectedRecord = getRecordObj(testRecordFilePath, { bondId: testRecordBondId, recordId: testRecordId, names: null }); expect(outputObj.length).toEqual(1); expect(outputObj[0]).toMatchObject(expectedRecord); @@ -391,20 +390,47 @@ describe('Test laconic CLI commands', () => { }); describe('Name operations', () => { + const testName = 'crn://laconic/watcher/erc20'; + test('laconic cns name set ', async () => { - // TODO + const result = spawnSync('laconic', ['cns', 'name', 'set', testName, testRecordId]); + const outputObj = checkResultAndRetrieveOutput(result); + + // Expected output + expect(outputObj).toEqual({ success: true }); }); test('laconic cns name lookup ', async () => { - // TODO + const result = spawnSync('laconic', ['cns', 'name', 'lookup', testName]); + const outputObj = checkResultAndRetrieveOutput(result); + + // Expected output + expect(outputObj.length).toEqual(1); + expect(outputObj[0]).toMatchObject({ latest: { id: testRecordId } }); }); test('laconic cns name resolve ', async () => { - // TODO + const result = spawnSync('laconic', ['cns', 'name', 'resolve', testName]); + const outputObj = checkResultAndRetrieveOutput(result); + + // Expected resolved record + const expectedRecord = getRecordObj(testRecordFilePath, { bondId: testRecordBondId, recordId: testRecordId, names: [testName] }); + + expect(outputObj.length).toEqual(1); + expect(outputObj[0]).toMatchObject(expectedRecord); }); test('laconic cns name delete ', async () => { - // TODO + const result = spawnSync('laconic', ['cns', 'name', 'delete', testName]); + const outputObj = checkResultAndRetrieveOutput(result); + + // Expected output + expect(outputObj).toEqual({ success: true }); + + // Check that name doesn't resolve + const resolveResult = spawnSync('laconic', ['cns', 'name', 'resolve', testName]); + const resolveOutputObj = checkResultAndRetrieveOutput(resolveResult); + expect(resolveOutputObj.length).toEqual(0); }); }); }); diff --git a/test/helpers.ts b/test/helpers.ts index 8874f5b..66e0938 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -56,12 +56,12 @@ export function getAccountObj(params: { address: string, balance?: number }): an }; } -export function getRecordObj(recordFilePath: string, params: { bondId: string, recordId: string }): any { +export function getRecordObj(recordFilePath: string, params: { bondId: string, recordId: string, names: any }): any { const recordContent = yaml.load(fs.readFileSync(recordFilePath, 'utf8')) as any; return { id: params.recordId, - names: null, + names: params.names, bondId: params.bondId, attributes: recordContent.record };