From 38b7ee301158bc138b1c46a8fe322b0b6944d513 Mon Sep 17 00:00:00 2001 From: Ali Hammoud Date: Tue, 1 Dec 2026 21:04:14 +0200 Subject: [PATCH] feat: open aoc days 1 day before reveal and show a countdown --- .../advent-of-code/advent-calendar.test.ts | 96 +++++++++---------- .../advent-of-code/advent-calendar.ts | 72 +++++++------- .../advent-of-code/advent-scheduler.ts | 31 +++--- 3 files changed, 107 insertions(+), 92 deletions(-) diff --git a/src/features/advent-of-code/advent-calendar.test.ts b/src/features/advent-of-code/advent-calendar.test.ts index e1760d5..266d2e7 100644 --- a/src/features/advent-of-code/advent-calendar.test.ts +++ b/src/features/advent-of-code/advent-calendar.test.ts @@ -2,7 +2,7 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { getAdventPostAction, - getAdventPostStep, + getAdventPostSteps, getPostContent, getThreadTitle, } from './advent-calendar.js'; @@ -10,54 +10,51 @@ import { const toPlainDate = (isoDate: string): Temporal.PlainDate => Temporal.PlainDate.from(isoDate); -void describe('getAdventPostStep', () => { - void it('returns null before the countdown window opens', () => { - assert.equal(getAdventPostStep(toPlainDate('2026-11-27')), null); - assert.equal(getAdventPostStep(toPlainDate('2026-01-05')), null); +void describe('getAdventPostSteps', () => { + void it('returns no steps before the first countdown window opens', () => { + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-11-27')), []); + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-01-05')), []); }); - void it('returns a day 1 countdown step from 3 days before the event until its start', () => { - assert.deepEqual(getAdventPostStep(toPlainDate('2026-11-28')), { - year: 2026, - day: 1, - phase: 'countdown', - }); - assert.deepEqual(getAdventPostStep(toPlainDate('2026-11-30')), { - year: 2026, - day: 1, - phase: 'countdown', - }); + void it('returns the day 1 countdown from 3 days before the event until its start', () => { + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-11-28')), [ + { year: 2026, day: 1, phase: 'countdown' }, + ]); + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-11-30')), [ + { year: 2026, day: 1, phase: 'countdown' }, + ]); }); - void it('returns the event day when the event is open', () => { - assert.deepEqual(getAdventPostStep(toPlainDate('2026-12-01')), { - year: 2026, - day: 1, - phase: 'open', - }); - assert.deepEqual(getAdventPostStep(toPlainDate('2026-12-05')), { - year: 2026, - day: 5, - phase: 'open', - }); - assert.deepEqual(getAdventPostStep(toPlainDate('2026-12-12')), { - year: 2026, - day: 12, - phase: 'open', - }); + void it('returns the next day countdown 1 day before it opens', () => { + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-12-01')), [ + { year: 2026, day: 1, phase: 'open' }, + { year: 2026, day: 2, phase: 'countdown' }, + ]); + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-12-05')), [ + { year: 2026, day: 1, phase: 'open' }, + { year: 2026, day: 2, phase: 'open' }, + { year: 2026, day: 3, phase: 'open' }, + { year: 2026, day: 4, phase: 'open' }, + { year: 2026, day: 5, phase: 'open' }, + { year: 2026, day: 6, phase: 'countdown' }, + ]); }); - void it('returns null after the final day', () => { - assert.equal(getAdventPostStep(toPlainDate('2026-12-13')), null); - assert.equal(getAdventPostStep(toPlainDate('2026-12-31')), null); + void it('returns every day as open once the final day has started', () => { + const steps = getAdventPostSteps(toPlainDate('2026-12-12')); + + assert.equal(steps.length, 12); + assert.ok(steps.every((step) => step.phase === 'open')); + assert.equal(steps.at(-1)?.day, 12); }); -}); -void describe('getAdventPostAction', () => { - void it('does nothing without a step', () => { - assert.equal(getAdventPostAction(null, false), 'none'); + void it('returns no steps after the final day', () => { + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-12-13')), []); + assert.deepEqual(getAdventPostSteps(toPlainDate('2026-12-31')), []); }); +}); +void describe('getAdventPostAction', () => { void it('creates a post when none exists for the step', () => { const countdownStep = { year: 2026, day: 1, phase: 'countdown' as const }; const openDayStep = { year: 2026, day: 4, phase: 'open' as const }; @@ -66,23 +63,19 @@ void describe('getAdventPostAction', () => { assert.equal(getAdventPostAction(openDayStep, false), 'create'); }); - void it('reveals the link on day 1 when the countdown post exists', () => { + void it('reveals the link on an open day when its post exists', () => { const openDayOneStep = { year: 2026, day: 1, phase: 'open' as const }; + const openDayFourStep = { year: 2026, day: 4, phase: 'open' as const }; assert.equal(getAdventPostAction(openDayOneStep, true), 'reveal'); + assert.equal(getAdventPostAction(openDayFourStep, true), 'reveal'); }); - void it('does nothing while the countdown is still running and the post exists', () => { - const countdownStep = { year: 2026, day: 1, phase: 'countdown' as const }; + void it('does nothing while a countdown is still running and the post exists', () => { + const countdownStep = { year: 2026, day: 2, phase: 'countdown' as const }; assert.equal(getAdventPostAction(countdownStep, true), 'none'); }); - - void it('does nothing for later days when the post exists', () => { - const openDayStep = { year: 2026, day: 2, phase: 'open' as const }; - - assert.equal(getAdventPostAction(openDayStep, true), 'none'); - }); }); void describe('getPostContent', () => { @@ -93,6 +86,13 @@ void describe('getPostContent', () => { ); }); + void it('renders a relative Discord timestamp for a later day countdown', () => { + assert.equal( + getPostContent({ year: 2026, day: 2, phase: 'countdown' }), + 'Day 2 starts ' + ); + }); + void it('links to the puzzle once the day is open', () => { assert.equal( getPostContent({ year: 2026, day: 3, phase: 'open' }), diff --git a/src/features/advent-of-code/advent-calendar.ts b/src/features/advent-of-code/advent-calendar.ts index 6ecb89f..23eeafc 100644 --- a/src/features/advent-of-code/advent-calendar.ts +++ b/src/features/advent-of-code/advent-calendar.ts @@ -1,6 +1,7 @@ import { time } from 'discord.js'; -export const POST_BEFORE_DAYS = 3; +export const POST_FIRST_DAY_BEFORE_DAYS = 3; +export const POST_BEFORE_DAYS = 1; export const FINAL_DAY = 12; export const EVENT_TIME_ZONE = 'EST'; @@ -14,52 +15,57 @@ export type AdventPostStep = { export type AdventPostAction = 'create' | 'reveal' | 'none'; -const getEventStartDate = (year: number): Temporal.PlainDate => - Temporal.PlainDate.from({ year, month: 12, day: 1 }); +const getDayOpenDate = (year: number, day: number): Temporal.PlainDate => + Temporal.PlainDate.from({ year, month: 12, day }); -const getEventStartInstant = (year: number): Temporal.Instant => - getEventStartDate(year) +const getDayOpenInstant = (year: number, day: number): Temporal.Instant => + getDayOpenDate(year, day) .toZonedDateTime({ timeZone: EVENT_TIME_ZONE }) .toInstant(); -export const getAdventPostStep = ( - today: Temporal.PlainDate -): AdventPostStep | null => { - const eventStartDate = getEventStartDate(today.year); - const countdownStartDate = eventStartDate.subtract({ - days: POST_BEFORE_DAYS, - }); - - if (Temporal.PlainDate.compare(today, countdownStartDate) < 0) { - return null; - } +const getCountdownDate = (year: number, day: number): Temporal.PlainDate => { + const daysBeforeOpen = + day === 1 ? POST_FIRST_DAY_BEFORE_DAYS : POST_BEFORE_DAYS; - if (Temporal.PlainDate.compare(today, eventStartDate) < 0) { - return { year: today.year, day: 1, phase: 'countdown' }; - } + return getDayOpenDate(year, day).subtract({ days: daysBeforeOpen }); +}; - const day = eventStartDate.until(today, { largestUnit: 'days' }).days + 1; +export const getAdventPostSteps = ( + today: Temporal.PlainDate +): AdventPostStep[] => { + const finalDayDate = getDayOpenDate(today.year, FINAL_DAY); - if (day > FINAL_DAY) { - return null; + if (Temporal.PlainDate.compare(today, finalDayDate) > 0) { + return []; } - return { year: today.year, day, phase: 'open' }; + return Array.from({ length: FINAL_DAY }, (_, index) => index + 1).flatMap( + (day): AdventPostStep[] => { + if ( + Temporal.PlainDate.compare(today, getCountdownDate(today.year, day)) < 0 + ) { + return []; + } + + const phase: AdventPostPhase = + Temporal.PlainDate.compare(today, getDayOpenDate(today.year, day)) < 0 + ? 'countdown' + : 'open'; + + return [{ year: today.year, day, phase }]; + } + ); }; export const getAdventPostAction = ( - step: AdventPostStep | null, + step: AdventPostStep, hasExistingPost: boolean ): AdventPostAction => { - if (step === null) { - return 'none'; - } - if (!hasExistingPost) { return 'create'; } - return step.phase === 'open' && step.day === 1 ? 'reveal' : 'none'; + return step.phase === 'open' ? 'reveal' : 'none'; }; export const getThreadTitle = (year: number, day: number): string => @@ -67,10 +73,12 @@ export const getThreadTitle = (year: number, day: number): string => export const getPostContent = (step: AdventPostStep): string => { if (step.phase === 'countdown') { - const startUnixSeconds = Math.floor( - getEventStartInstant(step.year).epochMilliseconds / 1000 + const openUnixSeconds = Math.floor( + getDayOpenInstant(step.year, step.day).epochMilliseconds / 1000 ); - return `Advent of Code ${step.year} starts ${time(startUnixSeconds, 'R')}`; + const label = + step.day === 1 ? `Advent of Code ${step.year}` : `Day ${step.day}`; + return `${label} starts ${time(openUnixSeconds, 'R')}`; } return `https://adventofcode.com/${step.year}/day/${step.day}`; diff --git a/src/features/advent-of-code/advent-scheduler.ts b/src/features/advent-of-code/advent-scheduler.ts index 3808dc5..73319a5 100644 --- a/src/features/advent-of-code/advent-scheduler.ts +++ b/src/features/advent-of-code/advent-scheduler.ts @@ -6,7 +6,7 @@ import { EVENT_TIME_ZONE, type AdventPostStep, getAdventPostAction, - getAdventPostStep, + getAdventPostSteps, getPostContent, getThreadTitle, } from './advent-calendar.js'; @@ -27,7 +27,7 @@ const createPost = async (step: AdventPostStep): Promise => { logger.info(`✅ Created Advent of Code post: ${title}`); }; -const revealFirstDay = async ( +const revealPost = async ( threadId: string, step: AdventPostStep ): Promise => { @@ -57,13 +57,7 @@ const revealFirstDay = async ( logger.info(`✅ Revealed Advent of Code day ${step.day} for ${step.year}`); }; -const runAdventPostCheck = async (): Promise => { - const step = getAdventPostStep(Temporal.Now.plainDateISO(EVENT_TIME_ZONE)); - - if (step === null) { - return; - } - +const processAdventPostStep = async (step: AdventPostStep): Promise => { const existingPost = await prisma.adventOfCode.findUnique({ where: { year_day: { year: step.year, day: step.day } }, }); @@ -75,7 +69,20 @@ const runAdventPostCheck = async (): Promise => { } if (action === 'reveal' && existingPost) { - await revealFirstDay(existingPost.threadId, step); + await revealPost(existingPost.threadId, step); + } +}; + +const runAdventPostCheck = async (): Promise => { + const steps = getAdventPostSteps(Temporal.Now.plainDateISO(EVENT_TIME_ZONE)); + + for (const step of steps) { + await processAdventPostStep(step).catch((error) => { + logger.error( + `❌ Failed to process Advent of Code day ${step.day} for ${step.year}:`, + error + ); + }); } }; @@ -86,8 +93,8 @@ const runScheduledAdventPostCheck = (): void => { }; /** - * Posts a countdown to the event start, edits it into the day 1 link when the - * event opens, and posts each following day at midnight EST. + * Posts each day's countdown before it opens, edits every post into its puzzle + * link when the day opens, and re-checks daily at midnight EST. */ export function initializeAdventScheduler(): void { logger.info('🎄 Initializing Advent of Code scheduler...');