From 63c3bb20e9f511a01aabe87e2181288727601f45 Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Wed, 26 Mar 2025 23:46:54 +0100 Subject: [PATCH] feat: implement command service with response queue --- src/server.ts | 6 ++++- src/services/command.ts | 38 +++++++++++++++++++++++++++++ src/services/response-queue.ts | 17 +++++++++++++ tests/unit/services/command.test.ts | 38 +++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/services/command.ts create mode 100644 src/services/response-queue.ts create mode 100644 tests/unit/services/command.test.ts diff --git a/src/server.ts b/src/server.ts index b1c2f5e..d0e68ef 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,5 +1,6 @@ import { Bun } from 'bun'; import { CommandService } from './services/command'; +import { ResponseQueue } from './services/response-queue'; const PORT = 3007; @@ -57,12 +58,15 @@ export class WebhookServer { // Forward to command service if applicable const messageText = data.message.conversation; if (messageText?.startsWith('/tarea')) { - await CommandService.handle({ + const responses = await CommandService.handle({ sender: data.key.participant, groupId: data.key.remoteJid, message: messageText, mentions: data.contextInfo?.mentionedJid || [] }); + + // Queue responses for sending + await ResponseQueue.add(responses); } } } diff --git a/src/services/command.ts b/src/services/command.ts new file mode 100644 index 0000000..4f6c231 --- /dev/null +++ b/src/services/command.ts @@ -0,0 +1,38 @@ +type CommandContext = { + sender: string; + groupId: string; + message: string; + mentions: string[]; +}; + +export type CommandResponse = { + recipient: string; + message: string; +}; + +export class CommandService { + private static async processTareaCommand( + context: CommandContext + ): Promise { + // Will implement actual command logic later + return [{ + recipient: context.sender, + message: 'Command received: ' + context.message + }]; + } + + static async handle(context: CommandContext): Promise { + if (!context.message.startsWith('/tarea')) { + return []; + } + + try { + return await this.processTareaCommand(context); + } catch (error) { + return [{ + recipient: context.sender, + message: 'Error processing command' + }]; + } + } +} diff --git a/src/services/response-queue.ts b/src/services/response-queue.ts new file mode 100644 index 0000000..d7c3ac6 --- /dev/null +++ b/src/services/response-queue.ts @@ -0,0 +1,17 @@ +type QueuedResponse = { + recipient: string; + message: string; +}; + +export const ResponseQueue = { + queue: [] as QueuedResponse[], + + async add(responses: QueuedResponse[]) { + this.queue.push(...responses); + console.log('Queued responses:', responses); + }, + + async process() { + // Will implement actual processing later + } +}; diff --git a/tests/unit/services/command.test.ts b/tests/unit/services/command.test.ts new file mode 100644 index 0000000..d39d250 --- /dev/null +++ b/tests/unit/services/command.test.ts @@ -0,0 +1,38 @@ +import { describe, test, expect, mock } from 'bun:test'; +import { CommandService } from '../../../src/services/command'; + +describe('CommandService', () => { + const testContext = { + sender: '1234567890@s.whatsapp.net', + groupId: 'group-id@g.us', + message: '/tarea nueva Test task', + mentions: [] + }; + + test('should ignore non-tarea commands', async () => { + const responses = await CommandService.handle({ + ...testContext, + message: '/othercommand' + }); + expect(responses).toEqual([]); + }); + + test('should handle tarea commands', async () => { + const responses = await CommandService.handle(testContext); + expect(responses.length).toBe(1); + expect(responses[0].recipient).toBe(testContext.sender); + expect(responses[0].message).toInclude('Command received'); + }); + + test('should return error response on failure', async () => { + const originalProcess = CommandService.processTareaCommand; + CommandService.processTareaCommand = mock(() => { + throw new Error('Test error'); + }); + + const responses = await CommandService.handle(testContext); + expect(responses[0].message).toInclude('Error processing'); + + CommandService.processTareaCommand = originalProcess; + }); +});