feat: centralizar /tarea en CommandService y usar DB
Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>pull/1/head
parent
db790064af
commit
a209c40ac3
@ -1,34 +1,62 @@
|
|||||||
import { describe, test, expect, beforeEach, mock } from 'bun:test';
|
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 { CommandService } from '../../../src/services/command';
|
||||||
|
import { TaskService } from '../../../src/tasks/service';
|
||||||
|
|
||||||
const testContext = {
|
let memDb: Database;
|
||||||
|
const testContextBase = {
|
||||||
sender: '1234567890',
|
sender: '1234567890',
|
||||||
groupId: 'test-group@g.us',
|
groupId: 'test-group@g.us',
|
||||||
message: '/tarea nueva Test task',
|
mentions: [] as string[],
|
||||||
mentions: []
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
memDb = new Database(':memory:');
|
||||||
|
initializeDatabase(memDb);
|
||||||
|
(CommandService as any).dbInstance = memDb;
|
||||||
|
(TaskService as any).dbInstance = memDb;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
try { memDb.close(); } catch {}
|
||||||
|
});
|
||||||
|
|
||||||
describe('CommandService', () => {
|
describe('CommandService', () => {
|
||||||
test('should ignore non-tarea commands', async () => {
|
test('should ignore non-tarea commands', async () => {
|
||||||
const responses = await CommandService.handle({
|
const responses = await CommandService.handle({
|
||||||
...testContext,
|
...testContextBase,
|
||||||
message: '/othercommand'
|
message: '/othercommand'
|
||||||
});
|
});
|
||||||
expect(responses).toEqual([]);
|
expect(responses).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle tarea commands', async () => {
|
test('should handle tarea nueva: crea y responde con id y descripción', async () => {
|
||||||
const responses = await CommandService.handle(testContext);
|
const responses = await CommandService.handle({
|
||||||
|
...testContextBase,
|
||||||
|
message: '/tarea nueva Test task'
|
||||||
|
});
|
||||||
|
|
||||||
expect(responses.length).toBe(1);
|
expect(responses.length).toBe(1);
|
||||||
expect(responses[0].recipient).toBe('1234567890');
|
expect(responses[0].recipient).toBe('1234567890');
|
||||||
expect(responses[0].message).toBe('Command received: /tarea nueva Test task');
|
expect(responses[0].message).toMatch(/^✅ Tarea \d+ creada: "Test task"/);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return error response on failure', async () => {
|
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({
|
const responses = await CommandService.handle({
|
||||||
...testContext,
|
...testContextBase,
|
||||||
message: '/tarea nueva Test task'
|
message: '/tarea nueva Test task'
|
||||||
});
|
});
|
||||||
expect(responses[0].message).toBe('Command received: /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;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue