// Proof test for fix path A:
// Move full identity records (method, publicKey, credentials, fingerprint
// reverse-indexes) from a per-daemon direct-write Hyperbee `db` into the
// Autobase view. Mirror's view replicates → mirror's getIdentity returns
// full record → "Linked identities" UI works cross-daemon.
//
// Today (broken):
// primary.db.put(`identity:<id>`, fullRecord) ← local-only
// primary.base.append({type:'add-identity-link', userId, identityId}) ← in view
// mirror's view shows user.identities=[<id>] but mirror.db is empty
// mirror.getIdentity(<id>) → null → "Linked identities (0)" symptom
//
// Fix:
// primary.base.append({type:'register-identity', identity: fullRecord})
// apply writes `identity:<id>`, `pubkey:<pub>`, `sshkey:<fp>` to view
// mirror replicates view → mirror.getIdentity(<id>) returns fullRecord
//
// This test proves the new op flow works in raw autobase.
import test from 'brittle'
import Autobase from 'autobase'
import Corestore from 'corestore'
import Hypercore from 'hypercore'
import Hyperbee from 'hyperbee'
import crypto from 'hypercore-crypto'
import b4a from 'b4a'
import os from '#node:os'
import path from '#node:path'
import fs from '#node:fs'
import { replicateAndSync } from 'autobase-test-helpers'
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'idrec-replicate-'))
let n = 0
const tmp = () => path.join(TMP, 'store-' + (n++))
// Apply: writes register-identity ops to the view, with reverse-lookup indexes.
// Mirrors what hyper-identity's identity-apply.js would need to do for fix A.
async function apply (nodes, view, host) {
for (const node of nodes) {
const op = node.value
if (!op || !op.type) continue
if (op.type === 'register-identity') {
const identity = op.identity
if (!identity || !identity.identityId) continue
// Primary record
await view.put(`identity:${identity.identityId}`, identity)
// Reverse indexes — by method
if (identity.method === 'ssh-key' && identity.credentials && identity.credentials.fingerprint) {
await view.put(`sshkey:${identity.credentials.fingerprint}`, { identityId: identity.identityId })
}
if (identity.publicKey) {
await view.put(`pubkey:${identity.publicKey}`, { identityId: identity.identityId })
}
} else if (op.type === 'add-writer') {
await host.addWriter(b4a.from(op.writerKey, 'hex'), { indexer: false })
}
}
}
const baseOpts = {
apply,
open: (s) => new Hyperbee(s.get('view'), { keyEncoding: 'utf-8', valueEncoding: 'json' }),
valueEncoding: 'json',
optimistic: true,
ackInterval: 1000,
ackThreshold: 4
}
test('register-identity op flows from primary view to mirror view (cross-daemon visibility, no local db)', async (t) => {
// ── PRIMARY ─────────────────────────────────────────────────
const primaryKp = crypto.keyPair()
const primaryStore = new Corestore(tmp()); await primaryStore.ready()
const primary = new Autobase(primaryStore, null, { ...baseOpts, keyPair: primaryKp })
await primary.ready()
// ── MIRROR (with the keet-pattern + keyPair fix shape) ──────
const newDeviceKp = crypto.keyPair()
const predictedWriterKey = Hypercore.key({ version: 1, signers: [{ publicKey: newDeviceKp.publicKey }] })
await primary.append({ type: 'add-writer', writerKey: b4a.toString(predictedWriterKey, 'hex') })
await primary.update()
const mirrorStore = new Corestore(tmp()); await mirrorStore.ready()
const mirror = new Autobase(mirrorStore, primary.key, { ...baseOpts, keyPair: newDeviceKp })
await mirror.ready()
// Wire replication
const sa = primaryStore.replicate(true)
const sb = mirrorStore.replicate(false)
sa.on('error', () => {}); sb.on('error', () => {})
sa.pipe(sb).pipe(sa)
await replicateAndSync([primary, mirror])
t.ok(mirror.writable, 'mirror writable (writer key matches)')
// ── PRIMARY appends a register-identity op (full record) ────
const fakeSshFp = 'SHA256:' + b4a.toString(crypto.randomBytes(32), 'base64')
const identityRecord = {
identityId: b4a.toString(crypto.randomBytes(16), 'hex'),
method: 'ssh-key',
publicKey: b4a.toString(crypto.randomBytes(32), 'hex'),
credentials: {
fingerprint: fakeSshFp,
keyType: 'ssh-ed25519',
comment: 'fleeky@tabertron'
},
userId: 'f0e7a8785361',
createdAt: Date.now()
}
await primary.append({ type: 'register-identity', identity: identityRecord })
await primary.update()
await replicateAndSync([primary, mirror])
// ── MIRROR reads the identity record from ITS view ─────────
// (no shared db, no rpc, no extra wiring — pure autobase view replication)
const fromView = await mirror.view.get(`identity:${identityRecord.identityId}`)
t.ok(fromView && fromView.value, 'mirror.view has identity:<id> entry replicated from primary')
t.is(fromView.value.method, 'ssh-key', 'method preserved')
t.is(fromView.value.publicKey, identityRecord.publicKey, 'publicKey preserved')
t.is(fromView.value.credentials.fingerprint, fakeSshFp, 'credentials.fingerprint preserved')
t.is(fromView.value.userId, 'f0e7a8785361', 'userId preserved')
// Reverse index: sshkey:<fp> → identityId
const fromSshIdx = await mirror.view.get(`sshkey:${fakeSshFp}`)
t.ok(fromSshIdx && fromSshIdx.value, 'mirror.view has sshkey:<fingerprint> reverse index')
t.is(fromSshIdx.value.identityId, identityRecord.identityId, 'sshkey reverse index points to identity')
// Reverse index: pubkey:<hex> → identityId
const fromPkIdx = await mirror.view.get(`pubkey:${identityRecord.publicKey}`)
t.ok(fromPkIdx && fromPkIdx.value, 'mirror.view has pubkey:<hex> reverse index')
t.is(fromPkIdx.value.identityId, identityRecord.identityId, 'pubkey reverse index points to identity')
console.log(' identityId:', identityRecord.identityId)
console.log(' primary.signedLength:', primary.signedLength)
console.log(' mirror.signedLength: ', mirror.signedLength)
await primary.close(); await primaryStore.close()
await mirror.close(); await mirrorStore.close()
})
test('CONTROL: today\'s pattern — write to local db only, mirror has empty db, lookup fails', async (t) => {
// Models the existing broken state. Primary writes to its own local
// Hyperbee (not autobase view). Mirror has its own separate local
// Hyperbee, never receives the identity record.
const primaryStore = new Corestore(tmp()); await primaryStore.ready()
const primaryDbCore = primaryStore.namespace('identity-db').get({ name: 'db' })
await primaryDbCore.ready()
const primaryDb = new Hyperbee(primaryDbCore, { keyEncoding: 'utf-8', valueEncoding: 'json' })
await primaryDb.ready()
const mirrorStore = new Corestore(tmp()); await mirrorStore.ready()
const mirrorDbCore = mirrorStore.namespace('identity-db').get({ name: 'db' })
await mirrorDbCore.ready()
const mirrorDb = new Hyperbee(mirrorDbCore, { keyEncoding: 'utf-8', valueEncoding: 'json' })
await mirrorDb.ready()
const identityId = b4a.toString(crypto.randomBytes(16), 'hex')
await primaryDb.put(`identity:${identityId}`, { method: 'ssh-key', userId: 'fleeky' })
// Even with corestore replication piped, the mirror's identity-db core
// is corestore-auto-derived per-machine and unrelated to primary's.
const sa = primaryStore.replicate(true)
const sb = mirrorStore.replicate(false)
sa.on('error', () => {}); sb.on('error', () => {})
sa.pipe(sb).pipe(sa)
await new Promise(r => setTimeout(r, 500))
const fromMirror = await mirrorDb.get(`identity:${identityId}`)
t.absent(fromMirror, 'mirror.db has NO identity record (today\'s prod symptom: getIdentity returns null on stortron)')
await primaryDb.close(); await primaryStore.close()
await mirrorDb.close(); await mirrorStore.close()
})