OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git-v3 / test / follow-registry-strict.test.js

82 lines · javascript ref:
// follow-registry-strict.test.js — FollowRegistry refuses to open
// without a bootstrap key. No auto-derive ever.

import test from 'brittle'
import Corestore from 'corestore'
import { mkdtempSync } from '#node:fs'
import { tmpdir } from '#node:os'
import { join } from '#node:path'

import { FollowRegistry } from '../lib/follow-registry.js'

const SRV_HEX = 'a'.repeat(64)

function makeStore () {
  return new Corestore(mkdtempSync(join(tmpdir(), 'pgv3-follow-reg-')))
}

test('openRepo without bootstrapKey throws ETRACKER_NOT_READY', async (t) => {
  const store = makeStore()
  const reg = new FollowRegistry(store)
  try {
    await reg.openRepo(SRV_HEX, 'foo', null)
    t.fail('should have thrown')
  } catch (err) {
    t.is(err.code, 'ETRACKER_NOT_READY', 'strict error code')
    t.ok(/bootstrap key required/.test(err.message), 'helpful message')
    t.ok(/repo:foo/.test(err.message), 'mentions the scope+name')
  }
  await reg.close()
  await store.close()
})

test('openOrg without bootstrapKey throws ETRACKER_NOT_READY', async (t) => {
  const store = makeStore()
  const reg = new FollowRegistry(store)
  try {
    await reg.openOrg(SRV_HEX, 'acme', undefined)
    t.fail('should have thrown')
  } catch (err) {
    t.is(err.code, 'ETRACKER_NOT_READY')
    t.ok(/org:acme/.test(err.message))
  }
  await reg.close()
  await store.close()
})

test('openRepo without a name throws', async (t) => {
  const store = makeStore()
  const reg = new FollowRegistry(store)
  await t.exception(reg.openRepo(SRV_HEX, '', 'b'.repeat(64)))
  await reg.close()
  await store.close()
})

test('openRepo without a sourceServerKey throws', async (t) => {
  const store = makeStore()
  const reg = new FollowRegistry(store)
  await t.exception(reg.openRepo(null, 'foo', 'b'.repeat(64)))
  await reg.close()
  await store.close()
})

test('constructor requires a corestore', (t) => {
  t.exception(() => new FollowRegistry(null), /requires a corestore/)
})

test('list() is empty before any open', (t) => {
  const reg = new FollowRegistry(makeStore())
  t.alike(reg.list(), [])
})

test('getOpenRepo returns null for nothing open', (t) => {
  const reg = new FollowRegistry(makeStore())
  t.is(reg.getOpenRepo(SRV_HEX, 'foo'), null)
})

test('close on non-open is a no-op', async (t) => {
  const reg = new FollowRegistry(makeStore())
  const closed = await reg.closeRepo(SRV_HEX, 'never-opened')
  t.is(closed, false)
})