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>;
|
|
|
|
|
|
|
|
|
|
type MockFileRecord = Record<string, unknown>;
|
|
|
|
|
|
|
|
|
|
type MockSelectChain = {
|
|
|
|
|
from: () => {
|
|
|
|
|
where: () => {
|
|
|
|
|
limit: () => Promise<MockFileRecord[]>;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Mock database layer
|
|
|
|
|
const emptySelectChain = (): MockSelectChain => ({
|
|
|
|
|
from: () => ({
|
|
|
|
|
where: () => ({
|
|
|
|
|
limit: () => Promise.resolve([]),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mockSelect = mock(() => emptySelectChain());
|
|
|
|
|
|
|
|
|
|
mock.module('../src/db/files', () => ({
|
|
|
|
|
findFileByPublicId: async () => {
|
|
|
|
|
const chain = mockSelect();
|
|
|
|
|
return (await chain.from().where().limit())[0] || null;
|
2026-05-18 07:29:01 +07:00
|
|
|
},
|
2026-05-18 07:09:29 +07:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Mock telegram utils
|
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-06 01:25:18 +07:00
|
|
|
mock.module('../src/utils/telegram', () => ({
|
2026-07-07 08:08:22 +07:00
|
|
|
forwardToStorage: async () => ({
|
|
|
|
|
telegramFileId: 'mock-tg-id',
|
|
|
|
|
telegramFileUniqueId: 'mock-tg-unique',
|
|
|
|
|
storageMessageId: 12345,
|
|
|
|
|
}),
|
2026-07-06 01:25:18 +07:00
|
|
|
getFileInfo: mockGetFileInfo,
|
|
|
|
|
}));
|
2026-05-18 07:09:29 +07:00
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('File Route Handlers', () => {
|
2026-05-21 22:31:55 +07:00
|
|
|
let handleFileRedirect: typeof import('../src/routes/files').handleFileRedirect;
|
|
|
|
|
let handleFileInfo: typeof import('../src/routes/files').handleFileInfo;
|
2026-05-18 07:09:29 +07:00
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
mockSelect.mockClear();
|
2026-07-06 01:25:18 +07:00
|
|
|
mockGetFileInfo.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-05-18 07:29:01 +07:00
|
|
|
const filesRoute = await import('../src/routes/files');
|
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-05-18 07:09:29 +07:00
|
|
|
mockSelect.mockImplementationOnce(() => ({
|
|
|
|
|
from: () => ({
|
|
|
|
|
where: () => ({
|
2026-05-18 07:29:01 +07:00
|
|
|
limit: () => Promise.resolve([]),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
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-05-18 07:09:29 +07:00
|
|
|
mockSelect.mockImplementationOnce(() => ({
|
|
|
|
|
from: () => ({
|
|
|
|
|
where: () => ({
|
2026-05-18 07:29:01 +07:00
|
|
|
limit: () =>
|
|
|
|
|
Promise.resolve([
|
|
|
|
|
{
|
|
|
|
|
id: 'uuid-123',
|
|
|
|
|
publicId: 'test-id',
|
|
|
|
|
telegramFileId: 'tg-file-id',
|
|
|
|
|
fileName: 'test.jpg',
|
2026-05-29 03:33:39 +07:00
|
|
|
mimeType: 'image/jpeg',
|
|
|
|
|
sizeBytes: 100,
|
2026-05-18 07:29:01 +07:00
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
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-05-18 07:09:29 +07:00
|
|
|
mockSelect.mockImplementationOnce(() => {
|
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-05-18 07:09:29 +07:00
|
|
|
mockSelect.mockImplementationOnce(() => ({
|
|
|
|
|
from: () => ({
|
|
|
|
|
where: () => ({
|
2026-05-18 07:29:01 +07:00
|
|
|
limit: () => Promise.resolve([]),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
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',
|
|
|
|
|
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-05-18 07:29:01 +07:00
|
|
|
createdAt: new Date('2026-05-18T00:00:00.000Z'),
|
2026-05-18 07:09:29 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockSelect.mockImplementationOnce(() => ({
|
|
|
|
|
from: () => ({
|
|
|
|
|
where: () => ({
|
2026-05-18 07:29:01 +07:00
|
|
|
limit: () => Promise.resolve([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-05-18 07:09:29 +07:00
|
|
|
mockSelect.mockImplementationOnce(() => {
|
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
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|