Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/shared/src/portfolio-import/draft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ describe('rowsToHoldings', () => {
const empty: PortfolioImportRow = { symbol: '', confidence: 0.3, issues: ['Missing symbol'] }
expect(rowsToHoldings([partial, empty])).toEqual([{ symbol: 'TSLA.US', name: '' }])
})

it('does not book trailing-percent quantity or cost and keeps the row for review', () => {
const rows = parsePaste('AAPL.US 100% 12.5%\nMSFT.US, 12.5%, 180.5')
const draft = createDraft('paste', rows)
expect(rowsToHoldings(rows)).toEqual([
{ symbol: 'AAPL.US', name: '' },
{ symbol: 'MSFT.US', name: '', costPrice: 180.5 },
])
expect(draft.warnings).toContain('2 rows need review')
expect(validateDraft(draft)).toEqual([
'AAPL.US: Quantity "100%" needs confirmation',
'AAPL.US: Cost price "12.5%" needs confirmation',
'MSFT.US: Quantity "12.5%" needs confirmation',
])
})
})

describe('draftToPortfolioInput', () => {
Expand Down
88 changes: 88 additions & 0 deletions packages/shared/src/portfolio-import/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ describe('toFiniteNumber', () => {
expect(toFiniteNumber(' 1,234.56 ')).toBe(1234.56)
})

it('does not coerce a trailing percent into a number', () => {
for (const value of ['100%', '12.5%', '-3.25%', '0%', '1,000%', ' 100 % ', '$12.5%']) {
expect(toFiniteNumber(value)).toBeUndefined()
}
})

it('leaves ordinary numbers and non-numeric percents unscaled', () => {
expect(toFiniteNumber('100')).toBe(100)
expect(toFiniteNumber('12.5')).toBe(12.5)
expect(toFiniteNumber('%')).toBeUndefined()
expect(toFiniteNumber('100%%')).toBeUndefined()
expect(toFiniteNumber('10%5')).toBeUndefined()
expect(toFiniteNumber('12% off')).toBeUndefined()
})

it('returns undefined for null, empty, and junk', () => {
expect(toFiniteNumber(null)).toBeUndefined()
expect(toFiniteNumber(undefined)).toBeUndefined()
Expand Down Expand Up @@ -132,6 +147,60 @@ describe('parseCsv', () => {
expect(mapped).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, issues: [] })
})

it('flags 100% and 12.5% quantity and cost for confirmation instead of booking them', () => {
const [both] = parseCsv('Symbol,Quantity,Cost\nAAPL.US,100%,12.5%')
expect(both.quantity).toBeUndefined()
expect(both.costPrice).toBeUndefined()
expect(both.confidence).toBe(0.6)
expect(both.issues).toEqual([
'Quantity "100%" needs confirmation',
'Cost price "12.5%" needs confirmation',
])

const [quantityPercent] = parseCsv('Symbol,Quantity,Cost\nMSFT.US,100%,180.5')
expect(quantityPercent.quantity).toBeUndefined()
expect(quantityPercent.costPrice).toBe(180.5)
expect(quantityPercent.confidence).toBe(0.6)
expect(quantityPercent.issues).toEqual(['Quantity "100%" needs confirmation'])

const [costPercent] = parseCsv('Symbol,Quantity,Cost\n0700.HK,500,12.5%')
expect(costPercent.quantity).toBe(500)
expect(costPercent.costPrice).toBeUndefined()
expect(costPercent.confidence).toBe(0.6)
expect(costPercent.issues).toEqual(['Cost price "12.5%" needs confirmation'])
})

it('flags decorated trailing percents and leaves ordinary numbers booked', () => {
const [decorated] = parseCsv('Symbol,Quantity,Cost\nAAPL.US," 100 % ","$12.5%"')
expect(decorated.quantity).toBeUndefined()
expect(decorated.costPrice).toBeUndefined()
expect(decorated.issues).toEqual([
'Quantity "100 %" needs confirmation',
'Cost price "$12.5%" needs confirmation',
])
const [thousands] = parseCsv('Symbol,Quantity,Cost\nMSFT.US,"1,000%",180.50')
expect(thousands.quantity).toBeUndefined()
expect(thousands.costPrice).toBe(180.5)
expect(thousands.issues).toEqual(['Quantity "1,000%" needs confirmation'])
const [plain] = parseCsv('Symbol,Quantity,Cost\n0700.HK,"1,000","$180.50"')
expect(plain).toMatchObject({ quantity: 1000, costPrice: 180.5, confidence: 1, issues: [] })
const [signed] = parseCsv('Symbol,Quantity,Cost\nTSLA.US,-3.25%,0%')
expect(signed.quantity).toBeUndefined()
expect(signed.costPrice).toBeUndefined()
expect(signed.issues).toEqual([
'Quantity "-3.25%" needs confirmation',
'Cost price "0%" needs confirmation',
])
})

it('keeps a percent that is not a plain trailing % invalid', () => {
const [row] = parseCsv('Symbol,Quantity,Cost\nAAPL.US,100%%,10%5')
expect(row.quantity).toBeUndefined()
expect(row.costPrice).toBeUndefined()
expect(row.confidence).toBe(0.6)
expect(row.issues).toEqual(['Invalid quantity "100%%"', 'Invalid cost price "10%5"'])
})

it('flags formatting-only quantities and costs without inventing zeros', () => {
const rows = parseCsv('Symbol,Quantity,Cost\nAAPL.US,100,$\nMSFT.US,",",180.5\n0700.HK,500,320')
expect(rows).toHaveLength(3)
Expand Down Expand Up @@ -301,6 +370,25 @@ describe('parsePaste (spec §46)', () => {
}
})

it('flags pasted 100% and 12.5% for confirmation instead of booking them', () => {
const [both] = parsePaste('AAPL.US 100% 12.5%')
expect(both.quantity).toBeUndefined()
expect(both.costPrice).toBeUndefined()
expect(both.confidence).toBe(0.6)
expect(both.issues).toEqual([
'Quantity "100%" needs confirmation',
'Cost price "12.5%" needs confirmation',
])
const [quantityPercent] = parsePaste('MSFT.US, 12.5%, 180.5')
expect(quantityPercent.quantity).toBeUndefined()
expect(quantityPercent.costPrice).toBe(180.5)
expect(quantityPercent.issues).toEqual(['Quantity "12.5%" needs confirmation'])
const [costPercent] = parsePaste('0700.HK, 500, 100%')
expect(costPercent.quantity).toBe(500)
expect(costPercent.costPrice).toBeUndefined()
expect(costPercent.issues).toEqual(['Cost price "100%" needs confirmation'])
})

it('parses SYMBOL QTY COST', () => {
const rows = parsePaste('AAPL.US 100 180.5')
expect(rows).toHaveLength(1)
Expand Down
59 changes: 42 additions & 17 deletions packages/shared/src/portfolio-import/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* user-safe `issues` (spec §103), so a single bad cell cannot sink an
* import. Confidence tiers (spec §48):
* 1.0 — symbol unambiguous, quantity and cost both parsed
* 0.6 — symbol unambiguous, quantity or cost missing (review required)
* 0.6 — symbol unambiguous, quantity or cost missing or awaiting confirmation (review required)
* 0.3 — symbol ambiguous (missing market suffix / unknown market)
*/
import type { ImportSource, PortfolioImportRow } from '@finagent/core'
Expand All @@ -16,6 +16,22 @@ export const SYMBOL_RE = /^[A-Z0-9]{1,8}\.(US|HK|SG|SH|SZ|HAS)$/
/** Bare ticker with no market suffix — usable, but ambiguous (review). */
export const BARE_TICKER_RE = /^[A-Z0-9]{1,8}$/

/**
* A plain decimal with one trailing `%`, after thousands separators, `$`, and
* whitespace are removed. `100%` or `12.5%` may be a ratio or a wrong column,
* so it is not a quantity or a cost until a person confirms it.
*/
const TRAILING_PERCENT_NUMBER_RE = /^-?\d+(\.\d+)?%$/

/** Same cleanup `toFiniteNumber` applies before parsing. */
function stripNumericDecorations(value: string): string {
return value.trim().replace(/[,$\s]/g, '')
}

function isTrailingPercentNumber(value: string): boolean {
return TRAILING_PERCENT_NUMBER_RE.test(stripNumericDecorations(value))
}

export type HeaderField = 'symbol' | 'name' | 'quantity' | 'cost' | 'currency' | 'account'

/**
Expand Down Expand Up @@ -85,7 +101,9 @@ export function checkSymbol(raw: string): SymbolCheck {

/**
* Coerce a CSV/paste cell to a finite number. Strings with thousands
* separators or a leading `$` are handled; empty/null/non-numeric → undefined.
* separators or a leading `$` are handled. A trailing `%` is not stripped:
* `100%` and `12.5%` stay non-numeric so they cannot be booked as 100 / 12.5.
* Empty/null/non-numeric → undefined.
*/
export function toFiniteNumber(value: unknown): number | undefined {
if (typeof value === 'number') {
Expand All @@ -94,11 +112,7 @@ export function toFiniteNumber(value: unknown): number | undefined {
if (typeof value !== 'string') {
return undefined
}
const trimmed = value.trim()
if (trimmed === '') {
return undefined
}
const cleaned = trimmed.replace(/[,$\s]/g, '')
const cleaned = stripNumericDecorations(value)
// Number('') is zero, but formatting alone does not supply a numeric value.
if (cleaned === '') {
return undefined
Expand Down Expand Up @@ -272,18 +286,29 @@ export function buildRow(fields: ParsedFields): PortfolioImportRow {
issues.push(symbolCheck.issue)
}

const quantity = toFiniteNumber(fields.quantity)
if (fields.quantity !== undefined && quantity === undefined) {
issues.push(`Invalid quantity "${fields.quantity}"`)
} else if (quantity === undefined && !symbolCheck.ambiguous) {
issues.push('Quantity missing')
let quantity: number | undefined
if (fields.quantity !== undefined && isTrailingPercentNumber(fields.quantity)) {
// Leave the cell unset. Confirming the draft must not turn `100%` into 100 shares.
issues.push(`Quantity "${fields.quantity}" needs confirmation`)
} else {
quantity = toFiniteNumber(fields.quantity)
if (fields.quantity !== undefined && quantity === undefined) {
issues.push(`Invalid quantity "${fields.quantity}"`)
} else if (quantity === undefined && !symbolCheck.ambiguous) {
issues.push('Quantity missing')
}
}

const costPrice = toFiniteNumber(fields.cost)
if (fields.cost !== undefined && costPrice === undefined) {
issues.push(`Invalid cost price "${fields.cost}"`)
} else if (costPrice === undefined && !symbolCheck.ambiguous) {
issues.push('Cost price missing')
let costPrice: number | undefined
if (fields.cost !== undefined && isTrailingPercentNumber(fields.cost)) {
issues.push(`Cost price "${fields.cost}" needs confirmation`)
} else {
costPrice = toFiniteNumber(fields.cost)
if (fields.cost !== undefined && costPrice === undefined) {
issues.push(`Invalid cost price "${fields.cost}"`)
} else if (costPrice === undefined && !symbolCheck.ambiguous) {
issues.push('Cost price missing')
}
}

const confidence = symbolCheck.ambiguous
Expand Down
18 changes: 18 additions & 0 deletions packages/shared/src/portfolio-import/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ describe('ManualPortfolioRepository', () => {
])
})

it('does not persist trailing-percent quantity or cost and marks them for confirmation', async () => {
const store = tempStore()
const draft = createDraft('csv', parseCsv('Symbol,Quantity,Cost\nAAPL.US,100%,12.5%\nMSFT.US,100,12.5%'))
expect(draft.warnings).toContain('2 rows need review')
expect(validateDraft(draft)).toEqual([
'AAPL.US: Quantity "100%" needs confirmation',
'AAPL.US: Cost price "12.5%" needs confirmation',
'MSFT.US: Cost price "12.5%" needs confirmation',
])

const created = await new ManualPortfolioRepository(store).create(draftToPortfolioInput(draft, 'Percent import'))
const reloaded = await new ManualPortfolioRepository(store).get(created.id)
expect(reloaded?.holdings).toEqual([
{ symbol: 'AAPL.US', name: '' },
{ symbol: 'MSFT.US', name: '', quantity: 100 },
])
})

it('lists nothing before the first create', async () => {
const repository = new ManualPortfolioRepository(tempStore())
expect(await repository.list()).toEqual([])
Expand Down