Calio/tests/export.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

97 lines
3.2 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { downloadExport, offsetDay, weightValue } from "../src/utils.ts";
import { defaultSettings, emptyNutrients } from "../shared/domain.js";
const record = (id, type, data, deleted = false) => ({
id,
type,
data,
deleted,
updatedAt: "2026-10-20T10:00:00.000Z",
});
const entry = {
date: "2026-10-20",
time: "06:28",
meal: "Lunch",
name: "=danger|\nfood",
serving: "1 bowl",
quantity: 1.5,
nutrients: { ...emptyNutrients(), calories: 200, protein: 20 },
};
const records = [
record("e1", "entry", entry),
record("e2", "entry", { ...entry, date: "2026-10-19" }),
record("deleted", "entry", entry, true),
record("w", "weight", {
date: "2026-10-20",
valueKg: 75,
notes: "=untrusted",
}),
];
async function capture(
format,
selected = ["calories", "protein"],
includeWeight = true,
) {
let blob;
const original = URL.createObjectURL;
const timer = globalThis.setTimeout;
globalThis.document = { createElement: () => ({ click() {} }) };
URL.createObjectURL = (value) => {
blob = value;
return "blob:test";
};
globalThis.setTimeout = () => 0;
try {
downloadExport(
records,
defaultSettings,
"2026-10-20",
"2026-10-20",
selected,
includeWeight,
format,
2,
);
return await blob.text();
} finally {
URL.createObjectURL = original;
globalThis.setTimeout = timer;
}
}
test("JSON export includes fractional serving totals, chosen dates/metrics, kg weights and pending status", async () => {
const data = JSON.parse(await capture("json"));
assert.equal(data.schemaVersion, 1);
assert.equal(data.pendingChanges, 2);
assert.equal(data.entries.length, 1);
assert.equal(data.entries[0].calories, 300);
assert.equal(data.entries[0].time, "06:28");
assert.equal(data.entries[0].protein, 30);
assert.equal("fat" in data.entries[0], false);
assert.equal(data.weights[0].valueKg, 75);
assert.equal(data.dailyTotals[0].calories, 300);
});
test("CSV quotes text and guards spreadsheet formula fields; Markdown keeps clean rows", async () => {
const csv = await capture("csv");
assert.match(csv, /"'=danger\|\nfood"/);
assert.match(csv, /"'=untrusted"/);
assert.match(csv, /"daily_total"/);
assert.match(csv, /"06:28"/);
const markdown = await capture("markdown");
assert.match(markdown, /\| 2026-10-20 \| 300 \| 30 \|/);
assert.match(markdown, /=danger\/ food/);
assert.match(markdown, /2026-10-20 06:28/);
assert.doesNotMatch(markdown, /2026-10-19/);
});
test("weight-only exports omit food records; excluding weight omits all weigh-ins", async () => {
const weights = JSON.parse(await capture("json", [], true));
assert.equal(weights.entries.length, 0);
assert.equal(weights.weights.length, 1);
const foods = JSON.parse(await capture("json", ["calories"], false));
assert.equal(foods.weights.length, 0);
});
test("local date arithmetic crosses month/year boundaries and weight unit conversion round trips", () => {
assert.equal(offsetDay("2026-01-01", -1), "2025-12-31");
assert.equal(offsetDay("2024-02-28", 1), "2024-02-29");
assert.ok(Math.abs(weightValue(75, "lb") / 2.2046226218 - 75) < 1e-10);
});