|
|
|
@ -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();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|