]> zoso.dev Git - libnemo.git/commitdiff
Fix typescript errors in test files.
authorChris Duncan <chris@zoso.dev>
Wed, 4 Dec 2024 05:09:17 +0000 (21:09 -0800)
committerChris Duncan <chris@zoso.dev>
Wed, 4 Dec 2024 05:09:17 +0000 (21:09 -0800)
test/GLOBALS.mjs
test/create-wallet.test.mjs
test/lock-unlock-wallet.mjs
test/manage-rolodex.mjs
test/refresh-accounts.test.mjs
test/sign-blocks.test.mjs
test/tools.test.mjs

index 8ae5cae951a9e07ebc5b8f03c2d9ddf4b734c119..1c780fdfec21e588e4bc41ebb46ce3047d3ed9f7 100644 (file)
@@ -5,7 +5,7 @@ import { EventEmitter } from 'node:events'
 EventEmitter.defaultMaxListeners = navigator.hardwareConcurrency * 2
 
 if (globalThis.sessionStorage == null) {
-       const _sessionStorage = {}
+       let _sessionStorage = {}
        Object.defineProperty(globalThis, 'sessionStorage', {
                value: {
                        length: Object.entries(_sessionStorage).length,
index d7511c93bece1df9b2b4dc208b08659c63d80860..83438b3d1ae3284095682aa722eb4c3ed02cfad1 100644 (file)
@@ -37,18 +37,24 @@ describe('creating a new wallet', async () => {
        it('BIP-44 replace invalid salt with empty string', async () => {\r
                const invalidArgs = [null, true, false, 0, 1, 2, { "foo": "bar" }]\r
                for (const arg of invalidArgs) {\r
+                       //@ts-expect-error\r
                        await assert.doesNotReject(Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD, arg), `Rejected ${arg}`)\r
                }\r
        })\r
 \r
        it('fail when using new', async () => {\r
+               //@ts-expect-error\r
                assert.throws(() => new Bip44Wallet())\r
+               //@ts-expect-error\r
                assert.throws(() => new Blake2bWallet())\r
+               //@ts-expect-error\r
                assert.throws(() => new LedgerWallet())\r
        })\r
 \r
        it('fail without a password', async () => {\r
+               //@ts-expect-error\r
                await assert.rejects(Bip44Wallet.create())\r
+               //@ts-expect-error\r
                await assert.rejects(Blake2bWallet.create())\r
        })\r
 \r
index 77a9e6d9f77ca19aa1328b28cce82c65d34aa84b..5fa4fd72e20cdc8aa0bb006e24e6a68fc26c0317 100644 (file)
@@ -91,6 +91,7 @@ describe('locking and unlocking a Bip44Wallet', async () => {
                const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -99,6 +100,7 @@ describe('locking and unlocking a Bip44Wallet', async () => {
 \r
                await wallet.lock('password')\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -110,6 +112,7 @@ describe('locking and unlocking a Bip44Wallet', async () => {
                const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -118,6 +121,7 @@ describe('locking and unlocking a Bip44Wallet', async () => {
 \r
                await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -206,6 +210,7 @@ describe('locking and unlocking a Blake2bWallet', async () => {
                const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -214,6 +219,7 @@ describe('locking and unlocking a Blake2bWallet', async () => {
 \r
                await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -225,6 +231,7 @@ describe('locking and unlocking a Blake2bWallet', async () => {
                const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
@@ -233,6 +240,7 @@ describe('locking and unlocking a Blake2bWallet', async () => {
 \r
                await wallet.lock(NANO_TEST_VECTORS.PASSWORD)\r
 \r
+               //@ts-expect-error\r
                await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' })\r
                assert.ok('mnemonic' in wallet)\r
                assert.ok('seed' in wallet)\r
index 8b6c5735c5f4c4abae0f41d04c218d1bd5fa67e9..983149f88b677f19221df5a38b4affd335758897 100644 (file)
@@ -60,9 +60,13 @@ describe('rolodex valid contact management', async () => {
 
        it('should return empty address array for blank contact names', () => {
                const rolodex = new Rolodex()
+               //@ts-expect-error
                assert.equal(Array.isArray(rolodex.getAddresses(undefined)), true)
+               //@ts-expect-error
                assert.equal(rolodex.getAddresses(undefined).length, 0)
+               //@ts-expect-error
                assert.equal(Array.isArray(rolodex.getAddresses(null)), true)
+               //@ts-expect-error
                assert.equal(rolodex.getAddresses(null).length, 0)
                assert.equal(Array.isArray(rolodex.getAddresses('')), true)
                assert.equal(rolodex.getAddresses('').length, 0)
@@ -78,9 +82,13 @@ describe('rolodex valid contact management', async () => {
        it('should return null for a blank address', async () => {
                const rolodex = new Rolodex()
                await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0)
+               //@ts-expect-error
                assert.equal(rolodex.getName(undefined), null)
+               //@ts-expect-error
                assert.notEqual(rolodex.getName(undefined), undefined)
+               //@ts-expect-error
                assert.equal(rolodex.getName(null), null)
+               //@ts-expect-error
                assert.notEqual(rolodex.getName(null), undefined)
                assert.equal(rolodex.getName(''), null)
                assert.notEqual(rolodex.getName(''), undefined)
@@ -90,20 +98,26 @@ describe('rolodex valid contact management', async () => {
 describe('rolodex exceptions', async () => {
        it('should throw if adding no data', async () => {
                const rolodex = new Rolodex()
+               //@ts-expect-error
                await assert.rejects(rolodex.add())
        })
 
        it('should throw if passed no address', async () => {
                const rolodex = new Rolodex()
+               //@ts-expect-error
                await assert.rejects(rolodex.add('JohnDoe'))
+               //@ts-expect-error
                await assert.rejects(rolodex.add('JohnDoe', undefined))
+               //@ts-expect-error
                await assert.rejects(rolodex.add('JohnDoe', null))
                await assert.rejects(rolodex.add('JohnDoe', ''))
        })
 
        it('should throw if name is blank', async () => {
                const rolodex = new Rolodex()
+               //@ts-expect-error
                await assert.rejects(rolodex.add(undefined, NANO_TEST_VECTORS.ADDRESS_0))
+               //@ts-expect-error
                await assert.rejects(rolodex.add(null, NANO_TEST_VECTORS.ADDRESS_0))
                await assert.rejects(rolodex.add('', NANO_TEST_VECTORS.ADDRESS_0))
        })
index cbb30d753ac250ebe67070d79e3ad2871038a58d..029519bcdda411d2c3001e6765ac4c9a0bffc10d 100644 (file)
@@ -11,7 +11,7 @@ import { Account, Bip44Wallet, Rpc } from '#dist/main.js'
 
 const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)
 await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
-const node = new Rpc(process.env.NODE_URL, process.env.API_KEY_NAME, process.env.API_KEY_VALUE)
+const node = new Rpc(process.env.NODE_URL ?? '', process.env.API_KEY_NAME)
 
 const skip = true
 
@@ -25,21 +25,21 @@ describe('refreshing account info', { skip }, async () => {
                assert.notEqual(account.balance, undefined)
                assert.notEqual(account.balance, null)
                assert.notEqual(account.balance, '')
-               assert.notEqual(account.balance < 0, true)
+               assert.notEqual(account.balance && account.balance < 0, true)
 
                assert.equal(typeof account.frontier, 'string')
                assert.notEqual(account.frontier, undefined)
                assert.notEqual(account.frontier, null)
                assert.notEqual(account.frontier, '')
-               assert.match(account.frontier, /^[0-9A-F]{64}$/i)
+               assert.match(account.frontier ?? '', /^[0-9A-F]{64}$/i)
 
-               assert.equal(account.representative.constructor, Account)
+               assert.equal(account.representative && account.representative.constructor, Account)
                assert.notEqual(account.representative, undefined)
                assert.notEqual(account.representative, null)
                assert.notEqual(account.representative, '')
-               assert.notEqual(account.representative.address, undefined)
-               assert.notEqual(account.representative.address, null)
-               assert.notEqual(account.representative.address, '')
+               assert.notEqual(account.representative?.address, undefined)
+               assert.notEqual(account.representative?.address, null)
+               assert.notEqual(account.representative?.address, '')
        })
 
        it('should throw when refreshing unopened account', async () => {
@@ -86,18 +86,27 @@ describe('finding next unopened account', { skip }, async () => {
        })
 
        it('should throw on invalid node URL', async () => {
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount())
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount(null))
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount(1))
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount(''))
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount('foo'))
        })
 
        it('should throw on invalid batch size', async () => {
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount(node, null))
                await assert.rejects(wallet.getNextNewAccount(node, -1))
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount(node, ''))
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount(node, 'foo'))
+               //@ts-expect-error
                await assert.rejects(wallet.getNextNewAccount(node, { 'foo': 'bar' }))
        })
 })
index 35f7c23bd7dda3baba354452cefbf37643b3ccdf..f8c947dad0229fe43b26fa5f9a479a22649e2ebd 100644 (file)
@@ -147,7 +147,7 @@ describe('block signing tests using official test vectors', async () => {
                        work,\r
                )\r
                await block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address\r
-               assert.equal(block.signature.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D')\r
+               assert.equal(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D')\r
                assert.equal(block.work, work)\r
        })\r
 \r
@@ -159,7 +159,7 @@ describe('block signing tests using official test vectors', async () => {
                        'F3C1D7B6EE97DA09D4C00538CEA93CBA5F74D78FD3FBE71347D2DFE7E53DF327'\r
                )\r
                await block.sign(NANO_TEST_VECTORS.PRIVATE_0)\r
-               assert.equal(block.signature.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305')\r
+               assert.equal(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305')\r
                assert.equal(block.work, '')\r
        })\r
 })\r
index b1b35ed21921b714877c261a9ca3a3d5182eb673..a595d197dc4816d7302df613017f7005b2f076dc 100644 (file)
@@ -13,7 +13,7 @@ const skip = true
 \r
 const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
 await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
-const rpc = new Rpc(process.env.NODE_URL, process.env.API_KEY_NAME, process.env.API_KEY_VALUE)\r
+const rpc = new Rpc(process.env.NODE_URL ?? '', process.env.API_KEY_NAME)\r
 \r
 describe('unit conversion tests', async () => {\r
        it('should convert nano to raw', async () => {\r
@@ -110,7 +110,7 @@ describe('signature tests', async () => {
                        'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou',\r
                        '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D',\r
                )\r
-               await sendBlock.sign(account.privateKey)\r
+               await sendBlock.sign(account.privateKey ?? '')\r
                const valid = await sendBlock.verify(account.publicKey)\r
                assert.equal(valid, true)\r
        })\r
@@ -126,26 +126,17 @@ describe('signature tests', async () => {
                        'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou',\r
                        '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D',\r
                )\r
-               await sendBlock.sign(account.privateKey)\r
+               await sendBlock.sign(account.privateKey ?? '')\r
 \r
-               sendBlock.account = new Account('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p')\r
+               sendBlock.account = Account.fromAddress('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p')\r
                const valid = await sendBlock.verify(account.publicKey)\r
                assert.equal(valid, false)\r
        })\r
-\r
-       it('should create a BLAKE2b hash of a single string', async () => {\r
-               const hash = await Tools.hash('asd')\r
-               assert.equal(hash, 'F787FBCDD2B4C6F6447921D6F163E8FDDFB83D08432430CACAAAB1BBEDD723FE')\r
-       })\r
-\r
-       it('should create a BLAKE2b hash of a string array', async () => {\r
-               const hash = await Tools.hash(['asd'])\r
-               assert.equal(hash, 'F787FBCDD2B4C6F6447921D6F163E8FDDFB83D08432430CACAAAB1BBEDD723FE')\r
-       })\r
 })\r
 \r
 describe('sweeper', async () => {\r
        it('throws without required parameters', async () => {\r
+               //@ts-expect-error\r
                await assert.rejects(Tools.sweep(),\r
                        { message: 'Missing required sweep arguments' })\r
        })\r