From 82cd0ea7a6c78ad022d8f9246853ae891cadeb77 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 3 Dec 2024 21:09:17 -0800 Subject: [PATCH] Fix typescript errors in test files. --- test/GLOBALS.mjs | 2 +- test/create-wallet.test.mjs | 6 ++++++ test/lock-unlock-wallet.mjs | 8 ++++++++ test/manage-rolodex.mjs | 14 ++++++++++++++ test/refresh-accounts.test.mjs | 23 ++++++++++++++++------- test/sign-blocks.test.mjs | 4 ++-- test/tools.test.mjs | 19 +++++-------------- 7 files changed, 52 insertions(+), 24 deletions(-) diff --git a/test/GLOBALS.mjs b/test/GLOBALS.mjs index 8ae5cae..1c780fd 100644 --- a/test/GLOBALS.mjs +++ b/test/GLOBALS.mjs @@ -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, diff --git a/test/create-wallet.test.mjs b/test/create-wallet.test.mjs index d7511c9..83438b3 100644 --- a/test/create-wallet.test.mjs +++ b/test/create-wallet.test.mjs @@ -37,18 +37,24 @@ describe('creating a new wallet', async () => { it('BIP-44 replace invalid salt with empty string', async () => { const invalidArgs = [null, true, false, 0, 1, 2, { "foo": "bar" }] for (const arg of invalidArgs) { + //@ts-expect-error await assert.doesNotReject(Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD, arg), `Rejected ${arg}`) } }) it('fail when using new', async () => { + //@ts-expect-error assert.throws(() => new Bip44Wallet()) + //@ts-expect-error assert.throws(() => new Blake2bWallet()) + //@ts-expect-error assert.throws(() => new LedgerWallet()) }) it('fail without a password', async () => { + //@ts-expect-error await assert.rejects(Bip44Wallet.create()) + //@ts-expect-error await assert.rejects(Blake2bWallet.create()) }) diff --git a/test/lock-unlock-wallet.mjs b/test/lock-unlock-wallet.mjs index 77a9e6d..5fa4fd7 100644 --- a/test/lock-unlock-wallet.mjs +++ b/test/lock-unlock-wallet.mjs @@ -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) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -99,6 +100,7 @@ describe('locking and unlocking a Bip44Wallet', async () => { await wallet.lock('password') + //@ts-expect-error await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -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) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -118,6 +121,7 @@ describe('locking and unlocking a Bip44Wallet', async () => { await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -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) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error await assert.rejects(wallet.lock(), { message: 'Failed to lock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -214,6 +219,7 @@ describe('locking and unlocking a Blake2bWallet', async () => { await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error await assert.rejects(wallet.unlock(), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -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) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error await assert.rejects(wallet.lock(1), { message: 'Failed to lock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) @@ -233,6 +240,7 @@ describe('locking and unlocking a Blake2bWallet', async () => { await wallet.lock(NANO_TEST_VECTORS.PASSWORD) + //@ts-expect-error await assert.rejects(wallet.unlock(1), { message: 'Failed to unlock wallet' }) assert.ok('mnemonic' in wallet) assert.ok('seed' in wallet) diff --git a/test/manage-rolodex.mjs b/test/manage-rolodex.mjs index 8b6c573..983149f 100644 --- a/test/manage-rolodex.mjs +++ b/test/manage-rolodex.mjs @@ -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)) }) diff --git a/test/refresh-accounts.test.mjs b/test/refresh-accounts.test.mjs index cbb30d7..029519b 100644 --- a/test/refresh-accounts.test.mjs +++ b/test/refresh-accounts.test.mjs @@ -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' })) }) }) diff --git a/test/sign-blocks.test.mjs b/test/sign-blocks.test.mjs index 35f7c23..f8c947d 100644 --- a/test/sign-blocks.test.mjs +++ b/test/sign-blocks.test.mjs @@ -147,7 +147,7 @@ describe('block signing tests using official test vectors', async () => { work, ) await block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address - assert.equal(block.signature.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D') + assert.equal(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D') assert.equal(block.work, work) }) @@ -159,7 +159,7 @@ describe('block signing tests using official test vectors', async () => { 'F3C1D7B6EE97DA09D4C00538CEA93CBA5F74D78FD3FBE71347D2DFE7E53DF327' ) await block.sign(NANO_TEST_VECTORS.PRIVATE_0) - assert.equal(block.signature.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305') + assert.equal(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305') assert.equal(block.work, '') }) }) diff --git a/test/tools.test.mjs b/test/tools.test.mjs index b1b35ed..a595d19 100644 --- a/test/tools.test.mjs +++ b/test/tools.test.mjs @@ -13,7 +13,7 @@ const skip = true const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) -const rpc = new Rpc(process.env.NODE_URL, process.env.API_KEY_NAME, process.env.API_KEY_VALUE) +const rpc = new Rpc(process.env.NODE_URL ?? '', process.env.API_KEY_NAME) describe('unit conversion tests', async () => { it('should convert nano to raw', async () => { @@ -110,7 +110,7 @@ describe('signature tests', async () => { 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ) - await sendBlock.sign(account.privateKey) + await sendBlock.sign(account.privateKey ?? '') const valid = await sendBlock.verify(account.publicKey) assert.equal(valid, true) }) @@ -126,26 +126,17 @@ describe('signature tests', async () => { 'nano_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ) - await sendBlock.sign(account.privateKey) + await sendBlock.sign(account.privateKey ?? '') - sendBlock.account = new Account('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p') + sendBlock.account = Account.fromAddress('nano_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p') const valid = await sendBlock.verify(account.publicKey) assert.equal(valid, false) }) - - it('should create a BLAKE2b hash of a single string', async () => { - const hash = await Tools.hash('asd') - assert.equal(hash, 'F787FBCDD2B4C6F6447921D6F163E8FDDFB83D08432430CACAAAB1BBEDD723FE') - }) - - it('should create a BLAKE2b hash of a string array', async () => { - const hash = await Tools.hash(['asd']) - assert.equal(hash, 'F787FBCDD2B4C6F6447921D6F163E8FDDFB83D08432430CACAAAB1BBEDD723FE') - }) }) describe('sweeper', async () => { it('throws without required parameters', async () => { + //@ts-expect-error await assert.rejects(Tools.sweep(), { message: 'Missing required sweep arguments' }) }) -- 2.34.1