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:
1 parent
7b1b0889c6
commit
a37474ab82
3 files changed
+57
-31
No files matched your search
@@ -8,12 +8,14 @@ const app = load({
|
|||||||
names: [
|
names: [
|
||||||
"ymd", "startOfDay", "EXCLUDED_TYPE", "ALWAYS_COUNTS", "excludedSet",
|
"ymd", "startOfDay", "EXCLUDED_TYPE", "ALWAYS_COUNTS", "excludedSet",
|
||||||
"excludedDays", "isExcluded", "countedEvents", "spansExcluded",
|
"excludedDays", "isExcluded", "countedEvents", "spansExcluded",
|
||||||
"pairWindows", "sleepWindows", "sleepMsInRange", "lastEventOfType",
|
"pairWindows", "sleepWindows", "sleepIntervals", "msInIntervals", "sleepMsInRange",
|
||||||
|
"lastEventOfType",
|
||||||
"gapsBetween",
|
"gapsBetween",
|
||||||
],
|
],
|
||||||
lets: ["excludedSet"],
|
lets: ["excludedSet"],
|
||||||
// gapsBetween defaults its window to the chart picker; the checks pass it in.
|
// gapsBetween takes its window from the chart range picker (chartSpan); here
|
||||||
stubs: { chartDays: () => 7 },
|
// that is the two weeks up to and including the checks' "now" day.
|
||||||
|
stubs: { chartSpan: () => ({ from: NOW - 14 * 24 * H, to: NOW + 24 * H }) },
|
||||||
});
|
});
|
||||||
|
|
||||||
const H = 3600_000;
|
const H = 3600_000;
|
||||||
@@ -30,7 +32,7 @@ suite("what a marked day takes out of the numbers");
|
|||||||
{ id: "c", type: "pee", at: at(20, 9) },
|
{ id: "c", type: "pee", at: at(20, 9) },
|
||||||
mark(19),
|
mark(19),
|
||||||
]);
|
]);
|
||||||
eq(app.gapsBetween(evs, "pee", 14), [],
|
eq(app.gapsBetween(evs, "pee"), [],
|
||||||
"a gap reaching across a marked day is discarded rather than measured");
|
"a gap reaching across a marked day is discarded rather than measured");
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
@@ -41,7 +43,7 @@ suite("what a marked day takes out of the numbers");
|
|||||||
{ id: "g", type: "pee", at: at(20, 12) },
|
{ id: "g", type: "pee", at: at(20, 12) },
|
||||||
mark(19),
|
mark(19),
|
||||||
]);
|
]);
|
||||||
eq(app.gapsBetween(evs, "pee", 14), [4 * H, 4 * H],
|
eq(app.gapsBetween(evs, "pee"), [4 * H, 4 * H],
|
||||||
"…while the gaps either side of it survive");
|
"…while the gaps either side of it survive");
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
|||||||
+45
-24
@@ -4,33 +4,52 @@
|
|||||||
import { load } from "./extract.mjs";
|
import { load } from "./extract.mjs";
|
||||||
import { suite, eq, ok, report } from "./assert.mjs";
|
import { suite, eq, ok, report } from "./assert.mjs";
|
||||||
|
|
||||||
const app = load({ names: ["foodTrend"] });
|
// The food line is dailyTrend fitted to each day's grams. Today is whichever day
|
||||||
const words = load({ names: ["foodTrendSentence"] });
|
// 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
|
// The range picker reaches the trend by deciding which days weeklyData builds —
|
||||||
// builds — there is no second mechanism, so this is the thing to hold still.
|
// there is no second mechanism, so this is the thing to hold still. The picker
|
||||||
let windowDays = 7;
|
// is the real one, reading what it stored.
|
||||||
|
const stored = new Map();
|
||||||
const weekly = load({
|
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: {
|
stubs: {
|
||||||
chartDays: () => windowDays,
|
localStorage: { getItem: (k) => stored.get(k) ?? null },
|
||||||
isExcluded: () => false,
|
isExcluded: () => false,
|
||||||
eventsForDay: () => [],
|
walkWindows: () => [],
|
||||||
sleepMsInRange: () => 0,
|
|
||||||
walkMsInRange: () => 0,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
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]) {
|
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`);
|
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 = [] } = {}) =>
|
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");
|
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");
|
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]);
|
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");
|
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 = [];
|
let kinds = [];
|
||||||
const split = load({
|
const split = load({
|
||||||
names: ["NO_KIND", "FOOD_COLORS", "foodTrend", "foodTrendSentence",
|
names: ["NO_KIND", "FOOD_COLORS", "dailyTrend", "noTrendSentence", "foodTrendSentence",
|
||||||
"foodTrendMoves", "foodSeriesSentences", "foodSeriesFor"],
|
"foodTrendMoves", "foodSeriesSentences", "foodSeriesFor"],
|
||||||
stubs: {
|
stubs: {
|
||||||
|
chartRange: () => ({ endsToday: true }),
|
||||||
liveFoodKinds: () => kinds,
|
liveFoodKinds: () => kinds,
|
||||||
loadFoodKinds: () => kinds,
|
loadFoodKinds: () => kinds,
|
||||||
foodKindNames: () => new Map(kinds.map(k => [k.id, k.name])),
|
foodKindNames: () => new Map(kinds.map(k => [k.id, k.name])),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// days carrying a per-kind split, as weeklyData builds them.
|
// days carrying a per-kind split, as weeklyData builds them; the last is today.
|
||||||
const byKind = (rows) => rows.map(r => ({
|
const byKind = (rows) => rows.map((r, i) => ({
|
||||||
|
isToday: i === rows.length - 1,
|
||||||
grams: Object.values(r).reduce((s, v) => s + v, 0),
|
grams: Object.values(r).reduce((s, v) => s + v, 0),
|
||||||
gramsByKind: r,
|
gramsByKind: r,
|
||||||
excluded: false,
|
excluded: false,
|
||||||
@@ -167,7 +188,7 @@ suite("what the sentence is allowed to say");
|
|||||||
const series = split.foodSeriesFor(days);
|
const series = split.foodSeriesFor(days);
|
||||||
eq(series.length, 1, "no kinds defined gives exactly one series");
|
eq(series.length, 1, "no kinds defined gives exactly one series");
|
||||||
eq(series[0].name, "No kind", "…the unnamed one");
|
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");
|
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");
|
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.
|
// food is real — it has to show under the name the tombstone kept.
|
||||||
kinds = [];
|
kinds = [];
|
||||||
const namesOnly = load({
|
const namesOnly = load({
|
||||||
names: ["NO_KIND", "FOOD_COLORS", "foodTrend", "foodSeriesFor"],
|
names: ["NO_KIND", "FOOD_COLORS", "dailyTrend", "foodSeriesFor"],
|
||||||
stubs: {
|
stubs: {
|
||||||
liveFoodKinds: () => [],
|
liveFoodKinds: () => [],
|
||||||
// The tombstone: gone from the picker, still carrying name and colour.
|
// 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 },
|
{ a: 300, b: 50, c: 40, e: 9 }, { a: 0, b: 0, c: 0, e: 0 },
|
||||||
]);
|
]);
|
||||||
const series = split.foodSeriesFor(days);
|
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.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.length <= 2, `four kinds give at most two lines, got ${lines.length}`);
|
||||||
ok(lines.some(l => /no clear trend/.test(l)),
|
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: 200, b: 100 }, { a: 205, b: 98 }, { a: 198, b: 101 },
|
||||||
{ a: 202, b: 99 }, { a: 200, b: 100 }, { a: 0, b: 0 },
|
{ 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");
|
eq(lines.length, 1, "one line when nothing is claimable");
|
||||||
ok(/no kind shows a trend/.test(lines[0]), "…saying so plainly");
|
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 selected = "2026-09-20";
|
||||||
let el = { hidden: false, textContent: "" };
|
let el = { hidden: false, textContent: "" };
|
||||||
const info = load({
|
const info = load({
|
||||||
names: ["NO_KIND", "FOOD_COLORS", "foodTrend", "foodSeriesFor", "renderFoodDayInfo"],
|
names: ["NO_KIND", "FOOD_COLORS", "dateFormatters", "fmtDate", "dailyTrend", "foodSeriesFor", "renderFoodDayInfo"],
|
||||||
stubs: {
|
stubs: {
|
||||||
document: { getElementById: () => el },
|
document: { getElementById: () => el },
|
||||||
selectedDay: () => new Date(selected + "T12:00:00"),
|
selectedDay: () => new Date(selected + "T12:00:00"),
|
||||||
|
|||||||
@@ -13,12 +13,15 @@ let excluded = new Set();
|
|||||||
let windowDays = 7;
|
let windowDays = 7;
|
||||||
|
|
||||||
const curves = load({
|
const curves = load({
|
||||||
names: ["startOfDay", "pairWindows", "sleepWindows", "sleepTrendCurves"],
|
names: ["startOfDay", "addDays", "pairWindows", "sleepWindows", "averageDays", "sleepTrendCurves"],
|
||||||
stubs: {
|
stubs: {
|
||||||
selectedDay: () => SEL,
|
selectedDay: () => SEL,
|
||||||
ymd: (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`,
|
ymd: (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`,
|
||||||
isExcluded: (d) => excluded.has(d.getDate()),
|
isExcluded: (d) => excluded.has(d.getDate()),
|
||||||
chartDays: () => windowDays,
|
// A preset window: the average runs over the N full days before the
|
||||||
|
// selected one (averageDays). A custom range isn't what's checked here.
|
||||||
|
chartRange: () => ({ custom: false, days: windowDays }),
|
||||||
|
chartDayList: () => [],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user