You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
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;
|
|
});
|
|
});
|