test: actualizar tests para validación de grupos

Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) <aider@aider.chat>
pull/1/head
borja 2 months ago
parent 206949aabe
commit e33fb646f1

@ -2,6 +2,7 @@ import { describe, test, expect, beforeEach, afterEach, beforeAll, afterAll, moc
import { Database } from 'bun:sqlite'; import { Database } from 'bun:sqlite';
import { WebhookServer } from '../../src/server'; import { WebhookServer } from '../../src/server';
import { ResponseQueue } from '../../src/services/response-queue'; import { ResponseQueue } from '../../src/services/response-queue';
import { GroupSyncService } from '../../src/services/group-sync';
import { initializeDatabase } from '../../src/db'; import { initializeDatabase } from '../../src/db';
// Mock the ResponseQueue // Mock the ResponseQueue
@ -37,6 +38,9 @@ beforeEach(() => {
testDb.exec('DELETE FROM tasks'); testDb.exec('DELETE FROM tasks');
testDb.exec('DELETE FROM users'); testDb.exec('DELETE FROM users');
testDb.exec('DELETE FROM groups'); testDb.exec('DELETE FROM groups');
// Mock GroupSyncService.isGroupActive to return true by default
mock(GroupSyncService.isGroupActive).mockReturnValue(true);
}); });
describe('WebhookServer', () => { describe('WebhookServer', () => {
@ -725,4 +729,46 @@ describe('WebhookServer', () => {
originalCommandService.CommandService.handle = originalCommandService.CommandService.handle; 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();
});
});
}); });

@ -145,4 +145,31 @@ describe('GroupSyncService', () => {
console.error = originalConsoleError; 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);
});
});
}); });

Loading…
Cancel
Save