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.
		
		
		
		
		
			
		
			
				
	
	
		
			68 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.1 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('acepta alias /t y responde con formato compacto', async () => {
 | |
|     const responses = await CommandService.handle({
 | |
|       ...testContextBase,
 | |
|       message: '/t n Test task'
 | |
|     });
 | |
| 
 | |
|     expect(responses.length).toBe(1);
 | |
|     expect(responses[0].recipient).toBe('1234567890');
 | |
|     // Debe empezar con "✅ <id> "
 | |
|     expect(responses[0].message).toMatch(/^✅ \d+ /);
 | |
|     // Debe contener la descripción en negrita compacta
 | |
|     expect(responses[0].message).toContain('*Test task*');
 | |
|     // No debe usar el texto antiguo "Tarea <id> creada"
 | |
|     expect(responses[0].message).not.toMatch(/Tarea \d+ creada/);
 | |
|   });
 | |
| 
 | |
|   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;
 | |
|   });
 | |
| });
 |