diff --git a/tests/helpers/index.js b/tests/helpers/index.js deleted file mode 100644 index 088b166f9..000000000 --- a/tests/helpers/index.js +++ /dev/null @@ -1,74 +0,0 @@ -import { KeyManager } from '@mapeo/crypto' -import Hypercore from 'hypercore' -import RAM from 'random-access-memory' - -export async function createCore(...args) { - const core = new Hypercore(RAM, ...args) - await core.ready() - return core -} - -/** - * @param {string} name - * @param {Buffer} [namespace] - 32 byte Buffer - */ -export function createCoreKeyPair(name, namespace = Buffer.alloc(32, 0)) { - const { keyManager } = createIdentityKeys() - const coreKeyPair = keyManager.getHypercoreKeypair(name, namespace) - return coreKeyPair -} - -export function createIdentityKeys() { - const rootKey = KeyManager.generateRootKey() - const keyManager = new KeyManager(rootKey) - const identityKeyPair = keyManager.getIdentityKeypair() - const identityId = identityKeyPair.publicKey.toString('hex') - return { rootKey, identityId, identityKeyPair, keyManager } -} - -export function replicate(peers) { - const connected = new Map() - - for (const peer1 of peers) { - const peer1Connections = new Set() - connected.set(peer1.id, peer1Connections) - - for (const peer2 of peers) { - if (peer1.id === peer2.id) { - continue - } - - const peer2Connections = connected.get(peer2.id) - - if ( - peer1Connections.has(peer2.id) || - (peer2Connections && peer2Connections.has(peer1.id)) - ) { - continue - } - - peer1Connections.add(peer2.id) - const stream1 = peer1.core.replicate(true, { live: true }) - const stream2 = peer2.core.replicate(false, { live: true }) - stream1.pipe(stream2).pipe(stream1) - } - } -} - -export async function waitForIndexing(stores) { - await Promise.all( - stores.map((store) => { - return store.indexing() - }) - ) -} - -export async function timeoutException(promise, timeout = 100) { - const timer = new Promise((resolve) => { - setTimeout(() => { - resolve('timeout') - }, timeout) - }) - - return (await Promise.race([promise, timer])) === 'timeout' -} diff --git a/tests/sync/core-sync-state.js b/tests/sync/core-sync-state.js index 759acbd79..de0bbd342 100644 --- a/tests/sync/core-sync-state.js +++ b/tests/sync/core-sync-state.js @@ -1,6 +1,8 @@ // @ts-check import NoiseSecretStream from '@hyperswarm/secret-stream' import test from 'brittle' +import Hypercore from 'hypercore' +import RAM from 'random-access-memory' import { deriveState, PeerState, @@ -10,8 +12,6 @@ import { import RemoteBitfield, { BITS_PER_PAGE, } from '../../src/core-manager/remote-bitfield.js' -import { createCore } from '../helpers/index.js' -// import { setTimeout } from 'timers/promises' import { once } from 'node:events' import pTimeout from 'p-timeout' import { EventEmitter } from 'node:events' @@ -351,6 +351,12 @@ test.skip('bitCount32', (t) => { } }) +async function createCore(key) { + const core = new Hypercore(RAM, key) + await core.ready() + return core +} + /** * Slow but understandable implementation to compare with fast obscure implementation * @param {number} n