Skip to content
Closed
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
23 changes: 22 additions & 1 deletion packages/bots/telegram/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 Down Expand Up @@ -31,3 +31,24 @@ describe('loadConfig', () => {
expect(config.maxConcurrentSessions).toBe(5);
});
});

describe('toBotEvent', () => {
const message = (timestamp: number) => ({
source: 'user-1',
sourceName: 'User',
text: 'hello',
timestamp,
chatId: 123,
isGroup: false,
attachments: [],
raw: {},
});

it('preserves valid message timestamps', () => {
expect(toBotEvent(message(Date.UTC(2026, 0, 1))).timestamp).toBe('2026-01-01T00:00:00.000Z');
});

it('falls back when the provider supplies an invalid timestamp', () => {
expect(toBotEvent(message(Number.NaN)).timestamp).toBe('1970-01-01T00:00:00.000Z');
});
});
5 changes: 3 additions & 2 deletions packages/bots/telegram/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ function parsePositiveIntegerEnv(value: string | undefined, fallback: number): n
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : fallback;
}

function toBotEvent(msg: IncomingMessage): BotEvent {
export function toBotEvent(msg: IncomingMessage): BotEvent {
const date = new Date(msg.timestamp);
return {
type: "message",
channel: String(msg.chatId),
Expand All @@ -127,7 +128,7 @@ function toBotEvent(msg: IncomingMessage): BotEvent {
},
text: msg.text,
attachments: msg.attachments.map((a) => ({ url: a.url, filename: a.filename })),
timestamp: new Date(msg.timestamp).toISOString(),
timestamp: Number.isNaN(date.getTime()) ? new Date(0).toISOString() : date.toISOString(),
raw: msg.raw,
};
}
Expand Down
Loading