OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / test / topic-order.test.js

30 lines · javascript ref:
import test from 'brittle'
import b4a from 'b4a'
import { orderedTopicCandidates } from '../lib/topic-order.js'

test('serverKey (publisher) is tried FIRST when present', (t) => {
  const serverKeyTopic = 'a'.repeat(64) // hex identity pubkey
  const registryTopic = b4a.alloc(32, 1)
  const cands = orderedTopicCandidates({ serverKeyTopic, registryTopic })

  t.is(cands.length, 2, 'both candidates present')
  t.is(cands[0].label, 'serverKey (publisher)', 'publisher topic is first — fresh releases resolve instantly')
  t.is(cands[1].label, 'registry (shared/multi-source)', 'shared topic is the fallback')
  t.is(cands[0].topic, serverKeyTopic)
  t.is(cands[0].short, 'a'.repeat(12), 'short label from hex string')
  t.is(cands[1].short, registryTopic.toString('hex').slice(0, 12), 'short label from buffer')
})

test('falls back to shared-only when there is no serverKey', (t) => {
  const registryTopic = b4a.alloc(32, 2)
  const cands = orderedTopicCandidates({ serverKeyTopic: null, registryTopic })

  t.is(cands.length, 1, 'only the shared topic')
  t.is(cands[0].label, 'registry (shared/multi-source)')
})

test('empty when neither topic is available', (t) => {
  t.is(orderedTopicCandidates({}).length, 0)
  t.is(orderedTopicCandidates().length, 0, 'no args → empty, no throw')
})