diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index b6947a7..f2a3d34 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -1,5 +1,10 @@ -import { describe, test, expect, beforeEach, afterEach } from 'bun:test'; +import { describe, test, expect, beforeEach, afterEach, mock } from 'bun:test'; import { WebhookServer } from '../../src/server'; +import { ResponseQueue } from '../../src/services/response-queue'; + +// Mock the ResponseQueue +const mockAdd = mock(() => Promise.resolve()); +ResponseQueue.add = mockAdd; describe('WebhookServer', () => { const envBackup = process.env; @@ -78,5 +83,142 @@ describe('WebhookServer', () => { const request = createTestRequest(payload); const response = await WebhookServer.handleRequest(request); expect(response.status).toBe(200); + expect(mockAdd).toHaveBeenCalled(); + }); + + test('should ignore empty message content', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { conversation: '' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).not.toHaveBeenCalled(); + }); + + test('should handle very long messages', async () => { + const longMessage = '/tarea nueva ' + 'A'.repeat(5000); + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { conversation: longMessage } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).toHaveBeenCalled(); + }); + + test('should handle messages with special characters and emojis', async () => { + const specialMessage = '/tarea nueva Test 😊 你好 @#$%^&*()'; + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { conversation: specialMessage } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).toHaveBeenCalled(); + }); + + test('should ignore non-/tarea commands', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { conversation: '/othercommand test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).not.toHaveBeenCalled(); + }); + + test('should ignore message with mentions but no command', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { + conversation: 'Hello everyone!', + contextInfo: { + mentionedJid: ['1234567890@s.whatsapp.net'] + } + } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).not.toHaveBeenCalled(); + }); + + test('should ignore media attachment messages', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { + imageMessage: { caption: 'This is an image' } + } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).not.toHaveBeenCalled(); + }); + + test('should handle XSS/SQL injection attempts', async () => { + const maliciousMessage = `/tarea nueva '; DROP TABLE tasks; --`; + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { conversation: maliciousMessage } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).toHaveBeenCalled(); }); });