2026-05-18 21:54:39 +07:00
|
|
|
import { afterEach, beforeAll, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
2026-05-18 08:54:24 +07:00
|
|
|
import { config } from '../src/env';
|
2026-05-18 08:55:22 +07:00
|
|
|
import logger from '../src/utils/logger';
|
2026-05-18 07:06:21 +07:00
|
|
|
|
2026-05-18 21:54:39 +07:00
|
|
|
let realPhotoBuffer: Buffer;
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(
|
|
|
|
|
'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png',
|
|
|
|
|
);
|
|
|
|
|
if (!res.ok) throw new Error('Wikimedia download failed');
|
|
|
|
|
const arrayBuffer = await res.arrayBuffer();
|
|
|
|
|
realPhotoBuffer = Buffer.from(arrayBuffer);
|
|
|
|
|
} catch {
|
|
|
|
|
// Fallback 1x1px JPEG
|
|
|
|
|
realPhotoBuffer = Buffer.from(
|
|
|
|
|
'ffd8ffe000104a46494600010101006000600000ffdb004300080606070605080707070909080a0c140d0c0b0b0c1912130f141d1a1f1e1d1a1c1c20242e2720222c231c1c2837292c30313434341f27393d38323c2e333432ffc0000b080001000101011100ffc4001f0000010501010110000000000000000000000102030405060708ffda000c03010002110311003f00a0ffd9',
|
|
|
|
|
'hex',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:06:21 +07:00
|
|
|
// Mock Telegraf and fetch
|
2026-05-18 07:29:01 +07:00
|
|
|
mock.module('telegraf', () => {
|
2026-05-18 07:06:21 +07:00
|
|
|
return {
|
|
|
|
|
Telegraf: class {
|
|
|
|
|
constructor(token) {
|
|
|
|
|
this.token = token;
|
2026-05-18 07:27:06 +07:00
|
|
|
this.telegram = {
|
2026-05-18 07:29:01 +07:00
|
|
|
sendPhoto: mock(() =>
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
message_id: 12345,
|
|
|
|
|
photo: [
|
|
|
|
|
{ file_id: 'photo_id_low', file_unique_id: 'unique_id_low' },
|
|
|
|
|
{ file_id: 'photo_id_high', file_unique_id: 'unique_id_high' },
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-05-18 08:18:55 +07:00
|
|
|
sendDocument: mock(() =>
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
message_id: 54321,
|
|
|
|
|
document: {
|
|
|
|
|
file_id: 'document_id',
|
|
|
|
|
file_unique_id: 'document_unique_id',
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-05-21 22:55:42 +07:00
|
|
|
getFile: mock(() =>
|
|
|
|
|
Promise.resolve({
|
|
|
|
|
file_id: 'some_file_id',
|
|
|
|
|
file_size: 98765,
|
|
|
|
|
mime_type: 'image/jpeg',
|
|
|
|
|
file_path: 'photos/file_0.jpg',
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-05-18 07:06:21 +07:00
|
|
|
};
|
|
|
|
|
}
|
2026-05-18 07:29:01 +07:00
|
|
|
},
|
2026-05-18 07:06:21 +07:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
const infoSpy = spyOn(logger, 'info');
|
|
|
|
|
const errorSpy = spyOn(logger, 'error');
|
2026-05-18 07:06:21 +07:00
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('Telegram API Utilities', () => {
|
2026-05-21 22:31:55 +07:00
|
|
|
let forwardToStorage: typeof import('../src/utils/telegram').forwardToStorage;
|
|
|
|
|
let getFileInfo: typeof import('../src/utils/telegram').getFileInfo;
|
|
|
|
|
let getBot: typeof import('../src/utils/telegram').getBot;
|
2026-05-18 07:06:21 +07:00
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
infoSpy.mockClear();
|
|
|
|
|
errorSpy.mockClear();
|
|
|
|
|
global.fetch = mock(() => Promise.resolve(new Response(JSON.stringify({ ok: true }))));
|
|
|
|
|
|
|
|
|
|
// Import dynamically so mocking is applied first
|
2026-05-18 07:29:01 +07:00
|
|
|
const telegramUtils = await import('../src/utils/telegram');
|
2026-05-18 07:06:21 +07:00
|
|
|
forwardToStorage = telegramUtils.forwardToStorage;
|
|
|
|
|
getFileInfo = telegramUtils.getFileInfo;
|
|
|
|
|
getBot = telegramUtils.getBot;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
delete global.fetch;
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('getBot', () => {
|
|
|
|
|
it('should return the telegraf bot instance', () => {
|
2026-05-18 07:06:21 +07:00
|
|
|
const bot = getBot();
|
|
|
|
|
expect(bot).toBeDefined();
|
2026-05-18 07:27:06 +07:00
|
|
|
expect(bot.telegram).toBeDefined();
|
2026-05-18 07:06:21 +07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('forwardToStorage', () => {
|
|
|
|
|
it('should forward photo to storage chat and return file details', async () => {
|
2026-05-18 21:54:39 +07:00
|
|
|
const chunk = realPhotoBuffer;
|
|
|
|
|
const fileName = 'test_photo.png';
|
2026-05-18 19:52:46 +07:00
|
|
|
const result = await forwardToStorage(chunk, fileName, 'photo');
|
2026-05-18 07:06:21 +07:00
|
|
|
|
|
|
|
|
expect(result).toEqual({
|
2026-05-18 07:29:01 +07:00
|
|
|
telegramFileId: 'photo_id_high',
|
|
|
|
|
telegramFileUniqueId: 'unique_id_high',
|
|
|
|
|
storageMessageId: 12345,
|
2026-05-18 07:06:21 +07:00
|
|
|
});
|
2026-05-18 07:29:01 +07:00
|
|
|
expect(infoSpy).toHaveBeenCalledWith('File forwarded to storage', {
|
2026-05-18 07:06:21 +07:00
|
|
|
fileName,
|
2026-05-18 07:29:01 +07:00
|
|
|
message: 12345,
|
2026-05-18 07:06:21 +07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 08:18:55 +07:00
|
|
|
it('should forward documents with source and filename payload', async () => {
|
|
|
|
|
const bot = getBot();
|
|
|
|
|
const chunk = Buffer.from('fake document data');
|
|
|
|
|
const fileName = 'document.pdf';
|
|
|
|
|
|
2026-05-18 19:52:46 +07:00
|
|
|
const result = await forwardToStorage(chunk, fileName, 'document');
|
2026-05-18 08:18:55 +07:00
|
|
|
|
|
|
|
|
expect(bot.telegram.sendDocument).toHaveBeenCalledWith(
|
2026-05-18 08:54:24 +07:00
|
|
|
config.storageChatId,
|
2026-05-18 08:18:55 +07:00
|
|
|
{ source: chunk, filename: fileName },
|
|
|
|
|
{ caption: `📁 ${fileName}` },
|
|
|
|
|
);
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
telegramFileId: 'document_id',
|
|
|
|
|
telegramFileUniqueId: 'document_unique_id',
|
|
|
|
|
storageMessageId: 54321,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
it('should handle error when forwarding fails', async () => {
|
2026-05-18 07:06:21 +07:00
|
|
|
const bot = getBot();
|
2026-05-18 07:29:01 +07:00
|
|
|
bot.telegram.sendPhoto = mock(() => Promise.reject(new Error('Telegram send failed')));
|
2026-05-18 07:06:21 +07:00
|
|
|
|
2026-05-18 21:54:39 +07:00
|
|
|
const chunk = realPhotoBuffer;
|
|
|
|
|
const fileName = 'test_photo.png';
|
2026-05-18 07:06:21 +07:00
|
|
|
|
2026-05-18 19:52:46 +07:00
|
|
|
await expect(forwardToStorage(chunk, fileName, 'photo')).rejects.toThrow(
|
2026-05-18 07:29:01 +07:00
|
|
|
'Telegram send failed',
|
|
|
|
|
);
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith('Failed to forward file to storage', {
|
2026-05-18 07:06:21 +07:00
|
|
|
fileName,
|
2026-05-18 07:29:01 +07:00
|
|
|
error: 'Telegram send failed',
|
2026-05-18 07:06:21 +07:00
|
|
|
});
|
|
|
|
|
});
|
2026-05-18 21:02:38 +07:00
|
|
|
|
|
|
|
|
it('should retry when telegram returns 429 Too Many Requests', async () => {
|
|
|
|
|
const bot = getBot();
|
|
|
|
|
let calls = 0;
|
|
|
|
|
bot.telegram.sendPhoto = mock(() => {
|
|
|
|
|
calls++;
|
|
|
|
|
if (calls === 1) {
|
|
|
|
|
return Promise.reject(new Error('429: Too Many Requests: retry after 1'));
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve({
|
|
|
|
|
message_id: 999,
|
|
|
|
|
photo: [{ file_id: 'retry_photo_id', file_unique_id: 'retry_unique_id' }],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 21:54:39 +07:00
|
|
|
const chunk = realPhotoBuffer;
|
|
|
|
|
const fileName = 'test_photo.png';
|
2026-05-18 21:02:38 +07:00
|
|
|
|
|
|
|
|
const startTime = Date.now();
|
|
|
|
|
const result = await forwardToStorage(chunk, fileName, 'photo');
|
|
|
|
|
const duration = Date.now() - startTime;
|
|
|
|
|
|
|
|
|
|
expect(calls).toBe(2);
|
|
|
|
|
expect(duration).toBeGreaterThanOrEqual(1000);
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
telegramFileId: 'retry_photo_id',
|
|
|
|
|
telegramFileUniqueId: 'retry_unique_id',
|
|
|
|
|
storageMessageId: 999,
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-05-18 07:06:21 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('getFileInfo', () => {
|
|
|
|
|
it('should fetch file details successfully', async () => {
|
2026-05-21 22:55:42 +07:00
|
|
|
const result = await getFileInfo('some_file_id');
|
2026-05-18 07:06:21 +07:00
|
|
|
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
file_size: 98765,
|
2026-05-18 07:29:01 +07:00
|
|
|
mime_type: 'image/jpeg',
|
|
|
|
|
file_path: 'photos/file_0.jpg',
|
2026-05-18 07:06:21 +07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|