import './lib/bare-globals.js'
import { Writer } from './lib/writer.js'
const sleep = (ms) => new Promise(r => setTimeout(r, ms))
const w = new Writer({})
const p1 = await w.post('reconnect-test-pre', { scope: 'local' })
console.log('PRE-RESTART post =>', p1.ok, p1.rkey)
await w._call('apply-write', { action: 'delete', collection: 'net.favela.local.post', rkey: p1.rkey }).catch(()=>{})
console.log('cached _uds now points at the CURRENT hpp socket; waiting 50s while hpp restarts...')
await sleep(50000)
const t0 = Date.now()
try {
const p2 = await w.post('reconnect-test-post', { scope: 'local' })
console.log('POST-RESTART post =>', p2.ok, p2.rkey, 'in', Date.now()-t0, 'ms (reconnected, NOT hung)')
await w._call('apply-write', { action: 'delete', collection: 'net.favela.local.post', rkey: p2.rkey }).catch(()=>{})
} catch (e) { console.log('POST-RESTART post threw in', Date.now()-t0, 'ms:', e.message) }
await w.close(); process.exit(0)