OverviewFilesCommitsIssuesDocsReleasesPeers

favela-gateway / test / probes.test.js

123 lines · javascript ref:
// bsky probes — "are we resolving to bsky?", asked of bsky itself (hyper-social #80).
//
// Every check here is something that silently broke at least once: the relay marking us offline
// (2026-10-13, and 2026-08), the relay holding an older rev than we serve, PLC pointing somewhere else,
// the AppView not resolving a profile. Each problem comes back NAMED, so the CLI and the panel can say
// what is wrong instead of showing a red light.

import test from 'brittle'
import { createProbes } from '../lib/probes.js'

const HOST = 'pds.7rnx.net'
const WJ = 'did:plc:wj62ebbusln6gxo5azo6ogzo'

// A fake fetch answering by URL prefix; anything unlisted is a 404.
function fakeFetch (routes, calls = []) {
  return async (url) => {
    calls.push(url)
    for (const [prefix, body] of Object.entries(routes)) {
      if (url.startsWith(prefix)) {
        if (body instanceof Error) throw body
        return { ok: true, status: 200, json: async () => body }
      }
    }
    return { ok: false, status: 404, json: async () => ({}) }
  }
}

const healthy = {
  ['https://bsky.network/xrpc/com.atproto.sync.getHostStatus']: { hostname: HOST, seq: 40267, status: 'active' },
  ['https://bsky.network/xrpc/com.atproto.sync.getLatestCommit']: { cid: 'bafyA', rev: '3mrhxnkbhv622' },
  ['https://plc.directory/' + WJ]: { alsoKnownAs: ['at://p2p.7rnx.net'], service: [{ id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: 'https://pds.7rnx.net' }] },
  ['https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile']: { did: WJ, handle: 'p2p.7rnx.net', postsCount: 199 },
  ['https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed']: { feed: [{ post: { uri: 'at://' + WJ + '/app.bsky.feed.post/3mvks7khp3u6w', indexedAt: '2026-09-15T14:05:39Z' } }] }
}

const ours = [{ did: WJ, rev: '3mrhxnkbhv622' }]

test('★ all green: relay active and in sync, PLC points here, the AppView resolves', async (t) => {
  const p = createProbes({ hostname: HOST, fetch: fakeFetch(healthy) })
  const r = await p.probe({ dids: ours, emitted: 40267 })
  t.is(r.relay.status, 'active')
  t.is(r.relay.seq, 40267)
  const d = r.dids[0]
  t.is(d.relayRev, '3mrhxnkbhv622')
  t.is(d.inSync, true, 'the relay holds the head we serve')
  t.is(d.plcPds, 'https://pds.7rnx.net')
  t.is(d.handle, 'p2p.7rnx.net')
  t.is(d.appview.resolves, true)
  t.is(d.appview.newestPost, 'at://' + WJ + '/app.bsky.feed.post/3mvks7khp3u6w')
  t.alike(r.problems, [], 'nothing to report')
})

test('⛔ each failure is named, not just counted', async (t) => {
  const broken = {
    ...healthy,
    ['https://bsky.network/xrpc/com.atproto.sync.getHostStatus']: { hostname: HOST, seq: 40262, status: 'offline' },
    ['https://bsky.network/xrpc/com.atproto.sync.getLatestCommit']: { cid: 'bafyOld', rev: '3mrhxnkbhv522' },
    ['https://plc.directory/' + WJ]: { service: [{ id: '#atproto_pds', serviceEndpoint: 'https://bsky.social' }] }
  }
  delete broken['https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile']
  const p = createProbes({ hostname: HOST, fetch: fakeFetch(broken) })
  const r = await p.probe({ dids: ours, emitted: 40267 })
  const text = r.problems.join('\n')
  t.ok(/relay.*offline/i.test(text), 'the relay has us offline')
  t.ok(/behind/i.test(text) && text.includes('3mrhxnkbhv522'), 'the relay is behind our head, with its rev')
  t.ok(/PLC/.test(text) && text.includes('bsky.social'), 'PLC points elsewhere, and where')
  t.ok(/AppView/.test(text), 'the AppView does not resolve the profile')
  t.is(r.dids[0].inSync, false)
})

