Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions src/features/advent-of-code/advent-calendar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,59 @@ import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import {
getAdventPostAction,
getAdventPostStep,
getAdventPostSteps,
getPostContent,
getThreadTitle,
} from './advent-calendar.js';

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 };
Expand All @@ -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', () => {
Expand All @@ -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 <t:1796187600:R>'
);
});

void it('links to the puzzle once the day is open', () => {
assert.equal(
getPostContent({ year: 2026, day: 3, phase: 'open' }),
Expand Down
72 changes: 40 additions & 32 deletions src/features/advent-of-code/advent-calendar.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -14,63 +15,70 @@ 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 =>
`Day ${day}, ${year}`;

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}`;
Expand Down
31 changes: 19 additions & 12 deletions src/features/advent-of-code/advent-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
EVENT_TIME_ZONE,
type AdventPostStep,
getAdventPostAction,
getAdventPostStep,
getAdventPostSteps,
getPostContent,
getThreadTitle,
} from './advent-calendar.js';
Expand All @@ -27,7 +27,7 @@ const createPost = async (step: AdventPostStep): Promise<void> => {
logger.info(`✅ Created Advent of Code post: ${title}`);
};

const revealFirstDay = async (
const revealPost = async (
threadId: string,
step: AdventPostStep
): Promise<void> => {
Expand Down Expand Up @@ -57,13 +57,7 @@ const revealFirstDay = async (
logger.info(`✅ Revealed Advent of Code day ${step.day} for ${step.year}`);
};

const runAdventPostCheck = async (): Promise<void> => {
const step = getAdventPostStep(Temporal.Now.plainDateISO(EVENT_TIME_ZONE));

if (step === null) {
return;
}

const processAdventPostStep = async (step: AdventPostStep): Promise<void> => {
const existingPost = await prisma.adventOfCode.findUnique({
where: { year_day: { year: step.year, day: step.day } },
});
Expand All @@ -75,7 +69,20 @@ const runAdventPostCheck = async (): Promise<void> => {
}

if (action === 'reveal' && existingPost) {
await revealFirstDay(existingPost.threadId, step);
await revealPost(existingPost.threadId, step);
}
};

const runAdventPostCheck = async (): Promise<void> => {
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
);
});
}
};

Expand All @@ -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...');
Expand Down
Loading