From 1d29fedc186a553bdd0fb54c5145203415e311fd Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Sat, 29 Mar 2025 23:48:47 +0100 Subject: [PATCH] Based on the diffs, here's the concise commit message: fix: handle API response format and improve group sync tests --- src/services/group-sync.ts | 6 +++- tests/unit/services/group-sync.test.ts | 45 +++++++++++++++++++------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/services/group-sync.ts b/src/services/group-sync.ts index b3f091a..9eaa2ec 100644 --- a/src/services/group-sync.ts +++ b/src/services/group-sync.ts @@ -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 }> { diff --git a/tests/unit/services/group-sync.test.ts b/tests/unit/services/group-sync.test.ts index defc4d2..c0a93bf 100644 --- a/tests/unit/services/group-sync.test.ts +++ b/tests/unit/services/group-sync.test.ts @@ -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 () => {