48 lines
2.2 KiB
TypeScript
48 lines
2.2 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { isValidTimeZone, parseScheduledAt } from "./campaignSchedule";
|
|
|
|
describe("isValidTimeZone", () => {
|
|
it("accepts IANA zones and rejects garbage", () => {
|
|
expect(isValidTimeZone("Europe/Berlin")).toBe(true);
|
|
expect(isValidTimeZone("America/New_York")).toBe(true);
|
|
expect(isValidTimeZone("Mars/Olympus")).toBe(false);
|
|
expect(isValidTimeZone("")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("parseScheduledAt", () => {
|
|
it("returns null for empty input", () => {
|
|
expect(parseScheduledAt(null, "Europe/Berlin")).toEqual({ ok: true, date: null });
|
|
});
|
|
|
|
it("interprets wall-clock time in the campaign timezone, not server time", () => {
|
|
const berlin = parseScheduledAt("2026-07-20T09:00", "Europe/Berlin");
|
|
const newYork = parseScheduledAt("2026-07-20T09:00", "America/New_York");
|
|
if (!berlin.ok || !newYork.ok) throw new Error("expected ok results");
|
|
// CEST is UTC+2 in July, EDT is UTC-4 — same wall clock, 6h apart.
|
|
expect(berlin.date?.toISOString()).toBe("2026-07-20T07:00:00.000Z");
|
|
expect(newYork.date?.toISOString()).toBe("2026-07-20T13:00:00.000Z");
|
|
});
|
|
|
|
it("respects winter/summer offsets of the same zone", () => {
|
|
const summer = parseScheduledAt("2026-07-20T09:00", "Europe/Berlin");
|
|
const winter = parseScheduledAt("2026-01-20T09:00", "Europe/Berlin");
|
|
if (!summer.ok || !winter.ok) throw new Error("expected ok results");
|
|
expect(summer.date?.toISOString()).toBe("2026-07-20T07:00:00.000Z");
|
|
expect(winter.date?.toISOString()).toBe("2026-01-20T08:00:00.000Z");
|
|
});
|
|
|
|
it("keeps explicit offsets untouched", () => {
|
|
const zulu = parseScheduledAt("2026-07-20T09:00:00Z", "America/New_York");
|
|
const offset = parseScheduledAt("2026-07-20T09:00:00+02:00", "America/New_York");
|
|
if (!zulu.ok || !offset.ok) throw new Error("expected ok results");
|
|
expect(zulu.date?.toISOString()).toBe("2026-07-20T09:00:00.000Z");
|
|
expect(offset.date?.toISOString()).toBe("2026-07-20T07:00:00.000Z");
|
|
});
|
|
|
|
it("rejects invalid timezone or unparseable date", () => {
|
|
expect(parseScheduledAt("2026-07-20T09:00", "Mars/Olympus")).toEqual({ ok: false });
|
|
expect(parseScheduledAt("not-a-date", "Europe/Berlin")).toEqual({ ok: false });
|
|
});
|
|
});
|