From 51a2f875d263df2c6cb956e26e96131f19f2520d Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 6 Dec 2024 13:10:56 -0800 Subject: [PATCH] Isolate test data to avoid contaminating other tests. --- test/manage-rolodex.mjs | 9 ++++++--- test/refresh-accounts.test.mjs | 26 ++++++++++++++++++++++++-- test/tools.test.mjs | 8 ++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/test/manage-rolodex.mjs b/test/manage-rolodex.mjs index 2ce9c10..bef4635 100644 --- a/test/manage-rolodex.mjs +++ b/test/manage-rolodex.mjs @@ -122,17 +122,20 @@ test('should throw if name is blank', async () => { }) console.log('> rolodex data signature verification <') -const data = 'Test data' -const signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, data) -const rolodex = new Rolodex() test('should verify valid data and signature', async () => { + const data = 'Test data' + const signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, data) + const rolodex = new Rolodex() await rolodex.add('JohnDoe', NANO_TEST_VECTORS.ADDRESS_0) const result = await rolodex.verify('JohnDoe', signature, data) assert.equals(result, true) }) test('should reject incorrect contact for signature', async () => { + const data = 'Test data' + const signature = await Tools.sign(NANO_TEST_VECTORS.PRIVATE_0, data) + const rolodex = new Rolodex() await rolodex.add('JaneSmith', NANO_TEST_VECTORS.ADDRESS_1) const result = await rolodex.verify('JaneSmith', signature, data) assert.equals(result, false) diff --git a/test/refresh-accounts.test.mjs b/test/refresh-accounts.test.mjs index 75d53c4..8fcbac5 100644 --- a/test/refresh-accounts.test.mjs +++ b/test/refresh-accounts.test.mjs @@ -7,8 +7,6 @@ import { assert, skip, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' 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) let rpc //@ts-ignore var process = process || null @@ -20,6 +18,8 @@ if (process) { console.log('refreshing account info') test('fetch balance, frontier, and representative', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts() const account = accounts[0] await account.refresh(rpc) @@ -46,6 +46,8 @@ test('fetch balance, frontier, and representative', async () => { }) test('throw when refreshing unopened account', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0x7fffffff) const account = accounts[0] await assert.rejects(account.refresh(rpc), @@ -53,11 +55,15 @@ test('throw when refreshing unopened account', async () => { }) test('throw when referencing invalid account index', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) await assert.rejects(wallet.accounts(0x80000000), { message: 'Invalid child key index 0x80000000' }) }) test('throw with invalid node', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const invalidNode = new Rpc('http://invalid.com') const accounts = await wallet.accounts() const account = accounts[0] @@ -68,6 +74,8 @@ test('throw with invalid node', async () => { console.log('finding next unopened account') test('return correct account from test vector', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.getNextNewAccount(rpc) assert.ok(account) assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) @@ -75,6 +83,8 @@ test('return correct account from test vector', async () => { }) test('return successfully for small batch size', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.getNextNewAccount(rpc, 1) assert.ok(account) assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) @@ -82,6 +92,8 @@ test('return successfully for small batch size', async () => { }) test('return successfully for large batch size', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const account = await wallet.getNextNewAccount(rpc, 100) assert.ok(account) assert.equals(account.address, NANO_TEST_VECTORS.ADDRESS_1) @@ -89,6 +101,8 @@ test('return successfully for large batch size', async () => { }) test('should throw on invalid node URL', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) //@ts-expect-error await assert.rejects(wallet.getNextNewAccount()) //@ts-expect-error @@ -102,6 +116,8 @@ test('should throw on invalid node URL', async () => { }) test('should throw on invalid batch size', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) //@ts-expect-error await assert.rejects(wallet.getNextNewAccount(rpc, null)) await assert.rejects(wallet.getNextNewAccount(rpc, -1)) @@ -116,6 +132,8 @@ test('should throw on invalid batch size', async () => { console.log('> refreshing wallet accounts <') skip('should get balance, frontier, and representative for one account', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.refresh(rpc) const account = accounts[0] assert.ok(account instanceof Account) @@ -126,11 +144,15 @@ skip('should get balance, frontier, and representative for one account', async ( }) skip('should get balance, frontier, and representative for multiple accounts', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.refresh(rpc, 0, 2) assert.equals(accounts.length, 1) assert.ok(accounts[0] instanceof Account) }) skip('should handle failure gracefully', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) await assert.doesNotReject(wallet.refresh(rpc, 0, 20)) }) diff --git a/test/tools.test.mjs b/test/tools.test.mjs index e28d7bd..1d713a9 100644 --- a/test/tools.test.mjs +++ b/test/tools.test.mjs @@ -7,8 +7,6 @@ import { assert, skip, test } from '#GLOBALS.mjs' import { RAW_MAX, NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Account, SendBlock, Rpc, Tools } 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) let rpc //@ts-ignore var process = process || null @@ -103,6 +101,8 @@ test('should verify a signature using the public key', async () => { }) test('should verify a block using the public key', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts() const account = accounts[0] const sendBlock = new SendBlock( @@ -119,6 +119,8 @@ test('should verify a block using the public key', async () => { }) test('should reject a block using the wrong public key', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts() const account = accounts[0] const sendBlock = new SendBlock( @@ -143,6 +145,8 @@ test('sweeper throws without required parameters', async () => { }) skip('sweeper fails gracefully for ineligible accounts', async () => { + const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const results = await Tools.sweep(rpc, wallet, NANO_TEST_VECTORS.ADDRESS_1) assert.ok(results) assert.equals(results.length, 1) -- 2.34.1