OverviewFilesCommitsIssuesDocsReleasesPeers

favela-gateway / test / bsky-bind.test.js

38 lines · javascript ref:
// bsky-bind.test.js — a fronted DID is bound to a favela identity only when BOTH sides say so (hyper-social #82,
// I2a / D4a).
//
// ⛔ TWO-SIDED, OR NOT AT ALL. The DID's repo (signed by the DID key) names a favela feed in net.favela.feed/self, AND
// that feed's reach/self (written by the identity's own devices) names the DID as its credential. Either half alone
// is a claim anyone could make: a feed can name any DID, and a DID's owner can name any feed. Only the pair binds —
// and only a binding lets bsky interactions on that DID reach that identity's notifications.

import test from 'brittle'
import { bindDid, FEED_CLAIM } from '../lib/bsky-bind.js'

const DID = 'did:plc:fleekyfleekyfleeky'
const FEED = 'd2'.repeat(32)

const front = (value) => ({ getRecord: async (c, r) => (c === FEED_CLAIM && r === 'self' && value ? { uri: `at://${DID}/${c}/${r}`, value } : null) })
const held = (credential) => ({ reach: async () => ({ networks: ['bsky'], credential }) })

test('★ bound when the DID names the feed AND the feed names the DID', async (t) => {
  const r = await bindDid(DID, { front: front({ $type: FEED_CLAIM, feed: FEED }), feedOf: async (f) => (f === FEED ? held(DID) : null) })
  t.alike(r, { did: DID, feed: FEED })
})

test('⛔ one-sided claims bind nothing', async (t) => {
  const noClaim = await bindDid(DID, { front: front(null), feedOf: async () => held(DID) })
  t.ok(noClaim.refused, 'the DID names no feed: ' + noClaim.refused)
  const otherDid = await bindDid(DID, { front: front({ feed: FEED }), feedOf: async () => held('did:plc:someoneelse') })
  t.ok(otherDid.refused, 'the feed names a different DID: ' + otherDid.refused)
  const notHeld = await bindDid(DID, { front: front({ feed: FEED }), feedOf: async () => null })
  t.ok(notHeld.refused, 'the gateway does not hold that feed: ' + notHeld.refused)
  const junk = await bindDid(DID, { front: front({ feed: 'not-a-key' }), feedOf: async () => held(DID) })
  t.ok(junk.refused, 'a malformed feed key: ' + junk.refused)
})

test('a read that fails is a refusal, never a throw', async (t) => {
  const r = await bindDid(DID, { front: { getRecord: async () => { throw new Error('offline') } }, feedOf: async () => held(DID) })
  t.ok(r.refused)
})