From 9b8feb5cd7ef841699887077f21b8025ebf53035 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 5 Dec 2024 21:33:09 -0800 Subject: [PATCH] Move test files around. Fix test runners for async functions. --- GLOBALS.mjs | 128 ++++++++ package.json | 2 +- {test/perf => perf}/account.perf.js | 5 +- perf/main.mjs | 7 + {test/perf => perf}/wallet.perf.js | 2 +- test.html | 1 + test/GLOBALS.mjs | 88 ----- test/create-wallet.test.mjs | 2 +- test/derive-accounts.test.mjs | 2 +- test/import-wallet.test.mjs | 492 ++++++++++++++-------------- test/lock-unlock-wallet.mjs | 2 +- test/main.mjs | 7 +- test/manage-rolodex.mjs | 2 +- test/refresh-accounts.test.mjs | 2 +- test/sign-blocks.test.mjs | 2 +- test/tools.test.mjs | 2 +- 16 files changed, 397 insertions(+), 349 deletions(-) create mode 100644 GLOBALS.mjs rename {test/perf => perf}/account.perf.js (91%) create mode 100644 perf/main.mjs rename {test/perf => perf}/wallet.perf.js (92%) delete mode 100644 test/GLOBALS.mjs diff --git a/GLOBALS.mjs b/GLOBALS.mjs new file mode 100644 index 0000000..f5649ee --- /dev/null +++ b/GLOBALS.mjs @@ -0,0 +1,128 @@ +// SPDX-FileCopyrightText: 2024 Chris Duncan +// SPDX-License-Identifier: GPL-3.0-or-later + +if (globalThis.sessionStorage == null) { + let _sessionStorage = {} + Object.defineProperty(globalThis, 'sessionStorage', { + value: { + length: Object.entries(_sessionStorage).length, + setItem: (key, value) => _sessionStorage[key] = value, + getItem: (key) => _sessionStorage[key], + removeItem: (key) => delete _sessionStorage[key], + clear: () => _sessionStorage = {} + }, + configurable: true, + enumerable: true + }) +} + +export function skip (name, ...args) { + console.log(`SKIP: ${name}`) +} + +export function test (name, fn) { + if (fn instanceof Promise) { + try { + fn.then(() => console.log(`PASS: ${name}`)) + .catch((err) => console.error(`FAIL: ${name}`)) + } catch (err) { + console.error(`FAIL: ${name}`) + console.error(err) + } + } else if (fn.constructor.name === 'AsyncFunction') { + try { + fn().then(() => console.log(`PASS: ${name}`)) + .catch((err) => console.error(`FAIL: ${name}`)) + } catch (err) { + console.error(`FAIL: ${name}`) + console.error(err) + } + } else { + try { + fn() + console.log(`PASS: ${name}`) + } catch (err) { + console.error(`FAIL: ${name}`) + console.error(err) + } + } +} + +export const assert = { + ok: (bool) => { + if (typeof bool !== 'boolean') { + throw new Error('Invalid assertion') + } + if (!bool) { + throw new Error(`test result falsy`) + } + return true + }, + exists: (a) => { + let b = a || null + if (b == null) { + throw new Error(`argument is ${typeof a}`) + } + return b != null + }, + equals: (a, b) => { + return a === b + }, + notEqual: (a, b) => { + return a !== b + }, + rejects: async (fn, msg) => { + if (fn instanceof Promise) { + try { + fn.then(() => { throw new Error(msg ?? 'expected async function to reject') }) + .catch((err) => { return true }) + } catch (err) { + return true + } + } else if (fn.constructor.name === 'AsyncFunction') { + try { + fn.then(() => { throw new Error(msg ?? 'expected async function to reject') }) + .catch((err) => { return true }) + } catch (err) { + return true + } + } else { + throw new Error(msg ?? 'expected async function') + } + }, + resolves: async (fn, msg) => { + if (fn instanceof Promise) { + try { + fn.then(() => { return true }) + .catch((err) => { throw new Error(msg ?? 'expected async function to resolve') }) + return true + } catch (err) { + throw new Error(msg ?? 'expected async function to resolve') + } + } else if (fn.constructor.name === 'AsyncFunction') { + try { + fn().then(() => { return true }) + .catch((err) => { throw new Error(msg ?? 'expected async function to resolve') }) + return true + } catch (err) { + throw new Error(msg ?? 'expected async function to resolve') + } + } else { + throw new Error('expected async function') + } + }, + throws: (fn, msg) => { + if (typeof fn !== 'function') { + throw new Error('expected function') + } + if (fn instanceof Promise || fn.constructor.name === 'AsyncFunction') { + throw new Error('expected synchronous function') + } + try { + fn() + throw new Error(msg ?? `expected function to throw an exception`) + } catch (err) { + return true + } + } +} diff --git a/package.json b/package.json index feee1b0..2edcd37 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "test:node": "npm run build -- --platform=node && node --test --test-force-exit --env-file .env", "test:coverage": "npm run test:node -- --experimental-test-coverage", "test:coverage:report": "npm run test:coverage -- --test-reporter=lcov --test-reporter-destination=coverage.info && genhtml coverage.info --output-directory test/coverage && rm coverage.info && xdg-open test/coverage/index.html", - "test:performance": "npm run build && npm run jest -- ./perf/*" + "test:performance": "npm run build && esbuild perf.min=perf/main.mjs --outdir=dist --target=esnext --format=esm --platform=browser --bundle --sourcemap" }, "imports": { "#*": "./*" diff --git a/test/perf/account.perf.js b/perf/account.perf.js similarity index 91% rename from test/perf/account.perf.js rename to perf/account.perf.js index e4d796a..59b6e44 100644 --- a/test/perf/account.perf.js +++ b/perf/account.perf.js @@ -3,7 +3,7 @@ 'use strict' -import { assert, skip, test } from '#test/GLOBALS.mjs' +import { assert, skip, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet } from '#dist/main.js' @@ -11,9 +11,9 @@ test('BIP-44 ckd performance test', async () => { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - console.log(`HERE`) const accounts = await wallet.accounts(0, 0x7fff) + console.log(`HERE`) assert.equals(accounts.length, 0x8000) }) @@ -23,5 +23,6 @@ test('BLAKE2b ckd performance test', async () => { const accounts = await wallet.accounts(0, 0x7fff) + console.log(`HERE 2`) assert.equals(accounts.length, 0x8000) }) diff --git a/perf/main.mjs b/perf/main.mjs new file mode 100644 index 0000000..eb99a2e --- /dev/null +++ b/perf/main.mjs @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024 Chris Duncan +// SPDX-License-Identifier: GPL-3.0-or-later + +import './account.perf.js' +import './wallet.perf.js' + +console.log('> TESTING COMPLETE <') diff --git a/test/perf/wallet.perf.js b/perf/wallet.perf.js similarity index 92% rename from test/perf/wallet.perf.js rename to perf/wallet.perf.js index 7c65a68..5024366 100644 --- a/test/perf/wallet.perf.js +++ b/perf/wallet.perf.js @@ -3,7 +3,7 @@ 'use strict' -import { assert, skip, test } from '#test/GLOBALS.mjs' +import { assert, skip, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet } from '#dist/main.js' diff --git a/test.html b/test.html index f3aa38b..7347f65 100644 --- a/test.html +++ b/test.html @@ -4,6 +4,7 @@ +