require('bare-process/global') const test = require('brittle') const { HyperP2PCreditLedger, PROTOCOL } = require('../index.js') test('exports', (t) => { t.ok(HyperP2PCreditLedger) t.is(PROTOCOL, 'credit-ledger/v1') }) test('credit debit transfer', async (t) => { const m = new HyperP2PCreditLedger() m.openAccount('a', 100) m.openAccount('b', 0) m.transfer('a', 'b', 30) t.is(m.balance('a'), 70) t.is(m.balance('b'), 30) await m.close() }) test('insufficient funds', async (t) => { const m = new HyperP2PCreditLedger() m.openAccount('a', 5) try { m.transfer('a', 'b', 10) } catch (e) { t.ok(e) } await m.close() }) test('remote ledger entry', async (t) => { const m = new HyperP2PCreditLedger() m.openAccount('x', 0) m._onGossip({ type: 'ledger-entry', accountId: 'x', kind: 'credit', balance: 50, at: Date.now() }) t.is(m.balance('x'), 50) await m.close() }) test('getStats', async (t) => { const m = new HyperP2PCreditLedger() m.openAccount('z') t.ok(m.getStats().accounts >= 1) await m.close() }) test('totalSupply and listAccounts', async (t) => { const m = new HyperP2PCreditLedger() m.openAccount('a', 10) m.openAccount('b', 20) t.is(m.totalSupply(), 30) t.ok(m.hasAccount('a')) t.alike(m.listAccounts().sort(), ['a', 'b']) await m.close() }) test('topBalances accountSnapshot', async (t) => { const m = new HyperP2PCreditLedger() m.openAccount('a', 5) m.openAccount('b', 50) t.is(m.topBalances(1)[0].id, 'b') t.is(m.accountSnapshot('a').balance, 5) await m.close() })