import { describe, test, expect, beforeEach, afterEach } from 'bun:test'; import { Database } from 'bun:sqlite'; import { initializeDatabase } from '../../../src/db'; import { CommandService } from '../../../src/services/command'; import { TaskService } from '../../../src/tasks/service'; let memDb: Database; const testContextBase = { sender: '1234567890', groupId: 'test-group@g.us', mentions: [] as string[], }; beforeEach(() => { memDb = new Database(':memory:'); initializeDatabase(memDb); (CommandService as any).dbInstance = memDb; (TaskService as any).dbInstance = memDb; }); afterEach(() => { try { memDb.close(); } catch {} }); describe('CommandService', () => { test('should ignore non-tarea commands', async () => { const responses = await CommandService.handle({ ...testContextBase, message: '/othercommand' }); expect(responses).toEqual([]); }); test('should handle tarea nueva: crea y responde con id y descripción', async () => { const responses = await CommandService.handle({ ...testContextBase, message: '/tarea nueva Test task' }); expect(responses.length).toBe(1); expect(responses[0].recipient).toBe('1234567890'); expect(responses[0].message).toMatch(/^✅ Tarea \d+ creada: "Test task"/); }); test('should return error response on failure', async () => { // Forzar error temporalmente const original = TaskService.createTask; (TaskService as any).createTask = () => { throw new Error('forced'); }; const responses = await CommandService.handle({ ...testContextBase, message: '/tarea nueva Test task' }); expect(responses.length).toBe(1); expect(responses[0].recipient).toBe('1234567890'); expect(responses[0].message).toBe('Error processing command'); // Restaurar (TaskService as any).createTask = original; }); });