From 3072d995bc9a219858bc1dad73f9b9292adbfe42 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 8 Aug 2025 11:11:46 -0700 Subject: [PATCH] Fix convert tool tests to be synchronous. Add convert tests for additional scenarios. --- test/test.tools.mjs | 64 ++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/test/test.tools.mjs b/test/test.tools.mjs index 1df3913..21c413d 100644 --- a/test/test.tools.mjs +++ b/test/test.tools.mjs @@ -37,63 +37,79 @@ const rpc = new Rpc(env?.NODE_URL ?? '', env?.API_KEY_NAME) await Promise.all([ suite('unit conversion tests', async () => { - await test('should convert nano to raw', async () => { - const result = await Tools.convert('1', 'NANO', 'RAW') + await test('convert nano to raw', async () => { + const result = Tools.convert('1', 'NANO', 'RAW') assert.equal(result, '1000000000000000000000000000000') }) - await test('should convert raw to nano', async () => { - const result = await Tools.convert('1000000000000000000000000000000', 'RAW', 'NANO') + await test('convert raw to nano', async () => { + const result = Tools.convert('1000000000000000000000000000000', 'RAW', 'NANO') assert.equal(result, '1') }) - await test('should convert 1 raw to 10^-29 nano', async () => { - const result = await Tools.convert('1', 'RAW', 'NANO') + await test('convert 1 raw to 10^-29 nano', async () => { + const result = Tools.convert('1', 'RAW', 'NANO') assert.equal(result, '0.000000000000000000000000000001') }) - await test('should ignore leading and trailing zeros', async () => { - const result = await Tools.convert('0011002200.0033004400', 'nano', 'nano') + await test('ignore leading and trailing zeros', async () => { + const result = Tools.convert('0011002200.0033004400', 'nano', 'nano') assert.equal(result, '11002200.00330044') }) - await test('should convert raw to nyano', async () => { - const result = await Tools.convert(MAX_SUPPLY, 'RAW', 'NYANO') + await test('convert raw to nyano', async () => { + const result = Tools.convert(MAX_SUPPLY, 'RAW', 'NYANO') assert.equal(result, '133248297920938.463463374607431768211455') }) - await test('should convert case-insensitive nyano to raw', async () => { - const result = await Tools.convert('0.000000000000000123456789', 'nYaNo', 'rAw') + await test('convert case-insensitive nyano to raw', async () => { + const result = Tools.convert('0.000000000000000123456789', 'nYaNo', 'rAw') assert.equal(result, '123456789') }) - await test('should convert nano to pico', async () => { - const result = await Tools.convert('123.456', 'nano', 'pico') + await test('convert nano to pico', async () => { + const result = Tools.convert('123.456', 'nano', 'pico') assert.equal(result, '123456') }) - await test('should convert knano to pico', async () => { - const result = await Tools.convert('123.456', 'knano', 'pico') + await test('convert knano to pico', async () => { + const result = Tools.convert('123.456', 'knano', 'pico') assert.equal(result, '123456000') }) - await test('should throw if amount exceeds raw max', async () => { - await assert.rejects(Tools.convert(MAX_RAW, 'NANO', 'RAW'), + await test('allow zero amount', async () => { + const result = Tools.convert('0', 'NANO', 'RAW') + assert.equal(result, '0') + }) + + await test('throw if amount exceeds raw max', async () => { + assert.throws(() => Tools.convert(MAX_RAW, 'NANO', 'RAW'), { message: 'Amount exceeds Nano limits' }) }) - await test('should throw if amount exceeds raw min', async () => { - await assert.rejects(Tools.convert('0.1', 'RAW', 'NANO'), + await test('throw if raw amount has a fraction', async () => { + assert.throws(() => Tools.convert('0.1', 'RAW', 'RAW'), + { message: 'Amount has fractional raw' }) + }) + + await test('throw if nano amount less than zero raw', async () => { + debugger + assert.throws(() => Tools.convert('0.0000000000000000000000000000001', 'NANO', 'RAW'), { message: 'Amount must be at least 1 raw' }) }) - await test('should throw if amount is blank', async () => { - await assert.rejects(Tools.convert('', 'RAW', 'NANO'), + await test('throw if amount is blank', async () => { + assert.throws(() => Tools.convert('', 'RAW', 'NANO'), + { message: 'Invalid amount' }) + }) + + await test('throw if amount is missing leading zero', async () => { + assert.throws(() => Tools.convert('.1', 'NANO', 'RAW'), { message: 'Invalid amount' }) }) - await test('should throw if amount has non-digit characters', async () => { - await assert.rejects(Tools.convert('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 'RAW', 'NANO'), + await test('throw if amount has non-digit characters', async () => { + assert.throws(() => Tools.convert('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 'RAW', 'NANO'), { message: 'Invalid amount' }) }) }), -- 2.47.3