@ -3,6 +3,7 @@ import { Database } from 'bun:sqlite';
import { initializeDatabase } from '../../../src/db' ;
import { CommandService } from '../../../src/services/command' ;
import { TaskService } from '../../../src/tasks/service' ;
import { GroupSyncService } from '../../../src/services/group-sync' ;
let memDb : Database ;
const testContextBase = {
@ -18,6 +19,132 @@ beforeEach(() => {
( TaskService as any ) . dbInstance = memDb ;
} ) ;
test ( 'listar grupo por defecto con /t ver en grupo e incluir “… y X más”' , async ( ) = > {
// Insert group and cache it as active
memDb . exec ( `
INSERT OR IGNORE INTO groups ( id , community_id , name , active )
VALUES ( 'test-group@g.us' , 'test-community' , 'Test Group' , 1 )
` );
GroupSyncService . activeGroupsCache . clear ( ) ;
GroupSyncService . activeGroupsCache . set ( 'test-group@g.us' , 'Test Group' ) ;
// Crear 12 tareas sin asignados en el grupo
for ( let i = 1 ; i <= 12 ; i ++ ) {
TaskService . createTask ( {
description : ` Task ${ i } ` ,
due_date : '2025-12-31' ,
group_id : 'test-group@g.us' ,
created_by : '1234567890' ,
} ) ;
}
const responses = await CommandService . handle ( {
sender : '1234567890' ,
groupId : 'test-group@g.us' ,
mentions : [ ] ,
message : '/t ver'
} ) ;
expect ( responses . length ) . toBe ( 1 ) ;
expect ( responses [ 0 ] . recipient ) . toBe ( '1234567890' ) ;
expect ( responses [ 0 ] . message ) . toContain ( 'Test Group' ) ;
// Debe indicar que hay 2 más (límite 10)
expect ( responses [ 0 ] . message ) . toContain ( '… y 2 más' ) ;
// Debe mostrar “sin dueño”
expect ( responses [ 0 ] . message ) . toContain ( 'sin dueño' ) ;
} ) ;
test ( 'listar “mis” por defecto en DM con /t ver' , async ( ) = > {
// Insert groups and cache them
memDb . exec ( `
INSERT OR REPLACE INTO groups ( id , community_id , name , active ) VALUES
( 'test-group@g.us' , 'test-community' , 'Test Group' , 1 ) ,
( 'group-2@g.us' , 'test-community' , 'Group 2' , 1 )
` );
GroupSyncService . activeGroupsCache . clear ( ) ;
GroupSyncService . activeGroupsCache . set ( 'test-group@g.us' , 'Test Group' ) ;
GroupSyncService . activeGroupsCache . set ( 'group-2@g.us' , 'Group 2' ) ;
// Crear 2 tareas asignadas al usuario en distintos grupos
const t1 = TaskService . createTask ( {
description : 'G1 Task' ,
due_date : '2025-11-20' ,
group_id : 'test-group@g.us' ,
created_by : '1111111111' ,
} , [ { user_id : '1234567890' , assigned_by : '1111111111' } ] ) ;
const t2 = TaskService . createTask ( {
description : 'G2 Task' ,
due_date : '2025-11-25' ,
group_id : 'group-2@g.us' ,
created_by : '2222222222' ,
} , [ { user_id : '1234567890' , assigned_by : '2222222222' } ] ) ;
const responses = await CommandService . handle ( {
sender : '1234567890' ,
// Contexto de DM: usar un JID que NO sea de grupo
groupId : '1234567890@s.whatsapp.net' ,
mentions : [ ] ,
message : '/t ver'
} ) ;
expect ( responses . length ) . toBe ( 1 ) ;
expect ( responses [ 0 ] . recipient ) . toBe ( '1234567890' ) ;
const msg = responses [ 0 ] . message ;
expect ( msg ) . toContain ( 'Test Group' ) ;
expect ( msg ) . toContain ( 'Group 2' ) ;
expect ( msg ) . toMatch ( /- \d+\) “\*G1 Task\*”/ ) ;
expect ( msg ) . toMatch ( /- \d+\) “\*G2 Task\*”/ ) ;
} ) ;
test ( 'completar tarea: camino feliz, ya completada y no encontrada' , async ( ) = > {
// Insertar grupo y cache
memDb . exec ( `
INSERT OR IGNORE INTO groups ( id , community_id , name , active )
VALUES ( 'test-group@g.us' , 'test-community' , 'Test Group' , 1 )
` );
GroupSyncService . activeGroupsCache . clear ( ) ;
GroupSyncService . activeGroupsCache . set ( 'test-group@g.us' , 'Test Group' ) ;
const taskId = TaskService . createTask ( {
description : 'Completar yo' ,
due_date : '2025-10-10' ,
group_id : 'test-group@g.us' ,
created_by : '1111111111' ,
} ) ;
// 1) Camino feliz
let responses = await CommandService . handle ( {
sender : '1234567890' ,
groupId : 'test-group@g.us' ,
mentions : [ ] ,
message : ` /t x ${ taskId } `
} ) ;
expect ( responses . length ) . toBe ( 1 ) ;
expect ( responses [ 0 ] . recipient ) . toBe ( '1234567890' ) ;
expect ( responses [ 0 ] . message ) . toContain ( ` ✔️ ${ taskId } completada ` ) ;
// 2) Ya completada
responses = await CommandService . handle ( {
sender : '1234567890' ,
groupId : 'test-group@g.us' ,
mentions : [ ] ,
message : ` /t x ${ taskId } `
} ) ;
expect ( responses . length ) . toBe ( 1 ) ;
expect ( responses [ 0 ] . message ) . toContain ( 'ya estaba completada' ) ;
// 3) No encontrada
responses = await CommandService . handle ( {
sender : '1234567890' ,
groupId : 'test-group@g.us' ,
mentions : [ ] ,
message : ` /t x 999999 `
} ) ;
expect ( responses . length ) . toBe ( 1 ) ;
expect ( responses [ 0 ] . message ) . toContain ( 'no encontrada' ) ;
} ) ;
afterEach ( ( ) = > {
try { memDb . close ( ) ; } catch { }
} ) ;