feat: implement command service with response queue
parent
b7ed60a471
commit
63c3bb20e9
@ -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<CommandResponse[]> {
|
||||||
|
// Will implement actual command logic later
|
||||||
|
return [{
|
||||||
|
recipient: context.sender,
|
||||||
|
message: 'Command received: ' + context.message
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
static async handle(context: CommandContext): Promise<CommandResponse[]> {
|
||||||
|
if (!context.message.startsWith('/tarea')) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await this.processTareaCommand(context);
|
||||||
|
} catch (error) {
|
||||||
|
return [{
|
||||||
|
recipient: context.sender,
|
||||||
|
message: 'Error processing command'
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
||||||
|
};
|
@ -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;
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue