Files
wyndham-Condon/tests/usage-history.test.mjs
2026-08-03 17:06:58 +08:00

42 lines
1.9 KiB
JavaScript

import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import vm from "node:vm";
const appSource = await readFile(new URL("../app.js", import.meta.url), "utf8");
const helperStart = appSource.indexOf("function dateOnlyMs");
const helperEnd = appSource.indexOf("\n\nconst I18N = ", helperStart);
assert.notEqual(helperStart, -1, "usage date helpers should exist");
assert.notEqual(helperEnd, -1, "usage date helpers should end before translations");
const helperContext = {};
vm.runInNewContext(`${appSource.slice(helperStart, helperEnd)}\nglobalThis.usageRecordsInDisplayOrder = usageRecordsInDisplayOrder;`, helperContext);
function localIsoOffset(offsetDays) {
const date = new Date();
date.setHours(12, 0, 0, 0);
date.setDate(date.getDate() + offsetDays);
return [date.getFullYear(), String(date.getMonth() + 1).padStart(2, "0"), String(date.getDate()).padStart(2, "0")].join("-");
}
test("usage history orders records by distance from today's stay interval", () => {
const records = [
{ id: "missing", checkin: "not-a-date", checkout: "not-a-date" },
{ id: "future-far", checkin: localIsoOffset(10), checkout: localIsoOffset(12) },
{ id: "past-near", checkin: localIsoOffset(-5), checkout: localIsoOffset(-2) },
{ id: "active", checkin: localIsoOffset(-1), checkout: localIsoOffset(1) },
{ id: "future-near", checkin: localIsoOffset(1), checkout: localIsoOffset(3) }
];
assert.deepEqual(
Array.from(helperContext.usageRecordsInDisplayOrder(records), record => record.id),
["active", "future-near", "past-near", "future-far", "missing"]
);
});
test("usage history keeps the requested page size and direct pagination surface", () => {
assert.match(appSource, /const USAGE_PAGE_SIZE = 30;/);
assert.match(appSource, /#stayPageNumbers/);
assert.match(appSource, /data-usage-page/);
});