diff --git a/checks/excluded-days.mjs b/checks/excluded-days.mjs index 5b4dfcc..8ea1c2f 100644 --- a/checks/excluded-days.mjs +++ b/checks/excluded-days.mjs @@ -8,12 +8,14 @@ const app = load({ names: [ "ymd", "startOfDay", "EXCLUDED_TYPE", "ALWAYS_COUNTS", "excludedSet", "excludedDays", "isExcluded", "countedEvents", "spansExcluded", - "pairWindows", "sleepWindows", "sleepMsInRange", "lastEventOfType", + "pairWindows", "sleepWindows", "sleepIntervals", "msInIntervals", "sleepMsInRange", + "lastEventOfType", "gapsBetween", ], lets: ["excludedSet"], - // gapsBetween defaults its window to the chart picker; the checks pass it in. - stubs: { chartDays: () => 7 }, + // gapsBetween takes its window from the chart range picker (chartSpan); here + // 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; @@ -30,7 +32,7 @@ suite("what a marked day takes out of the numbers"); { id: "c", type: "pee", at: at(20, 9) }, 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"); } { @@ -41,7 +43,7 @@ suite("what a marked day takes out of the numbers"); { id: "g", type: "pee", at: at(20, 12) }, 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"); } { diff --git a/checks/food-trend.mjs b/checks/food-trend.mjs index d07ef20..5e0582c 100644 --- a/checks/food-trend.mjs +++ b/checks/food-trend.mjs @@ -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"), diff --git a/checks/trend-curves.mjs b/checks/trend-curves.mjs index 4235fea..dfa9c98 100644 --- a/checks/trend-curves.mjs +++ b/checks/trend-curves.mjs @@ -13,12 +13,15 @@ let excluded = new Set(); let windowDays = 7; const curves = load({ - names: ["startOfDay", "pairWindows", "sleepWindows", "sleepTrendCurves"], + names: ["startOfDay", "addDays", "pairWindows", "sleepWindows", "averageDays", "sleepTrendCurves"], stubs: { selectedDay: () => SEL, ymd: (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`, 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: () => [], }, });