67 lines
4.6 KiB
JavaScript
67 lines
4.6 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { contentPaymentRpc } from './content-payments.js'
|
|
|
|
function visitor() {
|
|
let time = 1_800_000_000_000
|
|
const context = { state: {}, wallet: { ecash_sats: 10000, channel_sats: 10000, onchain_sats: 10000 },
|
|
entry: { id: 'film', filename: 'film.mp4', mime_type: 'video/mp4', size_bytes: 123, access: { paid: { price_sats: 1000 } } }, owned: [], now: () => time }
|
|
const call = (method, p = {}) => contentPaymentRpc('content.' + method, { onion: 'peer.onion', content_id: 'film', ...p }, context)
|
|
return { context, call, advance: () => { time += 2500 } }
|
|
}
|
|
test('fresh purchase lookups return explicit empty states, isolated per visitor', () => {
|
|
const a=visitor(), b=visitor()
|
|
assert.deepEqual(a.call('payment-status'), { attempts: [] })
|
|
assert.deepEqual(a.call('invoice-attempt'), { attempt: null })
|
|
assert.deepEqual(a.call('onchain-attempt'), { attempt: null })
|
|
a.call('purchase')
|
|
assert.deepEqual(b.call('payment-status'), { attempts: [] })
|
|
})
|
|
test('Cashu requires matching consent and debits once across retries and reopen', () => {
|
|
const {call,context}=visitor();const q=call('purchase')
|
|
assert.equal(q.state,'confirmation_required');assert.equal(context.wallet.ecash_sats,10000)
|
|
assert.throws(()=>call('purchase',{consent:{...q,envelope_sha256:'wrong'},max_wallet_debit:1000}))
|
|
const args={consent:{operation_id:q.operation_id,envelope_sha256:q.envelope_sha256,wallet_debit_sats:q.wallet_debit_sats},max_wallet_debit:1000}
|
|
assert.equal(call('purchase',args).owned,true);assert.equal(call('purchase',args).owned,true)
|
|
assert.equal(context.wallet.ecash_sats,9000);assert.equal(context.owned.length,1)
|
|
})
|
|
test('cancelled Cashu quotes can be replaced without a debit',()=>{
|
|
const {call,context}=visitor();const q=call('purchase')
|
|
const cancelled=contentPaymentRpc('content.cancel-purchase',{onion:'peer.onion',operation_id:q.operation_id},{...context,entry:undefined})
|
|
assert.equal(cancelled.state,'cancelled_unspent')
|
|
assert.notEqual(call('purchase').operation_id,q.operation_id);assert.equal(context.wallet.ecash_sats,10000)
|
|
})
|
|
test('native Lightning settles once and delivery/recovery preserve the same invoice',()=>{
|
|
const {call,context}=visitor();const q=call('invoice-create',{external_exposure:false});const p={operation_id:q.operation_id}
|
|
assert.throws(()=>call('invoice-download',p));assert.equal(call('invoice-pay',p).status,'succeeded');call('invoice-pay',p)
|
|
assert.equal(call('invoice-recover',p).paid,true);assert.equal(call('invoice-download',p).owned,true)
|
|
assert.equal(context.wallet.channel_sats,9000);assert.equal(context.owned.length,1)
|
|
})
|
|
test('external Lightning auto-settles without taking sats from the local wallet',()=>{
|
|
const {call,context,advance}=visitor();const q=call('invoice-create',{external_exposure:true})
|
|
assert.equal(call('invoice-recover',{operation_id:q.operation_id}).paid,false);advance()
|
|
assert.equal(call('invoice-recover',{operation_id:q.operation_id}).paid,true)
|
|
call('invoice-download',{operation_id:q.operation_id});assert.equal(context.wallet.channel_sats,10000)
|
|
})
|
|
test('cancelled external invoices never settle after their timer',()=>{
|
|
const {call,advance}=visitor();const q=call('invoice-create',{external_exposure:true})
|
|
const canceled=call('invoice-cancel',{operation_id:q.operation_id})
|
|
assert.equal(canceled.state,'canceled');assert.equal(canceled.can_switch_method,true)
|
|
advance()
|
|
assert.equal(call('invoice-recover',{operation_id:q.operation_id}).paid,false)
|
|
assert.equal(call('invoice-attempt').attempt,null)
|
|
})
|
|
test('on-chain review, confirmation, retries and download account for a single fee',()=>{
|
|
const {call,context}=visitor();const q=call('onchain-create');const p={operation_id:q.operation_id}
|
|
const plan=call('onchain-prepare',{...p,max_fee_sats:100});assert.equal(context.wallet.onchain_sats,10000)
|
|
assert.throws(()=>call('onchain-pay',{...p,plan_sha256:'wrong'}))
|
|
call('onchain-pay',{...p,plan_sha256:plan.plan_sha256});call('onchain-pay',{...p,plan_sha256:plan.plan_sha256})
|
|
assert.equal(call('onchain-download',p).owned,true);assert.equal(context.wallet.onchain_sats,8975)
|
|
})
|
|
test('external Bitcoin settles and unexposed cancellation permits another method',()=>{
|
|
const {call,context,advance}=visitor();const q=call('onchain-create');call('onchain-cancel',{operation_id:q.operation_id})
|
|
assert.equal(call('onchain-attempt').attempt,null)
|
|
const next=call('onchain-expose');advance();assert.equal(call('onchain-recover',{operation_id:next.operation_id}).paid,true)
|
|
assert.equal(call('onchain-download',{operation_id:next.operation_id}).owned,true);assert.equal(context.wallet.onchain_sats,10000)
|
|
})
|