fix(units): keep converted values on the rounding helpers

The narrative, the workout distance and the elevation chart converted
through the unrounded escape hatch, and the trend caption scaled its
noise floor off the raw factor. They now take the rounded helpers, so a
narrated pound or mmol/L figure reads at the grain every screen shows
it, and the caption's floors are declared canonically (the distance
floor in metres). The trends row joins the chart-feeding surfaces that
may hand a raw scale to a chart.

Refs #1067
This commit is contained in:
Marc-André Bombeck committed 2026-10-01 09:17:44 +02:00
1 parent 0039bd1d1f
commit 9a282ed698
7 files changed
+33 -24

No files matched your search

@@ -156,15 +156,15 @@ describe("assemblePeriodNarrativeContext — the reader's units", () => {
);
const g = ctx.metricDeltas.find((d) => d.type === "BLOOD_GLUCOSE")!;
expect(g.unit).toBe("mmol/L");
expect(g.current).toBe(5.99);
expect(g.prior).toBe(4.99);
expect(g.current).toBe(6);
expect(g.prior).toBe(5);
expect(g.delta).toBe(1);
// Percent is unit-free.
expect(g.deltaPercent).toBe(20);
const w = ctx.metricDeltas.find((d) => d.type === "WEIGHT")!;
expect(w.unit).toBe("lb");
expect(w.current).toBe(180.78);
expect(w.delta).toBe(4.41);
expect(w.current).toBe(180.8);
expect(w.delta).toBe(4.4);
const prose = buildDeterministicNarrative(ctx, "en");
expect(prose).toContain("mmol/L");
@@ -172,7 +172,7 @@ describe("assemblePeriodNarrativeContext — the reader's units", () => {
expect(prose).not.toContain("mg/dL");
expect(prose).not.toContain("kg");
const prompt = buildNarrativeUserPrompt(ctx, "en");
expect(prompt).toContain("BLOOD_GLUCOSE: 5.99 mmol/L");
expect(prompt).toContain("BLOOD_GLUCOSE: 6 mmol/L");
expect(prompt).not.toContain("mg/dL");
});
@@ -231,10 +231,10 @@ describe("assemblePeriodNarrativeContext — the reader's units", () => {
);
const t = ctx.bandTransitions.find((b) => b.type === "BLOOD_GLUCOSE")!;
expect(t.unit).toBe("mmol/L");
expect(t.center).toBe(7.77);
expect(t.center).toBe(7.8);
expect(t.movedOut).toBe(true);
expect(buildNarrativeUserPrompt(ctx, "en")).toContain(
"BLOOD_GLUCOSE: 7.77 mmol/L",
"BLOOD_GLUCOSE: 7.8 mmol/L",
);
});
});
@@ -57,7 +57,8 @@ import { assembleDiscoveryMatrix } from "@/lib/insights/discovery-matrix";
import { resolveModuleMap } from "@/lib/modules/gate";
import { DEFAULT_TIMEZONE } from "@/lib/tz/format";
import {
applyDisplayTransformUnrounded,
applyDisplayTransform,
applyDisplayTransformDelta,
getReadingTransform,
isUnitSensitiveType,
resolveUnitPreferences,
@@ -135,8 +136,10 @@ function narratedUnit(
const transform = getReadingTransform(type, units);
return {
unit: transform.displayUnit,
abs: (v) => round2(applyDisplayTransformUnrounded(v, transform)),
delta: (v) => round2(v * transform.factor),
// Rounded to the unit's own grain (one decimal for lb and mmol/L), the
// grain every screen shows the same reading at.
abs: (v) => round2(applyDisplayTransform(v, transform)),
delta: (v) => round2(applyDisplayTransformDelta(v, transform)),
};
}
if (type === "SLEEP_DURATION") {
+4 -2
View File
@@ -148,7 +148,8 @@ export interface TrendSlotDescriptorMeta {
/**
* For a metric whose unit follows the reader's preference: the
* measurement type the caller resolves the unit of (and converts the
* series and the noise floor through) before describing it.
* series and the noise floor through) before describing it. Such a slot
* declares its `absoluteFloor` in the CANONICAL unit (kg, m, °C).
*/
unitType?: string;
config: TrendDescriptorConfig;
@@ -196,7 +197,8 @@ export const TREND_SLOT_DESCRIPTOR_META: Record<
},
distance: {
unitType: "WALKING_RUNNING_DISTANCE",
config: { absoluteFloor: 0.3, relativeFloor: 0.05, decimals: 1 },
// 300 m — canonical metres, converted to the reader's km or mi.
config: { absoluteFloor: 300, relativeFloor: 0.05, decimals: 1 },
},
vo2_max: {
unit: "",