mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(insights): correct the active-energy assessment id and share the status card
The active-energy page passed `ACTIVE_ENERGY_BURNED` as `statusMetric`, but that is the DB MeasurementType, not the registry id the metric-status route accepts. The route's closed enum only knows `ACTIVE_ENERGY`, so every fetch 422'd and the assessment card silently fell back to its empty body. Point the page at the `ACTIVE_ENERGY` registry id and type `statusMetric` (plus the `useInsightMetricStatus` metric param) to the closed `MetricStatusMetricId` union instead of a bare string, so a future MeasurementType-vs-registry-id mismatch is a compile error rather than a silent 422. A typecheck pass over all 29 pages confirms active-energy was the only diverging one. Extract a shared `MetricStatusCard` that owns the `useInsightMetricStatus` hook and the assessment-card wiring, collapsing the duplicated mount block in the HealthKit metric scaffold and the bespoke sleep page to a single line each (only the icon differs). Behaviour is unchanged: the card mounts only on the data-bearing branch, so a source-less account never fires an assessment round-trip. Add a test asserting the active-energy page wires an accepted registry id. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> (cherry picked from commit 6e371aaeb9698f7b4251c22e1d8f381ddfa70a00)
This commit is contained in:
1 parent
5827ef58cb
commit
58b793ee4a
6 files changed
+112
-48
No files matched your search
@@ -50,6 +50,8 @@ vi.mock("@/hooks/use-insight-status", async (importOriginal) => {
|
||||
});
|
||||
|
||||
import { HealthKitMetricPage } from "../healthkit-metric-page";
|
||||
import { METRIC_STATUS_IDS } from "@/lib/insights/metric-status-registry";
|
||||
import InsightsActiveEnergyPage from "@/app/insights/active-energy/page";
|
||||
|
||||
function render(node: React.ReactNode, locale: "en" | "de" = "en") {
|
||||
const queryClient = new QueryClient({
|
||||
@@ -116,9 +118,10 @@ describe("<HealthKitMetricPage> status mount", () => {
|
||||
|
||||
const html = render(<HealthKitMetricPage {...baseProps} />);
|
||||
|
||||
// No assessment heading, and the generic fetch is disabled.
|
||||
// No assessment heading, and the card (which owns the hook) never
|
||||
// mounts, so the generic fetch is never wired.
|
||||
expect(html).not.toContain("Assessment");
|
||||
expect(metricStatusMock).toHaveBeenCalledWith("", false);
|
||||
expect(metricStatusMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("renders the insufficient-data empty state (no card) when the metric has no data", () => {
|
||||
@@ -132,9 +135,30 @@ describe("<HealthKitMetricPage> status mount", () => {
|
||||
/>,
|
||||
);
|
||||
|
||||
// Empty branch: no settled assessment text, and the status fetch is
|
||||
// disabled so a source-less account never fires the round-trip.
|
||||
// Empty branch: the page short-circuits to the empty state before the
|
||||
// card JSX, so no settled assessment text and the card (and its fetch)
|
||||
// never mounts — a source-less account never fires the round-trip.
|
||||
expect(html).not.toContain("Your HRV has trended upward");
|
||||
expect(metricStatusMock).toHaveBeenCalledWith("HEART_RATE_VARIABILITY", false);
|
||||
expect(metricStatusMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("wires the active-energy page to an accepted registry id (not the MeasurementType remap)", () => {
|
||||
// v1.8.7.1 (Design H1) — the active-energy page formerly passed
|
||||
// `ACTIVE_ENERGY_BURNED` (the DB MeasurementType), which the route's
|
||||
// closed enum rejects with 422; the registry id is `ACTIVE_ENERGY`.
|
||||
// Render the real page module and assert the hook receives a metric id
|
||||
// the route accepts.
|
||||
analyticsMock.mockReturnValue({
|
||||
data: { summaries: { ACTIVE_ENERGY_BURNED: { count: 30 } } },
|
||||
isEmpty: false,
|
||||
});
|
||||
metricStatusMock.mockReturnValue({ data: undefined, isLoading: false });
|
||||
|
||||
render(<InsightsActiveEnergyPage />);
|
||||
|
||||
expect(metricStatusMock).toHaveBeenCalled();
|
||||
const passedMetric = metricStatusMock.mock.calls[0][0] as string;
|
||||
expect(passedMetric).toBe("ACTIVE_ENERGY");
|
||||
expect(METRIC_STATUS_IDS).toContain(passedMetric);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user