This commit is contained in:
Raven Scott
2026-05-20 22:28:59 -04:00
parent 341542f41b
commit e4400872c0
82 changed files with 2533 additions and 775 deletions
@@ -4,23 +4,50 @@ const { HyperP2PStreamMultiplex, PROTOCOL } = require('../index.js')
test('exports', (t) => {
t.ok(HyperP2PStreamMultiplex)
t.ok(PROTOCOL)
t.is(PROTOCOL, 'stream-multiplex/v1')
})
test('basic operation', async (t) => {
const m = new HyperP2PStreamMultiplex()
m.write('hi'); t.ok(m.read())
await m.close()
test('openStream write read via ondata', async (t) => {
const mux = new HyperP2PStreamMultiplex()
const a = mux.openStream('a')
const chunks = []
a.ondata((c, fin) => { if (c) chunks.push(c); if (fin) t.pass() })
a.write(b4aFrom('hi'))
a.end()
t.is(chunks.length, 1)
await mux.close()
})
test('validation', async (t) => {
const m = new HyperP2PStreamMultiplex()
try { m.write(null) } catch (e) { t.ok(e) }
await m.close()
test('receiveFrame remote', async (t) => {
const mux = new HyperP2PStreamMultiplex()
const b = mux.openStream('b')
let got = false
b.ondata((c) => { if (c) got = true })
mux.receiveFrame({ type: 'frame', streamId: 'b', chunk: b4aFrom('x'), fin: false })
mux.receiveFrame({ type: 'frame', streamId: 'b', chunk: null, fin: true })
t.ok(got)
await mux.close()
})
function b4aFrom (s) {
return require('b4a').from(s)
}
test('validation duplicate stream id', async (t) => {
const mux = new HyperP2PStreamMultiplex()
mux.openStream('dup')
try {
mux.openStream('dup')
t.fail('expected throw')
} catch (e) {
t.ok(e instanceof Error)
}
await mux.close()
})
test('getStats', async (t) => {
const m = new HyperP2PStreamMultiplex()
t.ok(m.getStats().protocol)
await m.close()
const mux = new HyperP2PStreamMultiplex()
mux.openStream()
t.ok(mux.getStats().protocol)
await mux.close()
})