OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-identity2 / test / contract.test.js

61 lines · javascript ref:
// hyper-identity2 must satisfy the SAME contract as hyper-identity, assertion for assertion.
//
// The contract is imported from hyper-identity-core, not restated here. A binding that kept its
// own copy would drift the moment one was edited, and "both engines agree" would quietly become
// "each engine agrees with its own expectations".
//
// ⛔ This package does NOT depend on hyper-identity, not even for tests. A stray symlink to it once
// dragged autobase into favela-peer's published dependency closure — which is precisely what this
// package exists to prevent. The autobase arm of the contract runs in hyper-identity's own suite.

import test from 'brittle'
import Corestore from 'corestore'
import path from '#node:path'
import os from '#node:os'
import crypto from 'hypercore-crypto'
import { contract } from 'hyper-identity-core/test/contract.js'
import { IdentityRegistry } from '../index.js'

async function open () {
  const dir = path.join(os.tmpdir(), 'hi2-' + crypto.randomBytes(8).toString('hex'))
  const store = new Corestore(dir)
  await store.ready()
  const registry = new IdentityRegistry(store, {})
  await registry.ready()
  return { registry, cleanup: async () => { await registry.close(); await store.close() } }
}

test('[autobee] the IdentityRegistry contract', async (t) => {
  const { registry, cleanup } = await open()
  t.teardown(cleanup)
  t.is(registry.engine, 'autobee', 'the package IS the engine — it cannot report otherwise')
  await contract(t, registry)
})

test('⛔ [autobee] adaptHost pins isIndexer, which autobee defaults to TRUE', async (t) => {
  const { adaptHost } = await import('../lib/adapt-host.js')
  const seen = []
  const host = adaptHost({ addWriter: (k, o) => seen.push([k, o]), removeWriter: () => {} })

  host.addWriter('k1', { isIndexer: true })
  host.addWriter('k2', { isIndexer: false })
  host.addWriter('k3', {})

  t.alike(seen[0], ['k1', { isIndexer: true }])
  t.alike(seen[1], ['k2', { isIndexer: false }])
  // The whole reason this binding is not a bare passthrough.
  t.alike(seen[2], ['k3', { isIndexer: false }], 'an ABSENT flag is false — never autobee’s true default')
})

test('⛔ [autobee] fastForwardTo is refused rather than silently dropped', async (t) => {
  const dir = path.join(os.tmpdir(), 'hi2-ff-' + crypto.randomBytes(6).toString('hex'))
  const store = new Corestore(dir)
  await store.ready()
  t.teardown(() => store.close())

  // autobase's fastForward takes the log key; autobee's takes an oplog BOOT HEAD. Accepting the
  // option and ignoring it is the same class of defect as a dropped addWriter flag.
  const r = new IdentityRegistry(store, { fastForwardTo: 'ab'.repeat(32) })
  await t.exception(r.ready(), /no autobee equivalent/, 'it says so, loudly')
})