From 74b0b0d1258c5ceb6ee916742b16fb4ad49537de Mon Sep 17 00:00:00 2001 From: borja Date: Fri, 5 Sep 2025 13:35:50 +0200 Subject: [PATCH] =?UTF-8?q?test:=20agregar=20tests=20de=20integraci=C3=B3n?= =?UTF-8?q?=20para=20validaci=C3=B3n=20de=20usuarios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) --- tests/unit/server.test.ts | 131 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index 5924999..4103901 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -619,5 +619,136 @@ describe('WebhookServer', () => { // Reinitialize database for subsequent tests initializeDatabase(testDb); }); + + test('should integrate user validation completely in handleMessageUpsert with valid user', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: '1234567890@s.whatsapp.net' + }, + message: { conversation: '/tarea nueva Test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).toHaveBeenCalled(); + + // Verify user was created/updated in database + const user = testDb.query("SELECT * FROM users WHERE id = ?").get('1234567890'); + expect(user).toBeDefined(); + expect(user.id).toBe('1234567890'); + expect(user.first_seen).toBeDefined(); + expect(user.last_seen).toBeDefined(); + }); + + test('should use normalized ID in command service', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: '1234567890:12@s.whatsapp.net' // Raw ID with participant + }, + message: { conversation: '/tarea nueva Test' } + } + }; + + // Mock CommandService.handle to capture the sender parameter + const mockCommandHandle = mock(async () => []); + const originalCommandService = await import('../../src/services/command'); + originalCommandService.CommandService.handle = mockCommandHandle; + + const request = createTestRequest(payload); + await WebhookServer.handleRequest(request); + + // Verify CommandService.handle was called with normalized sender ID + expect(mockCommandHandle).toHaveBeenCalledWith({ + sender: '1234567890', // Normalized ID + groupId: 'group-id@g.us', + message: '/tarea nueva Test', + mentions: [] + }); + + // Restore original + originalCommandService.CommandService.handle = originalCommandService.CommandService.handle; + }); + + test('should handle errors in user validation during integration', async () => { + // Mock ensureUserExists to return null (simulating error) + const originalDb = await import('../../src/db'); + const originalEnsureUserExists = originalDb.ensureUserExists; + originalDb.ensureUserExists = mock(() => null); + + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: '1234567890@s.whatsapp.net' + }, + message: { conversation: '/tarea nueva Test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).not.toHaveBeenCalled(); + + // Restore original + originalDb.ensureUserExists = originalEnsureUserExists; + }); + + test('should handle end-to-end flow with valid user and command processing', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: '1234567890@s.whatsapp.net' + }, + message: { conversation: '/tarea nueva Test task' } + } + }; + + // Mock CommandService.handle to return a response + const mockCommandHandle = mock(async () => [{ + recipient: '1234567890', + message: 'Task created successfully' + }]); + const originalCommandService = await import('../../src/services/command'); + originalCommandService.CommandService.handle = mockCommandHandle; + + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + + // Verify user was created/updated + const user = testDb.query("SELECT * FROM users WHERE id = ?").get('1234567890'); + expect(user).toBeDefined(); + + // Verify CommandService was called with normalized ID + expect(mockCommandHandle).toHaveBeenCalledWith({ + sender: '1234567890', + groupId: 'group-id@g.us', + message: '/tarea nueva Test task', + mentions: [] + }); + + // Verify ResponseQueue.add was called with the response + expect(mockAdd).toHaveBeenCalledWith([{ + recipient: '1234567890', + message: 'Task created successfully' + }]); + + // Restore original + originalCommandService.CommandService.handle = originalCommandService.CommandService.handle; + }); }); });