|
| 1 | +/** |
| 2 | + * Tests that exportWillsToCSV() uses the correct decimal count for each |
| 3 | + * will's token rather than blindly dividing by 1 000 000 (USDC assumption). |
| 4 | + * |
| 5 | + * This file is the regression guard for issue #256. |
| 6 | + */ |
| 7 | +import { describe, it, expect } from 'vitest'; |
| 8 | +import type { Will, WillStatus } from '@sorowill/sdk'; |
| 9 | + |
| 10 | +import { exportWillsToCSV } from '@/lib/willExport'; |
| 11 | +import { formatTokenBalance, getTokenDecimals } from '@/lib/tokenDecimals'; |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// Helpers |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | + |
| 17 | +/** A token address that is NOT in the registry — falls back to 7 decimals. */ |
| 18 | +const UNKNOWN_8_DECIMAL_TOKEN = 'CTESTTOKEN8DECIMALS0000000000000000000000000000000000000001'; |
| 19 | + |
| 20 | +/** Build a minimal Will fixture. balance must be a string (SDK type). */ |
| 21 | +function makeWill(overrides: Partial<Will> = {}): Will { |
| 22 | + return { |
| 23 | + id: 'will-test-001', |
| 24 | + owner: 'GDBRZV77PZDK7LRBXEUPZNGJNQLFQKAZD6PKS7JFAZAKU4H3FDON4JL4', |
| 25 | + token: 'CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4', |
| 26 | + balance: '1000000', // 1 USDC at 6 decimals |
| 27 | + beneficiaries: [], |
| 28 | + guardians: [], |
| 29 | + guardianVotes: 0, |
| 30 | + status: 'Active' as WillStatus, |
| 31 | + lastCheckin: new Date('2026-01-01T00:00:00.000Z'), |
| 32 | + checkinPeriodDays: 90, |
| 33 | + gracePeriodDays: 7, |
| 34 | + triggerTime: null, |
| 35 | + ...overrides, |
| 36 | + } as Will; |
| 37 | +} |
| 38 | + |
| 39 | +/** Extract the Balance column value from a single-row CSV string. */ |
| 40 | +function extractBalance(csv: string): string { |
| 41 | + const [header, dataRow] = csv.split('\n'); |
| 42 | + const headers = header.split(','); |
| 43 | + const values = dataRow.split(','); |
| 44 | + const idx = headers.indexOf('Balance'); |
| 45 | + return values[idx]; |
| 46 | +} |
| 47 | + |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | +// Unit tests for formatTokenBalance / getTokenDecimals |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | + |
| 52 | +describe('getTokenDecimals', () => { |
| 53 | + it('returns 6 for a known USDC testnet address', () => { |
| 54 | + expect( |
| 55 | + getTokenDecimals('CCW67HTGNFMXKFGRR2MKRB2V6DNFGBLXJOFKLDLNOICL5UX4YK7CPLA'), |
| 56 | + ).toBe(6); |
| 57 | + }); |
| 58 | + |
| 59 | + it('is case-insensitive', () => { |
| 60 | + expect( |
| 61 | + getTokenDecimals('ccw67htgnfmxkfgrr2mkrb2v6dnfgblxjofkldlnoicl5ux4yk7cpla'), |
| 62 | + ).toBe(6); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns 7 (default) for an unknown token', () => { |
| 66 | + expect(getTokenDecimals(UNKNOWN_8_DECIMAL_TOKEN)).toBe(7); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('formatTokenBalance', () => { |
| 71 | + it('formats 1 000 000 base units as "1.000000" for a 6-decimal token', () => { |
| 72 | + expect(formatTokenBalance('1000000', 'any-token', 6)).toBe('1.000000'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('formats 10 000 000 base units as "1.0000000" for a 7-decimal token', () => { |
| 76 | + expect(formatTokenBalance('10000000', 'any-token', 7)).toBe('1.0000000'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('formats 100 base units as "1.00" for a 2-decimal token', () => { |
| 80 | + expect(formatTokenBalance('100', 'any-token', 2)).toBe('1.00'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('accepts a bigint balance', () => { |
| 84 | + expect(formatTokenBalance(1_000_000n, 'any-token', 6)).toBe('1.000000'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('includes thousands separators for large values', () => { |
| 88 | + // 1 234 000 000 base units at 6 decimals = 1,234.000000 |
| 89 | + expect(formatTokenBalance('1234000000', 'any-token', 6)).toBe('1,234.000000'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +// --------------------------------------------------------------------------- |
| 94 | +// Integration: exportWillsToCSV uses token-correct decimals |
| 95 | +// --------------------------------------------------------------------------- |
| 96 | + |
| 97 | +describe('exportWillsToCSV — token-aware balance formatting', () => { |
| 98 | + it('formats a 6-decimal USDC balance correctly (1 000 000 base → 1.000000)', () => { |
| 99 | + const will = makeWill({ balance: '1000000', token: 'CUSDC-6-DECIMALS', checkinPeriodDays: 90 }); |
| 100 | + const csv = exportWillsToCSV([will]); |
| 101 | + // formatTokenBalance('1000000', unknown token) uses DEFAULT_DECIMALS=7 |
| 102 | + // BUT we pass a decimalsOverride=6 in the CSV via getTokenDecimals lookup. |
| 103 | + // Since 'CUSDC-6-DECIMALS' is not in the registry the default (7) applies here — |
| 104 | + // the important assertion is that the value is NOT the USDC-hardcoded '1' (1e6/1e6). |
| 105 | + // Use a real registry token instead: |
| 106 | + const usdcTestnet = 'CCW67HTGNFMXKFGRR2MKRB2V6DNFGBLXJOFKLDLNOICL5UX4YK7CPLA'; |
| 107 | + const usdcWill = makeWill({ balance: '1000000', token: usdcTestnet }); |
| 108 | + const usdcCsv = exportWillsToCSV([usdcWill]); |
| 109 | + const balance = extractBalance(usdcCsv); |
| 110 | + // 1 000 000 base units / 10^6 = 1, formatted as '1.000000' |
| 111 | + expect(balance).toBe('1.000000'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('does NOT divide by 1 000 000 for a 7-decimal token (the old bug)', () => { |
| 115 | + // A token not in the registry → DEFAULT_DECIMALS = 7. |
| 116 | + // 10 000 000 base units at 7 decimals = 1.0000000 |
| 117 | + // At 6 decimals (old bug) the same balance would produce 10.000000 — wrong. |
| 118 | + const will = makeWill({ balance: '10000000', token: UNKNOWN_8_DECIMAL_TOKEN }); |
| 119 | + const csv = exportWillsToCSV([will]); |
| 120 | + const balance = extractBalance(csv); |
| 121 | + |
| 122 | + // Correct (7 decimals): 10000000 / 10^7 = 1.0000000 |
| 123 | + expect(balance).toBe('1.0000000'); |
| 124 | + // Wrong (6 decimals, old behaviour): would be 10.000000 |
| 125 | + expect(balance).not.toBe('10.000000'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('exports the correct balance for a hypothetical 2-decimal token', () => { |
| 129 | + // 100 base units at 2 decimals = 1.00 |
| 130 | + // We pass the decimalsOverride via formatTokenBalance directly in this assertion. |
| 131 | + const result = formatTokenBalance('100', 'any', 2); |
| 132 | + expect(result).toBe('1.00'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('handles zero balance', () => { |
| 136 | + // balance='0' is a non-empty string so formatTokenBalance is called; |
| 137 | + // it produces '0.0000000' for an unknown token (7 decimals default). |
| 138 | + const will = makeWill({ balance: '0', token: UNKNOWN_8_DECIMAL_TOKEN }); |
| 139 | + const csv = exportWillsToCSV([will]); |
| 140 | + // Correct: formatted zero with 7 decimal places (DEFAULT_DECIMALS). |
| 141 | + expect(extractBalance(csv)).toBe('0.0000000'); |
| 142 | + }); |
| 143 | +}); |
0 commit comments