|
| 1 | +import { test, expect } from "bun:test"; |
| 2 | +import { formatCalendarReminder, isAllDayEvent, isCancelledEvent } from "./format.ts"; |
| 3 | +import type { CalendarEvent } from "./types.ts"; |
| 4 | + |
| 5 | +function makeEvent(overrides: Partial<CalendarEvent> = {}): CalendarEvent { |
| 6 | + return { |
| 7 | + id: "event-1", |
| 8 | + status: "confirmed", |
| 9 | + summary: "Team Standup", |
| 10 | + htmlLink: "https://calendar.google.com/event?eid=abc123", |
| 11 | + start: { dateTime: "2026-03-03T10:00:00-05:00" }, |
| 12 | + end: { dateTime: "2026-03-03T10:30:00-05:00" }, |
| 13 | + ...overrides, |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +// formatCalendarReminder tests |
| 18 | + |
| 19 | +test("formatCalendarReminder: includes all fields", () => { |
| 20 | + const event = makeEvent({ |
| 21 | + location: "Conference Room A", |
| 22 | + hangoutLink: "https://meet.google.com/abc-defg-hij", |
| 23 | + description: "Daily sync to discuss progress", |
| 24 | + }); |
| 25 | + |
| 26 | + const { markdown } = formatCalendarReminder(event, "Engineering", 5); |
| 27 | + |
| 28 | + expect(markdown).toContain("Team Standup"); |
| 29 | + expect(markdown).toContain("in 5 min"); |
| 30 | + expect(markdown).toContain("Engineering"); |
| 31 | + expect(markdown).toContain("Conference Room A"); |
| 32 | + expect(markdown).toContain("https://meet.google.com/abc-defg-hij"); |
| 33 | + expect(markdown).toContain("Join video call"); |
| 34 | + expect(markdown).toContain("Daily sync to discuss progress"); |
| 35 | + expect(markdown).toContain("https://calendar.google.com/event?eid=abc123"); |
| 36 | +}); |
| 37 | + |
| 38 | +test("formatCalendarReminder: handles missing optional fields", () => { |
| 39 | + const event = makeEvent({ |
| 40 | + summary: undefined, |
| 41 | + htmlLink: undefined, |
| 42 | + location: undefined, |
| 43 | + hangoutLink: undefined, |
| 44 | + description: undefined, |
| 45 | + }); |
| 46 | + |
| 47 | + const { markdown } = formatCalendarReminder(event, "Work", 3); |
| 48 | + |
| 49 | + expect(markdown).toContain("(No title)"); |
| 50 | + expect(markdown).toContain("in 3 min"); |
| 51 | + expect(markdown).toContain("Work"); |
| 52 | + expect(markdown).not.toContain("Location"); |
| 53 | + expect(markdown).not.toContain("Join video call"); |
| 54 | +}); |
| 55 | + |
| 56 | +test("formatCalendarReminder: truncates long description", () => { |
| 57 | + const event = makeEvent({ |
| 58 | + description: "x".repeat(300), |
| 59 | + }); |
| 60 | + |
| 61 | + const { markdown } = formatCalendarReminder(event, "Cal", 2); |
| 62 | + |
| 63 | + expect(markdown).toContain("…"); |
| 64 | + // Should be truncated to ~200 chars + ellipsis |
| 65 | + const descLine = markdown.split("\n").find((l) => l.startsWith(">")); |
| 66 | + expect(descLine).toBeDefined(); |
| 67 | + expect(descLine!.length).toBeLessThan(210); |
| 68 | +}); |
| 69 | + |
| 70 | +test("formatCalendarReminder: strips HTML from description", () => { |
| 71 | + const event = makeEvent({ |
| 72 | + description: "<b>Important:</b> Bring <a href='#'>docs</a>", |
| 73 | + }); |
| 74 | + |
| 75 | + const { markdown } = formatCalendarReminder(event, "Cal", 4); |
| 76 | + |
| 77 | + expect(markdown).toContain("Important:"); |
| 78 | + expect(markdown).toContain("Bring"); |
| 79 | + expect(markdown).toContain("docs"); |
| 80 | + expect(markdown).not.toContain("<b>"); |
| 81 | + expect(markdown).not.toContain("<a"); |
| 82 | +}); |
| 83 | + |
| 84 | +test("formatCalendarReminder: shows 'starting now' for <=1 min", () => { |
| 85 | + const event = makeEvent(); |
| 86 | + |
| 87 | + const { markdown } = formatCalendarReminder(event, "Cal", 0.5); |
| 88 | + |
| 89 | + expect(markdown).toContain("starting now"); |
| 90 | + expect(markdown).not.toContain("in 0"); |
| 91 | +}); |
| 92 | + |
| 93 | +test("formatCalendarReminder: uses conferenceData when no hangoutLink", () => { |
| 94 | + const event = makeEvent({ |
| 95 | + hangoutLink: undefined, |
| 96 | + conferenceData: { |
| 97 | + entryPoints: [ |
| 98 | + { entryPointType: "phone", uri: "tel:+1234567890" }, |
| 99 | + { entryPointType: "video", uri: "https://zoom.us/j/123456" }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + const { markdown } = formatCalendarReminder(event, "Cal", 3); |
| 105 | + |
| 106 | + expect(markdown).toContain("https://zoom.us/j/123456"); |
| 107 | + expect(markdown).toContain("Join video call"); |
| 108 | +}); |
| 109 | + |
| 110 | +// isAllDayEvent tests |
| 111 | + |
| 112 | +test("isAllDayEvent: returns true for date-only events", () => { |
| 113 | + const event = makeEvent({ |
| 114 | + start: { date: "2026-03-03" }, |
| 115 | + end: { date: "2026-03-04" }, |
| 116 | + }); |
| 117 | + |
| 118 | + expect(isAllDayEvent(event)).toBe(true); |
| 119 | +}); |
| 120 | + |
| 121 | +test("isAllDayEvent: returns false for timed events", () => { |
| 122 | + const event = makeEvent(); |
| 123 | + |
| 124 | + expect(isAllDayEvent(event)).toBe(false); |
| 125 | +}); |
| 126 | + |
| 127 | +// isCancelledEvent tests |
| 128 | + |
| 129 | +test("isCancelledEvent: returns true for cancelled status", () => { |
| 130 | + const event = makeEvent({ status: "cancelled" }); |
| 131 | + |
| 132 | + expect(isCancelledEvent(event)).toBe(true); |
| 133 | +}); |
| 134 | + |
| 135 | +test("isCancelledEvent: returns false for confirmed status", () => { |
| 136 | + const event = makeEvent({ status: "confirmed" }); |
| 137 | + |
| 138 | + expect(isCancelledEvent(event)).toBe(false); |
| 139 | +}); |
| 140 | + |
| 141 | +// Dedup key logic (tested via Set behavior) |
| 142 | + |
| 143 | +test("dedup: same event ID + start time is deduplicated", () => { |
| 144 | + const remindedKeys = new Set<string>(); |
| 145 | + const key1 = "cal1:event-1:2026-03-03T10:00:00Z"; |
| 146 | + const key2 = "cal1:event-1:2026-03-03T10:00:00Z"; |
| 147 | + |
| 148 | + remindedKeys.add(key1); |
| 149 | + expect(remindedKeys.has(key2)).toBe(true); |
| 150 | +}); |
| 151 | + |
| 152 | +test("dedup: different event IDs are not deduplicated", () => { |
| 153 | + const remindedKeys = new Set<string>(); |
| 154 | + const key1 = "cal1:event-1:2026-03-03T10:00:00Z"; |
| 155 | + const key2 = "cal1:event-2:2026-03-03T10:00:00Z"; |
| 156 | + |
| 157 | + remindedKeys.add(key1); |
| 158 | + expect(remindedKeys.has(key2)).toBe(false); |
| 159 | +}); |
| 160 | + |
| 161 | +test("dedup: same event at different times is not deduplicated", () => { |
| 162 | + const remindedKeys = new Set<string>(); |
| 163 | + const key1 = "cal1:event-1:2026-03-03T10:00:00Z"; |
| 164 | + const key2 = "cal1:event-1:2026-03-04T10:00:00Z"; |
| 165 | + |
| 166 | + remindedKeys.add(key1); |
| 167 | + expect(remindedKeys.has(key2)).toBe(false); |
| 168 | +}); |
0 commit comments