From 75d2eb4009528df94e4230853d5bb9285bfecd9e Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Fri, 30 Oct 2026 07:05:55 +0000 Subject: [PATCH 1/2] =?UTF-8?q?fix(portfolio-import):=20=E5=89=A5=E7=A6=BB?= =?UTF-8?q?=E6=95=B0=E9=87=8F/=E6=88=90=E6=9C=AC=E5=B0=BE=E9=9A=8F?= =?UTF-8?q?=E7=99=BE=E5=88=86=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/portfolio-import/parsers.test.ts | 33 +++++++++++++++++++ .../shared/src/portfolio-import/parsers.ts | 13 ++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index 3e0e175b..e3762d13 100644 --- a/packages/shared/src/portfolio-import/parsers.test.ts +++ b/packages/shared/src/portfolio-import/parsers.test.ts @@ -79,6 +79,25 @@ describe('toFiniteNumber', () => { expect(toFiniteNumber(' 1,234.56 ')).toBe(1234.56) }) + it('strips a decorative trailing percent without dividing by 100', () => { + expect(toFiniteNumber('100%')).toBe(100) + expect(toFiniteNumber('12.5%')).toBe(12.5) + expect(toFiniteNumber('-3.25%')).toBe(-3.25) + expect(toFiniteNumber('0%')).toBe(0) + expect(toFiniteNumber('1,000%')).toBe(1000) + expect(toFiniteNumber(' 100 % ')).toBe(100) + expect(toFiniteNumber('$12.5%')).toBe(12.5) + }) + + it('leaves ordinary numbers and non-decorative percents unchanged', () => { + 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() @@ -132,6 +151,13 @@ describe('parseCsv', () => { expect(mapped).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, issues: [] }) }) + it('strips a decorative trailing percent on quantity and cost without scaling', () => { + const [row] = parseCsv('Symbol,Quantity,Cost\nAAPL.US,10%,100%') + expect(row).toMatchObject({ symbol: 'AAPL.US', quantity: 10, costPrice: 100, confidence: 1, issues: [] }) + const [fraction] = parseCsv('Symbol,Quantity,Cost\nMSFT.US,12.5%,180.5') + expect(fraction).toMatchObject({ symbol: 'MSFT.US', quantity: 12.5, costPrice: 180.5, confidence: 1, issues: [] }) + }) + 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) @@ -301,6 +327,13 @@ describe('parsePaste (spec §46)', () => { } }) + it('strips a decorative trailing percent on pasted quantity and cost', () => { + const [row] = parsePaste('AAPL.US 10% 100%') + expect(row).toMatchObject({ symbol: 'AAPL.US', quantity: 10, costPrice: 100, confidence: 1, issues: [] }) + const [fraction] = parsePaste('MSFT.US, 12.5%, 180.5') + expect(fraction).toMatchObject({ symbol: 'MSFT.US', quantity: 12.5, costPrice: 180.5, confidence: 1, issues: [] }) + }) + it('parses SYMBOL QTY COST', () => { const rows = parsePaste('AAPL.US 100 180.5') expect(rows).toHaveLength(1) diff --git a/packages/shared/src/portfolio-import/parsers.ts b/packages/shared/src/portfolio-import/parsers.ts index a2a141fd..37893ff1 100644 --- a/packages/shared/src/portfolio-import/parsers.ts +++ b/packages/shared/src/portfolio-import/parsers.ts @@ -16,6 +16,12 @@ 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 decorative trailing `%`. + * Only that `%` is removed; the number is not divided by 100 (`100%` stays 100). + */ +const DECORATIVE_TRAILING_PERCENT_RE = /^-?\d+(\.\d+)?%$/ + export type HeaderField = 'symbol' | 'name' | 'quantity' | 'cost' | 'currency' | 'account' /** @@ -85,7 +91,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 decorative trailing `%` on a + * plain decimal is removed and is not treated as a weight (`100%` → 100, not 1). + * Empty/null/non-numeric → undefined. */ export function toFiniteNumber(value: unknown): number | undefined { if (typeof value === 'number') { @@ -103,7 +111,8 @@ export function toFiniteNumber(value: unknown): number | undefined { if (cleaned === '') { return undefined } - const parsed = Number(cleaned) + const numeric = DECORATIVE_TRAILING_PERCENT_RE.test(cleaned) ? cleaned.slice(0, -1) : cleaned + const parsed = Number(numeric) return Number.isFinite(parsed) ? parsed : undefined } From 55379a17dc5254bd9686629f103603c83477adac Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:17:06 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix(portfolio-import):=20=E6=95=B0=E9=87=8F?= =?UTF-8?q?/=E6=88=90=E6=9C=AC=E5=B0=BE=E9=9A=8F=E7=99=BE=E5=88=86?= =?UTF-8?q?=E5=8F=B7=E6=94=B9=E4=B8=BA=E9=9C=80=E4=BA=BA=E5=B7=A5=E7=A1=AE?= =?UTF-8?q?=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 100% 与 12.5% 不再按数字入账。这类单元格记入 issues 并降为需复核,避免把比例或错列静默写成真实持仓。 Co-authored-by: wannT-1 --- .../shared/src/portfolio-import/draft.test.ts | 15 +++ .../src/portfolio-import/parsers.test.ts | 93 +++++++++++++++---- .../shared/src/portfolio-import/parsers.ts | 62 ++++++++----- .../src/portfolio-import/repository.test.ts | 18 ++++ 4 files changed, 146 insertions(+), 42 deletions(-) diff --git a/packages/shared/src/portfolio-import/draft.test.ts b/packages/shared/src/portfolio-import/draft.test.ts index 86fe3545..0e119899 100644 --- a/packages/shared/src/portfolio-import/draft.test.ts +++ b/packages/shared/src/portfolio-import/draft.test.ts @@ -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', () => { diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index e3762d13..032ff7e1 100644 --- a/packages/shared/src/portfolio-import/parsers.test.ts +++ b/packages/shared/src/portfolio-import/parsers.test.ts @@ -79,17 +79,13 @@ describe('toFiniteNumber', () => { expect(toFiniteNumber(' 1,234.56 ')).toBe(1234.56) }) - it('strips a decorative trailing percent without dividing by 100', () => { - expect(toFiniteNumber('100%')).toBe(100) - expect(toFiniteNumber('12.5%')).toBe(12.5) - expect(toFiniteNumber('-3.25%')).toBe(-3.25) - expect(toFiniteNumber('0%')).toBe(0) - expect(toFiniteNumber('1,000%')).toBe(1000) - expect(toFiniteNumber(' 100 % ')).toBe(100) - expect(toFiniteNumber('$12.5%')).toBe(12.5) + 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-decorative percents unchanged', () => { + it('leaves ordinary numbers and non-numeric percents unscaled', () => { expect(toFiniteNumber('100')).toBe(100) expect(toFiniteNumber('12.5')).toBe(12.5) expect(toFiniteNumber('%')).toBeUndefined() @@ -151,11 +147,58 @@ describe('parseCsv', () => { expect(mapped).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, issues: [] }) }) - it('strips a decorative trailing percent on quantity and cost without scaling', () => { - const [row] = parseCsv('Symbol,Quantity,Cost\nAAPL.US,10%,100%') - expect(row).toMatchObject({ symbol: 'AAPL.US', quantity: 10, costPrice: 100, confidence: 1, issues: [] }) - const [fraction] = parseCsv('Symbol,Quantity,Cost\nMSFT.US,12.5%,180.5') - expect(fraction).toMatchObject({ symbol: 'MSFT.US', quantity: 12.5, costPrice: 180.5, confidence: 1, 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', () => { @@ -327,11 +370,23 @@ describe('parsePaste (spec §46)', () => { } }) - it('strips a decorative trailing percent on pasted quantity and cost', () => { - const [row] = parsePaste('AAPL.US 10% 100%') - expect(row).toMatchObject({ symbol: 'AAPL.US', quantity: 10, costPrice: 100, confidence: 1, issues: [] }) - const [fraction] = parsePaste('MSFT.US, 12.5%, 180.5') - expect(fraction).toMatchObject({ symbol: 'MSFT.US', quantity: 12.5, costPrice: 180.5, confidence: 1, issues: [] }) + 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', () => { diff --git a/packages/shared/src/portfolio-import/parsers.ts b/packages/shared/src/portfolio-import/parsers.ts index 37893ff1..5907d8cf 100644 --- a/packages/shared/src/portfolio-import/parsers.ts +++ b/packages/shared/src/portfolio-import/parsers.ts @@ -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' @@ -17,10 +17,20 @@ export const SYMBOL_RE = /^[A-Z0-9]{1,8}\.(US|HK|SG|SH|SZ|HAS)$/ export const BARE_TICKER_RE = /^[A-Z0-9]{1,8}$/ /** - * A plain decimal with one decorative trailing `%`. - * Only that `%` is removed; the number is not divided by 100 (`100%` stays 100). + * 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 DECORATIVE_TRAILING_PERCENT_RE = /^-?\d+(\.\d+)?%$/ +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' @@ -91,8 +101,8 @@ export function checkSymbol(raw: string): SymbolCheck { /** * Coerce a CSV/paste cell to a finite number. Strings with thousands - * separators or a leading `$` are handled. A decorative trailing `%` on a - * plain decimal is removed and is not treated as a weight (`100%` → 100, not 1). + * 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 { @@ -102,17 +112,12 @@ 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 } - const numeric = DECORATIVE_TRAILING_PERCENT_RE.test(cleaned) ? cleaned.slice(0, -1) : cleaned - const parsed = Number(numeric) + const parsed = Number(cleaned) return Number.isFinite(parsed) ? parsed : undefined } @@ -281,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 diff --git a/packages/shared/src/portfolio-import/repository.test.ts b/packages/shared/src/portfolio-import/repository.test.ts index 5b434892..b65cd371 100644 --- a/packages/shared/src/portfolio-import/repository.test.ts +++ b/packages/shared/src/portfolio-import/repository.test.ts @@ -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([])