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
9 changes: 8 additions & 1 deletion listener/src/api/events-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,18 @@ export function createEventsServer(options: EventsServerOptions): http.Server {
return;
}

const executeAt = new Date(data.executeAt);
if (isNaN(executeAt.getTime())) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'executeAt is not a valid date' }));
return;
}

const notificationId = await options.notificationAPI!.scheduleNotification({
payload: data.payload,
notificationType: data.notificationType || NotificationType.DISCORD,
targetRecipient: data.targetRecipient,
executeAt: new Date(data.executeAt),
executeAt,
maxRetries: data.maxRetries,
priority: data.priority,
eventId: data.eventId,
Expand Down
8 changes: 6 additions & 2 deletions listener/src/services/notification-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ export class NotificationAPI {
requestId?: string
): Promise<number> {
// Validate input
if (!input.executeAt || input.executeAt < new Date()) {
throw new Error('executeAt must be a future date');
if (!input.executeAt || !(input.executeAt instanceof Date) || isNaN(input.executeAt.getTime())) {
throw new Error('executeAt must be a valid date');
}

if (input.executeAt <= new Date()) {
throw new Error('executeAt must be a future timestamp — the provided date has already expired');
}

if (!input.payload || typeof input.payload !== 'object') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ describe('NotificationScheduler (Refactored)', () => {

await expect(
api.scheduleNotification(pastInput)
).rejects.toThrow('executeAt must be a future date');
).rejects.toThrow('executeAt must be a future timestamp');
});

test('should schedule Discord notification', async () => {
Expand Down
42 changes: 40 additions & 2 deletions listener/src/tests/notification-scheduler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('NotificationScheduler', () => {
expect(notification!.status).toBe(NotificationStatus.PENDING);
});

test('should reject past execution time', async () => {
test('should reject expired execution time', async () => {
const pastDate = new Date(Date.now() - 60000);

await expect(
Expand All @@ -265,7 +265,45 @@ describe('NotificationScheduler', () => {
targetRecipient: 'test-webhook',
executeAt: pastDate,
})
).rejects.toThrow('executeAt must be a future date');
).rejects.toThrow('executeAt must be a future timestamp — the provided date has already expired');
});

test('should reject execution time equal to now', async () => {
// A timestamp at (or just before) the current moment is already expired
const now = new Date();

await expect(
api.scheduleNotification({
payload: { message: 'Test' },
notificationType: NotificationType.DISCORD,
targetRecipient: 'test-webhook',
executeAt: now,
})
).rejects.toThrow('executeAt must be a future timestamp');
});

test('should reject invalid date object', async () => {
await expect(
api.scheduleNotification({
payload: { message: 'Test' },
notificationType: NotificationType.DISCORD,
targetRecipient: 'test-webhook',
executeAt: new Date('not-a-date'),
})
).rejects.toThrow('executeAt must be a valid date');
});

test('should accept execution time 1 second in the future', async () => {
const nearFuture = new Date(Date.now() + 1000);

const id = await api.scheduleNotification({
payload: { message: 'Test' },
notificationType: NotificationType.DISCORD,
targetRecipient: 'test-webhook',
executeAt: nearFuture,
});

expect(id).toBeGreaterThan(0);
});

test('should schedule Discord notification', async () => {
Expand Down
Loading