uptime-kuma/test/backend-test/test-util.js
2026-01-01 17:06:05 +01:00

47 lines
1.8 KiB
JavaScript

const { describe, test } = require("node:test");
const assert = require("node:assert");
const dayjs = require("dayjs");
const { getDaysRemaining, getDaysBetween } = require("../../server/util-server");
const { SQL_DATETIME_FORMAT } = require("../../src/util");
dayjs.extend(require("dayjs/plugin/utc"));
dayjs.extend(require("dayjs/plugin/customParseFormat"));
describe("Server Utilities", () => {
test("getDaysBetween() calculates days between dates within same month", () => {
const days = getDaysBetween(new Date(2025, 9, 7), new Date(2025, 9, 10));
assert.strictEqual(days, 3);
});
test("getDaysBetween() calculates days between dates across years", () => {
const days = getDaysBetween(new Date(2024, 9, 7), new Date(2025, 9, 10));
assert.strictEqual(days, 368);
});
test("getDaysRemaining() returns positive value when target date is in future", () => {
const days = getDaysRemaining(new Date(2025, 9, 7), new Date(2025, 9, 10));
assert.strictEqual(days, 3);
});
test("getDaysRemaining() returns negative value when target date is in past", () => {
const days = getDaysRemaining(new Date(2025, 9, 10), new Date(2025, 9, 7));
assert.strictEqual(days, -3);
});
test("SQL_DATETIME_FORMAT constant matches MariaDB/MySQL format", () => {
assert.strictEqual(SQL_DATETIME_FORMAT, "YYYY-MM-DD HH:mm:ss");
});
test("SQL_DATETIME_FORMAT produces valid SQL datetime string", () => {
const current = dayjs.utc("2025-12-19T01:04:02.129Z");
const sqlFormat = current.utc().format(SQL_DATETIME_FORMAT);
assert.strictEqual(sqlFormat, "2025-12-19 01:04:02");
// Verify it can be parsed back
const parsedDate = dayjs.utc(sqlFormat, SQL_DATETIME_FORMAT);
assert.strictEqual(parsedDate.unix(), current.unix());
});
});