|
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { |
| 3 | + parseCsvText, |
| 4 | + validateAndBuildAllocations, |
| 5 | + coerceJsonRows, |
| 6 | +} from '../services/portfolioImportService.js' |
| 7 | + |
| 8 | +vi.mock('../services/assetRegistryService.js', () => ({ |
| 9 | + assetRegistryService: { |
| 10 | + getBySymbol: (sym: string) => { |
| 11 | + const registry: Record<string, { enabled: boolean; isQuarantined: boolean }> = { |
| 12 | + XLM: { enabled: true, isQuarantined: false }, |
| 13 | + USDC: { enabled: true, isQuarantined: false }, |
| 14 | + BTC: { enabled: true, isQuarantined: false }, |
| 15 | + ETH: { enabled: true, isQuarantined: false }, |
| 16 | + } |
| 17 | + return registry[sym] ?? null |
| 18 | + }, |
| 19 | + }, |
| 20 | +})) |
| 21 | + |
| 22 | +describe('portfolioImportService - structured error array', () => { |
| 23 | + it('returns all row-level errors for mixed valid/invalid CSV rows', async () => { |
| 24 | + const csv = [ |
| 25 | + 'asset,allocation_pct', |
| 26 | + 'XLM,30', |
| 27 | + 'BAD1,20', |
| 28 | + 'USDC,25', |
| 29 | + ',10', |
| 30 | + 'BTC,abc', |
| 31 | + 'ETH,15', |
| 32 | + ].join('\n') |
| 33 | + |
| 34 | + const parsed = parseCsvText(csv) |
| 35 | + const result = await validateAndBuildAllocations({ |
| 36 | + rows: parsed.rows, |
| 37 | + initialRowErrors: parsed.errors, |
| 38 | + }) |
| 39 | + |
| 40 | + expect('errors' in result).toBe(true) |
| 41 | + if ('errors' in result) { |
| 42 | + const rowErrors = result.errors.filter((e) => e.row >= 2) |
| 43 | + expect(rowErrors.length).toBeGreaterThanOrEqual(3) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + it('continues validating remaining rows after one row fails', async () => { |
| 48 | + const rows = [ |
| 49 | + { asset: 'XLM', allocation_pct: 50 }, |
| 50 | + { asset: '', allocation_pct: 20 }, |
| 51 | + { asset: 'USDC', allocation_pct: 30 }, |
| 52 | + ] |
| 53 | + |
| 54 | + const { rows: parsedRows, errors: initialErrors } = { |
| 55 | + rows, |
| 56 | + errors: [] as any[], |
| 57 | + } |
| 58 | + |
| 59 | + const result = await validateAndBuildAllocations({ |
| 60 | + rows: parsedRows, |
| 61 | + initialRowErrors: initialErrors, |
| 62 | + }) |
| 63 | + |
| 64 | + expect('errors' in result).toBe(true) |
| 65 | + if ('errors' in result) { |
| 66 | + const assetErrors = result.errors.filter( |
| 67 | + (e) => e.field === 'asset' && e.row === 3, |
| 68 | + ) |
| 69 | + expect(assetErrors.length).toBeGreaterThan(0) |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + it('caps reported errors at 100 with +N more summary', async () => { |
| 74 | + const rows: { asset: string; allocation_pct: number }[] = [] |
| 75 | + for (let i = 0; i < 150; i++) { |
| 76 | + rows.push({ asset: '', allocation_pct: NaN }) |
| 77 | + } |
| 78 | + |
| 79 | + const result = await validateAndBuildAllocations({ |
| 80 | + rows, |
| 81 | + initialRowErrors: [], |
| 82 | + }) |
| 83 | + |
| 84 | + expect('errors' in result).toBe(true) |
| 85 | + if ('errors' in result) { |
| 86 | + expect(result.errors.length).toBeLessThanOrEqual(102) |
| 87 | + const summaryError = result.errors.find((e) => e.field === 'summary') |
| 88 | + expect(summaryError).toBeDefined() |
| 89 | + expect(summaryError!.message).toMatch(/\+\d+ more/) |
| 90 | + expect(result.truncatedErrors).toBeDefined() |
| 91 | + expect(result.truncatedErrors).toBeGreaterThan(0) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + it('returns valid allocations when all rows are correct', async () => { |
| 96 | + const rows = [ |
| 97 | + { asset: 'XLM', allocation_pct: 60 }, |
| 98 | + { asset: 'USDC', allocation_pct: 40 }, |
| 99 | + ] |
| 100 | + |
| 101 | + const result = await validateAndBuildAllocations({ |
| 102 | + rows, |
| 103 | + initialRowErrors: [], |
| 104 | + }) |
| 105 | + |
| 106 | + expect('allocations' in result).toBe(true) |
| 107 | + if ('allocations' in result) { |
| 108 | + expect(result.allocations.XLM).toBe(60) |
| 109 | + expect(result.allocations.USDC).toBe(40) |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + it('coerceJsonRows collects errors for bad rows without stopping', () => { |
| 114 | + const jsonRows = [ |
| 115 | + { asset: 'XLM', allocation_pct: 50 }, |
| 116 | + { asset: '', allocation_pct: 20 }, |
| 117 | + { asset: 'USDC', allocation_pct: 'bad' }, |
| 118 | + { asset: 'BTC', allocation_pct: 30 }, |
| 119 | + ] |
| 120 | + |
| 121 | + const { errors } = coerceJsonRows(jsonRows) |
| 122 | + expect(errors.length).toBeGreaterThanOrEqual(2) |
| 123 | + }) |
| 124 | +}) |
0 commit comments