mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
style: prettier on touched insights/coach surfaces
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
21d064cfc6
commit
73e51d785e
40 files changed
+415
-413
No files matched your search
@@ -82,16 +82,13 @@ describe("dailyBriefingKeyFindingSchema", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.each(["good", "watch", "info"] as const)(
|
||||
"accepts tone=%s",
|
||||
(tone) => {
|
||||
const result = dailyBriefingKeyFindingSchema.safeParse({
|
||||
...baseFinding,
|
||||
tone,
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
},
|
||||
);
|
||||
it.each(["good", "watch", "info"] as const)("accepts tone=%s", (tone) => {
|
||||
const result = dailyBriefingKeyFindingSchema.safeParse({
|
||||
...baseFinding,
|
||||
tone,
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects an unknown tone", () => {
|
||||
const result = dailyBriefingKeyFindingSchema.safeParse({
|
||||
|
||||
@@ -62,9 +62,9 @@ describe("trendAnnotationsSchema", () => {
|
||||
|
||||
it("rejects an annotation > 200 chars", () => {
|
||||
const tooLong = "x".repeat(201);
|
||||
expect(
|
||||
trendAnnotationsSchema.safeParse({ bp: tooLong }).success,
|
||||
).toBe(false);
|
||||
expect(trendAnnotationsSchema.safeParse({ bp: tooLong }).success).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it("accepts an annotation at the 200-char boundary", () => {
|
||||
|
||||
@@ -110,8 +110,7 @@ describe("weeklyReportSchema", () => {
|
||||
it("rejects more than 5 entries in any bullet list", () => {
|
||||
const six = ["a", "b", "c", "d", "e", "f"];
|
||||
expect(
|
||||
weeklyReportSchema.safeParse({ ...validReport, goingWell: six })
|
||||
.success,
|
||||
weeklyReportSchema.safeParse({ ...validReport, goingWell: six }).success,
|
||||
).toBe(false);
|
||||
expect(
|
||||
weeklyReportSchema.safeParse({ ...validReport, worthWatching: six })
|
||||
@@ -134,8 +133,7 @@ describe("weeklyReportSchema", () => {
|
||||
|
||||
it("rejects empty strings in bullet arrays", () => {
|
||||
expect(
|
||||
weeklyReportSchema.safeParse({ ...validReport, goingWell: [""] })
|
||||
.success,
|
||||
weeklyReportSchema.safeParse({ ...validReport, goingWell: [""] }).success,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -21,9 +21,7 @@ function moodEntries(scores: number[]): Array<{ date: string; score: number }> {
|
||||
}));
|
||||
}
|
||||
|
||||
function weightSeries(
|
||||
values: number[],
|
||||
): Array<{ date: string; kg: number }> {
|
||||
function weightSeries(values: number[]): Array<{ date: string; kg: number }> {
|
||||
return values.map((kg, i) => ({
|
||||
date: new Date(Date.UTC(2026, 4, i + 1)).toISOString(),
|
||||
kg,
|
||||
@@ -36,24 +34,28 @@ describe("linearRegressionSlope", () => {
|
||||
it("returns null for fewer than two points", () => {
|
||||
expect(linearRegressionSlope([])).toBeNull();
|
||||
expect(
|
||||
linearRegressionSlope([
|
||||
{ date: "2026-05-01T00:00:00Z", value: 80 },
|
||||
]),
|
||||
linearRegressionSlope([{ date: "2026-05-01T00:00:00Z", value: 80 }]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("detects a clean upward slope (units / day)", () => {
|
||||
const slope = linearRegressionSlope(weightSeries([80, 81, 82, 83, 84]).map(
|
||||
(p) => ({ date: p.date, value: p.kg }),
|
||||
));
|
||||
const slope = linearRegressionSlope(
|
||||
weightSeries([80, 81, 82, 83, 84]).map((p) => ({
|
||||
date: p.date,
|
||||
value: p.kg,
|
||||
})),
|
||||
);
|
||||
expect(slope).not.toBeNull();
|
||||
expect(slope!).toBeCloseTo(1, 5);
|
||||
});
|
||||
|
||||
it("detects a clean downward slope", () => {
|
||||
const slope = linearRegressionSlope(weightSeries([85, 84, 83, 82, 81]).map(
|
||||
(p) => ({ date: p.date, value: p.kg }),
|
||||
));
|
||||
const slope = linearRegressionSlope(
|
||||
weightSeries([85, 84, 83, 82, 81]).map((p) => ({
|
||||
date: p.date,
|
||||
value: p.kg,
|
||||
})),
|
||||
);
|
||||
expect(slope!).toBeCloseTo(-1, 5);
|
||||
});
|
||||
});
|
||||
@@ -101,7 +103,9 @@ describe("weightTrendAlignment", () => {
|
||||
});
|
||||
|
||||
it("returns null with fewer than two readings", () => {
|
||||
expect(weightTrendAlignment(weightSeries([80]), { min: 75, max: 80 })).toBeNull();
|
||||
expect(
|
||||
weightTrendAlignment(weightSeries([80]), { min: 75, max: 80 }),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ export function weightTrendAlignment(
|
||||
// Below the band: slope > 0 closes the gap.
|
||||
const closing = latest > target.max ? slope < 0 : slope > 0;
|
||||
if (slope === 0) return 50; // exactly stable counts as "neither"
|
||||
return Math.round((closing ? 50 + 50 * normalised : 50 - 50 * normalised));
|
||||
return Math.round(closing ? 50 + 50 * normalised : 50 - 50 * normalised);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,9 +211,7 @@ export function computeHealthScore(
|
||||
previous?: HealthScoreInput,
|
||||
): HealthScoreResult {
|
||||
const bpValue =
|
||||
input.bpInTargetRate === null
|
||||
? null
|
||||
: clampToHundred(input.bpInTargetRate);
|
||||
input.bpInTargetRate === null ? null : clampToHundred(input.bpInTargetRate);
|
||||
|
||||
const target = deriveWeightTarget(input.weightTargetKg);
|
||||
const weightValue = weightTrendAlignment(input.weightSeriesLast30d, target);
|
||||
@@ -261,10 +259,7 @@ function redistribute(values: {
|
||||
for (const key of ["bp", "weight", "mood", "compliance"] as const) {
|
||||
if (values[key] !== null) present.push(key);
|
||||
}
|
||||
const totalBaseWeight = present.reduce(
|
||||
(s, key) => s + BASE_WEIGHTS[key],
|
||||
0,
|
||||
);
|
||||
const totalBaseWeight = present.reduce((s, key) => s + BASE_WEIGHTS[key], 0);
|
||||
const weightFor = (key: keyof typeof BASE_WEIGHTS): number => {
|
||||
if (values[key] === null) return 0;
|
||||
if (totalBaseWeight === 0) return 0;
|
||||
|
||||
@@ -70,8 +70,8 @@ describe("pearson", () => {
|
||||
it("returns r near 0 for noise, with p above the surface threshold", () => {
|
||||
// Deterministic noise (no Math.random) so the test is reproducible.
|
||||
const xs = [
|
||||
0.1, 0.5, 0.9, 0.2, 0.7, 0.4, 0.6, 0.3, 0.8, 0.05, 0.95, 0.15, 0.85,
|
||||
0.55, 0.25,
|
||||
0.1, 0.5, 0.9, 0.2, 0.7, 0.4, 0.6, 0.3, 0.8, 0.05, 0.95, 0.15, 0.85, 0.55,
|
||||
0.25,
|
||||
];
|
||||
const ys = [
|
||||
0.5, 0.1, 0.7, 0.3, 0.2, 0.9, 0.4, 0.6, 0.05, 0.95, 0.85, 0.55, 0.25,
|
||||
@@ -149,7 +149,10 @@ describe("correlateBpCompliance", () => {
|
||||
|
||||
it("returns insufficient (too_few_pairs) at n=13", () => {
|
||||
const daily = buildPairs(
|
||||
Array.from({ length: 13 }, (_, i) => [50 + i * 4, 150 - i * 2] as [number, number]),
|
||||
Array.from(
|
||||
{ length: 13 },
|
||||
(_, i) => [50 + i * 4, 150 - i * 2] as [number, number],
|
||||
),
|
||||
);
|
||||
const result = correlateBpCompliance({ daily });
|
||||
expect(result.status).toBe("insufficient");
|
||||
@@ -161,7 +164,10 @@ describe("correlateBpCompliance", () => {
|
||||
|
||||
it("returns ok with negative r for strong compliance↔BP support", () => {
|
||||
const daily = buildPairs(
|
||||
Array.from({ length: 20 }, (_, i) => [50 + i * 2.5, 150 - i * 1.5] as [number, number]),
|
||||
Array.from(
|
||||
{ length: 20 },
|
||||
(_, i) => [50 + i * 2.5, 150 - i * 1.5] as [number, number],
|
||||
),
|
||||
);
|
||||
const result = correlateBpCompliance({ daily });
|
||||
expect(result.status).toBe("ok");
|
||||
@@ -221,7 +227,9 @@ describe("correlateMoodPulse", () => {
|
||||
if (result.status === "ok") {
|
||||
expect(result.statistic).toBeLessThan(0);
|
||||
expect(result.pValue).toBeLessThan(MAX_P_VALUE);
|
||||
expect(result.interpretation).toMatch(/pattern worth watching|do not move/i);
|
||||
expect(result.interpretation).toMatch(
|
||||
/pattern worth watching|do not move/i,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -115,7 +115,8 @@ export function pearson(input: {
|
||||
throw new Error("pearson: xs and ys must have equal length");
|
||||
}
|
||||
const n = xs.length;
|
||||
if (n < minPairs) return { status: "insufficient", reason: "too_few_pairs", n };
|
||||
if (n < minPairs)
|
||||
return { status: "insufficient", reason: "too_few_pairs", n };
|
||||
|
||||
const sumX = xs.reduce((s, v) => s + v, 0);
|
||||
const sumY = ys.reduce((s, v) => s + v, 0);
|
||||
@@ -158,7 +159,13 @@ export function pearson(input: {
|
||||
const zHigh = z + 1.96 * se;
|
||||
const confidenceInterval: [number, number] = [tanh(zLow), tanh(zHigh)];
|
||||
|
||||
return { status: "ok", r: roundTo(clamped, 3), n, pValue, confidenceInterval };
|
||||
return {
|
||||
status: "ok",
|
||||
r: roundTo(clamped, 3),
|
||||
n,
|
||||
pValue,
|
||||
confidenceInterval,
|
||||
};
|
||||
}
|
||||
|
||||
function atanh(x: number): number {
|
||||
@@ -205,7 +212,7 @@ function normalCdf(x: number): number {
|
||||
let p =
|
||||
d *
|
||||
t *
|
||||
(0.319381530 +
|
||||
(0.31938153 +
|
||||
t *
|
||||
(-0.356563782 +
|
||||
t * (1.781477937 + t * (-1.821255978 + t * 1.330274429))));
|
||||
@@ -275,7 +282,13 @@ export function weekdayAnova(
|
||||
);
|
||||
|
||||
if (n < minN) {
|
||||
return { status: "insufficient", reason: "too_few_pairs", n, means, counts };
|
||||
return {
|
||||
status: "insufficient",
|
||||
reason: "too_few_pairs",
|
||||
n,
|
||||
means,
|
||||
counts,
|
||||
};
|
||||
}
|
||||
|
||||
const grandMean = daily.reduce((s, p) => s + p.value, 0) / n;
|
||||
@@ -301,7 +314,7 @@ export function weekdayAnova(
|
||||
const dfWithin = n - nonEmptyGroups;
|
||||
const fStat =
|
||||
dfWithin > 0 && withinSS > 0
|
||||
? (betweenSS / dfBetween) / (withinSS / dfWithin)
|
||||
? betweenSS / dfBetween / (withinSS / dfWithin)
|
||||
: Number.POSITIVE_INFINITY;
|
||||
const etaSquared = roundTo(betweenSS / totalSS, 3);
|
||||
const pValue = twoSidedPFromF(fStat, dfBetween, dfWithin);
|
||||
@@ -379,7 +392,10 @@ export interface BpComplianceInput {
|
||||
export function correlateBpCompliance(
|
||||
input: BpComplianceInput,
|
||||
): CorrelationResult {
|
||||
const points = input.daily.map((d) => ({ x: d.compliancePct, y: d.systolic }));
|
||||
const points = input.daily.map((d) => ({
|
||||
x: d.compliancePct,
|
||||
y: d.systolic,
|
||||
}));
|
||||
const xs = points.map((p) => p.x);
|
||||
const ys = points.map((p) => p.y);
|
||||
const r = pearson({ xs, ys });
|
||||
@@ -569,10 +585,7 @@ function bandFromInterval(
|
||||
return { low, high, label };
|
||||
}
|
||||
|
||||
function bandFromEtaSquared(
|
||||
eta: number,
|
||||
n: number,
|
||||
): CorrelationConfidenceBand {
|
||||
function bandFromEtaSquared(eta: number, n: number): CorrelationConfidenceBand {
|
||||
// ANOVA effect-size confidence — Cohen's conventions: eta² >= 0.14
|
||||
// is "large", >= 0.06 "medium", < 0.06 "small". n boosts the band.
|
||||
const adjusted = eta + (n >= 60 ? 0.02 : 0);
|
||||
|
||||
Reference in new issue
Block a user