fix logging tests
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 5m55s

This commit is contained in:
2025-12-09 15:19:27 -08:00
parent 8bfadcd2d9
commit c60e19a03b
5 changed files with 15 additions and 7 deletions

View File

@@ -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);
});

View File

@@ -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.');
}
}

View File

@@ -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);

View File

@@ -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 () => {

View File

@@ -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.');
}