feat(insights): resolve direction verdicts, chart reference lines and mood patterns on the server

This commit is contained in:
Marc-André Bombeck committed 2026-10-15 23:39:11 +02:00
1 parent b91328d67e
commit 79f7eb199b
35 files changed
+2454 -50

No files matched your search

@@ -41,6 +41,22 @@ const AGGREGATE = {
tags: [],
stability: year,
stabilityByWindow: { 30: month, 90: null, 180: year, 365: year },
patternsByWindow: {
30: [],
90: [],
180: [],
365: [
{
id: "weekend",
kind: "weekend",
strength: "strong",
direction: "higher",
values: { subject: 4.2, comparison: 3.1, delta: 1.1 },
sampleSize: { subject: 100, comparison: 250 },
qValue: 0.001,
},
],
},
};
const call = (query = "") =>
@@ -78,6 +94,23 @@ describe("GET /api/mood/insights — stability per period", () => {
expect(body.data.stabilityWindowDays).toBe(90);
});
it("serves the patterns of the same window, without the cache's internals", async () => {
const year = await (await call()).json();
expect(year.data.patterns).toEqual([
{
id: "weekend",
kind: "weekend",
strength: "strong",
direction: "higher",
values: { subject: 4.2, comparison: 3.1, delta: 1.1 },
sampleSize: { subject: 100, comparison: 250 },
},
]);
expect(year.data).not.toHaveProperty("patternsByWindow");
const month = await (await call("?days=30")).json();
expect(month.data.patterns).toEqual([]);
});
it("refuses a window it does not offer", async () => {
const res = await call("?days=45");
expect(res.status).toBe(422);
+10 -2
View File
@@ -12,6 +12,8 @@ import {
type MoodStabilityWindow,
} from "@/lib/insights/mood-aggregates";
import { requireModuleEnabled } from "@/lib/modules/gate";
import { getServerTranslator } from "@/lib/i18n/server-translator";
import { resolveJobLocale } from "@/lib/i18n/job-locale";
export const dynamic = "force-dynamic";
@@ -33,7 +35,8 @@ export const dynamic = "force-dynamic";
*/
/**
* `?days=N` cuts `stability` to the trailing N days, so a period picker shows
* the steadiness of the period it shows. Without it the score covers the
* the steadiness of the period it shows. Since v1.43 it cuts `patterns` the
* same way. Without it the score covers the
* whole year, as it always has; `stabilityWindowDays` names the window the
* served score covers either way.
*/
@@ -75,5 +78,10 @@ export const GET = apiHandler(async (request: Request) => {
},
});
return apiSuccess(moodInsightsWire(result, stabilityWindowDays));
// v1.43 — `patterns` follows the same `?days` window, and a catalogue
// tag in a pattern is named in the reader's language.
const translator = getServerTranslator(await resolveJobLocale(user.locale));
return apiSuccess(
moodInsightsWire(result, stabilityWindowDays, (key) => translator.t(key)),
);
});