// Proof + net-compat coverage. All stream I/O is done by stock bare-pipe
// (new Pipe(fd)); the companion only creates the abstract sockets.
//
// 1. net-compat round-trip: createServer(allowHalfOpen:false) echo +
// createConnection('connect' event) — the surface hyper-svc consumes.
// 2. async-error path: createConnection to an absent name must NOT throw
// synchronously; it emits 'error' (ECONNREFUSED) — hyper-svc's _probe
// depends on this.
const abstract = require('./index.js')
const NAME = 'bare-abstract-companion-proof'
let step = 0
function fail(msg) { console.log('FAIL: ' + msg); Bare.exit(1) }
const server = abstract.createServer({ allowHalfOpen: false }, (sock) => {
sock.on('data', (data) => sock.write(Buffer.concat([Buffer.from('echo:'), data])))
sock.on('error', () => {})
})
server.on('error', (err) => fail('server error: ' + err.message))
server.listen(NAME, () => {
// Test 1 — round-trip via the net-compat surface.
const socket = abstract.createConnection(NAME)
let connected = false
socket.on('connect', () => { connected = true })
socket.on('error', (err) => fail('client error: ' + err.message))
socket.on('data', (data) => {
if (data.toString() !== 'echo:hello') return fail('unexpected "' + data + '"')
if (!connected) return fail("'connect' event never fired")
console.log('PASS 1: net-compat round-trip (createServer/createConnection, connect event)')
socket.destroy()
runTest2()
})
socket.write(Buffer.from('hello'))
})
// Test 2 — connecting to a non-existent name emits 'error', never throws sync.
function runTest2() {
let threw = false
try {
const dead = abstract.createConnection('bare-abstract-does-not-exist-zzz')
dead.on('error', (err) => {
const code = err.code || err.message
console.log('PASS 2: absent name -> async error (' + code + '), no sync throw')
server.close(() => Bare.exit(0))
})
dead.on('connect', () => fail('connected to a non-existent socket'))
} catch (err) {
threw = true
fail('createConnection threw synchronously: ' + err.message)
}
if (threw) return
}
setTimeout(() => fail('TIMEOUT'), 4000)