merge: assemble capstone signal slices (S10 ECG narrative, S11 intraday pulse, S12 milestones)

This commit is contained in:
Marc-André Bombeck committed 2026-07-17 08:38:44 +02:00
commit 8ac38926c4
20 files changed
+806 -10

No files matched your search

@@ -0,0 +1,81 @@
import { describe, it, expect, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { I18nProvider } from "@/lib/i18n/context";
/**
* S10 — `<EcgCrossLink>` unit tests.
*
* The load-bearing behaviour under test:
* - data-availability gating: the pointer un-mounts entirely when the user
* has no ECG recordings;
* - the NON-DIAGNOSTIC framing: it surfaces only that recordings exist and
* the RECORDING DEVICE's own latest result, attributed to the device — no
* HealthLog interpretation, no waveform.
*
* `useAuth` + TanStack Query are mocked and assertions run through SSR, exactly
* like the sibling `ecg-section` test.
*/
vi.mock("@/hooks/use-auth", () => ({
useAuth: vi.fn(() => ({ isAuthenticated: true, user: null })),
}));
const useQueryMock = vi.fn();
vi.mock("@tanstack/react-query", () => ({
useQuery: (opts: unknown) => useQueryMock(opts),
}));
const { EcgCrossLink } = await import("../ecg-cross-link");
interface Item {
id: string;
classification: "IRREGULAR" | "NOT_DETECTED" | "INCONCLUSIVE" | null;
}
function render(
data: { recordings: Item[]; hasRecordings: boolean } | undefined,
) {
useQueryMock.mockReturnValue({ data });
return renderToStaticMarkup(
<I18nProvider initialLocale="en">
<EcgCrossLink />
</I18nProvider>,
);
}
describe("<EcgCrossLink>", () => {
it("renders nothing before the payload resolves", () => {
expect(render(undefined)).toBe("");
});
it("renders nothing when the user has no recordings (gate)", () => {
expect(render({ recordings: [], hasRecordings: false })).toBe("");
});
it("links into the ECG viewer and attributes the latest result to the device", () => {
const html = render({
recordings: [
{ id: "a", classification: "IRREGULAR" },
{ id: "b", classification: "NOT_DETECTED" },
],
hasRecordings: true,
});
expect(html).toContain('data-slot="ecg-cross-link"');
expect(html).toContain('href="/insights#ecg"');
expect(html).toContain("2 recordings on file.");
// The latest (first) recording's DEVICE result, attributed to the device.
expect(html).toContain("Latest device result:");
// Never a HealthLog interpretation.
expect(html).not.toMatch(/we (detected|found)|our (reading|analysis)/i);
});
it("omits the result line when the latest recording has no device verdict", () => {
const html = render({
recordings: [{ id: "a", classification: null }],
hasRecordings: true,
});
expect(html).toContain("1 recording on file.");
expect(html).not.toContain("Latest device result:");
});
});
+106
View File
@@ -0,0 +1,106 @@
"use client";
import Link from "next/link";
import { ArrowRight, HeartPulse } from "lucide-react";
import { useQuery } from "@tanstack/react-query";
import { useAuth } from "@/hooks/use-auth";
import { queryKeys } from "@/lib/query-keys";
import { apiGet } from "@/lib/api/api-fetch";
import { useTranslations } from "@/lib/i18n/context";
import { cn } from "@/lib/utils";
import { TileHeader } from "@/components/insights/tile-header";
/**
* S10 — ECG cross-link, the pointer from the resting-HR / pulse context into
* the ECG viewer (`/insights#ecg`).
*
* NON-DIAGNOSTIC (load-bearing, mirrors `EcgSection` / `RhythmEventsCard`): the
* card surfaces ONLY that recordings exist + the RECORDING DEVICE's OWN latest
* result, attributed to the device. HealthLog never reads or interprets the
* waveform, and this pointer never touches it — the list route returns metadata
* only. All copy renders as plain React text children (no markdown library).
*
* Data-availability-gated: the card un-mounts entirely (`return null`) when the
* user has no recordings, so a pulse page without any ECG never paints it. It
* reuses the SAME query cell as `EcgSection` (`insightsEcgList`), so the two
* surfaces share one cache entry with an identical response shape.
*/
type EcgClassification = "IRREGULAR" | "NOT_DETECTED" | "INCONCLUSIVE" | null;
interface EcgRecordingListItem {
id: string;
classification: EcgClassification;
}
interface EcgListResponse {
recordings: EcgRecordingListItem[];
hasRecordings: boolean;
}
/** The device verdict, surfaced verbatim and attributed to the device. */
const RESULT_LABEL_KEYS: Record<string, string> = {
IRREGULAR: "insights.ecg.result.irregular",
NOT_DETECTED: "insights.ecg.result.notDetected",
INCONCLUSIVE: "insights.ecg.result.inconclusive",
};
interface EcgCrossLinkProps {
enabled?: boolean;
className?: string;
}
export function EcgCrossLink({ enabled = true, className }: EcgCrossLinkProps) {
const { isAuthenticated } = useAuth();
const { t } = useTranslations();
const { data } = useQuery({
queryKey: queryKeys.insightsEcgList(),
queryFn: async () => {
try {
return await apiGet<EcgListResponse>("/api/insights/ecg");
} catch {
throw new Error(t("insights.ecg.loadError"));
}
},
enabled: enabled && isAuthenticated,
});
// Data-availability gate — never paint an empty pointer.
if (!data || !data.hasRecordings || data.recordings.length === 0) return null;
const count = data.recordings.length;
const latestClassification = data.recordings[0]?.classification ?? null;
const resultKey = latestClassification
? RESULT_LABEL_KEYS[latestClassification]
: null;
const countText =
count === 1
? t("insights.ecg.crossLink.recordingsOne")
: t("insights.ecg.crossLink.recordingsMany", { count });
const resultText = resultKey
? t("insights.ecg.crossLink.latestResult", { result: t(resultKey) })
: null;
return (
<Link
href="/insights#ecg"
data-slot="ecg-cross-link"
className={cn(
"bg-card hover:bg-accent/40 focus-visible:ring-ring/50 block space-y-1.5 rounded-xl border p-4 transition-colors focus-visible:ring-2 focus-visible:outline-none",
className,
)}
>
<TileHeader icon={HeartPulse} title={t("insights.ecg.crossLink.title")} />
<span className="text-muted-foreground block text-xs leading-snug">
{resultText ? `${countText} ${resultText}` : countText}
</span>
<span className="text-primary inline-flex shrink-0 items-center gap-1 text-xs font-medium">
{t("insights.ecg.crossLink.cta")}
<ArrowRight className="h-4 w-4" aria-hidden="true" />
</span>
</Link>
);
}
+2 -1
View File
@@ -118,9 +118,10 @@ export function EcgSection({ enabled = true, className }: EcgSectionProps) {
return (
<section
id="ecg"
data-slot="ecg-section"
aria-label={t("insights.ecg.sectionTitle")}
className={cn("space-y-3", className)}
className={cn("scroll-mt-24 space-y-3", className)}
>
<SectionHeading icon={Activity} title={t("insights.ecg.sectionTitle")} />
<div