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.
		
		
		
		
		
			
		
			
				
	
	
		
			93 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
| import { describe, test, expect, beforeAll, afterAll, beforeEach, afterEach } from 'bun:test';
 | |
| import { Database } from 'bun:sqlite';
 | |
| import { WebhookServer } from '../../../src/server';
 | |
| import { initializeDatabase } from '../../../src/db';
 | |
| import { ResponseQueue } from '../../../src/services/response-queue';
 | |
| import { GroupSyncService } from '../../../src/services/group-sync';
 | |
| 
 | |
| let testDb: Database;
 | |
| let originalAdd: any;
 | |
| 
 | |
| let simulatedQueue: any[] = [];
 | |
| const SimulatedResponseQueue = {
 | |
|   async add(responses: any[]) {
 | |
|     simulatedQueue.push(...responses);
 | |
|   },
 | |
|   clear() { simulatedQueue = []; },
 | |
|   get() { return simulatedQueue; }
 | |
| };
 | |
| 
 | |
| const createTestRequest = (payload: any) =>
 | |
|   new Request('http://localhost:3007', {
 | |
|     method: 'POST',
 | |
|     headers: { 'Content-Type': 'application/json' },
 | |
|     body: JSON.stringify(payload)
 | |
|   });
 | |
| 
 | |
| describe('WebhookServer - discovery guarda label del grupo si está en caché', () => {
 | |
|   const envBackup = process.env;
 | |
| 
 | |
|   beforeAll(() => {
 | |
|     testDb = new Database(':memory:');
 | |
|     initializeDatabase(testDb);
 | |
|     originalAdd = (ResponseQueue as any).add;
 | |
|   });
 | |
| 
 | |
|   afterAll(() => {
 | |
|     (ResponseQueue as any).add = originalAdd;
 | |
|     testDb.close();
 | |
|   });
 | |
| 
 | |
|   beforeEach(() => {
 | |
|     process.env = {
 | |
|       ...envBackup,
 | |
|       NODE_ENV: 'test',
 | |
|       GROUP_GATING_MODE: 'discover'
 | |
|     };
 | |
|     SimulatedResponseQueue.clear();
 | |
|     (ResponseQueue as any).add = SimulatedResponseQueue.add;
 | |
|     WebhookServer.dbInstance = testDb;
 | |
| 
 | |
|     // Limpiar tablas relevantes
 | |
|     testDb.exec('DELETE FROM response_queue');
 | |
|     testDb.exec('DELETE FROM allowed_groups');
 | |
|     testDb.exec('DELETE FROM users');
 | |
| 
 | |
|     // Poblar caché con el nombre del grupo
 | |
|     GroupSyncService.activeGroupsCache.clear();
 | |
|     GroupSyncService.activeGroupsCache.set('label-group@g.us', 'Proyecto Foo');
 | |
|   });
 | |
| 
 | |
|   afterEach(() => {
 | |
|     process.env = envBackup;
 | |
|   });
 | |
| 
 | |
|   test('registra pending con label del grupo desde la caché', async () => {
 | |
|     const payload = {
 | |
|       event: 'messages.upsert',
 | |
|       instance: 'test-instance',
 | |
|       data: {
 | |
|         key: {
 | |
|           remoteJid: 'label-group@g.us',
 | |
|           participant: '9999999999@s.whatsapp.net'
 | |
|         },
 | |
|         message: { conversation: '/t n hola' }
 | |
|       }
 | |
|     };
 | |
| 
 | |
|     const res = await WebhookServer.handleRequest(createTestRequest(payload));
 | |
|     expect(res.status).toBe(200);
 | |
| 
 | |
|     // No debe haber respuestas encoladas (retorno temprano)
 | |
|     expect(SimulatedResponseQueue.get().length).toBe(0);
 | |
| 
 | |
|     // Debe existir registro pending con label en allowed_groups
 | |
|     const row = testDb
 | |
|       .query(`SELECT status, label FROM allowed_groups WHERE group_id = 'label-group@g.us'`)
 | |
|       .get() as any;
 | |
|     expect(row).toBeDefined();
 | |
|     expect(String(row.status)).toBe('pending');
 | |
|     expect(String(row.label)).toBe('Proyecto Foo');
 | |
|   });
 | |
| });
 |