Files
modules/measurement-rate-control/hyper-p2p-sla-budget/test/test.js
T
Raven ScottandCursor 2e7063eb53 feat(messaging-gossip,measurement): manually deepen category APIs and tests
- gossip-mesh: pruneSeen, listRecent, setDefaults, publish stats, peerCount
- dedup-filter: addBatch, mergeRemote, snapshot, fillRatio, remove
- percentile-sketch: addBatch, merge, range, median, gossip report path
- sla-budget: transfer, setCap, usageRatio, allSnapshots, refill
- Expanded brittle tests and category README highlights

Co-authored-by: Cursor <[email protected]>
2026-05-21 02:14:41 -04:00

59 lines
1.4 KiB
JavaScript

require('bare-process/global')
const test = require('brittle')
const { HyperP2PSlaBudget, PROTOCOL } = require('../index.js')
test('exports', (t) => {
t.ok(HyperP2PSlaBudget)
t.is(PROTOCOL, 'sla-budget/v1')
})
test('allocate consume remaining', async (t) => {
const m = new HyperP2PSlaBudget()
m.allocate('api', 10)
t.ok(m.consume('api', 3))
t.is(m.remaining('api'), 7)
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PSlaBudget()
try { m.allocate('', 1) } catch (e) { t.ok(e) }
await m.close()
})
test('reject over consume', async (t) => {
const m = new HyperP2PSlaBudget()
m.allocate('x', 1)
t.not(m.consume('x', 5))
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PSlaBudget()
m.allocate('s', 2)
t.is(m.getStats().protocol, 'sla-budget/v1')
t.is(m.getStats().allocated, 1)
await m.close()
})
test('transfer and snapshots', async (t) => {
const m = new HyperP2PSlaBudget()
m.allocate('a', 10)
t.ok(m.transfer('a', 'b', 4))
t.is(m.remaining('a'), 6)
t.is(m.remaining('b'), 4)
t.ok(m.usageRatio('a') > 0)
t.is(m.allSnapshots().length, 2)
await m.close()
})
test('canConsume totalRemaining', async (t) => {
const m = new HyperP2PSlaBudget()
m.allocate('a', 5)
m.allocate('b', 3)
t.ok(m.canConsume('a', 4))
t.not(m.canConsume('a', 6))
t.is(m.totalRemaining(), 8)
await m.close()
})