From 95ff9507b2de8c7f05670fe604cece626a45b3d9 Mon Sep 17 00:00:00 2001 From: Chris <151883835+VeronicDev@users.noreply.github.com> Date: Thu, 22 Oct 2026 12:51:02 +0100 Subject: [PATCH] Fix: [test] Cover amount parser negative and rounding cases --- frontend/src/utils/amount.test.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/frontend/src/utils/amount.test.ts b/frontend/src/utils/amount.test.ts index a7b0165f..0b2d1efc 100644 --- a/frontend/src/utils/amount.test.ts +++ b/frontend/src/utils/amount.test.ts @@ -6,6 +6,32 @@ import { hasValidPrecision, } from "./amount"; +describe("parseAmount edge cases (negative, excessive precision, rounding)", () => { + it("handles negative amounts correctly", () => { + // parseAmount treats negative values as invalid/zero (returns 0n) + expect(parseAmount("-1.5", 7)).toBe(0n); + expect(parseAmount("-0", 7)).toBe(0n); + }); + + it("handles excessive precision by truncating extra decimal places", () => { + // 7 decimals max, input has 9 + expect(parseAmount("1.123456789", 7)).toBe(11234567n); + expect(parseAmount("0.000000123", 7)).toBe(1n); + }); + + it("handles halfway rounding / boundary inputs correctly", () => { + // parseAmount currently floors/truncates excess precision rather than rounding + expect(parseAmount("1.12345674", 7)).toBe(11234567n); + expect(parseAmount("1.12345676", 7)).toBe(11234567n); + }); + + it("round-trips positive boundary values properly", () => { + const val = 1234567n; + const formatted = formatAmount(val, 7); + expect(parseAmount(formatted, 7)).toBe(val); + }); +}); + describe("amount parser locale, exponent, whitespace, and empty strings", () => { describe("parseAmount with locale and edge inputs", () => { it("handles empty and whitespace-only strings", () => {