OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-identity / test / helpers.js

33 lines · javascript ref:
import Corestore from 'corestore'
import Hyperbee from 'hyperbee'
import RAM from 'random-access-memory'
import path from '#node:path'
import os from '#node:os'
import crypto from 'hypercore-crypto'

/**
 * Create an in-memory Corestore + Hyperbee for testing
 * @returns {{ store: Corestore, db: Hyperbee, cleanup: Function }}
 */
export async function createTestDb () {
  const tmpDir = path.join(os.tmpdir(), 'hyper-identity-test-' + crypto.randomBytes(8).toString('hex'))
  const store = new Corestore(tmpDir)
  await store.ready()

  const core = store.get({ name: 'test-db' })
  const db = new Hyperbee(core, {
    keyEncoding: 'utf-8',
    valueEncoding: 'json'
  })
  await db.ready()

  return {
    store,
    db,
    async cleanup () {
      await db.close()
      await store.close()
    }
  }
}