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.
		
		
		
		
		
			
		
			
				
	
	
		
			92 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			92 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
| import { describe, it, beforeEach, expect } from 'bun:test';
 | |
| import { Database } from 'bun:sqlite';
 | |
| import { initializeDatabase } from '../../../src/db';
 | |
| import { CommandService } from '../../../src/services/command';
 | |
| 
 | |
| describe('CommandService - configurar recordatorios', () => {
 | |
|   let memdb: Database;
 | |
|   const SENDER = '34600123456';
 | |
| 
 | |
|   beforeEach(() => {
 | |
|     process.env.NODE_ENV = 'test';
 | |
|     process.env.TZ = 'Europe/Madrid';
 | |
| 
 | |
|     memdb = new Database(':memory:');
 | |
|     initializeDatabase(memdb);
 | |
| 
 | |
|     // Inyectar DB
 | |
|     (CommandService as any).dbInstance = memdb;
 | |
| 
 | |
|     // Limpiar tablas
 | |
|     memdb.exec(`DELETE FROM response_queue;`);
 | |
|     memdb.exec(`DELETE FROM user_preferences;`);
 | |
|     memdb.exec(`DELETE FROM users;`);
 | |
|   });
 | |
| 
 | |
|   async function runCmd(msg: string) {
 | |
|     return await CommandService.handle({
 | |
|       sender: SENDER,
 | |
|       groupId: '123@g.us',
 | |
|       message: msg,
 | |
|       mentions: []
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   function getPref(): { freq: string; time: string | null } | null {
 | |
|     const row = memdb.prepare(`SELECT reminder_freq AS freq, reminder_time AS time FROM user_preferences WHERE user_id = ?`).get(SENDER) as any;
 | |
|     if (!row) return null;
 | |
|     return { freq: String(row.freq), time: row.time ? String(row.time) : null };
 | |
|   }
 | |
| 
 | |
|   it('configurar daily guarda preferencia y responde confirmación', async () => {
 | |
|     const res = await runCmd('/t configurar daily');
 | |
|     expect(res).toHaveLength(1);
 | |
|     expect(res[0].recipient).toBe(SENDER);
 | |
|     expect(res[0].message).toContain('✅ Recordatorios: diario');
 | |
| 
 | |
|     const pref = getPref();
 | |
|     expect(pref).not.toBeNull();
 | |
|     expect(pref!.freq).toBe('daily');
 | |
|     expect(pref!.time).toBe('08:30'); // default
 | |
|   });
 | |
| 
 | |
|   it('configurar weekly guarda preferencia y responde confirmación', async () => {
 | |
|     const res = await runCmd('/t configurar weekly');
 | |
|     expect(res).toHaveLength(1);
 | |
|     expect(res[0].message).toContain('semanal (lunes 08:30)');
 | |
| 
 | |
|     const pref = getPref();
 | |
|     expect(pref).not.toBeNull();
 | |
|     expect(pref!.freq).toBe('weekly');
 | |
|   });
 | |
| 
 | |
|   it('configurar off guarda preferencia y responde confirmación', async () => {
 | |
|     const res = await runCmd('/t configurar off');
 | |
|     expect(res).toHaveLength(1);
 | |
|     expect(res[0].message).toContain('apagado');
 | |
| 
 | |
|     const pref = getPref();
 | |
|     expect(pref).not.toBeNull();
 | |
|     expect(pref!.freq).toBe('off');
 | |
|   });
 | |
| 
 | |
|   it('configurar con opción inválida devuelve uso correcto y no escribe en DB', async () => {
 | |
|     const res = await runCmd('/t configurar foo');
 | |
|     expect(res).toHaveLength(1);
 | |
|     expect(res[0].message).toContain('Uso: /t configurar daily|weekly|off');
 | |
| 
 | |
|     const pref = getPref();
 | |
|     expect(pref).toBeNull();
 | |
|   });
 | |
| 
 | |
|   it('upsert idempotente: cambiar de daily a off actualiza la fila existente', async () => {
 | |
|     await runCmd('/t configurar daily');
 | |
|     let pref = getPref();
 | |
|     expect(pref!.freq).toBe('daily');
 | |
| 
 | |
|     await runCmd('/t configurar off');
 | |
|     pref = getPref();
 | |
|     expect(pref!.freq).toBe('off');
 | |
|   });
 | |
| });
 |