107 lines
3.2 KiB
TypeScript
107 lines
3.2 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
|
|
import {
|
|
DAY_MS,
|
|
enumerateUtcDays,
|
|
hasOverlap,
|
|
isPublicAllowedByDefaultPolicy,
|
|
isWeekendUtcDay,
|
|
normalizeUtcDayStart,
|
|
parseIsoDate,
|
|
} from "@/lib/booking";
|
|
|
|
describe("parseIsoDate", () => {
|
|
it("accepts ISO YYYY-MM-DD", () => {
|
|
const d = parseIsoDate("2026-06-15");
|
|
expect(d).toBeInstanceOf(Date);
|
|
expect(d?.toISOString().startsWith("2026-06-15")).toBe(true);
|
|
});
|
|
|
|
it("returns null for garbage input", () => {
|
|
expect(parseIsoDate("not a date")).toBeNull();
|
|
expect(parseIsoDate(null)).toBeNull();
|
|
expect(parseIsoDate(undefined)).toBeNull();
|
|
expect(parseIsoDate(123)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("normalizeUtcDayStart", () => {
|
|
it("zeroes out time components", () => {
|
|
const d = new Date("2026-06-15T17:30:45.123Z");
|
|
const n = normalizeUtcDayStart(d);
|
|
expect(n.getUTCHours()).toBe(0);
|
|
expect(n.getUTCMinutes()).toBe(0);
|
|
expect(n.getUTCSeconds()).toBe(0);
|
|
expect(n.getUTCMilliseconds()).toBe(0);
|
|
expect(n.toISOString().slice(0, 10)).toBe("2026-06-15");
|
|
});
|
|
});
|
|
|
|
describe("hasOverlap", () => {
|
|
const d = (iso: string) => new Date(iso);
|
|
|
|
it("detects partial overlap", () => {
|
|
expect(
|
|
hasOverlap(d("2026-06-10"), d("2026-06-15"), d("2026-06-13"), d("2026-06-20")),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("returns false for adjacent intervals (touching)", () => {
|
|
expect(
|
|
hasOverlap(d("2026-06-10"), d("2026-06-15"), d("2026-06-15"), d("2026-06-20")),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("returns false for fully separate", () => {
|
|
expect(
|
|
hasOverlap(d("2026-06-01"), d("2026-06-05"), d("2026-06-10"), d("2026-06-15")),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("returns true when one contains the other", () => {
|
|
expect(
|
|
hasOverlap(d("2026-06-01"), d("2026-06-30"), d("2026-06-10"), d("2026-06-15")),
|
|
).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("enumerateUtcDays", () => {
|
|
it("enumerates each day between start and end (exclusive)", () => {
|
|
const days = enumerateUtcDays(new Date("2026-06-10"), new Date("2026-06-13"));
|
|
expect(days.length).toBe(3);
|
|
expect(days[0].toISOString().slice(0, 10)).toBe("2026-06-10");
|
|
expect(days[2].toISOString().slice(0, 10)).toBe("2026-06-12");
|
|
});
|
|
|
|
it("returns empty when start === end", () => {
|
|
const days = enumerateUtcDays(new Date("2026-06-10"), new Date("2026-06-10"));
|
|
expect(days).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("isWeekendUtcDay", () => {
|
|
it("flags Saturday (2026-06-13)", () => {
|
|
expect(isWeekendUtcDay(new Date("2026-06-13"))).toBe(true);
|
|
});
|
|
it("flags Sunday (2026-06-14)", () => {
|
|
expect(isWeekendUtcDay(new Date("2026-06-14"))).toBe(true);
|
|
});
|
|
it("rejects Monday (2026-06-15)", () => {
|
|
expect(isWeekendUtcDay(new Date("2026-06-15"))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("isPublicAllowedByDefaultPolicy", () => {
|
|
it("blocks weekends by default (CE-priority policy)", () => {
|
|
expect(isPublicAllowedByDefaultPolicy(new Date("2026-06-13"))).toBe(false);
|
|
});
|
|
it("allows weekdays", () => {
|
|
expect(isPublicAllowedByDefaultPolicy(new Date("2026-06-15"))).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("DAY_MS constant", () => {
|
|
it("equals 86_400_000", () => {
|
|
expect(DAY_MS).toBe(86_400_000);
|
|
});
|
|
});
|