fix(fairshare): format CEEI allocation quantity to 2 decimals

ResourceTiles.tsx rendered the CEEI water-filling solver's raw iterative
output directly (bare {qty}, no formatting) for all 4 resource tiles. The
demo's seed data always used parallel need vectors across agents, which a
Fisher-market CEEI solve degenerates to a clean equal-budget-share split
for regardless of magnitude - so the bug never showed in the recorded
demo. A real resident's real reported need (or any non-parallel need
vectors across residents) produces a genuine fractional market-clearing
split with many decimal digits, rendered verbatim with no truncation.

Fixed with .toFixed(2), matching the project's existing 2-decimal
convention (Farms' harvest forecast, Water Watch's reading value). New
test uses a genuinely non-degenerate (non-parallel) need scenario,
confirmed via a sanity check that the raw solver output actually carries
more than 2 decimal digits before asserting the rendered text is
truncated to exactly 2 - proving the fix against the real bug mechanism,
not a coincidentally-round fixture.
This commit is contained in:
OmO Orchestrator committed 2026-11-20 19:46:27 -05:00
1 parent c630205fa1
commit 3b7bc6b67c
2 files changed
+67 -1

No files matched your search

@@ -297,6 +297,72 @@ describe("ResourceTiles (power/cooling allocation-cycle coherence fix)", () => {
});
});
describe("ResourceTiles (allocation display formatting - coherence audit)", () => {
it("truncates the CEEI solver's raw iterative output to 2 decimals instead of echoing its full floating-point precision", async () => {
await init();
const accountId = toAccountId(await getPublicKey());
// The exact multi-good, unequal-weight scenario the happy-path test
// above already uses - a single-resource market with equal budgets is
// degenerate (it always splits 50/50 regardless of stated need, since
// relative utility only matters across MULTIPLE competing goods). This
// is why the bug shipped unnoticed: the demo's seed data only ever
// drove power/cooling through single-resource top-ups. A real resident
// reporting needs across several resources at once - exactly what the
// "Report your need" form lets anyone do - genuinely produces a
// fractional, many-decimal market-clearing split.
// Deliberately NON-parallel need vectors across the two agents (not
// merely one agent scaling the other's preferences by a constant
// factor, which this screen's OWN happy-path test fixture above
// happens to do - and which a Fisher-market CEEI solve always
// degenerates to a clean equal-budget-share split for, regardless of
// need magnitude). This agent cares relatively more about power; the
// other cares relatively more about water - a genuine price-clearing
// solve across competing goods, exactly what produces a real
// fractional market outcome.
const supplies: Record<string, number> = { power: 1000, water: 800, food: 600, cooling: 400 };
for (const [resource, value] of Object.entries(supplies)) {
await append({ type: `seed.resource.${resource}`, resource, value });
}
const needsByAgent: Record<string, Record<string, number>> = {
[accountId]: { power: 10, water: 1, food: 3, cooling: 2 },
"other-agent": { power: 1, water: 10, food: 2, cooling: 3 },
};
for (const [agent, needs] of Object.entries(needsByAgent)) {
for (const resource of FAIRSHARE_RESOURCES) {
await append({ type: "fairshare.need", agent, resource, value: needs[resource] });
}
}
render(<ResourceTiles />);
const rawAllocation = computeFairshareAllocation(await readAll()).allocation[accountId];
// Sanity-check the fixture actually exercises the bug on at least one
// resource: some real, unrounded solver output here must carry more
// than 2 meaningful decimal digits, or this test would pass even with
// the bug still present and prove nothing.
const hasLongDecimal = FAIRSHARE_RESOURCES.some(
(resource) => (String(rawAllocation[resource]).split(".")[1] ?? "").length > 2,
);
expect(hasLongDecimal).toBe(true);
for (const resource of FAIRSHARE_RESOURCES) {
await waitFor(() => {
const text = screen.getByTestId(`tile-${resource}-allocation`).textContent ?? "";
// Exactly 2 digits after the decimal point on every resource tile -
// never the solver's raw 9+ digit precision (the user-visible bug:
// several resource tiles on this one screen each showing a long,
// unformatted float).
expect(text).toMatch(/^\d+\.\d\d$/);
expect(text).toBe(rawAllocation[resource].toFixed(2));
});
}
await waitForAllocationCycleCount("power", 1);
await waitForAllocationCycleCount("cooling", 1);
});
});
describe("ResourceTiles (failure path - no ledger balance yet)", () => {
it("shows an honest 'no balance yet' state for a brand-new identity, never a fabricated 0 or crash", async () => {
await init();
+1 -1
View File
@@ -192,7 +192,7 @@ export function ResourceTiles() {
className={styles.allocationValue}
data-testid={`tile-${resource}-allocation`}
>
{hasAllocation ? qty : "No allocation yet"}
{hasAllocation ? qty!.toFixed(2) : "No allocation yet"}
</span>
<span className={styles.supplyValue}>of {supply} pool</span>
</div>