Skip to content
Closed
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
54 changes: 54 additions & 0 deletions packages/shared/src/portfolio-import/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,60 @@ describe('confidence tiers (spec §48)', () => {
})

describe('parsePaste (spec §46)', () => {
it('keeps quantity in place when the trailing cost column is empty', () => {
const [row] = parsePaste('AAPL.US,100,')
expect(row.quantity).toBe(100)
expect(row.costPrice).toBeUndefined()
expect(row.issues).toEqual(['Cost price missing'])
expect(row.confidence).toBe(0.6)
})

it('keeps cost and currency in place when quantity is empty', () => {
const [row] = parsePaste('AAPL.US, ,180.5,USD')
expect(row.quantity).toBeUndefined()
expect(row.costPrice).toBe(180.5)
expect(row.currency).toBe('USD')
expect(row.issues).toEqual(['Quantity missing'])
expect(row.confidence).toBe(0.6)
})

it('keeps currency in place when cost is empty', () => {
const [row] = parsePaste('AAPL.US,100, ,USD')
expect(row.quantity).toBe(100)
expect(row.costPrice).toBeUndefined()
expect(row.currency).toBe('USD')
expect(row.issues).toEqual(['Cost price missing'])
})

it('does not turn an empty symbol into a quantity ticker', () => {
const [row] = parsePaste(',100,180.5,USD')
expect(row.symbol).toBe('')
expect(row.quantity).toBe(100)
expect(row.costPrice).toBe(180.5)
expect(row.currency).toBe('USD')
expect(row.issues).toEqual(['Missing symbol'])
})

it('keeps both numeric fields missing when both columns are empty', () => {
const [row] = parsePaste('AAPL.US, , ,USD')
expect(row.quantity).toBeUndefined()
expect(row.costPrice).toBeUndefined()
expect(row.currency).toBe('USD')
expect(row.issues).toEqual(['Quantity missing', 'Cost price missing'])
})

it('preserves the existing two-column symbol-cost shorthand', () => {
const [row] = parsePaste('AAPL.US,180.5')
expect(row.quantity).toBeUndefined()
expect(row.costPrice).toBe(180.5)
expect(row.issues).toEqual(['Quantity missing'])

const [empty] = parsePaste('AAPL.US, ')
expect(empty.quantity).toBeUndefined()
expect(empty.costPrice).toBeUndefined()
expect(empty.issues).toEqual(['Quantity missing', 'Cost price missing'])
})

it('flags formatting-only numeric cells in both paste formats', () => {
for (const input of ['AAPL.US $ $', 'AAPL.US, $, $']) {
const [row] = parsePaste(input)
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/src/portfolio-import/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ export function parsePasteLine(line: string): PortfolioImportRow {
const parts = trimmed.includes(',')
? trimmed
.split(',')
.map((part) => part.trim())
.filter((part) => part !== '')
// Empty cells still occupy a column; dropping them shifts holdings data.
.map((part) => part.trim() || undefined)
: trimmed.split(/\s+/)

const fields: ParsedFields = { symbol: '', name: undefined, quantity: undefined, cost: undefined, currency: undefined, account: undefined }
if (parts.length >= 1) fields.symbol = parts[0]
if (parts.length >= 1) fields.symbol = parts[0] ?? ''
if (parts.length >= 3) {
fields.quantity = parts[1]
fields.cost = parts[2]
Expand Down
23 changes: 22 additions & 1 deletion packages/shared/src/portfolio-import/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import type { ManualPortfolio } from '@finagent/core'
import { JsonFileStore } from '../storage/json-file-store.ts'
import { createDraft, draftToPortfolioInput, validateDraft } from './draft.ts'
import { createDraft, draftHasRecognizableSymbols, draftToPortfolioInput, validateDraft } from './draft.ts'
import { parseCsv, parsePaste } from './parsers.ts'
import { ManualPortfolioRepository } from './repository.ts'

Expand Down Expand Up @@ -125,6 +125,27 @@ describe('ManualPortfolioRepository', () => {
})

describe('confirm persists only after confirm (spec §93)', () => {
it('preserves empty paste columns through review and persisted holdings', async () => {
const store = tempStore()
const repository = new ManualPortfolioRepository(store)
const draft = createDraft('paste', parsePaste('AAPL.US,100,\nMSFT.US,,180.5,USD'))
expect(draft.warnings).toContain('2 rows need review')
expect(await repository.list()).toEqual([])

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

it('keeps a missing leading symbol blocked at draft confirmation', () => {
const draft = createDraft('paste', parsePaste(',100,180.5,USD'))
expect(draftHasRecognizableSymbols(draft)).toBe(false)
expect(draft.warnings).toContain('1 row with no recognizable symbol')
})

it('draft creation has zero side effects on disk', async () => {
const store = tempStore()
const repository = new ManualPortfolioRepository(store)
Expand Down