feat(insights): wire CorrelationRow + TrendsRow into /insights

Compute the three pre-defined correlation hypotheses
(BP × compliance, mood × pulse, weight × weekday) on the trailing
30-day window in `/api/analytics` and surface the typed result
shapes on the response payload. Each runner is gated on n >= 14
+ p < 0.05; below the bar the result.status === "insufficient"
and the per-card EmptyState paints in place of a misleading
near-zero scatter.

The insights page now mounts:

- `<CorrelationRow>` between the Daily Briefing and the Advisor
  card — the layout slot the artboard reserves for the patterns
  surface.
- `<TrendsRow>` immediately below it, fed from
  `advisor.payload.trendAnnotations` (lifted client-side via
  `trendAnnotationsSchema.safeParse`). Legacy cached payloads
  predating PROMPT_VERSION 4.20.1 surface the per-metric
  empty-state hint instead of a half-rendered annotation.

The wide-event annotation captures per-hypothesis status so admin
observability can attribute coverage gaps without re-querying
the DB.

10 source-scan tests pin the load-bearing imports, the JSX mounts,
the analytics-route surfacing, and the malformed-payload null
fallback in the advisor hook.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-10 16:53:29 +02:00
1 parent 42591c6cec
commit c83cb2311d
4 files changed
+359

No files matched your search

@@ -0,0 +1,97 @@
import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
/**
* v1.4.20 phase B3 — guards for the `/insights` wiring of the new
* Trends row + Correlation row.
*
* The page-source scan pattern mirrors `insights-polish.test.ts` —
* each test pins one load-bearing import / JSX mount so a future
* refactor can't accidentally drop a wave of features without
* breaking a CI lane.
*/
const ROOT = join(__dirname, "../../..");
const INSIGHTS_PATH = join(ROOT, "src/app/insights/page.tsx");
const ANALYTICS_ROUTE_PATH = join(
ROOT,
"src/app/api/analytics/route.ts",
);
const ADVISOR_HOOK_PATH = join(
ROOT,
"src/components/insights/use-insights-advisor.ts",
);
function load(path: string): string {
return readFileSync(path, "utf8");
}
describe("v1.4.20 B3 — /insights mounts CorrelationRow + TrendsRow", () => {
it("imports both new components", () => {
const src = load(INSIGHTS_PATH);
expect(src).toContain(
'from "@/components/insights/correlation-row"',
);
expect(src).toContain(
'from "@/components/insights/trends-row"',
);
});
it("mounts <CorrelationRow> behind a non-null analytics.correlations guard", () => {
const src = load(INSIGHTS_PATH);
expect(src).toMatch(/analytics\?\.correlations\s*&&\s*\(/);
expect(src).toMatch(/<CorrelationRow\b/);
});
it("mounts <TrendsRow> with annotations from the advisor payload", () => {
const src = load(INSIGHTS_PATH);
expect(src).toMatch(/<TrendsRow[\s\S]*?annotations=/);
expect(src).toMatch(/advisor\.payload\?\.trendAnnotations/);
});
it("legacy advisor payload (null trendAnnotations) is handled with `?? null`", () => {
const src = load(INSIGHTS_PATH);
expect(src).toMatch(/trendAnnotations\s*\?\?\s*null/);
});
});
describe("v1.4.20 B3 — /api/analytics surfaces correlations", () => {
it("imports the three correlation runners", () => {
const src = load(ANALYTICS_ROUTE_PATH);
expect(src).toContain("correlateBpCompliance");
expect(src).toContain("correlateMoodPulse");
expect(src).toContain("correlateWeightWeekday");
});
it("returns a `correlations` block on the success payload", () => {
const src = load(ANALYTICS_ROUTE_PATH);
expect(src).toMatch(/correlations,/);
expect(src).toMatch(/computeCorrelationHypotheses/);
});
it("annotates the wide event with per-hypothesis status", () => {
const src = load(ANALYTICS_ROUTE_PATH);
expect(src).toMatch(
/annotate\(\{[\s\S]*correlations:[\s\S]*bpCompliance:[\s\S]*moodPulse:[\s\S]*weightWeekday:/,
);
});
});
describe("v1.4.20 B3 — useInsightsAdvisorQuery lifts trendAnnotations", () => {
it("imports trendAnnotationsSchema for client-side validation", () => {
const src = load(ADVISOR_HOOK_PATH);
expect(src).toContain("trendAnnotationsSchema");
});
it("the lifted payload exposes `trendAnnotations`", () => {
const src = load(ADVISOR_HOOK_PATH);
expect(src).toMatch(/trendAnnotations\?:/);
expect(src).toMatch(/payload\.trendAnnotations\s*=/);
});
it("malformed cached annotations resolve to null (not a half-render)", () => {
const src = load(ADVISOR_HOOK_PATH);
expect(src).toMatch(/parsed\.success\s*\?\s*parsed\.data\s*:\s*null/);
});
});