// The phone's exact path, end to end: swarm only, no sidecar, cold store, no $HOME.
// Resolve the release, fetch its tree, and prove the addons are really fetchable.
import process from 'bare-process'
const dir = process.argv[process.argv.length - 1]
const REG = '1dd36bdfa9cb7f7d84adbd07f017e7200f7f90a3a546cd0761fac2a1f8a13e86'
const t0 = Date.now()
const { watchLatestRelease } = await import('pgh/resolve-release')
const w = await watchLatestRelease({
registryKeyHex: REG,
name: 'favela-surface-android',
storeDir: dir + '/registry',
onRelease: () => {}
})
// wait for the watch to produce an entry
let entry = null
for (let i = 0; i < 60 && !entry; i++) {
entry = w.latest()
if (!entry) await new Promise((r) => setTimeout(r, 500))
}
if (!entry) { console.log('RESOLVE: null after 30s'); await w.close(); process.exit(1) }
console.log('RESOLVE +' + (Date.now() - t0) + 'ms version=' + entry.version + ' tree=' + entry.treeHash.slice(0, 16))
const { NetworkFetcher } = await import('pgh/network-fetcher')
const f = new NetworkFetcher({ topic: entry.serverKey, destDir: dir + '/fetch', localRegistryDir: dir + '/reg-local' })
const t1 = Date.now()
const tree = await f.getTree(entry.treeHash)
console.log('getTree +' + (Date.now() - t1) + 'ms entries=' + tree.entries.length)
for (const e of tree.entries) console.log(' kind=' + e.kind + ' ' + e.name)
const addonDir = tree.entries.find((e) => e.name === 'addons' && e.kind === 1)
if (!addonDir) { console.log('NO addons/ IN THE PUBLISHED TREE'); await f.close?.(); await w.close(); process.exit(1) }
const t2 = Date.now()
const sub = await f.getTree(addonDir.childHash)
let bytes = 0
for (const e of sub.entries) bytes += 0
console.log('addons/ +' + (Date.now() - t2) + 'ms files=' + sub.entries.length)
console.log(' first: ' + sub.entries.slice(0, 3).map((e) => e.name).join(', '))
// actually pull one addon's bytes — the thing a phone must be able to do
const one = sub.entries[0]
const t3 = Date.now()
const content = await f.getContent(one.childHash)
console.log('getContent(' + one.name + ') +' + (Date.now() - t3) + 'ms ' + content.length + ' bytes exec=' + one.exec)
console.log('OTA PATH OK')
try { await f.close() } catch {}
await w.close()
process.exit(0)