Files
suaveplan/packages/foundation/data/templates/src/plugins/dynamic/DateTimePlugin.test.ts
2026-05-27 23:47:53 +01:00

66 lines
2.4 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import { TemplateEngine } from "../../engine/TemplateEngine.js";
import { createDateTimePlugin } from "./DateTimePlugin.js";
// Fixed clock: 2024-06-15 09:05:03.007
const FIXED_MS = new Date(2024, 5, 15, 9, 5, 3, 7).getTime();
const clock = () => FIXED_MS;
function engine() {
return new TemplateEngine().use(createDateTimePlugin({ clock }));
}
describe("createDateTimePlugin", () => {
it("formats _date as YYYY-MM-DD", { timeout: 5000 }, () => {
expect(engine().parse("{_date}", {})).toBe("2024-06-15");
});
it("formats _date with custom pattern", { timeout: 5000 }, () => {
expect(engine().parse("{_date:DD/MM/YYYY}", {})).toBe("15/06/2024");
});
it("formats _time as HH:mm:ss", { timeout: 5000 }, () => {
expect(engine().parse("{_time}", {})).toBe("09:05:03");
});
it("formats _time with custom pattern", { timeout: 5000 }, () => {
expect(engine().parse("{_time:HH:mm}", {})).toBe("09:05");
});
it("formats _datetime with default pattern", { timeout: 5000 }, () => {
expect(engine().parse("{_datetime}", {})).toBe("2024-06-15 09:05:03");
});
it("formats _timestamp in milliseconds", { timeout: 5000 }, () => {
expect(engine().parse("{_timestamp}", {})).toBe(String(FIXED_MS));
});
it("formats _timestamp in seconds", { timeout: 5000 }, () => {
expect(engine().parse("{_timestamp:s}", {})).toBe(
String(Math.floor(FIXED_MS / 1000))
);
});
it("uses real Date.now when no clock provided", { timeout: 5000 }, () => {
const e = new TemplateEngine().use(createDateTimePlugin());
const result = e.parse("{_date}", {});
// Should be a valid date string
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
});
it("returns original match for unknown type", { timeout: 5000 }, () => {
// The pattern won't match unknown types, this tests the default branch
// We inject directly by calling transform
const plugin = createDateTimePlugin({ clock });
const match = {
match: "{_other}",
groups: Object.assign(["{_other}", "other", undefined], {
index: 0,
input: "{_other}",
}) as RegExpMatchArray,
index: 0,
};
expect(plugin.transform(match)).toBe("{_other}");
});
});