test('a probe that throws is reported as unreachable, never thrown — metrics must always answer', async (t) => {
  const p = createProbes({ hostname: HOST, fetch: fakeFetch({ 'https://': new Error('ECONNRESET') }) })
  const r = await p.probe({ dids: ours, emitted: 1 })
  t.ok(r.relay.error, 'the relay probe failed, with the reason')
  t.ok(r.problems.some((s) => /unreachable|ECONNRESET/.test(s)), 'and that is a named problem')
})

test('⛔ cached: a second call within the TTL makes no request — page views must not reach bsky', async (t) => {
  const calls = []
  let now = 1e9
  const p = createProbes({ hostname: HOST, fetch: fakeFetch(healthy, calls), ttlMs: 5 * 60e3, now: () => now })
  await p.get({ dids: ours, emitted: 40267 })
  const n = calls.length
  now += 60e3
  await p.get({ dids: ours, emitted: 40267 })
  t.is(calls.length, n, 'no new requests one minute later')
  now += 5 * 60e3
  await p.get({ dids: ours, emitted: 40267 })
  t.ok(calls.length > n, 'past the TTL it probes again')
})

test('concurrent callers share one probe in flight', async (t) => {
  const calls = []
  const p = createProbes({ hostname: HOST, fetch: fakeFetch(healthy, calls) })
  await Promise.all([p.get({ dids: ours, emitted: 1 }), p.get({ dids: ours, emitted: 1 })])
  const hostCalls = calls.filter((u) => u.includes('getHostStatus')).length
  t.is(hostCalls, 1, 'one getHostStatus for two simultaneous asks')
})

// ── the public page's view: what bsky actually shows ─────────────────────────────────────────────────
const posts3 = { feed: [1, 2, 3].map((i) => ({ post: { uri: 'at://' + WJ + '/app.bsky.feed.post/3mr' + i + 'aaaaaaaaa', indexedAt: '2026-09-1' + i + 'T00:00:00Z', author: { handle: 'p2p.7rnx.net' }, record: { text: 'post ' + i + ' <b>', createdAt: '2026-09-1' + i + 'T00:00:00Z' } } })) }

test('★ per DID, the last 3 posts as the AppView shows them (text kept short)', async (t) => {
  const p = createProbes({ hostname: HOST, fetch: fakeFetch({ ...healthy, ['https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed']: posts3 }) })
  const r = await p.probe({ dids: ours, emitted: 1 })
  t.is(r.dids[0].appview.recent.length, 3)
  t.alike(r.dids[0].appview.recent[0], { uri: posts3.feed[0].post.uri, text: 'post 1 <b>', createdAt: '2026-09-11T00:00:00Z' })
  t.is(r.dids[0].appview.newestPost, posts3.feed[0].post.uri, 'newest is still the first')
})

test('★ each lens feed as bsky\'s getFeed returns it (top 10), or its error', async (t) => {
  const FEED = 'at://did:plc:lc/app.bsky.feed.generator/loser-club'
  const p = createProbes({ hostname: HOST, fetch: fakeFetch({ ...healthy, ['https://public.api.bsky.app/xrpc/app.bsky.feed.getFeed']: posts3 }) })
  const r = await p.probe({ dids: [], emitted: 1, feeds: [FEED] })
  t.is(r.feeds[0].uri, FEED)
  t.is(r.feeds[0].posts.length, 3)
  t.is(r.feeds[0].posts[0].author, 'p2p.7rnx.net')
  const broken = createProbes({ hostname: HOST, fetch: fakeFetch(healthy) })
  const b = await broken.probe({ dids: [], emitted: 1, feeds: [FEED] })
  t.ok(b.feeds[0].error, 'a feed bsky cannot load says so')
  t.ok(b.problems.some((s) => /loser-club/.test(s) && /getFeed/.test(s)), 'and it is a named problem')
})