|
|
|
|
@ -62,41 +62,54 @@ describe('GroupSyncService', () => {
|
|
|
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return empty result if WHATSAPP_COMMUNITY_ID is missing', async () => {
|
|
|
|
|
it('should upsert all groups even if WHATSAPP_COMMUNITY_ID is missing (multicommunity)', async () => {
|
|
|
|
|
process.env.WHATSAPP_COMMUNITY_ID = '';
|
|
|
|
|
|
|
|
|
|
const result = await GroupSyncService.syncGroups();
|
|
|
|
|
expect(result).toEqual({ added: 0, updated: 0 });
|
|
|
|
|
expect(result).toEqual({ added: 3, updated: 0 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should filter groups by community ID', async () => {
|
|
|
|
|
it('should upsert all groups regardless of community ID (multicommunity)', async () => {
|
|
|
|
|
// Reset last sync attempt to force a sync
|
|
|
|
|
GroupSyncService['lastSyncAttempt'] = 0;
|
|
|
|
|
|
|
|
|
|
const result = await GroupSyncService.syncGroups();
|
|
|
|
|
|
|
|
|
|
// Verify the correct groups were processed
|
|
|
|
|
// Verify all groups were processed
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
added: 1, // group1 should be added
|
|
|
|
|
updated: 0 // no existing groups to update
|
|
|
|
|
added: 3,
|
|
|
|
|
updated: 0
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Verify database state
|
|
|
|
|
const groups = testDb.query('SELECT * FROM groups').all();
|
|
|
|
|
expect(groups).toHaveLength(1);
|
|
|
|
|
|
|
|
|
|
const group = groups[0];
|
|
|
|
|
expect(group.id).toBe('group1');
|
|
|
|
|
expect(group.community_id).toBe('test-community');
|
|
|
|
|
expect(group.name).toBe('Group 1');
|
|
|
|
|
expect(group.active).toBe(1);
|
|
|
|
|
expect(group.last_verified).toBeTruthy();
|
|
|
|
|
const groups = testDb.query('SELECT id, community_id, name, active FROM groups').all() as any[];
|
|
|
|
|
expect(groups).toHaveLength(3);
|
|
|
|
|
|
|
|
|
|
const map = new Map(groups.map((g: any) => [g.id, g]));
|
|
|
|
|
expect(map.get('group1')).toEqual({
|
|
|
|
|
id: 'group1',
|
|
|
|
|
community_id: 'test-community',
|
|
|
|
|
name: 'Group 1',
|
|
|
|
|
active: 1
|
|
|
|
|
});
|
|
|
|
|
expect(map.get('group2')).toEqual({
|
|
|
|
|
id: 'group2',
|
|
|
|
|
community_id: 'other-community',
|
|
|
|
|
name: 'Group 2',
|
|
|
|
|
active: 1
|
|
|
|
|
});
|
|
|
|
|
expect(map.get('group3')).toEqual({
|
|
|
|
|
id: 'group3',
|
|
|
|
|
community_id: '',
|
|
|
|
|
name: 'Group 3',
|
|
|
|
|
active: 1
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Verify only the matching group was processed
|
|
|
|
|
// Verify API was called once
|
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update existing groups', async () => {
|
|
|
|
|
it('should update existing groups and insert new ones (multicommunity)', async () => {
|
|
|
|
|
// Add initial group
|
|
|
|
|
testDb.exec(
|
|
|
|
|
"INSERT INTO groups (id, community_id, name, active) VALUES ('group1', 'test-community', 'Old Name', 1)"
|
|
|
|
|
@ -104,18 +117,16 @@ describe('GroupSyncService', () => {
|
|
|
|
|
|
|
|
|
|
const result = await GroupSyncService.syncGroups();
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
added: 0,
|
|
|
|
|
added: 2,
|
|
|
|
|
updated: 1
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const group = testDb.query('SELECT * FROM groups WHERE id = ?').get('group1');
|
|
|
|
|
expect(group).toEqual({
|
|
|
|
|
id: 'group1',
|
|
|
|
|
community_id: 'test-community',
|
|
|
|
|
name: 'Group 1',
|
|
|
|
|
active: 1,
|
|
|
|
|
last_verified: expect.any(String)
|
|
|
|
|
});
|
|
|
|
|
const all = testDb.query('SELECT id, community_id, name, active FROM groups ORDER BY id').all();
|
|
|
|
|
expect(all).toEqual([
|
|
|
|
|
{ id: 'group1', community_id: 'test-community', name: 'Group 1', active: 1 },
|
|
|
|
|
{ id: 'group2', community_id: 'other-community', name: 'Group 2', active: 1 },
|
|
|
|
|
{ id: 'group3', community_id: '', name: 'Group 3', active: 1 }
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should mark non-matching groups as inactive', async () => {
|
|
|
|
|
|