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.
This commit is contained in:
OmO Orchestrator committed 2026-11-20 22:34:01 -05:00
1 parent 31fbe9112e
commit 8c434b5cb9
2 files changed
+63 -4

No files matched your search

+60 -1
View File
@@ -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(<ResourceTiles />);
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(<ResourceTiles />);
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(<ResourceTiles />);
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)", () => {
+3 -3
View File
@@ -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)}
</div>
</div>
@@ -187,7 +187,7 @@ export function ResourceTiles() {
>
{hasAllocation ? qty!.toFixed(2) : "No allocation yet"}
</span>
<span className={styles.supplyValue}>of {supply} pool</span>
<span className={styles.supplyValue}>of {supply.toFixed(2)} pool</span>
</div>
</article>
);