})
}
-const errors = []
-const logs = []
-function error (...args) {
- errors.push(args)
- console.error(...args)
+const failures = []
+const passes = []
+function fail (...args) {
+ failures.push(args)
+ console.error(`%cFAIL `, 'color:red', ...args)
}
-function log (...args) {
- logs.push(args)
- console.log(...args)
+function pass (...args) {
+ passes.push(args)
+ console.log(`%cPASS `, 'color:green', ...args)
}
+
/**
* Who watches the watchers?
*/
-console.assert(errors.length === 0)
-console.assert(logs.length === 0)
+console.assert(failures.length === 0)
+console.assert(passes.length === 0)
await test('promise should pass', new Promise(resolve => { resolve('') }))
-console.assert(errors.some(call => /.*promise should pass.*/.test(call[0])) === false, `good promise errored`)
-console.assert(logs.some(call => /.*promise should pass.*/.test(call[0])) === true, `good promise not logged`)
+console.assert(failures.some(call => /.*promise should pass.*/.test(call[0])) === false, `good promise errored`)
+console.assert(passes.some(call => /.*promise should pass.*/.test(call[0])) === true, `good promise not logged`)
await test('promise should fail', new Promise((resolve, reject) => { reject('FAILURE EXPECTED HERE') }))
-console.assert(errors.some(call => /.*promise should fail.*/.test(call[0])) === true, `bad promise not errored`)
-console.assert(logs.some(call => /.*promise should fail.*/.test(call[0])) === false, 'bad promise logged')
+console.assert(failures.some(call => /.*promise should fail.*/.test(call[0])) === true, `bad promise not errored`)
+console.assert(passes.some(call => /.*promise should fail.*/.test(call[0])) === false, 'bad promise logged')
-await test('async should pass', async () => {})
-console.assert(errors.some(call => /.*async should pass.*/.test(call[0])) === false, 'good async errored')
-console.assert(logs.some(call => /.*async should pass.*/.test(call[0])) === true, 'good async not logged')
+await test('async should pass', async () => { })
+console.assert(failures.some(call => /.*async should pass.*/.test(call[0])) === false, 'good async errored')
+console.assert(passes.some(call => /.*async should pass.*/.test(call[0])) === true, 'good async not logged')
await test('async should fail', async () => { throw new Error('FAILURE EXPECTED HERE') })
-console.assert(errors.some(call => /.*async should fail.*/.test(call[0])) === true, 'bad async not errored')
-console.assert(logs.some(call => /.*async should fail.*/.test(call[0])) === false, 'bad async logged')
+console.assert(failures.some(call => /.*async should fail.*/.test(call[0])) === true, 'bad async not errored')
+console.assert(passes.some(call => /.*async should fail.*/.test(call[0])) === false, 'bad async logged')
-await test('function should pass', () => {})
-console.assert(errors.some(call => /.*function should pass.*/.test(call[0])) === false, 'good function errored')
-console.assert(logs.some(call => /.*function should pass.*/.test(call[0])) === true, 'good function not logged')
+await test('function should pass', () => { })
+console.assert(failures.some(call => /.*function should pass.*/.test(call[0])) === false, 'good function errored')
+console.assert(passes.some(call => /.*function should pass.*/.test(call[0])) === true, 'good function not logged')
await test('function should fail', 'FAILURE EXPECTED HERE')
-console.assert(errors.some(call => /.*function should fail.*/.test(call[0])) === true, 'bad function not errored')
-console.assert(logs.some(call => /.*function should fail.*/.test(call[0])) === false, 'bad function logged')
+console.assert(failures.some(call => /.*function should fail.*/.test(call[0])) === true, 'bad function not errored')
+console.assert(passes.some(call => /.*function should fail.*/.test(call[0])) === false, 'bad function logged')
console.log(`> TEST RUNNER CHECK DONE <`)
console.log(` `)
-export function skip (name, ...args) {
- log(`SKIP: ${name}`)
+export function skip (name) {
+ console.log(`%cSKIP `, 'color:blue', name)
+}
+
+export function suite (name, fn) {
+ console.group(`%c=>%c ${name}`, 'font-weight:bold;color:green', 'font-weight:bold;color:unset')
+ if (fn.constructor.name === 'AsyncFunction') fn = fn()
+ if (typeof fn === 'function') fn = new Promise(resolve => resolve(fn()))
+ fn.then(console.groupEnd)
}
export function test (name, fn) {
- console.log(`=> Test: ${name}`)
if (fn instanceof Promise) {
try {
return fn
- .then(() => log(` -> PASS: ${name}`))
- .catch((err) => { error(` -> FAIL: ${name}: ${err}`) })
+ .then(() => pass(name))
+ .catch((err) => { fail(`${name}: ${err}`) })
} catch (err) {
- error(` -> FAIL: ${name}: ${err.message}`)
- error(err)
+ fail(`${name}: ${err.message}`)
+ fail(err)
}
} else if (fn?.constructor?.name === 'AsyncFunction') {
try {
return fn()
- .then(() => log(` -> PASS: ${name}`))
- .catch((err) => error(` -> FAIL: ${name}: ${err.message}`))
+ .then(() => pass(name))
+ .catch((err) => fail(`${name}: ${err.message}`))
} catch (err) {
- error(` -> FAIL: ${name}: ${err.message}`)
- error(err)
+ fail(`${name}: ${err.message}`)
+ fail(err)
}
} else if (typeof fn === 'function') {
try {
fn()
- log(` -> PASS: ${name}`)
+ pass(name)
} catch (err) {
- error(` -> FAIL: ${name}: ${err.message}`)
- error(err)
+ fail(`${name}: ${err.message}`)
+ fail(err)
}
} else {
- error(` -> FAIL: ${name}: test cannot execute on ${typeof fn} ${fn}`)
+ fail(`${name}: test cannot execute on ${typeof fn} ${fn}`)
}
}
\r
'use strict'\r
\r
-import { assert, skip, test } from '#GLOBALS.mjs'\r
+import { assert, skip, suite, test } from '#GLOBALS.mjs'\r
import { RAW_MAX, NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js'\r
import { Bip44Wallet, Account, SendBlock, Rpc, Tools } from '#dist/main.js'\r
\r
rpc = new Rpc(process?.env?.NODE_URL ?? '', process?.env?.API_KEY_NAME)\r
}\r
\r
-console.log('> unit conversion tests <')\r
-\r
-await test('should convert nano to raw', async () => {\r
- const result = await Tools.convert('1', 'NANO', 'RAW')\r
- assert.equals(result, '1000000000000000000000000000000')\r
-})\r
-\r
-await test('should convert raw to nano', async () => {\r
- const result = await Tools.convert('1000000000000000000000000000000', 'RAW', 'NANO')\r
- assert.equals(result, '1')\r
-})\r
-\r
-await test('should convert 1 raw to 10^-29 nano', async () => {\r
- const result = await Tools.convert('1', 'RAW', 'NANO')\r
- assert.equals(result, '.000000000000000000000000000001')\r
-})\r
-\r
-await test('should ignore leading and trailing zeros', async () => {\r
- const result = await Tools.convert('0011002200.0033004400', 'nano', 'nano')\r
- assert.equals(result, '11002200.00330044')\r
-})\r
-\r
-await test('should convert raw to nyano', async () => {\r
- const result = await Tools.convert(RAW_MAX, 'RAW', 'NYANO')\r
- assert.equals(result, '340282366920938.463463374607431768211455')\r
-})\r
-\r
-await test('should convert case-insensitive nyano to raw', async () => {\r
- const result = await Tools.convert('0.000000000000000123456789', 'nYaNo', 'rAw')\r
- assert.equals(result, '123456789')\r
-})\r
-\r
-await test('should convert nano to pico', async () => {\r
- const result = await Tools.convert('123.456', 'nano', 'pico')\r
- assert.equals(result, '123456')\r
-})\r
-\r
-await test('should convert knano to pico', async () => {\r
- const result = await Tools.convert('123.456', 'nano', 'pico')\r
- assert.equals(result, '123456')\r
-})\r
-\r
-await test('should throw if amount exceeds raw max', async () => {\r
- await assert.rejects(Tools.convert(RAW_MAX, 'NANO', 'RAW'),\r
- { message: 'Amount exceeds Nano limits' })\r
-})\r
-\r
-await test('should throw if amount exceeds raw min', async () => {\r
- await assert.rejects(Tools.convert('0.1', 'RAW', 'NANO'),\r
- { message: 'Amount must be at least 1 raw' })\r
-})\r
-\r
-await test('should throw if amount is blank', async () => {\r
- await assert.rejects(Tools.convert('', 'RAW', 'NANO'),\r
- { message: 'Invalid amount' })\r
-})\r
-\r
-await test('should throw if amount has non-digit characters', async () => {\r
- await assert.rejects(Tools.convert('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 'RAW', 'NANO'),\r
- { message: 'Invalid amount' })\r
+suite('unit conversion tests', async () => {\r
+\r
+ await test('should convert nano to raw', async () => {\r
+ const result = await Tools.convert('1', 'NANO', 'RAW')\r
+ assert.equals(result, '100000000000000000000000000000')\r
+ })\r
+\r
+ await test('should convert raw to nano', async () => {\r
+ const result = await Tools.convert('1000000000000000000000000000000', 'RAW', 'NANO')\r
+ assert.equals(result, '1')\r
+ })\r
+\r
+ await test('should convert 1 raw to 10^-29 nano', async () => {\r
+ const result = await Tools.convert('1', 'RAW', 'NANO')\r
+ assert.equals(result, '.000000000000000000000000000001')\r
+ })\r
+\r
+ await test('should ignore leading and trailing zeros', async () => {\r
+ const result = await Tools.convert('0011002200.0033004400', 'nano', 'nano')\r
+ assert.equals(result, '11002200.00330044')\r
+ })\r
+\r
+ await test('should convert raw to nyano', async () => {\r
+ const result = await Tools.convert(RAW_MAX, 'RAW', 'NYANO')\r
+ assert.equals(result, '340282366920938.463463374607431768211455')\r
+ })\r
+\r
+ await test('should convert case-insensitive nyano to raw', async () => {\r
+ const result = await Tools.convert('0.000000000000000123456789', 'nYaNo', 'rAw')\r
+ assert.equals(result, '123456789')\r
+ })\r
+\r
+ await test('should convert nano to pico', async () => {\r
+ const result = await Tools.convert('123.456', 'nano', 'pico')\r
+ assert.equals(result, '123456')\r
+ })\r
+\r
+ await test('should convert knano to pico', async () => {\r
+ const result = await Tools.convert('123.456', 'nano', 'pico')\r
+ assert.equals(result, '123456')\r
+ })\r
+\r
+ await test('should throw if amount exceeds raw max', async () => {\r
+ await assert.rejects(Tools.convert(RAW_MAX, 'NANO', 'RAW'),\r
+ { message: 'Amount exceeds Nano limits' })\r
+ })\r
+\r
+ await test('should throw if amount exceeds raw min', async () => {\r
+ await assert.rejects(Tools.convert('0.1', 'RAW', 'NANO'),\r
+ { message: 'Amount must be at least 1 raw' })\r
+ })\r
+\r
+ await test('should throw if amount is blank', async () => {\r
+ await assert.rejects(Tools.convert('', 'RAW', 'NANO'),\r
+ { message: 'Invalid amount' })\r
+ })\r
+\r
+ await test('should throw if amount has non-digit characters', async () => {\r
+ await assert.rejects(Tools.convert('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 'RAW', 'NANO'),\r
+ { message: 'Invalid amount' })\r
+ })\r
})\r
\r
console.log('> signature tests <')\r