mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
The weight tile's arrow, 7-day delta and comparison caption were wired as "up is bad", so a person below their own target who was gaining saw every step toward it coloured as a setback. Movement is now judged against the target they entered: below the band a gain is progress, above it a loss is, and inside it holding steady is, with a move either way shown neutral rather than orange. Without a stored target the tile reads exactly as before; the height-derived band is not treated as a target because nobody chose it. The direction is resolved once on the server and published on the dashboard snapshot as tiles.weightTrend, so the web tile and the native client colour the same reading the same way. The personal-record direction reads the same placement helper. The feature payload behind the briefing, the overview analysis and the Coach now carries the target band and which way is progress, so a narrative no longer calls a gain a regression when gaining is the goal. The insights vitals grid drops weight from its fixed "up is bad" set. Refs #1006
114 lines
4.5 KiB
TypeScript
114 lines
4.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
buildWeightTargetFeature,
|
|
resolveWeightTrend,
|
|
weightTargetPosition,
|
|
weightTrendReferenceKg,
|
|
} from "@/lib/targets/weight-trend";
|
|
import { getTrendSentiment } from "@/lib/insights/trend-sentiment";
|
|
|
|
const TARGET = { min: 70, max: 75 };
|
|
|
|
describe("weightTargetPosition", () => {
|
|
it("places a reading below, inside or above the stored target", () => {
|
|
expect(weightTargetPosition(TARGET, 65)).toBe("below");
|
|
expect(weightTargetPosition(TARGET, 70)).toBe("inside");
|
|
expect(weightTargetPosition(TARGET, 72.5)).toBe("inside");
|
|
expect(weightTargetPosition(TARGET, 75)).toBe("inside");
|
|
expect(weightTargetPosition(TARGET, 80)).toBe("above");
|
|
});
|
|
|
|
it("answers null without a target or without a usable reading", () => {
|
|
expect(weightTargetPosition(null, 80)).toBeNull();
|
|
expect(weightTargetPosition(TARGET, null)).toBeNull();
|
|
expect(weightTargetPosition(TARGET, Number.NaN)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("resolveWeightTrend", () => {
|
|
it("below the target, gaining is progress", () => {
|
|
const trend = resolveWeightTrend(TARGET, 62);
|
|
expect(trend).toEqual({ direction: "up-good", targetPosition: "below" });
|
|
expect(getTrendSentiment(0.6, trend.direction)).toBe("positive");
|
|
expect(getTrendSentiment(-0.6, trend.direction)).toBe("negative");
|
|
});
|
|
|
|
it("above the target, losing is progress", () => {
|
|
const trend = resolveWeightTrend(TARGET, 84);
|
|
expect(trend).toEqual({ direction: "up-bad", targetPosition: "above" });
|
|
expect(getTrendSentiment(-0.6, trend.direction)).toBe("positive");
|
|
expect(getTrendSentiment(0.6, trend.direction)).toBe("negative");
|
|
});
|
|
|
|
it("inside the target, holding steady is progress and no move is a setback", () => {
|
|
const trend = resolveWeightTrend(TARGET, 72);
|
|
expect(trend).toEqual({ direction: "hold", targetPosition: "inside" });
|
|
expect(getTrendSentiment(0, trend.direction)).toBe("positive");
|
|
expect(getTrendSentiment(0.01, trend.direction)).toBe("positive");
|
|
expect(getTrendSentiment(0.6, trend.direction)).toBe("neutral");
|
|
expect(getTrendSentiment(-0.6, trend.direction)).toBe("neutral");
|
|
});
|
|
|
|
it("without a stored target keeps the historical reading: a fall reads as good", () => {
|
|
const trend = resolveWeightTrend(null, 84);
|
|
expect(trend).toEqual({ direction: "up-bad", targetPosition: null });
|
|
expect(getTrendSentiment(-0.6, trend.direction)).toBe("positive");
|
|
});
|
|
|
|
it("with a target but no reading yet keeps the historical reading too", () => {
|
|
expect(resolveWeightTrend(TARGET, null)).toEqual({
|
|
direction: "up-bad",
|
|
targetPosition: null,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("weightTrendReferenceKg", () => {
|
|
it("prefers the 7-day average so one noisy reading at the band edge does not flip the colour", () => {
|
|
expect(weightTrendReferenceKg({ avg7: 71, latest: 69 })).toBe(71);
|
|
});
|
|
|
|
it("falls back to the latest reading, then to nothing", () => {
|
|
expect(weightTrendReferenceKg({ avg7: null, latest: 69 })).toBe(69);
|
|
expect(weightTrendReferenceKg({ avg7: null, latest: null })).toBeNull();
|
|
expect(weightTrendReferenceKg(undefined)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("getTrendSentiment — existing directions unchanged", () => {
|
|
it("still colours up-good, up-bad and neutral exactly as before", () => {
|
|
expect(getTrendSentiment(1, "up-good")).toBe("positive");
|
|
expect(getTrendSentiment(-1, "up-good")).toBe("negative");
|
|
expect(getTrendSentiment(1, "up-bad")).toBe("negative");
|
|
expect(getTrendSentiment(0.01, "up-bad")).toBe("neutral");
|
|
expect(getTrendSentiment(1, "neutral")).toBe("neutral");
|
|
expect(getTrendSentiment(null, "up-good")).toBe("neutral");
|
|
});
|
|
|
|
it("a hold direction with no signal at all stays neutral rather than claiming progress", () => {
|
|
expect(getTrendSentiment(null, "hold")).toBe("neutral");
|
|
});
|
|
});
|
|
|
|
describe("buildWeightTargetFeature", () => {
|
|
it("names the progress direction for each side of the band", () => {
|
|
expect(
|
|
buildWeightTargetFeature(TARGET, { avg7: 66, latest: 66 })?.progress,
|
|
).toBe("gaining");
|
|
expect(
|
|
buildWeightTargetFeature(TARGET, { avg7: 72, latest: 72 })?.progress,
|
|
).toBe("holding steady");
|
|
expect(
|
|
buildWeightTargetFeature(TARGET, { avg7: 80, latest: 80 })?.progress,
|
|
).toBe("losing");
|
|
});
|
|
|
|
it("claims nothing without a stored target or a reading", () => {
|
|
expect(buildWeightTargetFeature(null, { avg7: 80, latest: 80 })).toBeNull();
|
|
expect(
|
|
buildWeightTargetFeature(TARGET, { avg7: null, latest: null }),
|
|
).toBeNull();
|
|
});
|
|
});
|