Files
notification-wakeup-service/test/services/alertNotifyTime.test.ts
T

117 lines
3.5 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
formatHourMinuteUtc,
parseHourMinuteUtc,
storedNotifyLabel,
utcCalendarDay,
utcDayStartSeconds,
utcHourMinute,
} from "../../src/services/alertAuthorization.js";
describe("formatHourMinuteUtc", () => {
it("zero-pads both halves", () => {
assert.equal(formatHourMinuteUtc(0, 0), "00:00");
assert.equal(formatHourMinuteUtc(9, 5), "09:05");
assert.equal(formatHourMinuteUtc(18, 30), "18:30");
assert.equal(formatHourMinuteUtc(23, 59), "23:59");
});
it("pads so text ordering matches clock ordering", () => {
// The whole reason for the padding: "9:30" would sort above "10:00".
assert.ok(formatHourMinuteUtc(9, 30) < formatHourMinuteUtc(10, 0));
assert.ok(formatHourMinuteUtc(0, 0) < formatHourMinuteUtc(23, 59));
});
});
describe("parseHourMinuteUtc", () => {
it("round-trips whatever format produced", () => {
for (const [hour, minute] of [
[0, 0],
[9, 5],
[18, 30],
[23, 59],
]) {
assert.deepEqual(parseHourMinuteUtc(formatHourMinuteUtc(hour, minute)), {
hour,
minute,
});
}
});
it("refuses anything that is not a padded HH:MM", () => {
for (const bad of [
"24:00",
"07:60",
"7:00",
"07:00:00",
"0700",
"18:30-06:00",
"midnight",
"",
]) {
assert.equal(parseHourMinuteUtc(bad), undefined, bad);
}
});
});
describe("storedNotifyLabel", () => {
it("renders the pair for a log line", () => {
assert.equal(
storedNotifyLabel({ notifyHourUtc: 18, notifyMinuteUtc: 30 }),
"18:30"
);
assert.equal(
storedNotifyLabel({ notifyHourUtc: 0, notifyMinuteUtc: 5 }),
"00:05"
);
});
it("says none when either half is missing", () => {
assert.equal(storedNotifyLabel({}), "none");
assert.equal(storedNotifyLabel({ notifyHourUtc: 18 }), "none");
assert.equal(storedNotifyLabel({ notifyMinuteUtc: 30 }), "none");
});
});
describe("utcCalendarDay and utcHourMinute", () => {
const at = (iso: string) => Math.floor(Date.parse(iso) / 1000);
it("reads the UTC calendar day, not the server's", () => {
assert.equal(utcCalendarDay(at("2026-08-15T06:00:00Z")), "2026-08-15");
assert.equal(utcCalendarDay(at("2026-08-14T23:59:59Z")), "2026-08-14");
assert.equal(utcCalendarDay(at("2026-08-15T00:00:00Z")), "2026-08-15");
});
it("renders the UTC clock in the form the column stores", () => {
assert.equal(utcHourMinute(at("2026-08-15T00:00:00Z")), "00:00");
assert.equal(utcHourMinute(at("2026-08-15T18:30:59Z")), "18:30");
assert.equal(utcHourMinute(at("2026-08-15T23:59:00Z")), "23:59");
});
it("agrees with formatHourMinuteUtc, since both feed one comparison", () => {
const seconds = at("2026-08-15T09:05:00Z");
assert.equal(utcHourMinute(seconds), formatHourMinuteUtc(9, 5));
});
});
describe("utcDayStartSeconds", () => {
it("returns midnight UTC for a real date", () => {
assert.equal(
utcDayStartSeconds("2026-08-15"),
Math.floor(Date.parse("2026-08-15T00:00:00Z") / 1000)
);
});
it("refuses a date that does not exist rather than rolling it forward", () => {
for (const bad of ["2026-02-30", "2026-13-01", "2026-08-32", "2026-8-15", "x"]) {
assert.equal(utcDayStartSeconds(bad), undefined, bad);
}
});
it("keeps a real leap day", () => {
assert.ok(utcDayStartSeconds("2028-02-29") !== undefined);
assert.equal(utcDayStartSeconds("2027-02-29"), undefined);
});
});