Polish PWA gestures, transitions and nutrition averages
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 32s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 32s
This commit is contained in:
parent
b19de6f9ea
commit
7288e56c02
16 changed files with 1019 additions and 245 deletions
73
tests/nutrition-trends.test.js
Normal file
73
tests/nutrition-trends.test.js
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { nutritionSummary } from "../shared/nutrition-trends.js";
|
||||
import {
|
||||
defaultSettings,
|
||||
emptyNutrients,
|
||||
recordSchema,
|
||||
} from "../shared/domain.js";
|
||||
|
||||
const entry = (date, calories, protein, quantity = 1) => ({
|
||||
date,
|
||||
quantity,
|
||||
nutrients: { ...emptyNutrients(), calories, protein },
|
||||
});
|
||||
const dates = ["2026-10-19", "2026-10-20", "2026-10-21", "2026-10-22"];
|
||||
test("incomplete and empty days do not dilute calorie or macro averages; bars keep raw totals", () => {
|
||||
const entries = [
|
||||
entry(dates[0], 1000, 50, 2),
|
||||
entry(dates[1], 200, 5),
|
||||
entry(dates[2], 1800, 0),
|
||||
];
|
||||
const before = structuredClone(entries);
|
||||
const stats = nutritionSummary(entries, dates, 1000);
|
||||
assert.equal(stats.average.calories, 1900);
|
||||
assert.equal(stats.average.protein, 50); // A real zero macro on a complete day counts.
|
||||
assert.equal(stats.includedCount, 2);
|
||||
assert.equal(stats.excludedCount, 1);
|
||||
assert.equal(stats.days[1].nutrients.calories, 200);
|
||||
assert.equal(stats.days[1].included, false);
|
||||
assert.equal(stats.days[3].logged, false);
|
||||
assert.deepEqual(entries, before);
|
||||
});
|
||||
test("cutoff boundary counts; disabling the cutoff includes all logged days, even a zero", () => {
|
||||
const entries = [entry(dates[0], 1000, 20), entry(dates[1], 0, 10)];
|
||||
assert.equal(nutritionSummary(entries, dates, 1000).includedCount, 1);
|
||||
assert.equal(nutritionSummary(entries, dates, 0).average.calories, 500);
|
||||
assert.equal(nutritionSummary(entries, dates, 0).average.protein, 15);
|
||||
});
|
||||
test("no qualifying data yields a finite empty result and dates outside the period stay excluded", () => {
|
||||
const stats = nutritionSummary(
|
||||
[entry(dates[0], 200, 2), entry("2025-01-01", 2200, 100)],
|
||||
dates,
|
||||
1000,
|
||||
);
|
||||
assert.equal(stats.includedCount, 0);
|
||||
assert.deepEqual(stats.average, emptyNutrients());
|
||||
});
|
||||
test("average preference is optional for old journals and rejects invalid synced settings", () => {
|
||||
const record = {
|
||||
id: "settings",
|
||||
type: "settings",
|
||||
deleted: false,
|
||||
updatedAt: "2026-10-22T12:00:00.000Z",
|
||||
data: defaultSettings,
|
||||
};
|
||||
assert.equal(recordSchema.safeParse(record).success, true);
|
||||
for (const cutoff of [0, 1000, 100000])
|
||||
assert.equal(
|
||||
recordSchema.safeParse({
|
||||
...record,
|
||||
data: { ...defaultSettings, minimumAverageCalories: cutoff },
|
||||
}).success,
|
||||
true,
|
||||
);
|
||||
for (const cutoff of [-1, Infinity, "1000", 100001])
|
||||
assert.equal(
|
||||
recordSchema.safeParse({
|
||||
...record,
|
||||
data: { ...defaultSettings, minimumAverageCalories: cutoff },
|
||||
}).success,
|
||||
false,
|
||||
);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue