forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment-validation.test.ts
More file actions
113 lines (97 loc) · 3.27 KB
/
Copy pathpayment-validation.test.ts
File metadata and controls
113 lines (97 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { describe, expect, it } from 'vitest';
import { LilyValidationError } from '../src/errors/sdk-error';
import {
validateExecutePaymentRequest,
validateMemo,
validateMoneyAmount,
} from '../src/validation';
describe('validateMoneyAmount Stellar constraints', () => {
it('accepts amount with exactly 7 fractional digits', () => {
expect(() =>
validateMoneyAmount({ assetCode: 'USDC', amount: '10.1234567' }, 'test'),
).not.toThrow();
});
it('rejects amount with 8 fractional digits', () => {
expect(() =>
validateMoneyAmount({ assetCode: 'USDC', amount: '10.12345678' }, 'test'),
).toThrow(/at most 7 fractional digits/);
});
it('accepts zero amount', () => {
expect(() =>
validateMoneyAmount({ assetCode: 'XLM', amount: '0' }, 'test'),
).not.toThrow();
});
it('accepts integer amount without decimals', () => {
expect(() =>
validateMoneyAmount({ assetCode: 'XLM', amount: '100' }, 'test'),
).not.toThrow();
});
it('rejects negative amount', () => {
expect(() =>
validateMoneyAmount({ assetCode: 'USDC', amount: '-5' }, 'test'),
).toThrow(/non-negative decimal/);
});
it('rejects scientific notation', () => {
expect(() =>
validateMoneyAmount({ assetCode: 'USDC', amount: '1e3' }, 'test'),
).toThrow(/non-negative decimal/);
});
});
describe('validateMemo Stellar constraints', () => {
it('accepts memo within 28 byte limit', () => {
expect(() => validateMemo('short memo', 'test')).not.toThrow();
});
it('accepts exactly 28 character text memo', () => {
expect(() => validateMemo('a'.repeat(28), 'test')).not.toThrow();
});
it('rejects text memo exceeding 28 bytes', () => {
expect(() => validateMemo('a'.repeat(29), 'test')).toThrow(
/at most 28 bytes/,
);
});
it('accepts hex memo within 64 character limit', () => {
expect(() => validateMemo('ab'.repeat(32), 'test')).not.toThrow();
});
it('rejects hex memo exceeding 64 characters', () => {
expect(() => validateMemo('ab'.repeat(33), 'test')).toThrow(
/at most 64 characters/,
);
});
it('accepts undefined memo', () => {
expect(() => validateMemo(undefined, 'test')).not.toThrow();
});
it('rejects non-string memo', () => {
expect(() => validateMemo(123 as any, 'test')).toThrow(/must be a string/);
});
});
describe('validateExecutePaymentRequest with memo and MoneyAmount', () => {
it('accepts valid payment request with memo', () => {
expect(() =>
validateExecutePaymentRequest({
fromWalletId: 'wallet-1',
toAddress: 'GABC...',
amount: { assetCode: 'USDC', amount: '10.50' },
memo: 'payment ref',
}),
).not.toThrow();
});
it('rejects payment with over-long memo', () => {
expect(() =>
validateExecutePaymentRequest({
fromWalletId: 'wallet-1',
toAddress: 'GABC...',
amount: { assetCode: 'USDC', amount: '10.50' },
memo: 'x'.repeat(29),
}),
).toThrow(/memo/);
});
it('rejects payment with invalid MoneyAmount fractionals', () => {
expect(() =>
validateExecutePaymentRequest({
fromWalletId: 'wallet-1',
toAddress: 'GABC...',
amount: { assetCode: 'USDC', amount: '10.12345678' },
}),
).toThrow(/fractional digits/);
});
});