style: format and lint codebase using Biome v2

This commit is contained in:
MythEclipse
2026-05-18 07:29:01 +07:00
parent 3f4e697733
commit cf79a1f195
22 changed files with 638 additions and 528 deletions
+60 -56
View File
@@ -1,120 +1,124 @@
// @ts-nocheck
import { describe, it, expect, mock, beforeEach, afterAll } from "bun:test";
import { afterAll, beforeEach, describe, expect, it, mock } from 'bun:test';
// Mock db
const mockInsert = mock(() => ({
values: mock(() => Promise.resolve())
values: mock(() => Promise.resolve()),
}));
mock.module("../src/db/index", () => ({
mock.module('../src/db/index', () => ({
db: {
insert: mockInsert
insert: mockInsert,
},
files: {}
files: {},
}));
// Mock nanoid
mock.module("nanoid", () => ({
nanoid: () => "mocked-nanoid-id"
mock.module('nanoid', () => ({
nanoid: () => 'mocked-nanoid-id',
}));
// Mock telegram utils
mock.module("../src/utils/telegram", () => ({
forwardToStorage: mock(() => Promise.resolve({
telegramFileId: "tg-file-id-123",
telegramFileUniqueId: "tg-unique-id-abc",
storageMessageId: 98765
})),
mock.module('../src/utils/telegram', () => ({
forwardToStorage: mock(() =>
Promise.resolve({
telegramFileId: 'tg-file-id-123',
telegramFileUniqueId: 'tg-unique-id-abc',
storageMessageId: 98765,
}),
),
getBot: () => ({
telegram: {
getFile: mock(() => Promise.resolve({
file_id: "tg-file-id-123",
file_size: 1000,
mime_type: "image/jpeg"
}))
}
})
getFile: mock(() =>
Promise.resolve({
file_id: 'tg-file-id-123',
file_size: 1000,
mime_type: 'image/jpeg',
}),
),
},
}),
}));
describe("Upload Route Handler", () => {
let handleUpload;
describe('Upload Route Handler', () => {
let handleUpload: any;
beforeEach(async () => {
mockInsert.mockClear();
const uploadRoute = await import("../src/routes/upload");
const uploadRoute = await import('../src/routes/upload');
handleUpload = uploadRoute.handleUpload;
});
it("should reject unsupported content types with 400 status", async () => {
const req = new Request("http://localhost:3000/api/upload", {
method: "POST",
it('should reject unsupported content types with 400 status', async () => {
const req = new Request('http://localhost:3000/api/upload', {
method: 'POST',
headers: {
"content-type": "text/plain"
'content-type': 'text/plain',
},
body: "plain text data"
body: 'plain text data',
});
const res = await handleUpload(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("Unsupported content type");
expect(body.error).toContain('Unsupported content type');
});
it("should process JSON upload (base64) successfully", async () => {
const req = new Request("http://localhost:3000/api/upload", {
method: "POST",
it('should process JSON upload (base64) successfully', async () => {
const req = new Request('http://localhost:3000/api/upload', {
method: 'POST',
headers: {
"content-type": "application/json"
'content-type': 'application/json',
},
body: JSON.stringify({
file: Buffer.from("hello world").toString("base64"),
fileName: "test.txt"
})
file: Buffer.from('hello world').toString('base64'),
fileName: 'test.txt',
}),
});
const res = await handleUpload(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.public_id).toBe("mocked-nanoid-id");
expect(body.telegram_file_id).toBe("tg-file-id-123");
expect(body.telegram_file_unique_id).toBe("tg-unique-id-abc");
expect(body.file_name).toBe("test.txt");
expect(body.file_type).toBe("document");
expect(body.public_id).toBe('mocked-nanoid-id');
expect(body.telegram_file_id).toBe('tg-file-id-123');
expect(body.telegram_file_unique_id).toBe('tg-unique-id-abc');
expect(body.file_name).toBe('test.txt');
expect(body.file_type).toBe('document');
});
it("should reject JSON upload without file key", async () => {
const req = new Request("http://localhost:3000/api/upload", {
method: "POST",
it('should reject JSON upload without file key', async () => {
const req = new Request('http://localhost:3000/api/upload', {
method: 'POST',
headers: {
"content-type": "application/json"
'content-type': 'application/json',
},
body: JSON.stringify({
fileName: "test.txt"
})
fileName: 'test.txt',
}),
});
const res = await handleUpload(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("Invalid JSON");
expect(body.error).toContain('Invalid JSON');
});
it("should process multipart upload successfully", async () => {
it('should process multipart upload successfully', async () => {
const formData = new FormData();
const fileBlob = new Blob([Buffer.from("multipart hello")], { type: "text/plain" });
formData.append("file", fileBlob, "test_multi.txt");
const fileBlob = new Blob([Buffer.from('multipart hello')], { type: 'text/plain' });
formData.append('file', fileBlob, 'test_multi.txt');
const req = new Request("http://localhost:3000/api/upload", {
method: "POST",
body: formData
const req = new Request('http://localhost:3000/api/upload', {
method: 'POST',
body: formData,
});
const res = await handleUpload(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.public_id).toBe("mocked-nanoid-id");
expect(body.file_name).toBe("test_multi.txt");
expect(body.public_id).toBe('mocked-nanoid-id');
expect(body.file_name).toBe('test_multi.txt');
});
afterAll(() => {