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 -53
View File
@@ -1,11 +1,11 @@
// @ts-nocheck
import { describe, it, expect, mock, spyOn, beforeEach, afterAll } from "bun:test";
import logger from "../src/utils/logger";
import { afterAll, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import logger from '../src/utils/logger';
// Mock environment
process.env.BOT_TOKEN = "8605908810:AAFpUzlIBktfd_7wpEj7zMJob2CFxvG-ZGY";
process.env.STORAGE_CHANNEL_ID = "-1003996572954";
process.env.BASE_URL = "https://tele.asepharyana.tech";
process.env.BOT_TOKEN = '8605908810:AAFpUzlIBktfd_7wpEj7zMJob2CFxvG-ZGY';
process.env.STORAGE_CHANNEL_ID = '-1003996572954';
process.env.BASE_URL = 'https://tele.asepharyana.tech';
// Mock Telegraf
const mockLaunch = mock(() => Promise.resolve());
@@ -13,7 +13,7 @@ const mockCommand = mock();
const mockOn = mock();
const mockUse = mock();
mock.module("telegraf", () => {
mock.module('telegraf', () => {
return {
Telegraf: class {
constructor(token) {
@@ -23,35 +23,37 @@ mock.module("telegraf", () => {
this.on = mockOn;
this.use = mockUse;
}
}
},
};
});
// Mock database
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 forwardToStorage
const mockForwardToStorage = mock(() => Promise.resolve({
telegramFileId: "stored_file_id",
telegramFileUniqueId: "stored_unique_id",
storageMessageId: 9999
}));
mock.module("../src/utils/telegram", () => ({
forwardToStorage: mockForwardToStorage
const mockForwardToStorage = mock(() =>
Promise.resolve({
telegramFileId: 'stored_file_id',
telegramFileUniqueId: 'stored_unique_id',
storageMessageId: 9999,
}),
);
mock.module('../src/utils/telegram', () => ({
forwardToStorage: mockForwardToStorage,
}));
const infoSpy = spyOn(logger, "info");
const errorSpy = spyOn(logger, "error");
const infoSpy = spyOn(logger, 'info');
const errorSpy = spyOn(logger, 'error');
describe("Telegram Bot Handler", () => {
describe('Telegram Bot Handler', () => {
beforeEach(() => {
mockLaunch.mockClear();
mockCommand.mockClear();
@@ -63,36 +65,36 @@ describe("Telegram Bot Handler", () => {
errorSpy.mockClear();
});
it("should initialize and launch the bot", async () => {
const { startBot } = await import("../src/bot");
it('should initialize and launch the bot', async () => {
const { startBot } = await import('../src/bot');
const bot = await startBot();
expect(bot).toBeDefined();
expect(mockCommand).toHaveBeenCalledWith("start", expect.any(Function));
expect(mockCommand).toHaveBeenCalledWith('start', expect.any(Function));
expect(mockOn).toHaveBeenCalledWith(
["document", "photo", "video", "audio", "voice", "animation"],
expect.any(Function)
['document', 'photo', 'video', 'audio', 'voice', 'animation'],
expect.any(Function),
);
expect(mockUse).toHaveBeenCalled();
expect(mockLaunch).toHaveBeenCalled();
});
it("should handle /start command", async () => {
const { startBot } = await import("../src/bot");
it('should handle /start command', async () => {
const { startBot } = await import('../src/bot');
await startBot();
const startHandler = mockCommand.mock.calls.find(call => call[0] === "start")[1];
const startHandler = mockCommand.mock.calls.find((call) => call[0] === 'start')[1];
const replyMock = mock(() => Promise.resolve());
const ctx = {
reply: replyMock
reply: replyMock,
};
await startHandler(ctx);
expect(replyMock).toHaveBeenCalledWith(expect.stringContaining("Halo"));
expect(replyMock).toHaveBeenCalledWith(expect.stringContaining('Halo'));
});
it("should process document uploads and save to db", async () => {
const { startBot } = await import("../src/bot");
it('should process document uploads and save to db', async () => {
const { startBot } = await import('../src/bot');
await startBot();
const fileHandler = mockOn.mock.calls[0][1];
@@ -101,27 +103,30 @@ describe("Telegram Bot Handler", () => {
message: {
message_id: 42,
document: {
file_id: "doc_123",
file_unique_id: "doc_uniq_123",
file_id: 'doc_123',
file_unique_id: 'doc_uniq_123',
file_size: 1024,
mime_type: "application/pdf",
file_name: "cv.pdf"
}
mime_type: 'application/pdf',
file_name: 'cv.pdf',
},
},
from: {
id: 999
id: 999,
},
reply: replyMock
reply: replyMock,
};
await fileHandler(ctx);
expect(mockForwardToStorage).toHaveBeenCalledWith("doc_123", "cv.pdf");
expect(mockForwardToStorage).toHaveBeenCalledWith('doc_123', 'cv.pdf');
expect(mockInsert).toHaveBeenCalled();
expect(replyMock).toHaveBeenCalledWith(expect.stringContaining("File berhasil diupload"), expect.any(Object));
expect(replyMock).toHaveBeenCalledWith(
expect.stringContaining('File berhasil diupload'),
expect.any(Object),
);
});
it("should reject uploads exceeding max size limit", async () => {
const { startBot } = await import("../src/bot");
it('should reject uploads exceeding max size limit', async () => {
const { startBot } = await import('../src/bot');
await startBot();
const fileHandler = mockOn.mock.calls[0][1];
@@ -129,22 +134,24 @@ describe("Telegram Bot Handler", () => {
const ctx = {
message: {
message_id: 42,
photo: [{
file_id: "photo_123",
file_unique_id: "photo_uniq_123",
file_size: 20 * 1024 * 1024, // 20MB exceeds 10MB limit
mime_type: "image/jpeg"
}]
photo: [
{
file_id: 'photo_123',
file_unique_id: 'photo_uniq_123',
file_size: 20 * 1024 * 1024, // 20MB exceeds 10MB limit
mime_type: 'image/jpeg',
},
],
},
from: {
id: 999
id: 999,
},
reply: replyMock
reply: replyMock,
};
await fileHandler(ctx);
expect(mockForwardToStorage).not.toHaveBeenCalled();
expect(replyMock).toHaveBeenCalledWith(expect.stringContaining("exceeds"));
expect(replyMock).toHaveBeenCalledWith(expect.stringContaining('exceeds'));
});
afterAll(() => {