From 5f2a40fee8f2ffc3ec5c1d275576d69551e06f18 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 11 Nov 2024 01:27:33 -0800 Subject: [PATCH] Move polyfills into separate file from test vectors, and redesign them as side effect imports. --- test/GLOBALS.js | 23 +++++++++++++++++++++++ test/TEST_VECTORS.js | 17 ----------------- test/create-wallet.test.mjs | 3 ++- test/derive-accounts.test.mjs | 3 ++- test/import-wallet.test.mjs | 3 ++- test/lock-unlock-wallet.mjs | 3 ++- test/manage-rolodex.mjs | 1 + test/refresh-accounts.test.mjs | 3 ++- test/sign-blocks.test.mjs | 3 ++- test/tools.test.mjs | 3 ++- 10 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 test/GLOBALS.js diff --git a/test/GLOBALS.js b/test/GLOBALS.js new file mode 100644 index 0000000..fa1af78 --- /dev/null +++ b/test/GLOBALS.js @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2024 Chris Duncan +// SPDX-License-Identifier: GPL-3.0-or-later + +import { Worker } from 'node:worker_threads' + +globalThis.Worker ??= Worker +globalThis.Worker.onmessage ??= (handler) => Worker.prototype.on('message', handler) +globalThis.onmessage ??= (handler) => EventTarget.prototype.addEventListener('message', handler) + +if (globalThis.sessionStorage == null) { + const _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 + }) +} diff --git a/test/TEST_VECTORS.js b/test/TEST_VECTORS.js index b2c1710..32bfdf9 100644 --- a/test/TEST_VECTORS.js +++ b/test/TEST_VECTORS.js @@ -1,23 +1,6 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later -export const STORAGE = (() => { - if (globalThis.sessionStorage == null) { - const _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 const GENESIS_ADDRESS = 'nano_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3' export const RAW_MAX = '340282366920938463463374607431768211455' export const SUPPLY_MAX = '133248297920938463463374607431768211455' diff --git a/test/create-wallet.test.mjs b/test/create-wallet.test.mjs index 583e5df..b9e576f 100644 --- a/test/create-wallet.test.mjs +++ b/test/create-wallet.test.mjs @@ -3,9 +3,10 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { NANO_TEST_VECTORS, STORAGE, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' +import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' import { Account, Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.js' // WARNING: Do not send any funds to the test vectors below diff --git a/test/derive-accounts.test.mjs b/test/derive-accounts.test.mjs index 0765017..076a560 100644 --- a/test/derive-accounts.test.mjs +++ b/test/derive-accounts.test.mjs @@ -3,9 +3,10 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { NANO_TEST_VECTORS, STORAGE } from './TEST_VECTORS.js' +import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.js' const skip = true diff --git a/test/import-wallet.test.mjs b/test/import-wallet.test.mjs index 7fd35f0..91f1d07 100644 --- a/test/import-wallet.test.mjs +++ b/test/import-wallet.test.mjs @@ -3,9 +3,10 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { BIP32_TEST_VECTORS, CUSTOM_TEST_VECTORS, NANO_TEST_VECTORS, STORAGE, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' +import { BIP32_TEST_VECTORS, CUSTOM_TEST_VECTORS, NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' import { Account, Bip44Wallet, Blake2bWallet } from '../dist/main.js' // WARNING: Do not send any funds to the test vectors below diff --git a/test/lock-unlock-wallet.mjs b/test/lock-unlock-wallet.mjs index d8c3dab..e09f1d9 100644 --- a/test/lock-unlock-wallet.mjs +++ b/test/lock-unlock-wallet.mjs @@ -3,9 +3,10 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { NANO_TEST_VECTORS, STORAGE, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' +import { NANO_TEST_VECTORS, TREZOR_TEST_VECTORS } from './TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet } from '../dist/main.js' const skip = false diff --git a/test/manage-rolodex.mjs b/test/manage-rolodex.mjs index bcaff1a..8f666a0 100644 --- a/test/manage-rolodex.mjs +++ b/test/manage-rolodex.mjs @@ -3,6 +3,7 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' import { Rolodex, Tools } from '../dist/main.js' diff --git a/test/refresh-accounts.test.mjs b/test/refresh-accounts.test.mjs index 3983e67..fe26e7b 100644 --- a/test/refresh-accounts.test.mjs +++ b/test/refresh-accounts.test.mjs @@ -3,9 +3,10 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { NANO_TEST_VECTORS, STORAGE } from './TEST_VECTORS.js' +import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { Account, Bip44Wallet, Rpc } from '../dist/main.js' // WARNING: Do not send any funds to the test vectors below diff --git a/test/sign-blocks.test.mjs b/test/sign-blocks.test.mjs index a02a9c2..1df9b21 100644 --- a/test/sign-blocks.test.mjs +++ b/test/sign-blocks.test.mjs @@ -3,10 +3,11 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { SendBlock, ReceiveBlock, ChangeBlock } from '../dist/main.js' import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' +import { SendBlock, ReceiveBlock, ChangeBlock } from '../dist/main.js' // WARNING: Do not send any funds to the test vectors below // Test vectors from https://docs.nano.org/integration-guides/key-management/ diff --git a/test/tools.test.mjs b/test/tools.test.mjs index 1d11666..48ba1d5 100644 --- a/test/tools.test.mjs +++ b/test/tools.test.mjs @@ -3,9 +3,10 @@ 'use strict' +import './GLOBALS.js' import { describe, it } from 'node:test' import { strict as assert } from 'assert' -import { RAW_MAX, NANO_TEST_VECTORS, STORAGE } from './TEST_VECTORS.js' +import { RAW_MAX, NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { Bip44Wallet, Account, SendBlock, Rpc, Tools } from '../dist/main.js' const skip = true -- 2.34.1