forked from wraith-protocol/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchSend.test.ts
More file actions
368 lines (314 loc) · 12.5 KB
/
Copy pathbatchSend.test.ts
File metadata and controls
368 lines (314 loc) · 12.5 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
parseCsvRows,
serializeRowsToCsv,
validateRow,
validateRows,
MAX_BATCH_ROWS,
} from './batchSend';
import type { BatchRow } from './batchSend';
// ---------------------------------------------------------------------------
// Mock @wraith-protocol/sdk so tests run without network or key material
// ---------------------------------------------------------------------------
vi.mock('@wraith-protocol/sdk/chains/stellar', () => ({
decodeStealthMetaAddress: (addr: string) => {
if (!addr.startsWith('st:xlm:')) throw new Error('bad meta-address');
// Return fake key material — 32-byte arrays filled with a deterministic value
const seed = addr.charCodeAt(7) % 256;
return {
spendingPubKey: new Uint8Array(32).fill(seed),
viewingPubKey: new Uint8Array(32).fill((seed + 1) % 256),
};
},
generateStealthAddress: (_spendPub: Uint8Array, _viewPub: Uint8Array) => ({
stealthAddress: 'GSTEALTH123FAKEADDRESS',
ephemeralPubKey: new Uint8Array(32),
viewTag: 42,
}),
}));
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const VALID_META = 'st:xlm:AAAA_FAKE_META_ADDRESS';
const VALID_AMOUNT = '10';
function makeRow(overrides: Partial<BatchRow> = {}): BatchRow {
return {
index: 1,
metaAddress: VALID_META,
amountRaw: VALID_AMOUNT,
memo: '',
error: '',
status: 'idle',
...overrides,
};
}
// ---------------------------------------------------------------------------
// parseCsvRows
// ---------------------------------------------------------------------------
describe('parseCsvRows', () => {
it('returns empty array for empty input', () => {
expect(parseCsvRows('')).toHaveLength(0);
expect(parseCsvRows(' ')).toHaveLength(0);
expect(parseCsvRows('\n\n\n')).toHaveLength(0);
});
it('parses a single data row', () => {
const rows = parseCsvRows('st:xlm:AAA,10');
expect(rows).toHaveLength(1);
expect(rows[0].metaAddress).toBe('st:xlm:AAA');
expect(rows[0].amountRaw).toBe('10');
expect(rows[0].memo).toBe('');
});
it('parses three columns (memo present)', () => {
const rows = parseCsvRows('st:xlm:AAA,5.5,my-memo');
expect(rows).toHaveLength(1);
expect(rows[0].memo).toBe('my-memo');
});
it('skips blank lines', () => {
const csv = `st:xlm:AAA,1\n\nst:xlm:BBB,2\n`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(2);
});
it('skips comment lines starting with #', () => {
const csv = `# header comment\nst:xlm:AAA,1\n# another comment\nst:xlm:BBB,2`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(2);
});
it('skips header row (meta_address,amount)', () => {
const csv = `meta_address,amount\nst:xlm:AAA,1`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(1);
expect(rows[0].metaAddress).toBe('st:xlm:AAA');
});
it('skips header row case-insensitively (META_ADDRESS)', () => {
const csv = `META_ADDRESS,AMOUNT\nst:xlm:AAA,1`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(1);
});
it('skips header row with "recipient" keyword', () => {
const csv = `recipient,amount,memo\nst:xlm:AAA,1,test`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(1);
});
it('assigns sequential 1-based indexes', () => {
const csv = `st:xlm:AAA,1\nst:xlm:BBB,2\nst:xlm:CCC,3`;
const rows = parseCsvRows(csv);
expect(rows.map((r) => r.index)).toEqual([1, 2, 3]);
});
it('handles CRLF line endings', () => {
const csv = `st:xlm:AAA,1\r\nst:xlm:BBB,2\r\n`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(2);
});
it('handles quoted fields with commas inside', () => {
const csv = `"st:xlm:AAA,extra",10,memo`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(1);
// The quoted field becomes the meta-address including the inner comma
expect(rows[0].metaAddress).toBe('st:xlm:AAA,extra');
});
it('handles double-quote escape inside quoted field', () => {
const csv = `"st:xlm:AAA""B",5`;
const rows = parseCsvRows(csv);
expect(rows[0].metaAddress).toBe('st:xlm:AAA"B');
});
it('trims whitespace around fields', () => {
const csv = ` st:xlm:AAA , 10 , memo `;
const rows = parseCsvRows(csv);
expect(rows[0].metaAddress).toBe('st:xlm:AAA');
expect(rows[0].amountRaw).toBe('10');
expect(rows[0].memo).toBe('memo');
});
it('handles rows with only a meta-address (no amount)', () => {
const csv = `st:xlm:AAA`;
const rows = parseCsvRows(csv);
expect(rows).toHaveLength(1);
expect(rows[0].amountRaw).toBe('');
});
it('parses 20 rows correctly (benchmark size)', () => {
const lines = Array.from({ length: 20 }, (_, i) => `st:xlm:ADDR${i},${i + 1}`);
const rows = parseCsvRows(lines.join('\n'));
expect(rows).toHaveLength(20);
expect(rows[0].amountRaw).toBe('1');
expect(rows[19].amountRaw).toBe('20');
});
});
// ---------------------------------------------------------------------------
// validateRow
// ---------------------------------------------------------------------------
describe('validateRow', () => {
it('marks a fully valid row as valid', () => {
const result = validateRow(makeRow(), 'XLM');
expect(result.status).toBe('valid');
expect(result.error).toBe('');
expect(result.stealthAddress).toBe('GSTEALTH123FAKEADDRESS');
});
it('marks empty meta-address as invalid', () => {
const result = validateRow(makeRow({ metaAddress: '' }), 'XLM');
expect(result.status).toBe('invalid');
expect(result.error).toMatch(/required/i);
});
it('rejects meta-address not starting with st:xlm:', () => {
const result = validateRow(makeRow({ metaAddress: 'st:eth:0xABCD' }), 'XLM');
expect(result.status).toBe('invalid');
expect(result.error).toMatch(/st:xlm:/i);
});
it('rejects meta-address that fails SDK decode', () => {
// Our mock throws when address doesn't start with st:xlm:
const result = validateRow(makeRow({ metaAddress: 'garbage' }), 'XLM');
expect(result.status).toBe('invalid');
});
it('marks empty amount as invalid', () => {
const result = validateRow(makeRow({ amountRaw: '' }), 'XLM');
expect(result.status).toBe('invalid');
expect(result.error).toMatch(/required/i);
});
it('rejects non-numeric amount', () => {
const result = validateRow(makeRow({ amountRaw: 'abc' }), 'XLM');
expect(result.status).toBe('invalid');
expect(result.error).toMatch(/invalid/i);
});
it('rejects zero amount', () => {
const result = validateRow(makeRow({ amountRaw: '0' }), 'XLM');
expect(result.status).toBe('invalid');
});
it('rejects negative amount', () => {
const result = validateRow(makeRow({ amountRaw: '-1' }), 'XLM');
expect(result.status).toBe('invalid');
});
it('rejects amount with too many decimals for XLM (>7)', () => {
const result = validateRow(makeRow({ amountRaw: '1.12345678' }), 'XLM');
expect(result.status).toBe('invalid');
expect(result.error).toMatch(/decimal/i);
});
it('accepts amount with exactly 7 decimals for XLM', () => {
const result = validateRow(makeRow({ amountRaw: '1.1234567' }), 'XLM');
expect(result.status).toBe('valid');
});
it('accepts integer amount', () => {
const result = validateRow(makeRow({ amountRaw: '100' }), 'XLM');
expect(result.status).toBe('valid');
});
it('accepts minimum valid amount (0.0000001)', () => {
const result = validateRow(makeRow({ amountRaw: '0.0000001' }), 'XLM');
expect(result.status).toBe('valid');
});
it('rejects memo exceeding 28 bytes', () => {
const longMemo = 'a'.repeat(29); // 29 ASCII chars = 29 bytes
const result = validateRow(makeRow({ memo: longMemo }), 'XLM');
expect(result.status).toBe('invalid');
expect(result.error).toMatch(/memo/i);
});
it('accepts memo exactly 28 bytes', () => {
const okMemo = 'a'.repeat(28);
const result = validateRow(makeRow({ memo: okMemo }), 'XLM');
expect(result.status).toBe('valid');
});
it('accepts empty memo', () => {
const result = validateRow(makeRow({ memo: '' }), 'XLM');
expect(result.status).toBe('valid');
});
});
// ---------------------------------------------------------------------------
// validateRows
// ---------------------------------------------------------------------------
describe('validateRows', () => {
it('returns empty array for empty input', () => {
expect(validateRows([], 'XLM')).toHaveLength(0);
});
it('validates all rows', () => {
const rows: BatchRow[] = [
makeRow({ index: 1, amountRaw: '10' }),
makeRow({ index: 2, amountRaw: '' }),
makeRow({ index: 3, amountRaw: 'bad' }),
];
const result = validateRows(rows, 'XLM');
expect(result[0].status).toBe('valid');
expect(result[1].status).toBe('invalid');
expect(result[2].status).toBe('invalid');
});
it('marks rows beyond MAX_BATCH_ROWS as invalid', () => {
const rows: BatchRow[] = Array.from({ length: MAX_BATCH_ROWS + 2 }, (_, i) =>
makeRow({ index: i + 1 }),
);
const result = validateRows(rows, 'XLM');
// Rows within limit should be valid
expect(result[MAX_BATCH_ROWS - 1].status).toBe('valid');
// Rows beyond limit should be invalid
expect(result[MAX_BATCH_ROWS].status).toBe('invalid');
expect(result[MAX_BATCH_ROWS + 1].status).toBe('invalid');
expect(result[MAX_BATCH_ROWS].error).toMatch(/limit/i);
});
it('does not mutate original row objects', () => {
const original: BatchRow[] = [makeRow({ status: 'idle' })];
const result = validateRows(original, 'XLM');
// Result is a new array of new objects
expect(result).not.toBe(original);
expect(result[0]).not.toBe(original[0]);
// Original unchanged
expect(original[0].status).toBe('idle');
});
it('all 20 valid rows pass validation (benchmark size)', () => {
const rows: BatchRow[] = Array.from({ length: 20 }, (_, i) =>
makeRow({ index: i + 1, amountRaw: String(i + 1) }),
);
const result = validateRows(rows, 'XLM');
expect(result.every((r) => r.status === 'valid')).toBe(true);
});
});
// ---------------------------------------------------------------------------
// serializeRowsToCsv
// ---------------------------------------------------------------------------
describe('serializeRowsToCsv', () => {
it('returns empty string for empty input', () => {
expect(serializeRowsToCsv([])).toBe('');
});
it('serializes a two-column row when memo is empty', () => {
const csv = serializeRowsToCsv([{ metaAddress: 'st:xlm:AAA', amountRaw: '10', memo: '' }]);
expect(csv).toBe('st:xlm:AAA,10');
});
it('serializes a three-column row when memo is present', () => {
const csv = serializeRowsToCsv([
{ metaAddress: 'st:xlm:AAA', amountRaw: '5.5', memo: 'payment-1' },
]);
expect(csv).toBe('st:xlm:AAA,5.5,payment-1');
});
it('joins multiple rows with newlines', () => {
const csv = serializeRowsToCsv([
{ metaAddress: 'st:xlm:AAA', amountRaw: '10', memo: '' },
{ metaAddress: 'st:xlm:BBB', amountRaw: '5.5', memo: 'payment-1' },
]);
expect(csv).toBe('st:xlm:AAA,10\nst:xlm:BBB,5.5,payment-1');
});
it('quotes fields containing commas', () => {
const csv = serializeRowsToCsv([
{ metaAddress: 'st:xlm:AAA,extra', amountRaw: '10', memo: '' },
]);
expect(csv).toBe('"st:xlm:AAA,extra",10');
});
it('escapes embedded double quotes', () => {
const csv = serializeRowsToCsv([{ metaAddress: 'st:xlm:AAA"B', amountRaw: '5', memo: '' }]);
expect(csv).toBe('"st:xlm:AAA""B",5');
});
it('round-trips through parseCsvRows', () => {
const original = [
{ metaAddress: 'st:xlm:AAA', amountRaw: '10', memo: '' },
{ metaAddress: 'st:xlm:BBB', amountRaw: '5.5', memo: 'payment-1' },
{ metaAddress: 'st:xlm:AAA,extra', amountRaw: '2', memo: 'has "quotes"' },
];
const parsed = parseCsvRows(serializeRowsToCsv(original));
expect(parsed).toHaveLength(original.length);
parsed.forEach((row, i) => {
expect(row.metaAddress).toBe(original[i].metaAddress);
expect(row.amountRaw).toBe(original[i].amountRaw);
expect(row.memo).toBe(original[i].memo);
});
});
});
// ---------------------------------------------------------------------------
// MAX_BATCH_ROWS constant
// ---------------------------------------------------------------------------
describe('MAX_BATCH_ROWS', () => {
it('is 100', () => {
expect(MAX_BATCH_ROWS).toBe(100);
});
});