|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { getTransfers, maxSplits, maxTransferCalls } from './charge.js' |
| 4 | + |
| 5 | +describe('constants', () => { |
| 6 | + test('maxSplits is 10', () => { |
| 7 | + expect(maxSplits).toBe(10) |
| 8 | + }) |
| 9 | + |
| 10 | + test('maxTransferCalls is 1 + maxSplits', () => { |
| 11 | + expect(maxTransferCalls).toBe(1 + maxSplits) |
| 12 | + }) |
| 13 | +}) |
| 14 | + |
| 15 | +describe('getTransfers', () => { |
| 16 | + test('returns single primary transfer when no splits', () => { |
| 17 | + const result = getTransfers({ |
| 18 | + amount: '1000000', |
| 19 | + methodDetails: { memo: '0xaabb' }, |
| 20 | + recipient: '0x1111111111111111111111111111111111111111', |
| 21 | + }) |
| 22 | + expect(result).toEqual([ |
| 23 | + { |
| 24 | + amount: '1000000', |
| 25 | + memo: '0xaabb', |
| 26 | + recipient: '0x1111111111111111111111111111111111111111', |
| 27 | + }, |
| 28 | + ]) |
| 29 | + }) |
| 30 | + |
| 31 | + test('returns primary + split transfers', () => { |
| 32 | + const result = getTransfers({ |
| 33 | + amount: '1000000', |
| 34 | + methodDetails: { |
| 35 | + memo: '0xaabb', |
| 36 | + splits: [ |
| 37 | + { amount: '200000', recipient: '0x2222222222222222222222222222222222222222' }, |
| 38 | + { amount: '100000', recipient: '0x3333333333333333333333333333333333333333' }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + recipient: '0x1111111111111111111111111111111111111111', |
| 42 | + }) |
| 43 | + expect(result).toEqual([ |
| 44 | + { |
| 45 | + amount: '700000', |
| 46 | + memo: '0xaabb', |
| 47 | + recipient: '0x1111111111111111111111111111111111111111', |
| 48 | + }, |
| 49 | + { |
| 50 | + amount: '200000', |
| 51 | + recipient: '0x2222222222222222222222222222222222222222', |
| 52 | + }, |
| 53 | + { |
| 54 | + amount: '100000', |
| 55 | + recipient: '0x3333333333333333333333333333333333333333', |
| 56 | + }, |
| 57 | + ]) |
| 58 | + }) |
| 59 | + |
| 60 | + test('preserves split memo', () => { |
| 61 | + const result = getTransfers({ |
| 62 | + amount: '1000000', |
| 63 | + methodDetails: { |
| 64 | + splits: [ |
| 65 | + { |
| 66 | + amount: '200000', |
| 67 | + memo: '0xdeadbeef', |
| 68 | + recipient: '0x2222222222222222222222222222222222222222', |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + recipient: '0x1111111111111111111111111111111111111111', |
| 73 | + }) |
| 74 | + expect(result[1]!.memo).toBe('0xdeadbeef') |
| 75 | + }) |
| 76 | + |
| 77 | + test('primary transfer has no memo when methodDetails.memo is undefined', () => { |
| 78 | + const result = getTransfers({ |
| 79 | + amount: '1000000', |
| 80 | + methodDetails: { |
| 81 | + splits: [{ amount: '200000', recipient: '0x2222222222222222222222222222222222222222' }], |
| 82 | + }, |
| 83 | + recipient: '0x1111111111111111111111111111111111111111', |
| 84 | + }) |
| 85 | + expect(result[0]!.memo).toBeUndefined() |
| 86 | + }) |
| 87 | + |
| 88 | + test('throws when split amount is zero', () => { |
| 89 | + expect(() => |
| 90 | + getTransfers({ |
| 91 | + amount: '1000000', |
| 92 | + methodDetails: { |
| 93 | + splits: [{ amount: '0', recipient: '0x2222222222222222222222222222222222222222' }], |
| 94 | + }, |
| 95 | + recipient: '0x1111111111111111111111111111111111111111', |
| 96 | + }), |
| 97 | + ).toThrow('each split amount must be positive') |
| 98 | + }) |
| 99 | + |
| 100 | + test('throws when split amount is negative', () => { |
| 101 | + expect(() => |
| 102 | + getTransfers({ |
| 103 | + amount: '1000000', |
| 104 | + methodDetails: { |
| 105 | + splits: [{ amount: '-100', recipient: '0x2222222222222222222222222222222222222222' }], |
| 106 | + }, |
| 107 | + recipient: '0x1111111111111111111111111111111111111111', |
| 108 | + }), |
| 109 | + ).toThrow('each split amount must be positive') |
| 110 | + }) |
| 111 | + |
| 112 | + test('throws when split total equals total amount', () => { |
| 113 | + expect(() => |
| 114 | + getTransfers({ |
| 115 | + amount: '1000000', |
| 116 | + methodDetails: { |
| 117 | + splits: [{ amount: '1000000', recipient: '0x2222222222222222222222222222222222222222' }], |
| 118 | + }, |
| 119 | + recipient: '0x1111111111111111111111111111111111111111', |
| 120 | + }), |
| 121 | + ).toThrow('split total must be less than total amount') |
| 122 | + }) |
| 123 | + |
| 124 | + test('throws when split total exceeds total amount', () => { |
| 125 | + expect(() => |
| 126 | + getTransfers({ |
| 127 | + amount: '1000000', |
| 128 | + methodDetails: { |
| 129 | + splits: [ |
| 130 | + { amount: '600000', recipient: '0x2222222222222222222222222222222222222222' }, |
| 131 | + { amount: '600000', recipient: '0x3333333333333333333333333333333333333333' }, |
| 132 | + ], |
| 133 | + }, |
| 134 | + recipient: '0x1111111111111111111111111111111111111111', |
| 135 | + }), |
| 136 | + ).toThrow('split total must be less than total amount') |
| 137 | + }) |
| 138 | + |
| 139 | + test('handles empty splits array same as no splits', () => { |
| 140 | + const result = getTransfers({ |
| 141 | + amount: '1000000', |
| 142 | + methodDetails: { splits: [] }, |
| 143 | + recipient: '0x1111111111111111111111111111111111111111', |
| 144 | + }) |
| 145 | + expect(result).toHaveLength(1) |
| 146 | + expect(result[0]!.amount).toBe('1000000') |
| 147 | + }) |
| 148 | + |
| 149 | + test('handles undefined methodDetails', () => { |
| 150 | + const result = getTransfers({ |
| 151 | + amount: '1000000', |
| 152 | + recipient: '0x1111111111111111111111111111111111111111', |
| 153 | + }) |
| 154 | + expect(result).toHaveLength(1) |
| 155 | + expect(result[0]!.amount).toBe('1000000') |
| 156 | + }) |
| 157 | +}) |
0 commit comments