diff --git a/src/miniapps/fairshare/ResourceTiles.test.tsx b/src/miniapps/fairshare/ResourceTiles.test.tsx index 25bbbf1..6170705 100644 --- a/src/miniapps/fairshare/ResourceTiles.test.tsx +++ b/src/miniapps/fairshare/ResourceTiles.test.tsx @@ -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 = { 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> = { + [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(); + + 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(); diff --git a/src/miniapps/fairshare/ResourceTiles.tsx b/src/miniapps/fairshare/ResourceTiles.tsx index a1cbba5..4f90b0d 100644 --- a/src/miniapps/fairshare/ResourceTiles.tsx +++ b/src/miniapps/fairshare/ResourceTiles.tsx @@ -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"} of {supply} pool