From 8c434b5cb966fbf188fad37dbac5f7bda2cdecee Mon Sep 17 00:00:00 2001 From: OmO Orchestrator Date: Fri, 20 Nov 2026 22:34:01 -0500 Subject: [PATCH] fix(fairshare): format every remaining displayed number to 2 decimals The earlier fix only covered the CEEI allocation quantity (qty). Three more unformatted numbers remained on the same screen: the wallet balance, each tile's supply ("of X pool"), and the echoed need amount in the post-submit status message - all raw, no .toFixed(), all capable of showing many decimal digits (balance from any wallet.issue amount; supply from a real cross-app settlement like Farms' LP/GDD forecast, which is frequently non-round; the need echo straight from whatever a resident types). Fixed all three with .toFixed(2), matching the project's established convention. New tests prove each with a genuinely non-round value (not a coincidentally-clean fixture): a fractional wallet.issue amount, a fractional seed.resource.food value modeling Farms' real forecast credit, and a 9-decimal need amount typed into the real form and submitted via the real UI. --- src/miniapps/fairshare/ResourceTiles.test.tsx | 61 ++++++++++++++++++- src/miniapps/fairshare/ResourceTiles.tsx | 6 +- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/miniapps/fairshare/ResourceTiles.test.tsx b/src/miniapps/fairshare/ResourceTiles.test.tsx index 6170705..1ddf3d9 100644 --- a/src/miniapps/fairshare/ResourceTiles.test.tsx +++ b/src/miniapps/fairshare/ResourceTiles.test.tsx @@ -1,6 +1,6 @@ // @vitest-environment happy-dom import "@testing-library/jest-dom/vitest"; -import { cleanup, render, screen, waitFor } from "@testing-library/react"; +import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { db } from "../../lib/db"; import { append, readAll } from "../../lib/eventlog"; @@ -361,6 +361,65 @@ describe("ResourceTiles (allocation display formatting - coherence audit)", () = await waitForAllocationCycleCount("power", 1); await waitForAllocationCycleCount("cooling", 1); }); + + it("formats a fractional wallet balance to 2 decimals instead of echoing its raw precision", async () => { + await init(); + const accountId = toAccountId(await getPublicKey()); + + // A wallet.issue amount is just a number - nothing requires it to be a + // whole number, and the project's own convention elsewhere (e.g. + // Farms' harvest forecast) issues genuinely fractional amounts. + await append({ type: "wallet.issue", account: accountId, amount: 123.456789 }); + + render(); + + await waitFor(() => { + const text = screen.getByTestId("fairshare-balance").textContent ?? ""; + expect(text).toMatch(/^\d+\.\d\d$/); + expect(text).toBe((123.456789).toFixed(2)); + }); + }); + + it("formats a fractional resource supply (e.g. Farms' real LP/GDD forecast feeding food) to 2 decimals", async () => { + await init(); + const accountId = toAccountId(await getPublicKey()); + + // Mirrors the real cross-app chain: Farms' settleFarmsForecast credits + // food via a seed.resource.food event carrying the LP optimizer's real + // (frequently non-round) totalForecastYieldKg - this is not a + // contrived fixture, it is exactly how the food supply pool is + // populated in production. + await append({ type: "seed.resource.food", resource: "food", value: 187.654321 }); + await append({ type: "fairshare.need", agent: accountId, resource: "food", value: 1 }); + + render(); + + await waitFor(() => { + const text = screen.getByTestId("tile-food").textContent ?? ""; + expect(text).toContain((187.654321).toFixed(2)); + expect(text).not.toContain("187.654321"); + }); + }); + + it("formats the echoed need amount in the submit-status message to 2 decimals, even if a resident types more precision", async () => { + await init(); + + render(); + + await waitFor(() => { + expect(screen.getByTestId("fairshare-need-resource")).toBeInTheDocument(); + }); + fireEvent.change(screen.getByTestId("fairshare-need-amount"), { + target: { value: "9.123456789" }, + }); + fireEvent.submit(screen.getByRole("form", { name: "Report your need" })); + + await waitFor(() => { + const text = screen.getByTestId("fairshare-need-status").textContent ?? ""; + expect(text).toContain((9.123456789).toFixed(2)); + expect(text).not.toContain("9.123456789"); + }); + }); }); describe("ResourceTiles (failure path - no ledger balance yet)", () => { diff --git a/src/miniapps/fairshare/ResourceTiles.tsx b/src/miniapps/fairshare/ResourceTiles.tsx index 1e0dfe6..df37dcc 100644 --- a/src/miniapps/fairshare/ResourceTiles.tsx +++ b/src/miniapps/fairshare/ResourceTiles.tsx @@ -132,7 +132,7 @@ export function ResourceTiles() { } try { await postFairshareNeed(accountId, needResource, value); - setNeedStatus(`Reported a need of ${value} for ${needResource}.`); + setNeedStatus(`Reported a need of ${value.toFixed(2)} for ${needResource}.`); setNeedAmount(""); } catch (err) { setNeedStatus(err instanceof Error ? err.message : String(err)); @@ -148,7 +148,7 @@ export function ResourceTiles() { ? "--" : balance === undefined ? "No balance yet" - : balance} + : balance.toFixed(2)} @@ -187,7 +187,7 @@ export function ResourceTiles() { > {hasAllocation ? qty!.toFixed(2) : "No allocation yet"} - of {supply} pool + of {supply.toFixed(2)} pool );