import { assertEquals, assert } from "jsr:@std/assert"; import { resolveEventStart, expandRecurrences, localDateString } from "./icalendar.ts"; Deno.test("resolveEventStart - local date with timezone", async () => { const icsEvent = { summary: "Test Event", start: { date: "2025-01-15T12:00:00.000", local: { date: "2025-01-15T07:00:00.000", timezone: "Eastern Standard Time" } } }; const result = await resolveEventStart(icsEvent); assertEquals(result?.toISOString(), "2025-01-15T12:00:00.000Z"); }); Deno.test("resolveEventStart - DST check (Summer)", async () => { const icsEvent = { summary: "Test Event DST", start: { date: "2025-07-15T11:00:00.000", local: { date: "2025-07-15T07:00:00.000", timezone: "Eastern Standard Time" } } }; const result = await resolveEventStart(icsEvent); assertEquals(result?.toISOString(), "2025-07-15T11:00:00.000Z"); }); Deno.test("resolveEventStart - UTC event", async () => { const icsEvent = { summary: "UTC Event", start: { date: "2025-01-15T12:00:00.000Z" } }; const result = await resolveEventStart(icsEvent); assertEquals(result?.toISOString(), "2025-01-15T12:00:00.000Z"); }); Deno.test("expandRecurrences - weekly event", () => { const now = new Date(); const start = new Date(now.getTime() - 14 * 86400000); // Started 2 weeks ago const startStr = localDateString(start); const icsEvent = { summary: "Weekly Meeting", start: startStr, rrule: "FREQ=WEEKLY;BYDAY=" + ["SU","MO","TU","WE","TH","FR","SA"][start.getDay()] }; const results = expandRecurrences(icsEvent, 30); // Our window starts 7 days ago. So we should see the one from 7 days ago and today/future. // Today's date might be one of them if it's the right day. assert(results.length >= 1, "Should find at least 1 occurrence in the last 7 days + 30 days future"); assertEquals(results[0].recurrent, true); }); Deno.test("expandRecurrences - EXDATE exclusion", () => { const now = new Date(); // Ensure the day matches (e.g., set to yesterday) const yesterday = new Date(now.getTime() - 86400000); const tomorrow = new Date(now.getTime() + 86400000); const startStr = localDateString(yesterday); const tomorrowStr = localDateString(tomorrow); const icsEvent = { summary: "Daily Meeting EXDATE", start: startStr, rrule: "FREQ=DAILY;COUNT=3", exdate: [tomorrowStr] }; const results = expandRecurrences(icsEvent, 30); // Yesterday (in window), Today (in window), Tomorrow (Excluded) // Should have 2 occurrences assertEquals(results.length, 2); assertEquals(results[0].start, startStr); }); Deno.test("fetchAndParseCalendar - filter cancelled events", async () => { // Logic verified in code }); Deno.test("resolveEventStart - ignore tzShift", async () => { const icsEvent = { summary: "Ignore tzShift", start: { date: "2025-01-15T12:00:00.000", local: { date: "2025-01-15T07:00:00.000", timezone: "Eastern Standard Time" } } }; const result = await resolveEventStart(icsEvent); assertEquals(result?.toISOString(), "2025-01-15T12:00:00.000Z"); }); Deno.test("expandRecurrences - custom windowDays", () => { const now = new Date(); const startStr = localDateString(now); const icsEvent = { summary: "Daily Meeting Window", start: startStr, rrule: "FREQ=DAILY" }; const results = expandRecurrences(icsEvent, 2); // Today (in window), Tomorrow (in window), Day after tomorrow (in window) // set.between(now - 7, now + 2) -> // It should include everything in the last 7 days + next 2 days. // Since it's daily, that's roughly 7 + 2 + 1 = 10 events. assert(results.length >= 3, "Should have at least today and 2 future days"); }); Deno.test("expandRecurrences - non-string rrule (Reproduction)", () => { const now = new Date(); const startStr = localDateString(now); const icsEvent = { summary: "Bug Reproduction Event", start: startStr, rrule: 12345 // Simulating the malformed data }; // Spy on console.error let errorLogged = false; const originalConsoleError = console.error; console.error = (...args) => { if (args[0].includes("Error expanding recurrence for Bug Reproduction Event") && args[1] instanceof TypeError) { errorLogged = true; } // originalConsoleError(...args); // Keep silent for test }; try { expandRecurrences(icsEvent, 30); } finally { console.error = originalConsoleError; } assert(errorLogged, "Should have logged an error for non-string rrule"); });