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
18 changes: 17 additions & 1 deletion packages/bots/discord/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { contractTestBot } from '@profullstack/sh1pt-core/testing';
import bot, { loadConfig } from './index.js';
import bot, { loadConfig, toBotEvent } from './index.js';

contractTestBot(bot, { sampleConfig: {}, sampleChannel: '1234567890' });

Expand All @@ -18,3 +18,19 @@ describe('loadConfig', () => {
expect(config.maxConcurrentSessions).toBe(5);
});
});

describe('toBotEvent', () => {
it('falls back when Discord provides an out-of-range timestamp', () => {
expect(toBotEvent({
source: 'user-1',
sourceName: 'User',
text: 'hello',
timestamp: Number.NEGATIVE_INFINITY,
channelId: 'channel-1',
guildId: null,
isGuild: false,
attachments: [],
raw: undefined as never,
}).timestamp).toBe('1970-01-01T00:00:00.000Z');
});
});
5 changes: 3 additions & 2 deletions packages/bots/discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ function isTextChannel(channel: unknown): channel is SendableChannel {
return !!channel && typeof channel === "object" && "send" in channel;
}

function toBotEvent(msg: IncomingMessage): BotEvent {
export function toBotEvent(msg: IncomingMessage): BotEvent {
const date = new Date(msg.timestamp);
return {
type: "message",
channel: msg.channelId,
Expand All @@ -192,7 +193,7 @@ function toBotEvent(msg: IncomingMessage): BotEvent {
filename: a.filename,
mimeType: a.contentType,
})),
timestamp: new Date(msg.timestamp).toISOString(),
timestamp: Number.isNaN(date.getTime()) ? new Date(0).toISOString() : date.toISOString(),
raw: msg.raw,
};
}
Expand Down
Loading