// bun test names/withdraw.test.js import { test, expect } from 'bun:test'; import { withdrawService } from './withdraw.js'; const OUR = '02' + 'aa'.repeat(32), THEIRS = '03' + 'bb'.repeat(32); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); function rig({ payBehaviour = 'ok', circularCap = 1e9 } = {}) { const state = {}; const invoices = {}; // our invoices: hash -> { status } const pays = {}; // outbound: hash -> status const fakes = {}; // bolt11 string -> decoded let n = 0; const ln = { async call(m, p) { if (m === 'invoice') { const hash = 'f' + (++n); invoices[hash] = { status: 'unpaid' }; return { bolt11: 'lnfund' + hash, payment_hash: hash }; } if (m === 'listinvoices') return { invoices: invoices[p.payment_hash] ? [invoices[p.payment_hash]] : [] }; if (m === 'decode') { const d = fakes[p.string]; if (!d) throw new Error('bad'); return d; } if (m === 'pay') { const d = fakes[p.bolt11]; if (payBehaviour === 'fail') { pays[d.payment_hash] = 'failed'; throw new Error('no route'); } if (payBehaviour === 'hang') { pays[d.payment_hash] = 'pending'; throw new Error('timed out'); } pays[d.payment_hash] = 'complete'; return { status: 'complete', amount_msat: d.amount_msat, amount_sent_msat: d.amount_msat + 3000 }; } if (m === 'waitsendpay') { pays[p.payment_hash] = 'complete'; return { status: 'complete' }; } if (m === 'listsendpays') return { payments: pays[p.payment_hash] ? [{ status: pays[p.payment_hash] }] : [] }; throw new Error('unexpected ' + m); }, }; let circular = 0; const svc = withdrawService({ state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR, circularPick: async () => ({ cap: circularCap }), payCircular: async () => { circular++; return { feeMsat: 1500 }; }, publicBase: 'https://names.test', }); const invoice = (sat, payee = THEIRS, extra = {}) => { const hash = 'c' + (++n); const s = 'lnbc' + hash; fakes[s] = { valid: true, payment_hash: hash, amount_msat: sat * 1000, payee, created_at: Math.floor(Date.now() / 1000), expiry: 3600, ...extra }; return s; }; return { svc, state, invoices, pays, invoice, circular: () => circular }; } async function funded(r, opts) { const c = await r.svc.create({ pubkey: 'me', ...opts }); expect(r.svc.params(c.id).status).toBe('ERROR'); // unfunded is invisible r.invoices[c.paymentHash].status = 'paid'; expect(r.svc.onSettled(c.paymentHash)).toBe(true); return c; } test('a funded link pays a claim, then is used up', async () => { const r = rig(); const c = await funded(r, { sat: 1000 }); const p = r.svc.params(c.id); expect(p.tag).toBe('withdrawRequest'); expect(p.maxWithdrawable).toBe(1_000_000); expect(p.callback).toBe(`https://names.test/w/${c.id}/cb`); expect(await r.svc.callback(c.id, 'nope', r.invoice(1000))).toMatchObject({ status: 'ERROR' }); expect(await r.svc.callback(c.id, p.k1, r.invoice(1001))).toMatchObject({ status: 'ERROR' }); expect(await r.svc.callback(c.id, p.k1, r.invoice(1000))).toEqual({ status: 'OK' }); await sleep(5); const v = r.svc.mine('me')[0]; expect(v.remainingSat).toBe(0); expect(v.claims[0].status).toBe('paid'); expect(r.svc.params(c.id).status).toBe('ERROR'); expect(await r.svc.callback(c.id, p.k1, r.invoice(1))).toMatchObject({ status: 'ERROR' }); }); test('multi-use: each claim up to the per-claim amount, no invoice paid twice', async () => { const r = rig(); const c = await funded(r, { sat: 500, uses: 3 }); const k1 = r.svc.params(c.id).k1; const inv = r.invoice(500); expect((await r.svc.callback(c.id, k1, inv)).status).toBe('OK'); expect((await r.svc.callback(c.id, k1, inv)).status).toBe('ERROR'); expect((await r.svc.callback(c.id, k1, r.invoice(200))).status).toBe('OK'); await sleep(5); const v = r.svc.mine('me')[0]; expect(v.used).toBe(2); expect(v.remainingSat).toBe(800); expect(r.svc.params(c.id).maxWithdrawable).toBe(500_000); }); test('an invoice on our own node goes round the circular route', async () => { const r = rig(); const c = await funded(r, { sat: 1000 }); expect((await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000, OUR))).status).toBe('OK'); await sleep(5); expect(r.circular()).toBe(1); expect(r.svc.mine('me')[0].claims[0]).toMatchObject({ status: 'paid' }); }); test('a failed payout goes back on the link', async () => { const r = rig({ payBehaviour: 'fail' }); const c = await funded(r, { sat: 1000 }); expect((await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000))).status).toBe('OK'); await sleep(5); const v = r.svc.mine('me')[0]; expect(v.remainingSat).toBe(1000); expect(v.used).toBe(0); expect(r.svc.params(c.id).tag).toBe('withdrawRequest'); }); test('an unresolved payout stays reserved', async () => { const r = rig({ payBehaviour: 'hang' }); const c = await funded(r, { sat: 1000 }); await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000)); await sleep(5); expect(r.svc.mine('me')[0]).toMatchObject({ remainingSat: 0 }); }); test('expired and amountless invoices are refused', async () => { const r = rig(); const c = await funded(r, { sat: 1000 }); const k1 = r.svc.params(c.id).k1; expect((await r.svc.callback(c.id, k1, r.invoice(100, THEIRS, { created_at: 1000, expiry: 60 }))).status).toBe('ERROR'); expect((await r.svc.callback(c.id, k1, r.invoice(0))).status).toBe('ERROR'); }); test('reclaim pays the rest to the creator and closes the link', async () => { const r = rig(); const c = await funded(r, { sat: 400, uses: 2 }); const k1 = r.svc.params(c.id).k1; await r.svc.callback(c.id, k1, r.invoice(400)); await sleep(5); await expect(r.svc.reclaim(c.id, 'someone-else', r.invoice(400, OUR))).rejects.toThrow('not your link'); const out = await r.svc.reclaim(c.id, 'me', r.invoice(400, OUR)); expect(out).toMatchObject({ status: 'closed', remainingSat: 0, paid: true }); expect(r.svc.params(c.id).status).toBe('ERROR'); }); test('confirmFunded checks the invoice', async () => { const r = rig(); const c = await r.svc.create({ pubkey: 'me', sat: 10 }); await expect(r.svc.confirmFunded(c.id, 'me')).rejects.toThrow('not paid'); r.invoices[c.paymentHash].status = 'paid'; expect((await r.svc.confirmFunded(c.id, 'me')).status).toBe('open'); expect(r.svc.onSettled(c.paymentHash)).toBe(false); // the settle loop arriving later is a no-op });