|
|
|
@ -619,5 +619,136 @@ describe('WebhookServer', () => {
|
|
|
|
// Reinitialize database for subsequent tests
|
|
|
|
// Reinitialize database for subsequent tests
|
|
|
|
initializeDatabase(testDb);
|
|
|
|
initializeDatabase(testDb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('should integrate user validation completely in handleMessageUpsert with valid user', async () => {
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
|
|
|
event: 'messages.upsert',
|
|
|
|
|
|
|
|
instance: 'test-instance',
|
|
|
|
|
|
|
|
data: {
|
|
|
|
|
|
|
|
key: {
|
|
|
|
|
|
|
|
remoteJid: 'group-id@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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify user was created/updated in database
|
|
|
|
|
|
|
|
const user = testDb.query("SELECT * FROM users WHERE id = ?").get('1234567890');
|
|
|
|
|
|
|
|
expect(user).toBeDefined();
|
|
|
|
|
|
|
|
expect(user.id).toBe('1234567890');
|
|
|
|
|
|
|
|
expect(user.first_seen).toBeDefined();
|
|
|
|
|
|
|
|
expect(user.last_seen).toBeDefined();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('should use normalized ID in command service', async () => {
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
|
|
|
event: 'messages.upsert',
|
|
|
|
|
|
|
|
instance: 'test-instance',
|
|
|
|
|
|
|
|
data: {
|
|
|
|
|
|
|
|
key: {
|
|
|
|
|
|
|
|
remoteJid: 'group-id@g.us',
|
|
|
|
|
|
|
|
participant: '1234567890:12@s.whatsapp.net' // Raw ID with participant
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
message: { conversation: '/tarea nueva Test' }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock CommandService.handle to capture the sender parameter
|
|
|
|
|
|
|
|
const mockCommandHandle = mock(async () => []);
|
|
|
|
|
|
|
|
const originalCommandService = await import('../../src/services/command');
|
|
|
|
|
|
|
|
originalCommandService.CommandService.handle = mockCommandHandle;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const request = createTestRequest(payload);
|
|
|
|
|
|
|
|
await WebhookServer.handleRequest(request);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify CommandService.handle was called with normalized sender ID
|
|
|
|
|
|
|
|
expect(mockCommandHandle).toHaveBeenCalledWith({
|
|
|
|
|
|
|
|
sender: '1234567890', // Normalized ID
|
|
|
|
|
|
|
|
groupId: 'group-id@g.us',
|
|
|
|
|
|
|
|
message: '/tarea nueva Test',
|
|
|
|
|
|
|
|
mentions: []
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Restore original
|
|
|
|
|
|
|
|
originalCommandService.CommandService.handle = originalCommandService.CommandService.handle;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('should handle errors in user validation during integration', async () => {
|
|
|
|
|
|
|
|
// Mock ensureUserExists to return null (simulating error)
|
|
|
|
|
|
|
|
const originalDb = await import('../../src/db');
|
|
|
|
|
|
|
|
const originalEnsureUserExists = originalDb.ensureUserExists;
|
|
|
|
|
|
|
|
originalDb.ensureUserExists = mock(() => null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
|
|
|
event: 'messages.upsert',
|
|
|
|
|
|
|
|
instance: 'test-instance',
|
|
|
|
|
|
|
|
data: {
|
|
|
|
|
|
|
|
key: {
|
|
|
|
|
|
|
|
remoteJid: 'group-id@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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Restore original
|
|
|
|
|
|
|
|
originalDb.ensureUserExists = originalEnsureUserExists;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('should handle end-to-end flow with valid user and command processing', async () => {
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
|
|
|
event: 'messages.upsert',
|
|
|
|
|
|
|
|
instance: 'test-instance',
|
|
|
|
|
|
|
|
data: {
|
|
|
|
|
|
|
|
key: {
|
|
|
|
|
|
|
|
remoteJid: 'group-id@g.us',
|
|
|
|
|
|
|
|
participant: '1234567890@s.whatsapp.net'
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
message: { conversation: '/tarea nueva Test task' }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock CommandService.handle to return a response
|
|
|
|
|
|
|
|
const mockCommandHandle = mock(async () => [{
|
|
|
|
|
|
|
|
recipient: '1234567890',
|
|
|
|
|
|
|
|
message: 'Task created successfully'
|
|
|
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
const originalCommandService = await import('../../src/services/command');
|
|
|
|
|
|
|
|
originalCommandService.CommandService.handle = mockCommandHandle;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const request = createTestRequest(payload);
|
|
|
|
|
|
|
|
const response = await WebhookServer.handleRequest(request);
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify user was created/updated
|
|
|
|
|
|
|
|
const user = testDb.query("SELECT * FROM users WHERE id = ?").get('1234567890');
|
|
|
|
|
|
|
|
expect(user).toBeDefined();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify CommandService was called with normalized ID
|
|
|
|
|
|
|
|
expect(mockCommandHandle).toHaveBeenCalledWith({
|
|
|
|
|
|
|
|
sender: '1234567890',
|
|
|
|
|
|
|
|
groupId: 'group-id@g.us',
|
|
|
|
|
|
|
|
message: '/tarea nueva Test task',
|
|
|
|
|
|
|
|
mentions: []
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify ResponseQueue.add was called with the response
|
|
|
|
|
|
|
|
expect(mockAdd).toHaveBeenCalledWith([{
|
|
|
|
|
|
|
|
recipient: '1234567890',
|
|
|
|
|
|
|
|
message: 'Task created successfully'
|
|
|
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Restore original
|
|
|
|
|
|
|
|
originalCommandService.CommandService.handle = originalCommandService.CommandService.handle;
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|