|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { |
| 3 | + buildCreateBillTx, |
| 4 | + buildPayBillTx, |
| 5 | + buildCancelBillTx, |
| 6 | +} from './bill-payments' |
| 7 | +import * as StellarSdk from '@stellar/stellar-sdk' |
| 8 | + |
| 9 | +vi.spyOn(StellarSdk.Horizon.Server.prototype, 'loadAccount').mockImplementation(async (accountId: string) => { |
| 10 | + if (accountId.startsWith('G')) { |
| 11 | + return { sequence: '123' } as any |
| 12 | + } |
| 13 | + throw new Error('invalid-account') |
| 14 | +}) |
| 15 | + |
| 16 | +describe('bill-payments helper', () => { |
| 17 | + let validPublicKey: string |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + vi.clearAllMocks() |
| 21 | + validPublicKey = StellarSdk.Keypair.random().publicKey() |
| 22 | + }) |
| 23 | + |
| 24 | + describe('buildCreateBillTx', () => { |
| 25 | + it('returns a valid XDR for a one-time bill', async () => { |
| 26 | + const owner = validPublicKey |
| 27 | + const name = 'Electric Bill' |
| 28 | + const amount = 50 |
| 29 | + const dueDate = new Date(Date.now() + 86400000).toISOString() // tomorrow |
| 30 | + const recurring = false |
| 31 | + |
| 32 | + const xdr = await buildCreateBillTx(owner, name, amount, dueDate, recurring) |
| 33 | + expect(typeof xdr).toBe('string') |
| 34 | + expect(xdr.length).toBeGreaterThan(0) |
| 35 | + |
| 36 | + const tx = new StellarSdk.Transaction(xdr, StellarSdk.Networks.TESTNET) |
| 37 | + expect(tx.operations).toHaveLength(4) |
| 38 | + }) |
| 39 | + |
| 40 | + it('returns a valid XDR for a recurring bill', async () => { |
| 41 | + const owner = validPublicKey |
| 42 | + const name = 'Internet Bill' |
| 43 | + const amount = 80 |
| 44 | + const dueDate = new Date(Date.now() + 86400000).toISOString() |
| 45 | + const recurring = true |
| 46 | + const frequencyDays = 30 |
| 47 | + |
| 48 | + const xdr = await buildCreateBillTx(owner, name, amount, dueDate, recurring, frequencyDays) |
| 49 | + expect(typeof xdr).toBe('string') |
| 50 | + |
| 51 | + const tx = new StellarSdk.Transaction(xdr, StellarSdk.Networks.TESTNET) |
| 52 | + expect(tx.operations).toHaveLength(5) |
| 53 | + }) |
| 54 | + |
| 55 | + it('throws error for invalid owner public key', async () => { |
| 56 | + await expect( |
| 57 | + buildCreateBillTx('invalid-key', 'Bill', 50, new Date().toISOString(), false) |
| 58 | + ).rejects.toThrow('invalid-owner') |
| 59 | + }) |
| 60 | + |
| 61 | + it('throws error for invalid amount', async () => { |
| 62 | + await expect( |
| 63 | + buildCreateBillTx(validPublicKey, 'Bill', -10, new Date().toISOString(), false) |
| 64 | + ).rejects.toThrow('invalid-amount') |
| 65 | + }) |
| 66 | + |
| 67 | + it('throws error for invalid frequency', async () => { |
| 68 | + await expect( |
| 69 | + buildCreateBillTx(validPublicKey, 'Bill', 50, new Date().toISOString(), true, -5) |
| 70 | + ).rejects.toThrow('invalid-frequency') |
| 71 | + }) |
| 72 | + |
| 73 | + it('throws error for invalid due date', async () => { |
| 74 | + await expect( |
| 75 | + buildCreateBillTx(validPublicKey, 'Bill', 50, 'not-a-date', false) |
| 76 | + ).rejects.toThrow('invalid-dueDate') |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + describe('buildPayBillTx', () => { |
| 81 | + it('returns a valid XDR for paying a bill', async () => { |
| 82 | + const xdr = await buildPayBillTx(validPublicKey, 'bill-123') |
| 83 | + expect(typeof xdr).toBe('string') |
| 84 | + const tx = new StellarSdk.Transaction(xdr, StellarSdk.Networks.TESTNET) |
| 85 | + expect(tx.operations).toHaveLength(1) |
| 86 | + }) |
| 87 | + |
| 88 | + it('throws error for invalid caller', async () => { |
| 89 | + await expect(buildPayBillTx('invalid', 'bill-123')).rejects.toThrow('invalid-caller') |
| 90 | + }) |
| 91 | + |
| 92 | + it('throws error for missing billId', async () => { |
| 93 | + await expect(buildPayBillTx(validPublicKey, '')).rejects.toThrow('invalid-billId') |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe('buildCancelBillTx', () => { |
| 98 | + it('returns a valid XDR for canceling a bill', async () => { |
| 99 | + const xdr = await buildCancelBillTx(validPublicKey, 'bill-456') |
| 100 | + expect(typeof xdr).toBe('string') |
| 101 | + const tx = new StellarSdk.Transaction(xdr, StellarSdk.Networks.TESTNET) |
| 102 | + expect(tx.operations).toHaveLength(1) |
| 103 | + }) |
| 104 | + |
| 105 | + it('throws error for invalid caller', async () => { |
| 106 | + await expect(buildCancelBillTx('invalid', 'bill-123')).rejects.toThrow('invalid-caller') |
| 107 | + }) |
| 108 | + |
| 109 | + it('throws error for missing billId', async () => { |
| 110 | + await expect(buildCancelBillTx(validPublicKey, '')).rejects.toThrow('invalid-billId') |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments