OverviewFilesCommitsIssuesDocsReleasesPeers

favela-identity / test / bridge-claim.test.js

51 lines · javascript ref:
// bridge-claim.test.js — the wire format itself, pinned.
//
// ⛔ WHY THIS FILE EXISTS AND WHY IT IS HERE. Two packages sign and verify these bytes — favela-peer
// builds them, ap-bridge rebuilds them — and NEITHER may import the other: favela-peer's
// boundary.test.js forbids naming a projection, and ap-bridge has no business depending on the
// daemon. So no test can drive both sides at once, and this package is the only place both agree on.
//
// ⛔ AND A DISAGREEMENT IS SILENT. If the two sides ever differ by a byte, `readAttested` returns
// null and the bridge answers "proof does not attest that identity" — which reads as a forged or
// hostile caller. There is no error that says "we disagree about the format", so the format is pinned
// to its exact string here and a drift on either side fails HERE, with a diff a person can read.

import test from 'brittle'
import b4a from 'b4a'
import { claimBytes, releaseBytes, frontBytes, collectBytes, cursor } from '../lib/bridge-claim.js'

const s = (buf) => b4a.toString(buf, 'utf8')
const N = 'ab12'

test('★★ the four formats, exactly', async (t) => {
  t.is(s(claimBytes({ nonce: N, username: 'alice', feedKey: 'ff' })), 'favela-bridge:v1:claim:ab12:alice:ff')
  t.is(s(releaseBytes({ nonce: N, username: 'alice' })), 'favela-bridge:v1:release:ab12:alice')
  t.is(s(frontBytes({ nonce: N, id: 'did:plc:x' })), 'favela-bridge:v1:front:ab12:did:plc:x')
  t.is(s(collectBytes({ nonce: N, username: 'alice', since: 7 })), 'favela-bridge:v1:collect:ab12:alice:7')
})

// ⛔ THE PREFIXES ARE WHAT KEEP ONE ACT FROM BECOMING ANOTHER. Releasing BURNS a name permanently, so
// a routine collect whose bytes could be repurposed as a release would be a peer handing strangers a
// weapon on every poll.
test('⛔ no two acts ever produce the same bytes', async (t) => {
  const all = [
    s(claimBytes({ nonce: N, username: 'alice', feedKey: '' })),
    s(releaseBytes({ nonce: N, username: 'alice' })),
    s(frontBytes({ nonce: N, id: 'alice' })),
    s(collectBytes({ nonce: N, username: 'alice', since: 0 }))
  ]
  t.is(new Set(all).size, all.length, 'four acts, four distinct strings')
})

// ⛔ THE CURSOR NORMALISES ON BOTH SIDES OR NEITHER. The peer has a number; the bridge has whatever
// JSON handed back. If `0`, `'0'`, `undefined` and `-1` did not all produce the same text, an
// ordinary first collect would fail as a bad proof.
test('⛔ every way of saying "from the start" produces the same bytes', async (t) => {
  const base = s(collectBytes({ nonce: N, username: 'alice', since: 0 }))
  for (const since of [0, '0', undefined, null, -1, NaN, 0.4]) {
    t.is(s(collectBytes({ nonce: N, username: 'alice', since })), base, 'since=' + String(since))
  }
  t.is(cursor('12'), '12', 'and a real cursor survives as itself')
  t.is(cursor(12.9), '12', 'floored, never rounded up past a row we were not given')
})