Skip to content
Prev Previous commit
Next Next commit
test(wallet): state the truncation invariant instead of the patch his…
…tory
  • Loading branch information
askalf committed Sep 18, 2026
commit 8725595e5c1c36d4ceb82c41814ae2e1a9258d7c
43 changes: 15 additions & 28 deletions src/main/wallet-balances.trailing-zeros.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import { describe, expect, it } from "vitest";
import { formatTokenBalance, formatTokenBalanceFull } from "../shared/tokens";

// Both formatters promise "up to 4 significant digits with trailing zeros
// removed", but they trimmed the fraction *before* capping it to 4 digits.
// When the cut lands on a zero that was only interior in the untrimmed
// fraction, that zero becomes trailing and was still rendered: 1.20001 HD
// showed as "1.2000", which reads as a balance pinned to 4 decimals rather
// than the exact 1.2 the digits round to.
// Truncated fractional output omits the trailing zeros the truncation
// exposes while preserving interior zeros: 1.20001 renders as "1.2", never
// as "1.2000".
describe("token balance trailing zeros after the 4-digit cut", () => {
it("drops zeros the cut turns into trailing zeros", () => {
// 1.20001 tokens → "1.2", not "1.2000"
Expand All @@ -22,8 +19,8 @@ describe("token balance trailing zeros after the 4-digit cut", () => {
});

it("re-trims a sub-1 balance whose leading zeros are preserved", () => {
// 0.0001200005 tokens → "0.00012": the three leading zeros stay, the
// zeros exposed by the cut go.
// 0.0001200005 tokens → "0.00012": the leading zeros stay, the zeros
// exposed by the truncation go.
expect(formatTokenBalanceFull("120000500000000", 18)).toBe("0.00012");
});

Expand All @@ -33,50 +30,40 @@ describe("token balance trailing zeros after the 4-digit cut", () => {
});

it("re-trims through the compact formatter below the K threshold", () => {
// 999.90001 tokens stays under 1,000, so the compact path falls through
// to the full formatter and inherits the same cut.
// 999.90001 tokens stays under 1,000, so the compact formatter renders it
// in full and truncates the same way.
expect(formatTokenBalance("999900010000000000000", 18)).toBe("999.9");
});

it("keeps interior zeros that survive the cut (control)", () => {
// 1.2001 tokens — all four digits are significant, so the trim must not
// reach the interior zeros. Passes with and without the fix; it pins the
// over-trimming regression the fix must not introduce.
// 1.2001 tokens: every visible digit is significant, so nothing is trimmed.
expect(formatTokenBalanceFull("1200100000000000000", 18)).toBe("1.2001");
});

it("keeps an interior zero while dropping the tail the cut exposes", () => {
// 1.0200001 tokens → "1.02". The trim is anchored, so it must stop at the
// "2" and leave the zero in front of it alone while still removing the
// two zeros the cut turned into a tail. Was "1.0200".
// 1.0200001 tokens → "1.02": the interior zero stays, the exposed tail goes.
expect(formatTokenBalanceFull("1020000100000000000", 18)).toBe("1.02");
});

it("re-trims a sub-1 balance with no leading zeros", () => {
// 0.100001 tokens → "0.1". firstNonZero is 0 here, so no leading zeros are
// re-emitted and the cut's exposed tail is the whole visible fraction bar
// the first digit. Was "0.1000".
// 0.100001 tokens → "0.1": with no leading zeros, the exposed tail is the
// whole visible fraction but its first digit.
expect(formatTokenBalanceFull("100001000000000000", 18)).toBe("0.1");
});

it("re-trims a sub-1 balance for 6-decimal tokens", () => {
// 0.100001 USDC-like → "0.1". The sub-1 branch is reached at a different
// decimals scale than the 18-decimal cases above. Was "0.1000".
// 0.100001 USDC-like → "0.1", at a 6-decimal scale.
expect(formatTokenBalanceFull("100001", 6)).toBe("0.1");
});

it("returns the bare integer when the fraction is past the 4-digit window", () => {
// 1.000000001 tokens → "1". The "< 0.0001" early return only guards the
// sub-1 branch, so here the cut yields four zeros, the re-trim empties
// them, and the existing `capped ?` guard drops the point. Was "1.0000".
// 1.000000001 tokens → "1": the visible fraction is all zeros, so the
// decimal point goes with it.
expect(formatTokenBalanceFull("1000000001000000000", 18)).toBe("1");
});

it("leaves the compact K path untouched (control)", () => {
// 1000.20001 tokens is at/above the K threshold, so formatTokenBalance
// returns via compactScale and never reaches the changed lines. Passes
// with and without the fix; it controls for the compact path, which the
// control above does not cover.
// 1000.20001 tokens is at the K threshold, so the compact scale renders it.
expect(formatTokenBalance("1000200010000000000000", 18)).toBe("1K");
});
});
7 changes: 2 additions & 5 deletions src/shared/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,14 @@ export function formatTokenBalanceFull(raw: string, decimals: number): string {
const trimmedFrac = fractionalPart.replace(/0+$/, "");

if (integerPart !== "0") {
// Trim again after capping: the cut can land on a zero that was only
// interior before (1.20001 → "20001" → "2000"), and those zeros are
// trailing once the digits behind them are dropped.
// Truncation can expose trailing zeros (1.20001 → "2000").
const capped = trimmedFrac.slice(0, 4).replace(/0+$/, "");
return capped ? `${integerPart}.${capped}` : integerPart;
}

if (firstNonZero >= 4) return "< 0.0001";
const significantDigits = trimmedFrac.slice(firstNonZero);
// Same cut, same re-trim (0.0001200005 → "1200005" → "1200" → "12"). The
// leading digit is non-zero, so this always keeps at least one digit.
// The first significant digit is nonzero, so trimming keeps at least one.
const visible = significantDigits.slice(0, 4).replace(/0+$/, "");
return `0.${"0".repeat(firstNonZero)}${visible}`;
}
Expand Down