diff options
Diffstat (limited to 'names/withdraw.test.js')
| -rw-r--r-- | names/withdraw.test.js | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/names/withdraw.test.js b/names/withdraw.test.js index 80da774..ee856f1 100644 --- a/names/withdraw.test.js +++ b/names/withdraw.test.js @@ -1,6 +1,7 @@ // bun test names/withdraw.test.js import { test, expect } from 'bun:test'; -import { withdrawService } from './withdraw.js'; +import { withdrawService, RETRY_DELAYS } from './withdraw.js'; +const RETRIES = RETRY_DELAYS.length; const OUR = '02' + 'aa'.repeat(32), THEIRS = '03' + 'bb'.repeat(32); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); @@ -32,6 +33,7 @@ function rig({ payBehaviour = 'ok', circularCap = 1e9 } = {}) { const svc = withdrawService({ state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR, circularPick: async () => ({ cap: circularCap }), payCircular: async () => { circular++; return { feeMsat: 1500 }; }, + wait: async () => {}, publicBase: 'https://names.test', }); const invoice = (sat, payee = THEIRS, extra = {}) => { @@ -100,6 +102,7 @@ test('a failed payout goes back on the link', async () => { const v = r.svc.mine('me')[0]; expect(v.remainingSat).toBe(1000); expect(v.used).toBe(0); + expect(v.claims[0]).toMatchObject({ status: 'failed', tries: RETRIES + 1 }); expect(r.svc.params(c.id).tag).toBe('withdrawRequest'); }); @@ -139,3 +142,47 @@ test('confirmFunded checks the invoice', async () => { 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 }); + +test('a claim with no route right now is tried again, and pays when one turns up', async () => { + const waits = []; + const state = {}; const pays = {}; const fakes = {}; let tries = 0; + const ln = { + async call(m, p) { + if (m === 'invoice') return { bolt11: 'lnfundx', payment_hash: 'fx' }; + if (m === 'decode') return fakes[p.string]; + if (m === 'pay') { tries++; const d = fakes[p.bolt11]; if (tries < 3) { pays[d.payment_hash] = 'failed'; throw new Error('could not find a usable set of paths'); } pays[d.payment_hash] = 'complete'; return { status: 'complete', amount_msat: d.amount_msat, amount_sent_msat: d.amount_msat }; } + if (m === 'listsendpays') return { payments: pays[p.payment_hash] ? [{ status: pays[p.payment_hash] }] : [] }; + throw new Error('unexpected ' + m); + }, + }; + const svc = withdrawService({ state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR, publicBase: 'https://names.test', wait: async (ms) => { waits.push(ms); } }); + const made = await svc.create({ pubkey: 'me', sat: 1000 }); + svc.onSettled('fx'); + fakes.lnbcz = { valid: true, payment_hash: 'cz', amount_msat: 1_000_000, payee: THEIRS, created_at: Math.floor(Date.now() / 1000), expiry: 3600 }; + expect((await svc.callback(made.id, svc.params(made.id).k1, 'lnbcz')).status).toBe('OK'); + await sleep(10); + expect(tries).toBe(3); + expect(waits.length).toBe(2); + expect(svc.mine('me')[0]).toMatchObject({ remainingSat: 0, used: 1 }); + expect(svc.mine('me')[0].claims[0]).toMatchObject({ status: 'paid', tries: 3 }); +}); + +test('retries stop short of the invoice expiring, then the sats go back', async () => { + const state = {}; const pays = {}; let tries = 0; + const ln = { + async call(m, p) { + if (m === 'invoice') return { bolt11: 'lnfundy', payment_hash: 'fy' }; + if (m === 'decode') return { valid: true, payment_hash: 'cy', amount_msat: 500_000, payee: THEIRS, created_at: Math.floor(Date.now() / 1000), expiry: 120 }; + if (m === 'pay') { tries++; pays.cy = 'failed'; throw new Error('no route'); } + if (m === 'listsendpays') return { payments: pays[p.payment_hash] ? [{ status: pays[p.payment_hash] }] : [] }; + throw new Error('unexpected ' + m); + }, + }; + const svc = withdrawService({ state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR, publicBase: 'https://names.test', wait: async () => {} }); + const made = await svc.create({ pubkey: 'me', sat: 500 }); + svc.onSettled('fy'); + await svc.callback(made.id, svc.params(made.id).k1, 'lnbcy'); + await sleep(10); + expect(tries).toBe(2); // 30 s + a minute's margin fits in 120 s, 90 s more doesn't + expect(svc.mine('me')[0]).toMatchObject({ remainingSat: 500, used: 0 }); +}); |
