From e33fb646f17e667045325d1774408ddde13ef332 Mon Sep 17 00:00:00 2001 From: borja Date: Fri, 5 Sep 2025 13:51:01 +0200 Subject: [PATCH] =?UTF-8?q?test:=20actualizar=20tests=20para=20validaci?= =?UTF-8?q?=C3=B3n=20de=20grupos?= 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 | 46 ++++++++++++++++++++++++++ tests/unit/services/group-sync.test.ts | 27 +++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index 8948623..f5235f6 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -2,6 +2,7 @@ import { describe, test, expect, beforeEach, afterEach, beforeAll, afterAll, moc import { Database } from 'bun:sqlite'; import { WebhookServer } from '../../src/server'; import { ResponseQueue } from '../../src/services/response-queue'; +import { GroupSyncService } from '../../src/services/group-sync'; import { initializeDatabase } from '../../src/db'; // Mock the ResponseQueue @@ -37,6 +38,9 @@ beforeEach(() => { testDb.exec('DELETE FROM tasks'); testDb.exec('DELETE FROM users'); testDb.exec('DELETE FROM groups'); + + // Mock GroupSyncService.isGroupActive to return true by default + mock(GroupSyncService.isGroupActive).mockReturnValue(true); }); describe('WebhookServer', () => { @@ -725,4 +729,46 @@ describe('WebhookServer', () => { originalCommandService.CommandService.handle = originalCommandService.CommandService.handle; }); }); + + describe('Group validation in handleMessageUpsert', () => { + test('should ignore messages from inactive groups', async () => { + // Mock isGroupActive to return false for this test + mock(GroupSyncService.isGroupActive).mockReturnValue(false); + + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'inactive-group@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(); + }); + + test('should proceed with messages from active groups', async () => { + // Ensure isGroupActive returns true (already mocked in beforeEach) + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'active-group@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(); + }); + }); }); diff --git a/tests/unit/services/group-sync.test.ts b/tests/unit/services/group-sync.test.ts index 8fc1ba3..3701a1e 100644 --- a/tests/unit/services/group-sync.test.ts +++ b/tests/unit/services/group-sync.test.ts @@ -145,4 +145,31 @@ describe('GroupSyncService', () => { console.error = originalConsoleError; }); }); + + describe('isGroupActive', () => { + beforeEach(() => { + // Clear cache and add test groups + GroupSyncService['activeGroupsCache'].clear(); + db.exec('DELETE FROM groups'); + db.exec("INSERT INTO groups (id, name, active) VALUES ('active-group', 'Active Group', 1)"); + db.exec("INSERT INTO groups (id, name, active) VALUES ('inactive-group', 'Inactive Group', 0)"); + // Populate cache + GroupSyncService['cacheActiveGroups'](); + }); + + it('should return true for active groups', () => { + const result = GroupSyncService.isGroupActive('active-group'); + expect(result).toBe(true); + }); + + it('should return false for inactive groups', () => { + const result = GroupSyncService.isGroupActive('inactive-group'); + expect(result).toBe(false); + }); + + it('should return false for non-existent groups', () => { + const result = GroupSyncService.isGroupActive('non-existent-group'); + expect(result).toBe(false); + }); + }); });