A local-first personal finance app
[AI] fix(core): fall back for non-finite arithmetic results (#9309)
doksanbes945fce10bb4b
diff --git a/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx b/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsxindex f244138fcc..dc5df4b6fe 100644--- a/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx+++ b/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx@@ -774,6 +774,34 @@ describe('Transactions', () => {expect(input.value).toEqual(oldValue);});+ test.each(['debit', 'credit'] as const)(+ 'invalid arithmetic in %s preserves the amount and allows correction',+ async field => {+ const onTransactionsChange = vi.fn();+ const { container } = renderTransactions({ onTransactionsChange });+ const input = await editField(container, field, 0);+ await userEvent.clear(input);+ await userEvent.type(input, '1/0[Tab]');++ expect(onTransactionsChange).toHaveBeenLastCalledWith(+ expect.arrayContaining([expect.objectContaining({ amount: -2777 })]),+ );+ expect(queryField(container, 'debit', 'div', 0).textContent).toBe(+ '27.77',+ );+ expect(queryField(container, 'credit', '', 0).textContent).toBe('');++ const correctedInput = await editField(container, field, 0);+ await userEvent.clear(correctedInput);+ await userEvent.type(correctedInput, '12/2[Tab]');+ expect(onTransactionsChange).toHaveBeenLastCalledWith(+ expect.arrayContaining([+ expect.objectContaining({ amount: field === 'debit' ? -600 : 600 }),+ ]),+ );+ },+ );+test('text fields save when moved away from', async () => {const { container, getTransactions } = renderTransactions();diff --git a/packages/loot-core/src/shared/arithmetic.test.ts b/packages/loot-core/src/shared/arithmetic.test.tsindex 7cb789f9f7..0953f6a228 100644--- a/packages/loot-core/src/shared/arithmetic.test.ts+++ b/packages/loot-core/src/shared/arithmetic.test.ts@@ -33,6 +33,21 @@ describe('arithmetic', () => {expect(evalArithmetic('2400 / 2 / 5')).toEqual(240);});+ test.each(['1/0', '-1/0', '0/0', '2^1024', '(-1)^.5'])(+ 'returns the default for a non-finite result from %s',+ expression => {+ expect(evalArithmetic(expression)).toBeNull();+ expect(evalArithmetic(expression, 42)).toBe(42);+ expect(evalArithmetic(expression, 0)).toBe(0);+ },+ );++ test('keeps valid arithmetic results instead of the default', () => {+ expect(evalArithmetic('12/2', 42)).toBe(6);+ expect(evalArithmetic('-12/2', 42)).toBe(-6);+ expect(evalArithmetic('0/2', 42)).toBe(0);+ });+test('handles order of operations', () => {expect(evalArithmetic('(5 + 3) * 10')).toEqual(80);expect(evalArithmetic('5 + 3 * 10')).toEqual(35);diff --git a/packages/loot-core/src/shared/arithmetic.ts b/packages/loot-core/src/shared/arithmetic.tsindex ed26a40135..30d8e0a6b7 100644--- a/packages/loot-core/src/shared/arithmetic.ts+++ b/packages/loot-core/src/shared/arithmetic.ts@@ -165,6 +165,6 @@ export function evalArithmetic(return defaultValue;}- // Never return NaN- return isNaN(result) ? defaultValue : result;+ // Never return a non-finite result+ return Number.isFinite(result) ? result : defaultValue;}diff --git a/upcoming-release-notes/fix-non-finite-arithmetic.md b/upcoming-release-notes/fix-non-finite-arithmetic.mdnew file mode 100644index 0000000000..d11859cbfd--- /dev/null+++ b/upcoming-release-notes/fix-non-finite-arithmetic.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [doksanbes]+---++Prevent invalid calculations from interrupting transaction amount editing.