From 4b93109582734997c1c05d2c9f65375c45c55291 Mon Sep 17 00:00:00 2001 From: Felix Schulze Date: Mon, 27 Oct 2025 12:53:56 +0100 Subject: [PATCH] add block duration and test --- event-dates.json | 3 ++- lib/signup-time-check.test.ts | 40 +++++++++++++++++++++++++++++++++++ lib/signup-time-check.ts | 26 ++++++++++++++--------- 3 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 lib/signup-time-check.test.ts diff --git a/event-dates.json b/event-dates.json index a2429dd..8efd364 100644 --- a/event-dates.json +++ b/event-dates.json @@ -1,7 +1,8 @@ { "eventDates": ["2025-09-05", "2025-10-25", "2025-12-06", "1999-01-01"], "cutoffTime": "23:00", + "blockDurationHours": 6, "message": "Sign-ups are closed for today's event. Please come back tomorrow.", - "internalComment": "Add event dates in YYYY-MM-DD format. Signups will be disabled after 11pm (23:00) on these dates by default." + "internalComment": "Add event dates in YYYY-MM-DD format. Signups will be disabled after cutoffTime (23:00) for blockDurationHours (6 hours)." } diff --git a/lib/signup-time-check.test.ts b/lib/signup-time-check.test.ts new file mode 100644 index 0000000..f64ae1c --- /dev/null +++ b/lib/signup-time-check.test.ts @@ -0,0 +1,40 @@ +import { isSignupBlocked } from "./signup-time-check"; + +// Test helper to check different scenarios +function testScenario(description: string, date: Date, expected: boolean) { + const result = isSignupBlocked(date); + const status = result.blocked === expected ? "✅ PASS" : "❌ FAIL"; + console.log(`${status} ${description}`); + console.log(` Expected blocked: ${expected}, Got: ${result.blocked}`); + if (result.message) { + console.log(` Message: ${result.message}`); + } + console.log(); +} + +// Run tests +console.log("Testing signup blocking logic...\n"); + +// October 25, 2025 at 22:00 (before cutoff) +testScenario("Oct 25 at 22:00 - Before cutoff", new Date("2025-10-25T22:00:00"), false); + +// October 25, 2025 at 23:00 (exactly at cutoff) +testScenario("Oct 25 at 23:00 - At cutoff time", new Date("2025-10-25T23:00:00"), true); + +// October 25, 2025 at 23:30 (during block period) +testScenario("Oct 25 at 23:30 - During block period", new Date("2025-10-25T23:30:00"), true); + +// October 26, 2025 at 02:00 (3 hours into block) +testScenario("Oct 26 at 02:00 - 3 hours into block", new Date("2025-10-26T02:00:00"), true); + +// October 26, 2025 at 04:59 (just before block ends) +testScenario("Oct 26 at 04:59:59 - Just before block ends", new Date("2025-10-26T04:59:59"), true); + +// October 26, 2025 at 05:00 (block period over) +testScenario("Oct 26 at 05:00 - Block period ended", new Date("2025-10-26T05:00:00"), false); + +// October 26, 2025 at 12:00 (well after block) +testScenario("Oct 26 at 12:00 - Well after block", new Date("2025-10-26T12:00:00"), false); + +// Random non-event date +testScenario("Oct 20 at 23:00 - Non-event date", new Date("2025-10-20T23:00:00"), false); diff --git a/lib/signup-time-check.ts b/lib/signup-time-check.ts index db93342..2b6727f 100644 --- a/lib/signup-time-check.ts +++ b/lib/signup-time-check.ts @@ -1,17 +1,23 @@ import eventConfig from "@/event-dates.json"; -export function isSignupBlocked(): { blocked: boolean; message?: string } { - const now = new Date(); - const currentDate = now.toISOString().split("T")[0]; // YYYY-MM-DD format - const currentTime = now.toTimeString().slice(0, 5); // HH:MM format +export function isSignupBlocked(currentTime?: Date): { blocked: boolean; message?: string } { + const now = currentTime || new Date(); + const cutoffTime = eventConfig.cutoffTime || "15:00"; + const blockDurationHours = eventConfig.blockDurationHours || 6; - // Check if today is an event date - const isEventDay = eventConfig.eventDates.includes(currentDate); + // Check each event date to see if we're in a block period + for (const eventDate of eventConfig.eventDates) { + // Parse the event date and cutoff time in local timezone + const [year, month, day] = eventDate.split("-").map(Number); + const [hours, minutes] = cutoffTime.split(":").map(Number); + const blockStart = new Date(year, month - 1, day, hours, minutes, 0, 0); - if (isEventDay) { - // Check if current time is after the cutoff time (default 15:00 / 3pm) - const cutoffTime = eventConfig.cutoffTime || "15:00"; - if (currentTime >= cutoffTime) { + // Calculate when the block period ends (using wall-clock hours to handle DST correctly) + const blockEnd = new Date(blockStart); + blockEnd.setHours(blockStart.getHours() + blockDurationHours); + + // Check if current time is within the block period + if (now >= blockStart && now < blockEnd) { return { blocked: true, message: eventConfig.message,