diff --git a/.env.template b/.env.template new file mode 100644 index 00000000..6e28c60f --- /dev/null +++ b/.env.template @@ -0,0 +1,7 @@ +# This is a template for using a .env file to configure your test setup +# +# $ cp .env.template .env + +ENTROPY_SDK_TESTS_RETRY_COUNT_DEFAULT=20 +ENTROPY_SDK_TESTS_RETRY_TIMEOUT_DEFAULT=1000 +# ENTROPY_SDK_TESTS_DONT_KILL=true diff --git a/.gitignore b/.gitignore index f998b4df..8f4d4123 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ docs dist +.env # Created by https://www.toptal.com/developers/gitignore/api/macos,node # Edit at https://www.toptal.com/developers/gitignore?templates=macos,node @@ -193,4 +194,4 @@ prepTests.sh *.sublime-workspace # Testing npm package -bootstrap/ \ No newline at end of file +bootstrap/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eb7c673..4a4c3897 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil ### Added - utility method to return substrate api instance to interact with without needing to instantiate entropy instance (#435)[https://github.com/entropyxyz/sdk/pull/435] - verifying method for signatures `keccak` and `blake2_256` hashed signatures + ### Fixed ### Changed @@ -22,8 +23,16 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil ### Broke - sign and sign with adapters no longer returns just the signature. It now returns a object `SignatureData` that contains the signature and information pronating to it see the `SignatureData` interface in (./src/signing/index.ts)(./src/signing/index.ts) - `entropy.sign` and `entropy.signWithAdaptersInOrder` function argument interface key renamed `sigRequestHash` -> `hexMessage` + ### Dev +- added `dotenv` for handling ENV in tests. See `.env.template` + - new ENV: + - `ENTROPY_SDK_TESTS_RETRY_COUNT_DEFAULT` - default number of times to run `retryUntil` function + - `ENTROPY_SDK_TESTS_RETRY_TIMEOUT_DEFAULT` - default timeout between runs of `retryUntil` function + - renamed ENV: + - `ENTROPY_DONT_KILL` => `ENTROPY_SDK_TESTS_DONT_KILL` + ### Meta ## [0.4.0] Fa - 2024-11-05 (entropy-core compatibility: 0.3.0) diff --git a/dev/testing-utils.mjs b/dev/testing-utils.mjs index b0a0ddd2..1cf38679 100644 --- a/dev/testing-utils.mjs +++ b/dev/testing-utils.mjs @@ -3,10 +3,27 @@ import { fileURLToPath } from 'url'; import { execFileSync } from 'child_process'; import { WebSocket } from 'ws'; import { promisify } from 'util' +import 'dotenv/config' const __dirname = dirname(fileURLToPath(import.meta.url)); const moduleRoot = join(__dirname, '..') + +const { + GITHUB_WORKSPACE, + ENTROPY_SDK_TESTS_RETRY_COUNT_DEFAULT, + ENTROPY_SDK_TESTS_RETRY_TIMEOUT_DEFAULT +} = process.env + const SECONDS = 1000 +const DEFAULT_RETRY_COUNT = ( + parseInt(ENTROPY_SDK_TESTS_RETRY_COUNT_DEFAULT) || + GITHUB_WORKSPACE && 60 || + 20 +) +const DEFAULT_RETRY_TIMEOUT = ( + parseInt(ENTROPY_SDK_TESTS_RETRY_TIMEOUT_DEFAULT) || + 1 * SECONDS +) // NOTE: you need to edit your /etc/hosts file to use these. See dev/README.md @@ -31,9 +48,10 @@ export async function spinNetworkUp (networkType = 'four-nodes') { async function retryUntil (fn, isSuccess = Boolean, opts = {}) { const { - triesRemaining = process.env.GITHUB_WORKSPACE ? 60 : 10, - timeout = 1 * SECONDS + retryCount = DEFAULT_RETRY_COUNT, + retryTimeout = DEFAULT_RETRY_TIMEOUT } = opts + return fn() .then(result => { if (isSuccess(result)) return result @@ -41,16 +59,17 @@ async function retryUntil (fn, isSuccess = Boolean, opts = {}) { }) .catch(async (err) => { // out of tries, do not recurse - if (triesRemaining === 1) throw err - await promisify(setTimeout)(timeout) + if (retryCount === 1) throw err + await promisify(setTimeout)(retryTimeout) return retryUntil(fn, isSuccess, { - triesRemaining: triesRemaining - 1, - timeout + retryCount: retryCount - 1, + retryTimeout }) }) } + async function isWebSocketReady (endpoint) { return new Promise((resolve, reject) => { try { @@ -136,8 +155,8 @@ total-block-time: ${headersSenseStart} blocks } export async function spinNetworkDown (networkType = 'four-nodes') { - if (process.env.ENTROPY_DONT_KILL) { - console.warn('$ENTROPY_DONT_KILL is set not spinning the network down') + if (process.env.ENTROPY_SDK_TESTS_DONT_KILL) { + console.warn('$ENTROPY_SDK_TESTS_DONT_KILL is set, so not spinning the network down') return false } try { diff --git a/package.json b/package.json index 4b4ec768..25007e58 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "@typescript-eslint/eslint-plugin": "^5.43.0", "@typescript-eslint/parser": "^5.43.0", "depcheck": "^1.4.7", + "dotenv": "^16.4.7", "eslint": "^8.57.0", "husky": "^9.0.11", "lint-staged": ">=10", diff --git a/tests/four-nodes.test.ts b/tests/four-nodes.test.ts index 4525c81b..48fe3482 100644 --- a/tests/four-nodes.test.ts +++ b/tests/four-nodes.test.ts @@ -1,16 +1,14 @@ import test from 'tape' -import { readFileSync } from 'fs' + import Entropy, { wasmGlobalsReady } from '../src' import Keyring from '../src/keys' -import * as util from '@polkadot/util' import { - sleep, promiseRunner, spinNetworkUp, jumpStartNetwork, eveSeed, - eveAddress, + // eveAddress, spinNetworkDown, } from './testing-utils' @@ -21,8 +19,6 @@ test('test the four-nodes docker script', async (t) => { // context: all run does is checks that it runs await run('network up', spinNetworkUp(networkType)) - await sleep(process.env.GITHUB_WORKSPACE ? 30_000 * 2 : 5_000 * 2) - // this gets called after all tests are run t.teardown(async () => { await entropy.close() diff --git a/yarn.lock b/yarn.lock index bed5632c..060ccc6c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1975,6 +1975,11 @@ doctrine@^3.0.0: dependencies: esutils "^2.0.2" +dotenv@^16.4.7: + version "16.4.7" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.7.tgz#0e20c5b82950140aa99be360a8a5f52335f53c26" + integrity sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ== + dotignore@^0.1.2: version "0.1.2" resolved "https://registry.npmjs.org/dotignore/-/dotignore-0.1.2.tgz"