Calio/tests/entry.test.js
Elijah ddea7fc512
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 28s
Extensive UI changes
2026-10-20 19:46:08 -07:00

67 lines
2 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { suggestedMeal, localTime, nextFoodName } from "../shared/entry.js";
import { recordSchema, emptyNutrients } from "../shared/domain.js";
test("meal suggestions follow local time at each meal boundary, including overnight snacks", () => {
for (const [hour, expected] of [
[0, "Snacks"],
[4, "Snacks"],
[5, "Breakfast"],
[7, "Breakfast"],
[10, "Breakfast"],
[11, "Lunch"],
[15, "Lunch"],
[16, "Dinner"],
[21, "Dinner"],
[22, "Snacks"],
[23, "Snacks"],
]) {
assert.equal(suggestedMeal(new Date(2026, 9, 20, hour, 28)), expected);
}
assert.equal(localTime(new Date(2026, 9, 20, 6, 28)), "06:28");
assert.equal(localTime(new Date(2026, 9, 20, 0, 5)), "00:05");
});
test("unnamed entries advance the category sequence without collisions after removals", () => {
assert.equal(nextFoodName("Dinner", []), "Dinner 1");
assert.equal(
nextFoodName("Dinner", ["Dinner 1", "Dinner 3", "Pasta", "Lunch 8"]),
"Dinner 4",
);
assert.equal(
nextFoodName("Breakfast", ["Breakfast 1", "Breakfast toast"]),
"Breakfast 2",
);
assert.equal(nextFoodName("Food", ["Food 1"]), "Food 2");
});
test("entry times validate, while legacy records without a time remain compatible", () => {
const entry = {
id: "e1",
type: "entry",
updatedAt: "2026-10-20T12:00:00.000Z",
deleted: false,
data: {
name: "Dinner 1",
date: "2026-10-20",
meal: "Dinner",
quantity: 1,
serving: "1 serving",
nutrients: emptyNutrients(),
},
};
assert.equal(recordSchema.safeParse(entry).success, true);
for (const time of ["00:00", "06:28", "23:59"])
assert.equal(
recordSchema.safeParse({ ...entry, data: { ...entry.data, time } })
.success,
true,
);
for (const time of ["24:00", "12:60", "6:28", "invalid", ""])
assert.equal(
recordSchema.safeParse({ ...entry, data: { ...entry.data, time } })
.success,
false,
);
});