Files
HealthLog/src/lib/insights/__tests__/memory.test.ts
T
Marc-André Bombeck b36c7b6697 feat(insights): serve status notes from the new table, only while available
Every status note reader and writer moves to InsightStatusCache: the
generators, the unchanged-data gates, the stale-while-revalidate read,
the ingest debounce, the previous-note prompt block and the
medication-compliance card with its per-medication lines. A stall now
opens a negative-cache window next to the note instead of a stub row
that shadowed it.

Stored model text is never served while its capability is unavailable,
whatever the reason: the status readers and gates check statusText, the
period narrative hides a model-written row while periodNarrative is
unavailable (the deterministic narrative is data and always shown), and
the daily digest drops reaction lines while reactionLines is
unavailable. A read-only miss now reports why (`unavailable` with the
capability's reason) and keeps hasProvider meaning provider presence, so
a withdrawn consent no longer reads as "no provider".

runStatusCompletion takes the capability as a required argument and
re-checks it before the provider chain is resolved, so a job queued
before a switch flipped still stops at the wire. The comprehensive
briefing re-checks briefing the same way. hasUsableStatusProvider is the
one presence probe.

Refs #975
2026-09-24 09:06:22 +02:00

152 lines
5.3 KiB
TypeScript

import { describe, expect, it, vi, beforeEach } from "vitest";
vi.mock("@/lib/db", () => ({
prisma: {
insightStatusCache: { findUnique: vi.fn() },
},
}));
// A reversible stand-in for the at-rest codec; what this file pins is which
// stored note becomes prompt context, not AES-GCM.
vi.mock("@/lib/ai/coach/bytes-codec", () => ({
encryptToBytes: (plain: string) => new TextEncoder().encode(`enc:${plain}`),
decryptFromBytes: (buf: Uint8Array) => {
const text = new TextDecoder().decode(buf);
if (!text.startsWith("enc:")) throw new Error("unknown key id");
return text.slice(4);
},
}));
import { prisma } from "@/lib/db";
import {
formatPreviousContextForPrompt,
getPreviousInsightContext,
} from "../memory";
function noteRow(text: string | null, generatedAt: Date | null) {
return {
textEncrypted:
text === null ? null : new TextEncoder().encode(`enc:${text}`),
itemsEncrypted: null,
inputHash: null,
snapshotHash: null,
dateKey: "2026-05-01",
generatedAt,
retryAt: null,
negativeReason: null,
};
}
beforeEach(() => {
vi.mocked(prisma.insightStatusCache.findUnique).mockReset();
});
describe("getPreviousInsightContext", () => {
it("returns null when no stored note exists", async () => {
vi.mocked(prisma.insightStatusCache.findUnique).mockResolvedValueOnce(null);
const ctx = await getPreviousInsightContext("u-1", "general-status", "en");
expect(ctx).toBeNull();
});
it("returns null when the row carries no note (only a negative window)", async () => {
vi.mocked(prisma.insightStatusCache.findUnique).mockResolvedValueOnce(
noteRow(null, null) as never,
);
const ctx = await getPreviousInsightContext("u-1", "general-status", "en");
expect(ctx).toBeNull();
});
it("returns the stored note with its age", async () => {
const sevenDaysAgo = new Date(Date.now() - 7 * 86_400_000);
vi.mocked(prisma.insightStatusCache.findUnique).mockResolvedValueOnce(
noteRow("Your BP averaged 130/80 — high-normal.", sevenDaysAgo) as never,
);
const ctx = await getPreviousInsightContext("u-1", "general-status", "en");
expect(ctx).not.toBeNull();
expect(ctx!.ageDays).toBe(7);
expect(ctx!.text).toContain("130/80");
expect(ctx!.generatedAt).toBe(sevenDaysAgo.toISOString());
});
it("caps the text length so a verbose snapshot cannot bloat the prompt", async () => {
const big = "x".repeat(5000);
vi.mocked(prisma.insightStatusCache.findUnique).mockResolvedValueOnce(
noteRow(big, new Date(Date.now() - 86_400_000)) as never,
);
const ctx = await getPreviousInsightContext("u-1", "general-status", "en");
expect(ctx!.text.length).toBeLessThanOrEqual(1502); // 1500 + "…"
expect(ctx!.text.endsWith("…")).toBe(true);
});
it("filters by minAgeHours so today's earlier run isn't treated as 'previous'", async () => {
// Written 23 h ago: inside a 24 h floor, so not "previous".
vi.mocked(prisma.insightStatusCache.findUnique).mockResolvedValueOnce(
noteRow("Earlier today.", new Date(Date.now() - 23 * 3_600_000)) as never,
);
expect(
await getPreviousInsightContext("u-1", "general-status", "en", 24),
).toBeNull();
const call = vi.mocked(prisma.insightStatusCache.findUnique).mock
.calls[0]?.[0] as {
where: { userId_metric_locale: Record<string, string> };
};
expect(call.where.userId_metric_locale).toEqual({
userId: "u-1",
metric: "general",
locale: "en",
});
// Written 25 h ago: past the floor, so it is the previous analysis.
vi.mocked(prisma.insightStatusCache.findUnique).mockResolvedValueOnce(
noteRow("Yesterday.", new Date(Date.now() - 25 * 3_600_000)) as never,
);
const ctx = await getPreviousInsightContext(
"u-1",
"general-status",
"en",
24,
);
expect(ctx?.text).toBe("Yesterday.");
});
});
describe("formatPreviousContextForPrompt", () => {
it("returns a 'no history' instruction when ctx is null (English)", () => {
const out = formatPreviousContextForPrompt(null, "en");
expect(out).toContain("PREVIOUS ANALYSIS: none on file");
expect(out).toContain("no improvement/regression delta to surface");
});
it("returns a 'no history' instruction when ctx is null (German)", () => {
const out = formatPreviousContextForPrompt(null, "de");
expect(out).toContain("VORHERIGE ANALYSE: keine vorhanden");
});
it("formats a 7-days-ago context with date + text + comparison instruction (English)", () => {
const out = formatPreviousContextForPrompt(
{
generatedAt: "2026-05-01T08:00:00.000Z",
ageDays: 7,
text: "Your BP averaged 130/80 last week.",
},
"en",
);
expect(out).toContain("PREVIOUS ANALYSIS (7 days ago, 2026-05-01)");
expect(out).toContain("Your BP averaged 130/80 last week.");
expect(out).toContain('"down 4 mmHg from your last check"');
});
it("formats a same-day context (German)", () => {
const out = formatPreviousContextForPrompt(
{
generatedAt: "2026-05-08T06:00:00.000Z",
ageDays: 0,
text: "Frühanalyse: 130/80 grenzwertig.",
},
"de",
);
expect(out).toContain("VORHERIGE ANALYSE (heute früher, 2026-05-08)");
expect(out).toContain("Frühanalyse: 130/80 grenzwertig.");
});
});