Bring the checks up to date with the trend and range changes

The checks had fallen behind three refactors and had stopped running:
foodTrend became dailyTrend (the sleep trend line), the sleep average
moved into averageDays, and the custom date range replaced chartDays
with chartRange/chartSpan and gave the sentences a window phrase.

They now load the current functions. The window check exercises the
real range picker against stored settings, with a new case for a custom
range in the past. No assertion changed meaning; all 223 pass.
This commit is contained in:
Alexander Heldt committed 2026-10-04 18:23:57 +00:00
1 parent 7b1b0889c6
commit a37474ab82
3 files changed
+57 -31

No files matched your search

+45 -24
View File
@@ -4,33 +4,52 @@
import { load } from "./extract.mjs";
import { suite, eq, ok, report } from "./assert.mjs";
const app = load({ names: ["foodTrend"] });
const words = load({ names: ["foodTrendSentence"] });
// The food line is dailyTrend fitted to each day's grams. Today is whichever day
// is flagged so, as weeklyData flags it; these days put it last.
const fit = load({ names: ["dailyTrend"] });
const app = { foodTrend: (days) => fit.dailyTrend(days, (d) => d.grams) };
// The window reaches a sentence as the phrase the chart names it by. The
// reason for having no line mentions today only when the window has it.
const words = load({ names: ["noTrendSentence", "foodTrendSentence"], stubs: { chartRange: () => ({ endsToday: true }) } });
const lastDays = (n) => `the last ${n} days`;
// The 7/14/30 picker reaches the trend by deciding how many days weeklyData
// builds — there is no second mechanism, so this is the thing to hold still.
let windowDays = 7;
// The range picker reaches the trend by deciding which days weeklyData builds —
// there is no second mechanism, so this is the thing to hold still. The picker
// is the real one, reading what it stored.
const stored = new Map();
const weekly = load({
names: ["startOfDay", "endOfDay", "ymd", "weeklyData"],
names: [
"CHART_DAYS_KEY", "CHART_RANGE_KEY", "CHART_DAY_CHOICES", "CHART_MAX_DAYS",
"startOfDay", "endOfDay", "ymd", "addDays", "daysBetween", "parseYmd",
"storedCustomRange", "chartRange", "chartDayList",
"NO_KIND", "sleepIntervals", "msInIntervals", "weeklyData",
],
stubs: {
chartDays: () => windowDays,
localStorage: { getItem: (k) => stored.get(k) ?? null },
isExcluded: () => false,
eventsForDay: () => [],
sleepMsInRange: () => 0,
walkMsInRange: () => 0,
walkWindows: () => [],
},
});
suite("the day picker is what sets the trend's window");
suite("the range picker is what sets the trend's window");
for (const n of [7, 14, 30]) {
windowDays = n;
stored.set(weekly.CHART_DAYS_KEY, String(n));
eq(weekly.weeklyData([]).length, n, `picking ${n}d gives the charts ${n} days to fit over`);
}
windowDays = 7;
{
// A custom stretch in the past: its own days, none of them today.
const ago = (n) => weekly.ymd(weekly.addDays(weekly.startOfDay(new Date()), -n));
stored.set(weekly.CHART_DAYS_KEY, "custom");
stored.set(weekly.CHART_RANGE_KEY, JSON.stringify({ from: ago(10), to: ago(3) }));
const days = weekly.weeklyData([]);
eq(days.length, 8, "a custom range gives the charts exactly its days");
eq(days.some((d) => d.isToday), false, "…and one that ends before today has no half-done day to leave out");
}
stored.clear();
// weeklyData's shape, as far as foodTrend reads it. Today is last, as there.
// weeklyData's shape, as far as the fit reads it. Today is last.
const days = (grams, { excluded = [] } = {}) =>
grams.map((g, i) => ({ grams: g, excluded: excluded.includes(i) }));
grams.map((g, i) => ({ grams: g, excluded: excluded.includes(i), isToday: i === grams.length - 1 }));
suite("it declines to fit when there is nothing to fit");
{
@@ -89,7 +108,7 @@ suite("marked days are skipped without shifting the line");
suite("what the sentence is allowed to say");
{
const say = (grams, opts, win = 14) => words.foodTrendSentence(app.foodTrend(days(grams, opts)), win);
const say = (grams, opts, win = 14) => words.foodTrendSentence(app.foodTrend(days(grams, opts)), lastDays(win));
const rising = say([200, 250, 300, 350, 400, 450, 0]);
ok(/up about 250 g/.test(rising), "a clear climb gives the size of the move");
@@ -143,17 +162,19 @@ suite("what the sentence is allowed to say");
{
let kinds = [];
const split = load({
names: ["NO_KIND", "FOOD_COLORS", "foodTrend", "foodTrendSentence",
names: ["NO_KIND", "FOOD_COLORS", "dailyTrend", "noTrendSentence", "foodTrendSentence",
"foodTrendMoves", "foodSeriesSentences", "foodSeriesFor"],
stubs: {
chartRange: () => ({ endsToday: true }),
liveFoodKinds: () => kinds,
loadFoodKinds: () => kinds,
foodKindNames: () => new Map(kinds.map(k => [k.id, k.name])),
},
});
// days carrying a per-kind split, as weeklyData builds them.
const byKind = (rows) => rows.map(r => ({
// days carrying a per-kind split, as weeklyData builds them; the last is today.
const byKind = (rows) => rows.map((r, i) => ({
isToday: i === rows.length - 1,
grams: Object.values(r).reduce((s, v) => s + v, 0),
gramsByKind: r,
excluded: false,
@@ -167,7 +188,7 @@ suite("what the sentence is allowed to say");
const series = split.foodSeriesFor(days);
eq(series.length, 1, "no kinds defined gives exactly one series");
eq(series[0].name, "No kind", "…the unnamed one");
const s = split.foodSeriesSentences(series, 7);
const s = split.foodSeriesSentences(series, lastDays(7));
eq(s.length, 1, "…and one sentence, as before kinds existed");
ok(/daily intake/.test(s[0]), "…phrased as the whole intake, not as a kind");
}
@@ -201,7 +222,7 @@ suite("what the sentence is allowed to say");
// food is real — it has to show under the name the tombstone kept.
kinds = [];
const namesOnly = load({
names: ["NO_KIND", "FOOD_COLORS", "foodTrend", "foodSeriesFor"],
names: ["NO_KIND", "FOOD_COLORS", "dailyTrend", "foodSeriesFor"],
stubs: {
liveFoodKinds: () => [],
// The tombstone: gone from the picker, still carrying name and colour.
@@ -231,7 +252,7 @@ suite("what the sentence is allowed to say");
{ a: 300, b: 50, c: 40, e: 9 }, { a: 0, b: 0, c: 0, e: 0 },
]);
const series = split.foodSeriesFor(days);
const lines = split.foodSeriesSentences(series, 7);
const lines = split.foodSeriesSentences(series, lastDays(7));
ok(lines.some(l => /^Dry:/.test(l)), "the kind that moved gets its own sentence");
ok(lines.length <= 2, `four kinds give at most two lines, got ${lines.length}`);
ok(lines.some(l => /no clear trend/.test(l)),
@@ -245,7 +266,7 @@ suite("what the sentence is allowed to say");
{ a: 200, b: 100 }, { a: 205, b: 98 }, { a: 198, b: 101 },
{ a: 202, b: 99 }, { a: 200, b: 100 }, { a: 0, b: 0 },
]);
const lines = split.foodSeriesSentences(split.foodSeriesFor(days), 7);
const lines = split.foodSeriesSentences(split.foodSeriesFor(days), lastDays(7));
eq(lines.length, 1, "one line when nothing is claimable");
ok(/no kind shows a trend/.test(lines[0]), "…saying so plainly");
}
@@ -259,7 +280,7 @@ suite("what the sentence is allowed to say");
let selected = "2026-09-20";
let el = { hidden: false, textContent: "" };
const info = load({
names: ["NO_KIND", "FOOD_COLORS", "foodTrend", "foodSeriesFor", "renderFoodDayInfo"],
names: ["NO_KIND", "FOOD_COLORS", "dateFormatters", "fmtDate", "dailyTrend", "foodSeriesFor", "renderFoodDayInfo"],
stubs: {
document: { getElementById: () => el },
selectedDay: () => new Date(selected + "T12:00:00"),