Based on the diffs, here's the concise commit message:

fix: handle API response format and improve group sync tests
main
borja (aider) 3 months ago
parent ff82f62659
commit 1d29fedc18

@ -55,7 +55,11 @@ export class GroupSyncService {
throw new Error(`API request failed: ${response.statusText}`);
}
return response.json();
const data = await response.json();
if (data.status !== 'success') {
throw new Error(`API error: ${data.message || 'Unknown error'}`);
}
return data.response;
}
private static async upsertGroups(groups: EvolutionGroup[]): Promise<{ added: number; updated: number }> {

@ -52,12 +52,21 @@ describe('GroupSyncService', () => {
it('should filter groups by community ID', async () => {
const result = await GroupSyncService.syncGroups();
expect(result.added).toBe(1);
expect(result.updated).toBe(0);
expect(result).toEqual({
added: 1,
updated: 0
});
const groups = db.query('SELECT * FROM groups').all();
expect(groups).toHaveLength(1);
expect(groups[0].id).toBe('group1');
expect(groups).toEqual([
{
id: 'group1',
community_id: 'test-community',
name: 'Group 1',
active: 1,
last_verified: expect.any(String)
}
]);
});
it('should update existing groups', async () => {
@ -67,23 +76,37 @@ describe('GroupSyncService', () => {
);
const result = await GroupSyncService.syncGroups();
expect(result.added).toBe(0);
expect(result.updated).toBe(1);
expect(result).toEqual({
added: 0,
updated: 1
});
const group = db.query('SELECT * FROM groups WHERE id = ?').get('group1');
expect(group.name).toBe('Group 1');
expect(group.active).toBe(1);
expect(group).toEqual({
id: 'group1',
community_id: 'test-community',
name: 'Group 1',
active: 1,
last_verified: expect.any(String)
});
});
it('should mark non-matching groups as inactive', async () => {
// Add initial group not in current sync
db.exec(
"INSERT INTO groups (id, community_id, name, active) VALUES ('old-group', 'test-community', 'Old Group', 1)"
"INSERT INTO groups (id, community_id, name, active, last_verified) VALUES ('old-group', 'test-community', 'Old Group', 1, '2023-01-01')"
);
await GroupSyncService.syncGroups();
const group = db.query('SELECT active FROM groups WHERE id = ?').get('old-group');
expect(group.active).toBe(0);
const group = db.query('SELECT * FROM groups WHERE id = ?').get('old-group');
expect(group).toEqual({
id: 'old-group',
community_id: 'test-community',
name: 'Old Group',
active: 0,
last_verified: expect.any(String)
});
expect(group.last_verified).not.toBe('2023-01-01'); // Should be updated
});
it('should handle API errors', async () => {

Loading…
Cancel
Save