2026-05-18 07:29:01 +07:00
|
|
|
import { afterAll, beforeEach, describe, expect, it, mock } from 'bun:test';
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
type RequestWithParams = Request & {
|
|
|
|
|
params?: {
|
|
|
|
|
public_id?: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
type ErrorBody = {
|
|
|
|
|
error: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type FileInfoBody = {
|
|
|
|
|
public_id: string;
|
|
|
|
|
file_name: string;
|
|
|
|
|
mime_type: string;
|
|
|
|
|
size_bytes: number;
|
|
|
|
|
file_type: string;
|
|
|
|
|
created_at: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type JsonBody = ErrorBody | FileInfoBody | Record<string, unknown>;
|
|
|
|
|
|
2026-07-29 16:39:52 +07:00
|
|
|
type MockFileRecord = {
|
|
|
|
|
publicId: string;
|
|
|
|
|
fileName: string;
|
|
|
|
|
mimeType: string;
|
|
|
|
|
sizeBytes: number;
|
|
|
|
|
fileType: string;
|
|
|
|
|
uploaderId?: number;
|
|
|
|
|
createdAt?: Date;
|
|
|
|
|
telegramFileId?: string;
|
|
|
|
|
storageBackend?: string | null;
|
|
|
|
|
archiveEntryName?: string | null;
|
|
|
|
|
fileHash?: string | null;
|
|
|
|
|
archiveTelegramFileId?: string | null;
|
2026-05-21 22:31:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const requestWithPublicId = (url: string, publicId: string): RequestWithParams => {
|
|
|
|
|
const req = new Request(url) as RequestWithParams;
|
|
|
|
|
req.params = { public_id: publicId };
|
|
|
|
|
return req;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const responseJson = async <T extends JsonBody>(res: Response): Promise<T> => {
|
|
|
|
|
return (await res.json()) as T;
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-29 16:39:52 +07:00
|
|
|
// Mock the DI module — file-controller imports fileRepository + chunkedStorage from here
|
|
|
|
|
const mockFindByPublicId = mock(
|
|
|
|
|
(_publicId: string): Promise<MockFileRecord | null> => Promise.resolve(null),
|
|
|
|
|
);
|
2026-05-21 22:31:55 +07:00
|
|
|
|
2026-07-06 01:25:18 +07:00
|
|
|
const mockGetFileInfo = mock(async (_telegramFileId: string) => ({
|
|
|
|
|
file_size: 98765,
|
|
|
|
|
mime_type: 'image/jpeg',
|
|
|
|
|
file_path: 'photos/file_0.jpg',
|
|
|
|
|
bot_token: '123456:ABC-DEF',
|
2026-05-18 07:09:29 +07:00
|
|
|
}));
|
|
|
|
|
|
2026-07-29 16:39:52 +07:00
|
|
|
const mockCreateChunkedObjectResponse = mock(async () => new Response(null, { status: 200 }));
|
|
|
|
|
|
|
|
|
|
mock.module('../src/infrastructure/di', () => ({
|
|
|
|
|
fileRepository: {
|
|
|
|
|
findByPublicId: mockFindByPublicId,
|
|
|
|
|
findByHash: async () => null,
|
|
|
|
|
findByUniqueId: async () => null,
|
|
|
|
|
findByBucketAndKey: async () => null,
|
|
|
|
|
create: async (data: Record<string, unknown>) => ({
|
|
|
|
|
...data,
|
|
|
|
|
id: 'mock-id',
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
}),
|
|
|
|
|
softDelete: async () => true,
|
|
|
|
|
softDeleteBatch: async () => 1,
|
|
|
|
|
countByBucket: async () => 0,
|
|
|
|
|
listByPrefix: async () => ({ objects: [], prefixes: [] }),
|
|
|
|
|
findOrphansByBucket: async () => [],
|
|
|
|
|
},
|
|
|
|
|
chunkedStorage: {
|
|
|
|
|
createChunkedObjectResponse: mockCreateChunkedObjectResponse,
|
|
|
|
|
buildChunkedObjectSources: async () => [],
|
|
|
|
|
uploadFileInTelegramChunks: async () => ({ parts: [], fileHash: '', totalSizeBytes: 0 }),
|
|
|
|
|
storeFileInTelegramChunks: async () => ({
|
|
|
|
|
id: 'mock-id',
|
|
|
|
|
publicId: 'mock-public',
|
|
|
|
|
telegramFileId: 'mock-tg',
|
|
|
|
|
telegramFileUniqueId: 'mock-tg-unique',
|
|
|
|
|
storageChatId: 0,
|
|
|
|
|
storageMessageId: 0,
|
|
|
|
|
fileName: 'mock',
|
|
|
|
|
mimeType: 'application/octet-stream',
|
|
|
|
|
sizeBytes: 0,
|
|
|
|
|
fileType: 'document',
|
|
|
|
|
uploaderId: 0,
|
|
|
|
|
fileHash: null,
|
|
|
|
|
archiveTelegramFileId: null,
|
|
|
|
|
archiveStorageMessageId: null,
|
|
|
|
|
archiveFileName: null,
|
|
|
|
|
archiveEntryName: null,
|
|
|
|
|
archiveMimeType: null,
|
|
|
|
|
archiveSizeBytes: null,
|
|
|
|
|
bucketId: null,
|
|
|
|
|
s3Key: null,
|
|
|
|
|
storageBackend: 'telegram',
|
|
|
|
|
isDeleted: false,
|
|
|
|
|
multipartUploadId: null,
|
|
|
|
|
partCount: null,
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Mock botPool.getFileInfo used in file redirect
|
|
|
|
|
mock.module('../src/infrastructure/telegram/bot-pool', () => ({
|
|
|
|
|
botPool: {
|
|
|
|
|
getFileInfo: mockGetFileInfo,
|
|
|
|
|
forwardToStorage: async () => ({
|
|
|
|
|
telegramFileId: 'mock-tg-id',
|
|
|
|
|
telegramFileUniqueId: 'mock-tg-unique',
|
|
|
|
|
storageMessageId: 12345,
|
|
|
|
|
}),
|
|
|
|
|
size: 1,
|
|
|
|
|
getEffectiveConcurrency: () => 1,
|
|
|
|
|
},
|
2026-07-06 01:25:18 +07:00
|
|
|
}));
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('File Route Handlers', () => {
|
2026-07-29 16:39:52 +07:00
|
|
|
let handleFileRedirect: typeof import('../src/interfaces/http/controllers/file-controller').handleFileRedirect;
|
|
|
|
|
let handleFileInfo: typeof import('../src/interfaces/http/controllers/file-controller').handleFileInfo;
|
2026-05-18 07:09:29 +07:00
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2026-07-29 16:39:52 +07:00
|
|
|
mockFindByPublicId.mockClear();
|
2026-07-06 01:25:18 +07:00
|
|
|
mockGetFileInfo.mockClear();
|
2026-07-29 16:39:52 +07:00
|
|
|
mockCreateChunkedObjectResponse.mockClear();
|
2026-05-18 07:09:29 +07:00
|
|
|
|
|
|
|
|
// Set up mock token
|
2026-05-18 07:29:01 +07:00
|
|
|
process.env.BOT_TOKEN = '123456:ABC-DEF';
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-07-29 16:39:52 +07:00
|
|
|
const filesRoute = await import('../src/interfaces/http/controllers/file-controller');
|
2026-05-18 07:09:29 +07:00
|
|
|
handleFileRedirect = filesRoute.handleFileRedirect;
|
|
|
|
|
handleFileInfo = filesRoute.handleFileInfo;
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-29 03:33:39 +07:00
|
|
|
afterAll(() => {
|
|
|
|
|
mock.restore();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('handleFileRedirect', () => {
|
|
|
|
|
it('should return 404 if file is not found in database', async () => {
|
2026-07-29 16:39:52 +07:00
|
|
|
mockFindByPublicId.mockImplementationOnce(async () => null);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const req = requestWithPublicId('http://localhost:3000/f/missing-id', 'missing-id');
|
2026-05-18 07:27:06 +07:00
|
|
|
const res = await handleFileRedirect(req);
|
2026-05-18 07:09:29 +07:00
|
|
|
expect(res.status).toBe(404);
|
2026-05-21 22:31:55 +07:00
|
|
|
const body = await responseJson<ErrorBody>(res);
|
2026-05-18 07:29:01 +07:00
|
|
|
expect(body.error).toBe('File not found');
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-29 03:43:49 +07:00
|
|
|
it('should redirect to telegram file url with 302', async () => {
|
2026-07-29 16:39:52 +07:00
|
|
|
mockFindByPublicId.mockImplementationOnce(async () => ({
|
|
|
|
|
publicId: 'test-id',
|
|
|
|
|
telegramFileId: 'tg-file-id',
|
|
|
|
|
telegramFileUniqueId: 'tg-unique',
|
|
|
|
|
storageChatId: -100123,
|
|
|
|
|
storageMessageId: 42,
|
|
|
|
|
fileName: 'test.jpg',
|
|
|
|
|
mimeType: 'image/jpeg',
|
|
|
|
|
sizeBytes: 100,
|
|
|
|
|
fileType: 'photo',
|
|
|
|
|
uploaderId: 0,
|
|
|
|
|
fileHash: 'abc123',
|
|
|
|
|
archiveTelegramFileId: null,
|
|
|
|
|
archiveStorageMessageId: null,
|
|
|
|
|
archiveFileName: null,
|
|
|
|
|
archiveEntryName: null,
|
|
|
|
|
archiveMimeType: null,
|
|
|
|
|
archiveSizeBytes: null,
|
|
|
|
|
bucketId: null,
|
|
|
|
|
s3Key: null,
|
|
|
|
|
storageBackend: 'telegram',
|
|
|
|
|
isDeleted: false,
|
|
|
|
|
multipartUploadId: null,
|
|
|
|
|
partCount: null,
|
|
|
|
|
createdAt: new Date('2026-05-18T00:00:00.000Z'),
|
|
|
|
|
updatedAt: new Date('2026-05-18T00:00:00.000Z'),
|
2026-05-18 07:09:29 +07:00
|
|
|
}));
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const req = requestWithPublicId('http://localhost:3000/f/test-id', 'test-id');
|
2026-05-18 07:27:06 +07:00
|
|
|
const res = await handleFileRedirect(req);
|
2026-05-29 03:33:39 +07:00
|
|
|
|
2026-05-29 03:43:49 +07:00
|
|
|
expect(res.status).toBe(302);
|
|
|
|
|
expect(res.headers.get('Location')).toBe(
|
|
|
|
|
'https://api.telegram.org/file/bot123456:ABC-DEF/photos/file_0.jpg',
|
|
|
|
|
);
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
it('should return 500 on database or external errors', async () => {
|
2026-07-29 16:39:52 +07:00
|
|
|
mockFindByPublicId.mockImplementationOnce(async () => {
|
2026-05-18 07:29:01 +07:00
|
|
|
throw new Error('DB Connection Error');
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const req = requestWithPublicId('http://localhost:3000/f/test-id', 'test-id');
|
2026-05-18 07:27:06 +07:00
|
|
|
const res = await handleFileRedirect(req);
|
2026-05-18 07:09:29 +07:00
|
|
|
expect(res.status).toBe(500);
|
2026-05-21 22:31:55 +07:00
|
|
|
const body = await responseJson<ErrorBody>(res);
|
2026-05-18 07:29:01 +07:00
|
|
|
expect(body.error).toBe('Server error');
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('handleFileInfo', () => {
|
|
|
|
|
it('should return 404 if file is not found in database', async () => {
|
2026-07-29 16:39:52 +07:00
|
|
|
mockFindByPublicId.mockImplementationOnce(async () => null);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const req = requestWithPublicId('http://localhost:3000/file/missing-id/info', 'missing-id');
|
2026-05-18 07:27:06 +07:00
|
|
|
const res = await handleFileInfo(req);
|
2026-05-18 07:09:29 +07:00
|
|
|
expect(res.status).toBe(404);
|
2026-05-21 22:31:55 +07:00
|
|
|
const body = await responseJson<ErrorBody>(res);
|
2026-05-18 07:29:01 +07:00
|
|
|
expect(body.error).toBe('File not found');
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-29 03:33:39 +07:00
|
|
|
it('should return file info JSON without internal fields', async () => {
|
2026-05-18 07:09:29 +07:00
|
|
|
const dbFile = {
|
2026-05-18 07:29:01 +07:00
|
|
|
publicId: 'test-id',
|
2026-07-29 16:39:52 +07:00
|
|
|
telegramFileId: 'tg-file-id',
|
|
|
|
|
telegramFileUniqueId: 'tg-unique',
|
|
|
|
|
storageChatId: -100123,
|
|
|
|
|
storageMessageId: 42,
|
2026-05-18 07:29:01 +07:00
|
|
|
fileName: 'image.png',
|
|
|
|
|
mimeType: 'image/png',
|
2026-05-18 07:09:29 +07:00
|
|
|
sizeBytes: 2048,
|
2026-05-18 07:29:01 +07:00
|
|
|
fileType: 'photo',
|
2026-05-18 07:09:29 +07:00
|
|
|
uploaderId: 99999,
|
2026-07-29 16:39:52 +07:00
|
|
|
fileHash: null,
|
|
|
|
|
archiveTelegramFileId: null,
|
|
|
|
|
archiveStorageMessageId: null,
|
|
|
|
|
archiveFileName: null,
|
|
|
|
|
archiveEntryName: null,
|
|
|
|
|
archiveMimeType: null,
|
|
|
|
|
archiveSizeBytes: null,
|
|
|
|
|
bucketId: null,
|
|
|
|
|
s3Key: null,
|
|
|
|
|
storageBackend: 'telegram',
|
|
|
|
|
isDeleted: false,
|
|
|
|
|
multipartUploadId: null,
|
|
|
|
|
partCount: null,
|
2026-05-18 07:29:01 +07:00
|
|
|
createdAt: new Date('2026-05-18T00:00:00.000Z'),
|
2026-07-29 16:39:52 +07:00
|
|
|
updatedAt: new Date('2026-05-18T00:00:00.000Z'),
|
2026-05-18 07:09:29 +07:00
|
|
|
};
|
|
|
|
|
|
2026-07-29 16:39:52 +07:00
|
|
|
mockFindByPublicId.mockImplementationOnce(async () => dbFile);
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const req = requestWithPublicId('http://localhost:3000/file/test-id/info', 'test-id');
|
2026-05-18 07:27:06 +07:00
|
|
|
const res = await handleFileInfo(req);
|
2026-05-18 07:09:29 +07:00
|
|
|
expect(res.status).toBe(200);
|
2026-05-21 22:31:55 +07:00
|
|
|
const body = await responseJson<FileInfoBody>(res);
|
2026-05-18 07:09:29 +07:00
|
|
|
expect(body).toEqual({
|
2026-05-18 07:29:01 +07:00
|
|
|
public_id: 'test-id',
|
|
|
|
|
file_name: 'image.png',
|
|
|
|
|
mime_type: 'image/png',
|
2026-05-18 07:09:29 +07:00
|
|
|
size_bytes: 2048,
|
2026-05-18 07:29:01 +07:00
|
|
|
file_type: 'photo',
|
|
|
|
|
created_at: '2026-05-18T00:00:00.000Z',
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
2026-05-29 03:33:39 +07:00
|
|
|
// No internal fields
|
|
|
|
|
expect(body).not.toHaveProperty('uploader_id');
|
|
|
|
|
expect(body).not.toHaveProperty('telegram_file_id');
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
it('should return 500 on database or external errors', async () => {
|
2026-07-29 16:39:52 +07:00
|
|
|
mockFindByPublicId.mockImplementationOnce(async () => {
|
2026-05-18 07:29:01 +07:00
|
|
|
throw new Error('DB Connection Error');
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-21 22:31:55 +07:00
|
|
|
const req = requestWithPublicId('http://localhost:3000/file/test-id/info', 'test-id');
|
2026-05-18 07:27:06 +07:00
|
|
|
const res = await handleFileInfo(req);
|
2026-05-18 07:09:29 +07:00
|
|
|
expect(res.status).toBe(500);
|
2026-05-21 22:31:55 +07:00
|
|
|
const body = await responseJson<ErrorBody>(res);
|
2026-05-18 07:29:01 +07:00
|
|
|
expect(body.error).toBe('Server error');
|
2026-05-18 07:09:29 +07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|