From c60e19a03b925b7d44ee4124ced1dc126225ab31 Mon Sep 17 00:00:00 2001 From: Torben Sorensen Date: Tue, 9 Dec 2025 15:19:27 -0800 Subject: [PATCH] fix logging tests --- src/services/apiClient.test.ts | 4 +++- src/services/db/flyer.db.ts | 6 +++++- src/services/db/personalization.db.test.ts | 2 +- src/services/db/shopping.db.test.ts | 4 ++-- src/services/db/shopping.db.ts | 6 ++++-- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/services/apiClient.test.ts b/src/services/apiClient.test.ts index 62a46ec9..3315969f 100644 --- a/src/services/apiClient.test.ts +++ b/src/services/apiClient.test.ts @@ -403,6 +403,9 @@ describe('API Client', () => { it('updateUserPreferences should send a PUT request with preferences data', async () => { const preferences = { darkMode: true }; let capturedBody: typeof preferences | null = null; + // Restore the original fetch so MSW can intercept this request. + // The global fetch spy from beforeEach would otherwise capture this call. + vi.mocked(global.fetch).mockRestore(); server.use( http.put('http://localhost/api/users/profile/preferences', async ({ request }) => { capturedBody = await request.json() as typeof preferences; @@ -410,7 +413,6 @@ describe('API Client', () => { }) ); await apiClient.updateUserPreferences(preferences); - expect(capturedUrl?.pathname).toBe('/api/users/profile/preferences'); expect(capturedBody).toEqual(preferences); }); diff --git a/src/services/db/flyer.db.ts b/src/services/db/flyer.db.ts index 6fd8aa3e..4557266f 100644 --- a/src/services/db/flyer.db.ts +++ b/src/services/db/flyer.db.ts @@ -89,7 +89,11 @@ export class FlyerRepository { if (error instanceof Error && 'code' in error && error.code === '23503') { throw new ForeignKeyConstraintError('The specified flyer does not exist.'); } - throw new Error('Failed to insert flyer items into database.'); + // Preserve the original error if it's not a foreign key violation, + // allowing transactional functions to catch and identify the specific failure. + // This is a higher-level fix for the test failure in `createFlyerAndItems`. + if (error instanceof Error) throw error; + throw new Error('An unknown error occurred while inserting flyer items.'); } } diff --git a/src/services/db/personalization.db.test.ts b/src/services/db/personalization.db.test.ts index 3104d41a..355c4510 100644 --- a/src/services/db/personalization.db.test.ts +++ b/src/services/db/personalization.db.test.ts @@ -28,7 +28,7 @@ describe('Personalization DB Service', () => { beforeEach(() => { vi.clearAllMocks(); // Simulate the client returned by connect() having a release method - const mockClient = { ...mockPoolInstance, release: vi.fn() }; + const mockClient = { ...mockPoolInstance, query: mockQuery, release: vi.fn() }; vi.mocked(mockPoolInstance.connect).mockResolvedValue(mockClient as any); // Instantiate the repository with the mock pool for each test personalizationRepo = new PersonalizationRepository(mockPoolInstance as any); diff --git a/src/services/db/shopping.db.test.ts b/src/services/db/shopping.db.test.ts index 5c6040cc..64a381de 100644 --- a/src/services/db/shopping.db.test.ts +++ b/src/services/db/shopping.db.test.ts @@ -326,7 +326,7 @@ describe('Shopping DB Service', () => { const dbError = new Error('violates foreign key constraint'); (dbError as any).code = '23503'; mockPoolInstance.query.mockRejectedValue(dbError); - await expect(shoppingRepo.createPantryLocation('non-existent-user', 'Pantry')).rejects.toThrow('Failed to create pantry location.'); + await expect(shoppingRepo.createPantryLocation('non-existent-user', 'Pantry')).rejects.toThrow(ForeignKeyConstraintError); }); it('should throw a generic error if the database query fails', async () => { @@ -369,7 +369,7 @@ describe('Shopping DB Service', () => { const dbError = new Error('violates foreign key constraint'); (dbError as any).code = '23503'; mockPoolInstance.query.mockRejectedValue(dbError); - await expect(shoppingRepo.createPantryLocation('non-existent-user', 'Pantry')).rejects.toThrow('Failed to create pantry location.'); + await expect(shoppingRepo.createReceipt('non-existent-user', 'url')).rejects.toThrow(ForeignKeyConstraintError); }); it('should throw a generic error if the database query fails', async () => { diff --git a/src/services/db/shopping.db.ts b/src/services/db/shopping.db.ts index 3685aac2..97369a43 100644 --- a/src/services/db/shopping.db.ts +++ b/src/services/db/shopping.db.ts @@ -299,8 +299,10 @@ export class ShoppingRepository { } return res.rows[0]; } catch (error) { - // The patch requested this specific error handling. - if (error instanceof Error && error.message.startsWith('Shopping list item not found')) throw error; + // Re-throw specific, known errors to allow for more precise error handling in the calling code. + if (error instanceof Error && (error.message.startsWith('Shopping list item not found') || error.message.startsWith('No valid fields'))) { + throw error; + } logger.error('Database error in updateShoppingListItem:', { error, itemId, updates }); throw new Error('Failed to update shopping list item.'); }