From 11702142d582329872a85a8c759af886f4c43268 Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Thu, 27 Mar 2025 23:42:49 +0100 Subject: [PATCH] feat: enhance /tarea command logging with detailed parsing --- src/server.ts | 23 ++++++++++++++ tests/unit/server.test.ts | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/server.ts b/src/server.ts index 599bad6..2a79e39 100644 --- a/src/server.ts +++ b/src/server.ts @@ -104,6 +104,29 @@ export class WebhookServer { // 2. Starts with /tarea command const messageText = data.message.conversation; if (typeof messageText === 'string' && messageText.trim().startsWith('/tarea')) { + // Parse command components + const commandParts = messageText.trim().split(/\s+/); + const command = commandParts[0].toLowerCase(); + const args = commandParts.slice(1).join(' '); + + // Extract due date if present (format: /tarea [due:YYYY-MM-DD] description) + let dueDate = ''; + const dueDateMatch = args.match(/due:(\d{4}-\d{2}-\d{2})/i); + if (dueDateMatch) { + dueDate = dueDateMatch[1]; + } + + console.log('📝 /tarea command received:', { + timestamp: new Date().toISOString(), + sender: data.key.participant, + group: data.key.remoteJid, + command: command, + arguments: args, + dueDate: dueDate || 'none', + mentions: data.contextInfo?.mentionedJid || [], + fullMessage: messageText + }); + const responses = await CommandService.handle({ sender: data.key.participant, groupId: data.key.remoteJid, diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index 5191767..1fd695d 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -223,6 +223,69 @@ describe('WebhookServer', () => { } }); + describe('/tarea command logging', () => { + let consoleSpy: any; + + beforeEach(() => { + consoleSpy = mock(() => {}); + console.log = consoleSpy; + }); + + afterEach(() => { + consoleSpy.mockRestore(); + }); + + test('should log basic /tarea command', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group123@g.us', + participant: 'user123@s.whatsapp.net' + }, + message: { conversation: '/tarea test' } + } + }; + + await WebhookServer.handleRequest(createTestRequest(payload)); + + expect(consoleSpy).toHaveBeenCalledWith( + expect.stringContaining('/tarea command received:'), + expect.objectContaining({ + command: '/tarea', + arguments: 'test', + dueDate: 'none', + fullMessage: '/tarea test' + }) + ); + }); + + test('should log command with due date', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group123@g.us', + participant: 'user123@s.whatsapp.net' + }, + message: { conversation: '/tarea due:2025-04-30 Finish project' } + } + }; + + await WebhookServer.handleRequest(createTestRequest(payload)); + + expect(consoleSpy).toHaveBeenCalledWith( + expect.stringContaining('/tarea command received:'), + expect.objectContaining({ + dueDate: '2025-04-30', + arguments: 'due:2025-04-30 Finish project' + }) + ); + }); + }); + test('should handle XSS/SQL injection attempts', async () => { const maliciousMessage = `/tarea nueva '; DROP TABLE tasks; --`; const payload = {