Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0371947065 | ||
| 296698758c | |||
|
|
18c1161587 | ||
| 0010396780 | |||
|
|
d4557e13fb | ||
| 3e41130c69 | |||
|
|
d9034563d6 | ||
| 5836a75157 | |||
|
|
790008ae0d | ||
|
|
b5b91eb968 | ||
| 38eb810e7a | |||
|
|
458588a6e7 | ||
| 0b4113417f | |||
|
|
b59d2a9533 | ||
| 6740b35f8a | |||
|
|
92ad82a012 | ||
| 672e4ca597 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.6.2",
|
||||
"version": "0.7.4",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.6.2",
|
||||
"version": "0.7.4",
|
||||
"dependencies": {
|
||||
"@bull-board/api": "^6.14.2",
|
||||
"@bull-board/express": "^6.14.2",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "flyer-crawler",
|
||||
"private": true,
|
||||
"version": "0.6.2",
|
||||
"version": "0.7.4",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "concurrently \"npm:start:dev\" \"vite\"",
|
||||
|
||||
@@ -268,13 +268,12 @@ describe('FlyerUploader', () => {
|
||||
|
||||
// We need at least one 'active' response to establish a timeout loop so we have something to clear
|
||||
// The second call should be a rejection, as this is how getJobStatus signals a failure.
|
||||
mockedAiApiClient.getJobStatus.mockImplementation(async () => {
|
||||
// Fail on the second call by checking the number of times the mock has been invoked.
|
||||
if (mockedAiApiClient.getJobStatus.mock.calls.length > 1) {
|
||||
throw new aiApiClientModule.JobFailedError('Fatal Error', 'UNKNOWN_ERROR');
|
||||
}
|
||||
return { state: 'active', progress: { message: 'Working...' } } as aiApiClientModule.JobStatus;
|
||||
});
|
||||
mockedAiApiClient.getJobStatus
|
||||
.mockResolvedValueOnce({
|
||||
state: 'active',
|
||||
progress: { message: 'Working...' },
|
||||
} as aiApiClientModule.JobStatus)
|
||||
.mockRejectedValueOnce(new aiApiClientModule.JobFailedError('Fatal Error', 'UNKNOWN_ERROR'));
|
||||
|
||||
renderComponent();
|
||||
const file = new File(['content'], 'flyer.pdf', { type: 'application/pdf' });
|
||||
@@ -289,10 +288,10 @@ describe('FlyerUploader', () => {
|
||||
await waitFor(() => expect(screen.getByText(/Polling failed: Fatal Error/i)).toBeInTheDocument(), { timeout: 4000 });
|
||||
});
|
||||
|
||||
it('should clear the polling timeout when the component unmounts', async () => {
|
||||
const clearTimeoutSpy = vi.spyOn(global, 'clearTimeout');
|
||||
console.log('--- [TEST LOG] ---: 1. Setting up mocks for unmount timeout clearance.');
|
||||
it('should stop polling for job status when the component unmounts', async () => {
|
||||
console.log('--- [TEST LOG] ---: 1. Setting up mocks for unmount polling stop.');
|
||||
mockedAiApiClient.uploadAndProcessFlyer.mockResolvedValue({ jobId: 'job-unmount' });
|
||||
// Mock getJobStatus to always return 'active' to keep polling
|
||||
mockedAiApiClient.getJobStatus.mockResolvedValue({
|
||||
state: 'active',
|
||||
progress: { message: 'Polling...' },
|
||||
@@ -304,18 +303,29 @@ describe('FlyerUploader', () => {
|
||||
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
|
||||
// Wait for the first poll to complete and the UI to show the polling state
|
||||
// Wait for the first poll to complete and UI to update
|
||||
await screen.findByText('Polling...');
|
||||
|
||||
// Now that we are in a polling state (and a timeout is set), unmount the component
|
||||
console.log('--- [TEST LOG] ---: 2. Unmounting component to trigger cleanup effect.');
|
||||
// Wait for exactly one call to be sure polling has started.
|
||||
await waitFor(() => {
|
||||
expect(mockedAiApiClient.getJobStatus).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
console.log('--- [TEST LOG] ---: 2. First poll confirmed.');
|
||||
|
||||
// Record the number of calls before unmounting.
|
||||
const callsBeforeUnmount = mockedAiApiClient.getJobStatus.mock.calls.length;
|
||||
|
||||
// Now unmount the component, which should stop the polling.
|
||||
console.log('--- [TEST LOG] ---: 3. Unmounting component.');
|
||||
unmount();
|
||||
|
||||
// Verify that the cleanup function in the useEffect hook was called
|
||||
expect(clearTimeoutSpy).toHaveBeenCalled();
|
||||
console.log('--- [TEST LOG] ---: 3. clearTimeout confirmed.');
|
||||
// Wait for a duration longer than the polling interval (3s) to see if more calls are made.
|
||||
console.log('--- [TEST LOG] ---: 4. Waiting for 4 seconds to check for further polling.');
|
||||
await act(() => new Promise((resolve) => setTimeout(resolve, 4000)));
|
||||
|
||||
clearTimeoutSpy.mockRestore();
|
||||
// Verify that getJobStatus was not called again after unmounting.
|
||||
console.log('--- [TEST LOG] ---: 5. Asserting no new polls occurred.');
|
||||
expect(mockedAiApiClient.getJobStatus).toHaveBeenCalledTimes(callsBeforeUnmount);
|
||||
});
|
||||
|
||||
it('should handle a duplicate flyer error (409)', async () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { useState, useCallback, useRef, useEffect } from 'react';
|
||||
import { logger } from '../services/logger.client';
|
||||
import { notifyError } from '../services/notificationService';
|
||||
|
||||
|
||||
/**
|
||||
* A custom React hook to simplify API calls, including loading and error states.
|
||||
* It is designed to work with apiClient functions that return a `Promise<Response>`.
|
||||
|
||||
@@ -106,7 +106,10 @@ export const useFlyerUploader = () => {
|
||||
// Consolidate state derivation for the UI from the react-query hooks using useMemo.
|
||||
// This improves performance by memoizing the derived state and makes the logic easier to follow.
|
||||
const { processingState, errorMessage, duplicateFlyerId, flyerId, statusMessage } = useMemo(() => {
|
||||
// The order of these checks is critical. Errors must be checked first to override
|
||||
// any stale `jobStatus` from a previous successful poll.
|
||||
const state: ProcessingState = (() => {
|
||||
if (uploadMutation.isError || pollError) return 'error';
|
||||
if (uploadMutation.isPending) return 'uploading';
|
||||
if (jobStatus && (jobStatus.state === 'active' || jobStatus.state === 'waiting'))
|
||||
return 'polling';
|
||||
@@ -114,7 +117,6 @@ export const useFlyerUploader = () => {
|
||||
if (!jobStatus.returnValue?.flyerId) return 'error';
|
||||
return 'completed';
|
||||
}
|
||||
if (uploadMutation.isError || jobStatus?.state === 'failed' || pollError) return 'error';
|
||||
return 'idle';
|
||||
})();
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
// FIX: Stabilize the apiFunction passed to useApi.
|
||||
// By wrapping this in useCallback, we ensure the same function instance is passed to
|
||||
// useApi on every render. This prevents the `execute` function returned by `useApi`
|
||||
// from being recreated, which in turn breaks the infinite re-render loop in the useEffect below.
|
||||
// from being recreated, which in turn breaks the infinite re-render loop in the useEffect.
|
||||
const getProfileCallback = useCallback(() => apiClient.getAuthenticatedUserProfile(), []);
|
||||
|
||||
const { execute: checkTokenApi } = useApi<UserProfile, []>(getProfileCallback);
|
||||
|
||||
@@ -4,17 +4,21 @@ import { FlyersContext, FlyersContextType } from '../contexts/FlyersContext';
|
||||
import type { Flyer } from '../types';
|
||||
import * as apiClient from '../services/apiClient';
|
||||
import { useInfiniteQuery } from '../hooks/useInfiniteQuery';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
export const FlyersProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
||||
// Memoize the fetch function to ensure stability for the useInfiniteQuery hook.
|
||||
const fetchFlyersFn = useCallback(apiClient.fetchFlyers, []);
|
||||
|
||||
const {
|
||||
data: flyers,
|
||||
isLoading: isLoadingFlyers,
|
||||
isLoading: isLoadingFlyers,
|
||||
error: flyersError,
|
||||
fetchNextPage: fetchNextFlyersPage,
|
||||
hasNextPage: hasNextFlyersPage,
|
||||
refetch: refetchFlyers,
|
||||
isRefetching: isRefetchingFlyers,
|
||||
} = useInfiniteQuery<Flyer>(apiClient.fetchFlyers);
|
||||
} = useInfiniteQuery<Flyer>(fetchFlyersFn);
|
||||
|
||||
const value: FlyersContextType = {
|
||||
flyers: flyers || [],
|
||||
@@ -26,5 +30,5 @@ export const FlyersProvider: React.FC<{ children: ReactNode }> = ({ children })
|
||||
refetchFlyers,
|
||||
};
|
||||
|
||||
return <FlyersContext.Provider value={value}>{children}</FlyersContext.Provider>;
|
||||
return <FlyersContext.Provider value={value}>{children}</FlyersContext.Provider>;
|
||||
};
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
// src/providers/MasterItemsProvider.tsx
|
||||
import React, { ReactNode, useMemo } from 'react';
|
||||
import React, { ReactNode, useMemo, useEffect, useCallback } from 'react';
|
||||
import { MasterItemsContext } from '../contexts/MasterItemsContext';
|
||||
import type { MasterGroceryItem } from '../types';
|
||||
import * as apiClient from '../services/apiClient';
|
||||
import { useApiOnMount } from '../hooks/useApiOnMount';
|
||||
import { logger } from '../services/logger.client';
|
||||
|
||||
export const MasterItemsProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
||||
const { data, loading, error } = useApiOnMount<MasterGroceryItem[], []>(() =>
|
||||
apiClient.fetchMasterItems(),
|
||||
);
|
||||
// LOGGING: Check if the provider is unmounting/remounting repeatedly
|
||||
useEffect(() => {
|
||||
logger.debug('MasterItemsProvider: MOUNTED');
|
||||
return () => logger.debug('MasterItemsProvider: UNMOUNTED');
|
||||
}, []);
|
||||
|
||||
// Memoize the fetch function to ensure stability for the useApiOnMount hook.
|
||||
const fetchFn = useCallback(() => apiClient.fetchMasterItems(), []);
|
||||
|
||||
const { data, loading, error } = useApiOnMount<MasterGroceryItem[], []>(fetchFn);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// src/providers/UserDataProvider.tsx
|
||||
import React, { useState, useEffect, useMemo, ReactNode } from 'react';
|
||||
import { logger } from '../services/logger.client';
|
||||
import React, { useState, useEffect, useMemo, ReactNode, useCallback } from 'react';
|
||||
import { UserDataContext } from '../contexts/UserDataContext';
|
||||
import type { MasterGroceryItem, ShoppingList } from '../types';
|
||||
import * as apiClient from '../services/apiClient';
|
||||
@@ -9,18 +10,25 @@ import { useAuth } from '../hooks/useAuth';
|
||||
export const UserDataProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
||||
const { userProfile } = useAuth();
|
||||
|
||||
// Wrap the API calls in useCallback to prevent unnecessary re-renders.
|
||||
const fetchWatchedItemsFn = useCallback(
|
||||
() => apiClient.fetchWatchedItems(),
|
||||
[],
|
||||
);
|
||||
const fetchShoppingListsFn = useCallback(() => apiClient.fetchShoppingLists(), []);
|
||||
|
||||
const {
|
||||
data: watchedItemsData,
|
||||
loading: isLoadingWatched,
|
||||
error: watchedItemsError,
|
||||
} = useApiOnMount<MasterGroceryItem[], []>(() => apiClient.fetchWatchedItems(), [userProfile], {
|
||||
} = useApiOnMount<MasterGroceryItem[], []>(fetchWatchedItemsFn, [userProfile], {
|
||||
enabled: !!userProfile,
|
||||
});
|
||||
const {
|
||||
data: shoppingListsData,
|
||||
loading: isLoadingShoppingLists,
|
||||
loading: isLoadingShoppingLists,
|
||||
error: shoppingListsError,
|
||||
} = useApiOnMount<ShoppingList[], []>(() => apiClient.fetchShoppingLists(), [userProfile], {
|
||||
} = useApiOnMount<ShoppingList[], []>(fetchShoppingListsFn, [userProfile], {
|
||||
enabled: !!userProfile,
|
||||
});
|
||||
|
||||
@@ -32,7 +40,7 @@ export const UserDataProvider: React.FC<{ children: ReactNode }> = ({ children }
|
||||
useEffect(() => {
|
||||
// When the user logs out (user becomes null), immediately clear all user-specific data.
|
||||
// This also serves to clear out old data when a new user logs in, before their new data arrives.
|
||||
if (!userProfile) {
|
||||
if (!userProfile) {
|
||||
setWatchedItems([]);
|
||||
setShoppingLists([]);
|
||||
return;
|
||||
@@ -60,7 +68,7 @@ export const UserDataProvider: React.FC<{ children: ReactNode }> = ({ children }
|
||||
watchedItemsError,
|
||||
shoppingListsError,
|
||||
],
|
||||
);
|
||||
);
|
||||
|
||||
return <UserDataContext.Provider value={value}>{children}</UserDataContext.Provider>;
|
||||
};
|
||||
|
||||
@@ -165,6 +165,38 @@ describe('Auth Routes (/api/auth)', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should allow registration with an empty string for avatar_url', async () => {
|
||||
// Arrange
|
||||
const email = 'avatar-user@test.com';
|
||||
const mockNewUser = createMockUserProfile({
|
||||
user: { user_id: 'avatar-user-id', email },
|
||||
});
|
||||
mockedAuthService.registerAndLoginUser.mockResolvedValue({
|
||||
newUserProfile: mockNewUser,
|
||||
accessToken: 'avatar-access-token',
|
||||
refreshToken: 'avatar-refresh-token',
|
||||
});
|
||||
|
||||
// Act
|
||||
const response = await supertest(app).post('/api/auth/register').send({
|
||||
email,
|
||||
password: strongPassword,
|
||||
full_name: 'Avatar User',
|
||||
avatar_url: '', // Send an empty string
|
||||
});
|
||||
|
||||
// Assert
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body.message).toBe('User registered successfully!');
|
||||
expect(mockedAuthService.registerAndLoginUser).toHaveBeenCalledWith(
|
||||
email,
|
||||
strongPassword,
|
||||
'Avatar User',
|
||||
undefined, // The preprocess step in the Zod schema should convert '' to undefined
|
||||
mockLogger,
|
||||
);
|
||||
});
|
||||
|
||||
it('should set a refresh token cookie on successful registration', async () => {
|
||||
const mockNewUser = createMockUserProfile({
|
||||
user: { user_id: 'new-user-id', email: 'cookie@test.com' },
|
||||
|
||||
@@ -51,7 +51,11 @@ const registerSchema = z.object({
|
||||
}),
|
||||
// Sanitize optional string inputs.
|
||||
full_name: z.string().trim().optional(),
|
||||
avatar_url: z.string().trim().url().optional(),
|
||||
// Allow empty string or valid URL. If empty string is received, convert to undefined.
|
||||
avatar_url: z.preprocess(
|
||||
(val) => (val === '' ? undefined : val),
|
||||
z.string().trim().url().optional(),
|
||||
),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -19,6 +19,12 @@ router.get(
|
||||
validateRequest(emptySchema),
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
// LOGGING: Track how often this heavy DB call is actually made vs served from cache
|
||||
req.log.info('Fetching master items list from database...');
|
||||
|
||||
// Optimization: This list changes rarely. Instruct clients to cache it for 1 hour (3600s).
|
||||
res.set('Cache-Control', 'public, max-age=3600');
|
||||
|
||||
const masterItems = await db.personalizationRepo.getAllMasterItems(req.log);
|
||||
res.json(masterItems);
|
||||
} catch (error) {
|
||||
|
||||
@@ -585,6 +585,27 @@ describe('User Routes (/api/users)', () => {
|
||||
expect(response.body).toEqual(updatedProfile);
|
||||
});
|
||||
|
||||
it('should allow updating the profile with an empty string for avatar_url', async () => {
|
||||
// Arrange
|
||||
const profileUpdates = { avatar_url: '' };
|
||||
// The service should receive `undefined` after Zod preprocessing
|
||||
const updatedProfile = createMockUserProfile({ ...mockUserProfile, avatar_url: undefined });
|
||||
vi.mocked(db.userRepo.updateUserProfile).mockResolvedValue(updatedProfile);
|
||||
|
||||
// Act
|
||||
const response = await supertest(app).put('/api/users/profile').send(profileUpdates);
|
||||
|
||||
// Assert
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual(updatedProfile);
|
||||
// Verify that the Zod schema preprocessed the empty string to undefined
|
||||
expect(db.userRepo.updateUserProfile).toHaveBeenCalledWith(
|
||||
mockUserProfile.user.user_id,
|
||||
{ avatar_url: undefined },
|
||||
expectLogger,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return 500 on a generic database error', async () => {
|
||||
const dbError = new Error('DB Connection Failed');
|
||||
vi.mocked(db.userRepo.updateUserProfile).mockRejectedValue(dbError);
|
||||
|
||||
@@ -26,7 +26,13 @@ const router = express.Router();
|
||||
|
||||
const updateProfileSchema = z.object({
|
||||
body: z
|
||||
.object({ full_name: z.string().optional(), avatar_url: z.string().url().optional() })
|
||||
.object({
|
||||
full_name: z.string().optional(),
|
||||
avatar_url: z.preprocess(
|
||||
(val) => (val === '' ? undefined : val),
|
||||
z.string().trim().url().optional(),
|
||||
),
|
||||
})
|
||||
.refine((data) => Object.keys(data).length > 0, {
|
||||
message: 'At least one field to update must be provided.',
|
||||
}),
|
||||
|
||||
@@ -6,12 +6,13 @@ import type { FlyerStatus, MasterGroceryItem, UserProfile } from '../types';
|
||||
// Import the class, not the singleton instance, so we can instantiate it with mocks.
|
||||
import {
|
||||
AIService,
|
||||
AiFlyerDataSchema,
|
||||
aiService as aiServiceSingleton,
|
||||
DuplicateFlyerError,
|
||||
type RawFlyerItem,
|
||||
} from './aiService.server';
|
||||
import { createMockMasterGroceryItem } from '../tests/utils/mockFactories';
|
||||
import { ValidationError } from './db/errors.db';
|
||||
import { AiFlyerDataSchema } from '../types/ai';
|
||||
|
||||
// Mock the logger to prevent the real pino instance from being created, which causes issues with 'pino-pretty' in tests.
|
||||
vi.mock('./logger.server', () => ({
|
||||
@@ -1058,4 +1059,56 @@ describe('AI Service (Server)', () => {
|
||||
expect(aiServiceSingleton).toBeInstanceOf(AIService);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_normalizeExtractedItems (private method)', () => {
|
||||
it('should correctly normalize items with null or undefined price_in_cents', () => {
|
||||
const rawItems: RawFlyerItem[] = [
|
||||
{
|
||||
item: 'Valid Item',
|
||||
price_display: '$1.99',
|
||||
price_in_cents: 199,
|
||||
quantity: '1',
|
||||
category_name: 'Category A',
|
||||
master_item_id: 1,
|
||||
},
|
||||
{
|
||||
item: 'Item with Null Price',
|
||||
price_display: null,
|
||||
price_in_cents: null, // Test case for null
|
||||
quantity: '1',
|
||||
category_name: 'Category B',
|
||||
master_item_id: 2,
|
||||
},
|
||||
{
|
||||
item: 'Item with Undefined Price',
|
||||
price_display: '$2.99',
|
||||
price_in_cents: undefined, // Test case for undefined
|
||||
quantity: '1',
|
||||
category_name: 'Category C',
|
||||
master_item_id: 3,
|
||||
},
|
||||
{
|
||||
item: null, // Test null item name
|
||||
price_display: undefined, // Test undefined display price
|
||||
price_in_cents: 50,
|
||||
quantity: null, // Test null quantity
|
||||
category_name: undefined, // Test undefined category
|
||||
master_item_id: null, // Test null master_item_id
|
||||
},
|
||||
];
|
||||
|
||||
// Access the private method for testing
|
||||
const normalized = (aiServiceInstance as any)._normalizeExtractedItems(rawItems);
|
||||
|
||||
expect(normalized).toHaveLength(4);
|
||||
expect(normalized[0].price_in_cents).toBe(199);
|
||||
expect(normalized[1].price_in_cents).toBe(null); // null should remain null
|
||||
expect(normalized[2].price_in_cents).toBe(null); // undefined should become null
|
||||
expect(normalized[3].item).toBe('Unknown Item');
|
||||
expect(normalized[3].quantity).toBe('');
|
||||
expect(normalized[3].category_name).toBe('Other/Miscellaneous');
|
||||
expect(normalized[3].master_item_id).toBeUndefined(); // nullish coalescing to undefined
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
* It is intended to be used only by the backend (e.g., server.ts) and should never be imported into client-side code.
|
||||
* The `.server.ts` naming convention helps enforce this separation.
|
||||
*/
|
||||
|
||||
import { GoogleGenAI, type GenerateContentResponse, type Content, type Tool } from '@google/genai';
|
||||
import fsPromises from 'node:fs/promises';
|
||||
import type { Logger } from 'pino';
|
||||
@@ -26,29 +25,11 @@ import type { Job } from 'bullmq';
|
||||
import { createFlyerAndItems } from './db/flyer.db';
|
||||
import { generateFlyerIcon } from '../utils/imageProcessor';
|
||||
import path from 'path';
|
||||
import { ValidationError } from './db/errors.db';
|
||||
|
||||
// Helper for consistent required string validation (handles missing/null/empty)
|
||||
const requiredString = (message: string) =>
|
||||
z.preprocess((val) => val ?? '', z.string().min(1, message));
|
||||
|
||||
// --- Zod Schemas for AI Response Validation (exported for the transformer) ---
|
||||
const ExtractedFlyerItemSchema = z.object({
|
||||
item: z.string(),
|
||||
price_display: z.string(),
|
||||
price_in_cents: z.number().nullable(),
|
||||
quantity: z.string(),
|
||||
category_name: z.string(),
|
||||
master_item_id: z.number().nullish(), // .nullish() allows null or undefined
|
||||
});
|
||||
|
||||
export const AiFlyerDataSchema = z.object({
|
||||
store_name: requiredString('Store name cannot be empty'),
|
||||
valid_from: z.string().nullable(),
|
||||
valid_to: z.string().nullable(),
|
||||
store_address: z.string().nullable(),
|
||||
items: z.array(ExtractedFlyerItemSchema),
|
||||
});
|
||||
import { ValidationError } from './db/errors.db'; // Keep this import for ValidationError
|
||||
import {
|
||||
AiFlyerDataSchema,
|
||||
ExtractedFlyerItemSchema,
|
||||
} from '../types/ai'; // Import consolidated schemas
|
||||
|
||||
interface FlyerProcessPayload extends Partial<ExtractedCoreData> {
|
||||
checksum?: string;
|
||||
@@ -89,10 +70,10 @@ interface IAiClient {
|
||||
* This type is intentionally loose to accommodate potential null/undefined values
|
||||
* from the AI before they are cleaned and normalized.
|
||||
*/
|
||||
type RawFlyerItem = {
|
||||
item: string;
|
||||
export type RawFlyerItem = {
|
||||
item: string | null;
|
||||
price_display: string | null | undefined;
|
||||
price_in_cents: number | null;
|
||||
price_in_cents: number | null | undefined;
|
||||
quantity: string | null | undefined;
|
||||
category_name: string | null | undefined;
|
||||
master_item_id?: number | null | undefined;
|
||||
@@ -507,7 +488,7 @@ export class AIService {
|
||||
userProfileAddress?: string,
|
||||
logger: Logger = this.logger,
|
||||
): Promise<{
|
||||
store_name: string;
|
||||
store_name: string | null;
|
||||
valid_from: string | null;
|
||||
valid_to: string | null;
|
||||
store_address: string | null;
|
||||
@@ -606,6 +587,8 @@ export class AIService {
|
||||
item.category_name === null || item.category_name === undefined
|
||||
? 'Other/Miscellaneous'
|
||||
: String(item.category_name),
|
||||
// Ensure undefined is converted to null to match the Zod schema.
|
||||
price_in_cents: item.price_in_cents ?? null,
|
||||
master_item_id: item.master_item_id ?? undefined,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -283,7 +283,10 @@ export const fetchFlyerById = (flyerId: number): Promise<Response> =>
|
||||
* Fetches all master grocery items from the backend.
|
||||
* @returns A promise that resolves to an array of MasterGroceryItem objects.
|
||||
*/
|
||||
export const fetchMasterItems = (): Promise<Response> => publicGet('/personalization/master-items');
|
||||
export const fetchMasterItems = (): Promise<Response> => {
|
||||
logger.debug('apiClient: fetchMasterItems called');
|
||||
return publicGet('/personalization/master-items');
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches all categories from the backend.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { FlyerAiProcessor } from './flyerAiProcessor.server';
|
||||
import { AiDataValidationError } from './processingErrors';
|
||||
import { logger } from './logger.server';
|
||||
import { logger } from './logger.server'; // Keep this import for the logger instance
|
||||
import type { AIService } from './aiService.server';
|
||||
import type { PersonalizationRepository } from './db/personalization.db';
|
||||
import type { FlyerJobData } from '../types/job-data';
|
||||
@@ -127,4 +127,98 @@ describe('FlyerAiProcessor', () => {
|
||||
expect(result.needsReview).toBe(true);
|
||||
expect(logger.warn).toHaveBeenCalledWith(expect.any(Object), expect.stringContaining('contains no items. The flyer will be saved with an item_count of 0. Flagging for review.'));
|
||||
});
|
||||
|
||||
describe('Batching Logic', () => {
|
||||
it('should process images in batches and merge the results correctly', async () => {
|
||||
// Arrange
|
||||
const jobData = createMockJobData({});
|
||||
// 5 images, with BATCH_SIZE = 4, should result in 2 batches.
|
||||
const imagePaths = [
|
||||
{ path: 'page1.jpg', mimetype: 'image/jpeg' },
|
||||
{ path: 'page2.jpg', mimetype: 'image/jpeg' },
|
||||
{ path: 'page3.jpg', mimetype: 'image/jpeg' },
|
||||
{ path: 'page4.jpg', mimetype: 'image/jpeg' },
|
||||
{ path: 'page5.jpg', mimetype: 'image/jpeg' },
|
||||
];
|
||||
|
||||
const mockAiResponseBatch1 = {
|
||||
store_name: 'Batch 1 Store',
|
||||
valid_from: '2025-01-01',
|
||||
valid_to: '2025-01-07',
|
||||
store_address: '123 Batch St',
|
||||
items: [
|
||||
{ item: 'Item A', price_display: '$1', price_in_cents: 100, quantity: '1', category_name: 'Cat A', master_item_id: 1 },
|
||||
{ item: 'Item B', price_display: '$2', price_in_cents: 200, quantity: '1', category_name: 'Cat B', master_item_id: 2 },
|
||||
],
|
||||
};
|
||||
|
||||
const mockAiResponseBatch2 = {
|
||||
store_name: 'Batch 2 Store', // This should be ignored in the merge
|
||||
valid_from: null,
|
||||
valid_to: null,
|
||||
store_address: null,
|
||||
items: [
|
||||
{ item: 'Item C', price_display: '$3', price_in_cents: 300, quantity: '1', category_name: 'Cat C', master_item_id: 3 },
|
||||
],
|
||||
};
|
||||
|
||||
// Mock the AI service to return different results for each batch call
|
||||
vi.mocked(mockAiService.extractCoreDataFromFlyerImage)
|
||||
.mockResolvedValueOnce(mockAiResponseBatch1)
|
||||
.mockResolvedValueOnce(mockAiResponseBatch2);
|
||||
|
||||
// Act
|
||||
const result = await service.extractAndValidateData(imagePaths, jobData, logger);
|
||||
|
||||
// Assert
|
||||
// 1. AI service was called twice (for 2 batches)
|
||||
expect(mockAiService.extractCoreDataFromFlyerImage).toHaveBeenCalledTimes(2);
|
||||
|
||||
// 2. Check the arguments for each call
|
||||
expect(mockAiService.extractCoreDataFromFlyerImage).toHaveBeenNthCalledWith(1, imagePaths.slice(0, 4), [], undefined, undefined, logger);
|
||||
expect(mockAiService.extractCoreDataFromFlyerImage).toHaveBeenNthCalledWith(2, imagePaths.slice(4, 5), [], undefined, undefined, logger);
|
||||
|
||||
// 3. Check the merged data
|
||||
expect(result.data.store_name).toBe('Batch 1 Store'); // Metadata from the first batch
|
||||
expect(result.data.valid_from).toBe('2025-01-01');
|
||||
expect(result.data.valid_to).toBe('2025-01-07');
|
||||
expect(result.data.store_address).toBe('123 Batch St');
|
||||
|
||||
// 4. Check that items from both batches are merged
|
||||
expect(result.data.items).toHaveLength(3);
|
||||
expect(result.data.items).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({ item: 'Item A' }),
|
||||
expect.objectContaining({ item: 'Item B' }),
|
||||
expect.objectContaining({ item: 'Item C' }),
|
||||
]));
|
||||
|
||||
// 5. Check that the job is not flagged for review
|
||||
expect(result.needsReview).toBe(false);
|
||||
});
|
||||
|
||||
it('should fill in missing metadata from subsequent batches', async () => {
|
||||
// Arrange
|
||||
const jobData = createMockJobData({});
|
||||
const imagePaths = [
|
||||
{ path: 'page1.jpg', mimetype: 'image/jpeg' }, { path: 'page2.jpg', mimetype: 'image/jpeg' }, { path: 'page3.jpg', mimetype: 'image/jpeg' }, { path: 'page4.jpg', mimetype: 'image/jpeg' }, { path: 'page5.jpg', mimetype: 'image/jpeg' },
|
||||
];
|
||||
|
||||
const mockAiResponseBatch1 = { store_name: null, valid_from: '2025-01-01', valid_to: '2025-01-07', store_address: null, items: [{ item: 'Item A', price_display: '$1', price_in_cents: 100, quantity: '1', category_name: 'Cat A', master_item_id: 1 }] };
|
||||
const mockAiResponseBatch2 = { store_name: 'Batch 2 Store', valid_from: '2025-01-02', valid_to: null, store_address: '456 Subsequent St', items: [{ item: 'Item C', price_display: '$3', price_in_cents: 300, quantity: '1', category_name: 'Cat C', master_item_id: 3 }] };
|
||||
|
||||
vi.mocked(mockAiService.extractCoreDataFromFlyerImage)
|
||||
.mockResolvedValueOnce(mockAiResponseBatch1)
|
||||
.mockResolvedValueOnce(mockAiResponseBatch2);
|
||||
|
||||
// Act
|
||||
const result = await service.extractAndValidateData(imagePaths, jobData, logger);
|
||||
|
||||
// Assert
|
||||
expect(result.data.store_name).toBe('Batch 2 Store'); // Filled from batch 2
|
||||
expect(result.data.valid_from).toBe('2025-01-01'); // Kept from batch 1
|
||||
expect(result.data.valid_to).toBe('2025-01-07'); // Kept from batch 1
|
||||
expect(result.data.store_address).toBe('456 Subsequent St'); // Filled from batch 2
|
||||
expect(result.data.items).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,28 +5,11 @@ import type { AIService } from './aiService.server';
|
||||
import type { PersonalizationRepository } from './db/personalization.db';
|
||||
import { AiDataValidationError } from './processingErrors';
|
||||
import type { FlyerJobData } from '../types/job-data';
|
||||
|
||||
// Helper for consistent required string validation (handles missing/null/empty)
|
||||
const requiredString = (message: string) =>
|
||||
z.preprocess((val) => val ?? '', z.string().min(1, message));
|
||||
|
||||
// --- Zod Schemas for AI Response Validation ---
|
||||
const ExtractedFlyerItemSchema = z.object({
|
||||
item: z.string().nullable(),
|
||||
price_display: z.string().nullable(),
|
||||
price_in_cents: z.number().nullable(),
|
||||
quantity: z.string().nullable(),
|
||||
category_name: z.string().nullable(),
|
||||
master_item_id: z.number().nullish(),
|
||||
});
|
||||
|
||||
export const AiFlyerDataSchema = z.object({
|
||||
store_name: z.string().nullable(),
|
||||
valid_from: z.string().nullable(),
|
||||
valid_to: z.string().nullable(),
|
||||
store_address: z.string().nullable(),
|
||||
items: z.array(ExtractedFlyerItemSchema),
|
||||
});
|
||||
import {
|
||||
AiFlyerDataSchema,
|
||||
ExtractedFlyerItemSchema,
|
||||
requiredString,
|
||||
} from '../types/ai'; // Import consolidated schemas and helper
|
||||
|
||||
export type ValidatedAiDataType = z.infer<typeof AiFlyerDataSchema>;
|
||||
|
||||
@@ -94,19 +77,64 @@ export class FlyerAiProcessor {
|
||||
jobData: FlyerJobData,
|
||||
logger: Logger,
|
||||
): Promise<AiProcessorResult> {
|
||||
logger.info(`Starting AI data extraction.`);
|
||||
logger.info(`Starting AI data extraction for ${imagePaths.length} pages.`);
|
||||
const { submitterIp, userProfileAddress } = jobData;
|
||||
const masterItems = await this.personalizationRepo.getAllMasterItems(logger);
|
||||
logger.debug(`Retrieved ${masterItems.length} master items for AI matching.`);
|
||||
|
||||
const extractedData = await this.ai.extractCoreDataFromFlyerImage(
|
||||
imagePaths,
|
||||
masterItems,
|
||||
submitterIp,
|
||||
userProfileAddress,
|
||||
logger,
|
||||
);
|
||||
// BATCHING LOGIC: Process images in chunks to avoid hitting AI payload/token limits.
|
||||
const BATCH_SIZE = 4;
|
||||
const batches = [];
|
||||
for (let i = 0; i < imagePaths.length; i += BATCH_SIZE) {
|
||||
batches.push(imagePaths.slice(i, i + BATCH_SIZE));
|
||||
}
|
||||
|
||||
return this._validateAiData(extractedData, logger);
|
||||
// Initialize container for merged data
|
||||
const mergedData: ValidatedAiDataType = {
|
||||
store_name: null,
|
||||
valid_from: null,
|
||||
valid_to: null,
|
||||
store_address: null,
|
||||
items: [],
|
||||
};
|
||||
|
||||
logger.info(`Processing ${imagePaths.length} pages in ${batches.length} batches (Batch Size: ${BATCH_SIZE}).`);
|
||||
|
||||
for (const [index, batch] of batches.entries()) {
|
||||
logger.info(`Processing batch ${index + 1}/${batches.length} (${batch.length} pages)...`);
|
||||
|
||||
// The AI service handles rate limiting internally (e.g., max 5 RPM).
|
||||
// Processing these sequentially ensures we respect that limit.
|
||||
const batchResult = await this.ai.extractCoreDataFromFlyerImage(
|
||||
batch,
|
||||
masterItems,
|
||||
submitterIp,
|
||||
userProfileAddress,
|
||||
logger,
|
||||
);
|
||||
|
||||
// MERGE LOGIC:
|
||||
// 1. Metadata (Store Name, Dates): Prioritize the first batch (usually the cover page).
|
||||
// If subsequent batches have data and the current is null, fill it in.
|
||||
if (index === 0) {
|
||||
mergedData.store_name = batchResult.store_name;
|
||||
mergedData.valid_from = batchResult.valid_from;
|
||||
mergedData.valid_to = batchResult.valid_to;
|
||||
mergedData.store_address = batchResult.store_address;
|
||||
} else {
|
||||
if (!mergedData.store_name && batchResult.store_name) mergedData.store_name = batchResult.store_name;
|
||||
if (!mergedData.valid_from && batchResult.valid_from) mergedData.valid_from = batchResult.valid_from;
|
||||
if (!mergedData.valid_to && batchResult.valid_to) mergedData.valid_to = batchResult.valid_to;
|
||||
if (!mergedData.store_address && batchResult.store_address) mergedData.store_address = batchResult.store_address;
|
||||
}
|
||||
|
||||
// 2. Items: Append all found items to the master list.
|
||||
mergedData.items.push(...batchResult.items);
|
||||
}
|
||||
|
||||
logger.info(`Batch processing complete. Total items extracted: ${mergedData.items.length}`);
|
||||
|
||||
// Validate the final merged dataset
|
||||
return this._validateAiData(mergedData, logger);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@
|
||||
import path from 'path';
|
||||
import type { z } from 'zod';
|
||||
import type { Logger } from 'pino';
|
||||
import type { FlyerInsert, FlyerItemInsert, FlyerStatus } from '../types';
|
||||
import type { AiFlyerDataSchema, AiProcessorResult } from './flyerAiProcessor.server';
|
||||
import type { FlyerInsert, FlyerItemInsert } from '../types';
|
||||
import type { AiProcessorResult } from './flyerAiProcessor.server'; // Keep this import for AiProcessorResult
|
||||
import { AiFlyerDataSchema } from '../types/ai'; // Import consolidated schema
|
||||
import { generateFlyerIcon } from '../utils/imageProcessor';
|
||||
import { TransformationError } from './processingErrors';
|
||||
|
||||
/**
|
||||
* This class is responsible for transforming the validated data from the AI service
|
||||
@@ -56,41 +58,47 @@ export class FlyerDataTransformer {
|
||||
): Promise<{ flyerData: FlyerInsert; itemsForDb: FlyerItemInsert[] }> {
|
||||
logger.info('Starting data transformation from AI output to database format.');
|
||||
|
||||
const { data: extractedData, needsReview } = aiResult;
|
||||
try {
|
||||
const { data: extractedData, needsReview } = aiResult;
|
||||
|
||||
const firstImage = imagePaths[0].path;
|
||||
const iconFileName = await generateFlyerIcon(
|
||||
firstImage,
|
||||
path.join(path.dirname(firstImage), 'icons'),
|
||||
logger,
|
||||
);
|
||||
const firstImage = imagePaths[0].path;
|
||||
const iconFileName = await generateFlyerIcon(
|
||||
firstImage,
|
||||
path.join(path.dirname(firstImage), 'icons'),
|
||||
logger,
|
||||
);
|
||||
|
||||
const itemsForDb: FlyerItemInsert[] = extractedData.items.map((item) => this._normalizeItem(item));
|
||||
const itemsForDb: FlyerItemInsert[] = extractedData.items.map((item) => this._normalizeItem(item));
|
||||
|
||||
const storeName = extractedData.store_name || 'Unknown Store (auto)';
|
||||
if (!extractedData.store_name) {
|
||||
logger.warn('AI did not return a store name. Using fallback "Unknown Store (auto)".');
|
||||
const storeName = extractedData.store_name || 'Unknown Store (auto)';
|
||||
if (!extractedData.store_name) {
|
||||
logger.warn('AI did not return a store name. Using fallback "Unknown Store (auto)".');
|
||||
}
|
||||
|
||||
const flyerData: FlyerInsert = {
|
||||
file_name: originalFileName,
|
||||
image_url: `/flyer-images/${path.basename(firstImage)}`,
|
||||
icon_url: `/flyer-images/icons/${iconFileName}`,
|
||||
checksum,
|
||||
store_name: storeName,
|
||||
valid_from: extractedData.valid_from,
|
||||
valid_to: extractedData.valid_to,
|
||||
store_address: extractedData.store_address, // The number of items is now calculated directly from the transformed data.
|
||||
item_count: itemsForDb.length,
|
||||
uploaded_by: userId,
|
||||
status: needsReview ? 'needs_review' : 'processed',
|
||||
};
|
||||
|
||||
logger.info(
|
||||
{ itemCount: itemsForDb.length, storeName: flyerData.store_name },
|
||||
'Data transformation complete.',
|
||||
);
|
||||
|
||||
return { flyerData, itemsForDb };
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Transformation process failed');
|
||||
// Wrap and rethrow with the new error class
|
||||
throw new TransformationError('Flyer Data Transformation Failed');
|
||||
}
|
||||
|
||||
const flyerData: FlyerInsert = {
|
||||
file_name: originalFileName,
|
||||
image_url: `/flyer-images/${path.basename(firstImage)}`,
|
||||
icon_url: `/flyer-images/icons/${iconFileName}`,
|
||||
checksum,
|
||||
store_name: storeName,
|
||||
valid_from: extractedData.valid_from,
|
||||
valid_to: extractedData.valid_to,
|
||||
store_address: extractedData.store_address, // The number of items is now calculated directly from the transformed data.
|
||||
item_count: itemsForDb.length,
|
||||
uploaded_by: userId,
|
||||
status: needsReview ? 'needs_review' : 'processed',
|
||||
};
|
||||
|
||||
logger.info(
|
||||
{ itemCount: itemsForDb.length, storeName: flyerData.store_name },
|
||||
'Data transformation complete.',
|
||||
);
|
||||
|
||||
return { flyerData, itemsForDb };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
// src/services/flyerProcessingService.server.test.ts
|
||||
import { describe, it, expect, vi, beforeEach, type Mocked } from 'vitest';
|
||||
import sharp from 'sharp';
|
||||
import { Job, UnrecoverableError } from 'bullmq';
|
||||
import type { Dirent } from 'node:fs';
|
||||
import type { Logger } from 'pino';
|
||||
import { z } from 'zod';
|
||||
import { AiFlyerDataSchema } from './flyerAiProcessor.server';
|
||||
import type { Flyer, FlyerInsert, FlyerItemInsert } from '../types';
|
||||
import { AiFlyerDataSchema } from '../types/ai';
|
||||
import type { FlyerInsert } from '../types';
|
||||
import type { CleanupJobData, FlyerJobData } from '../types/job-data';
|
||||
|
||||
// 1. Create hoisted mocks FIRST
|
||||
|
||||
@@ -133,6 +133,12 @@ export class FlyerProcessingService {
|
||||
return { flyerId: flyer.flyer_id };
|
||||
} catch (error) {
|
||||
logger.warn('Job failed. Temporary files will NOT be cleaned up to allow for manual inspection.');
|
||||
// Add detailed logging of the raw error object
|
||||
if (error instanceof Error) {
|
||||
logger.error({ err: error, stack: error.stack }, 'Raw error object in processJob catch block');
|
||||
} else {
|
||||
logger.error({ error }, 'Raw non-Error object in processJob catch block');
|
||||
}
|
||||
// This private method handles error reporting and re-throwing.
|
||||
await this._reportErrorAndThrow(error, job, logger, stages);
|
||||
// This line is technically unreachable because the above method always throws,
|
||||
@@ -197,6 +203,14 @@ export class FlyerProcessingService {
|
||||
logger: Logger,
|
||||
initialStages: ProcessingStage[],
|
||||
): Promise<never> {
|
||||
// Map specific error codes to their corresponding processing stage names.
|
||||
// This is more maintainable than a long if/else if chain.
|
||||
const errorCodeToStageMap = new Map<string, string>([
|
||||
['PDF_CONVERSION_FAILED', 'Preparing Inputs'],
|
||||
['UNSUPPORTED_FILE_TYPE', 'Preparing Inputs'],
|
||||
['AI_VALIDATION_FAILED', 'Extracting Data with AI'],
|
||||
['TRANSFORMATION_FAILED', 'Transforming AI Data'], // Add new mapping
|
||||
]);
|
||||
const normalizedError = error instanceof Error ? error : new Error(String(error));
|
||||
let errorPayload: { errorCode: string; message: string; [key: string]: any };
|
||||
let stagesToReport: ProcessingStage[] = [...initialStages]; // Create a mutable copy
|
||||
@@ -209,16 +223,15 @@ export class FlyerProcessingService {
|
||||
}
|
||||
|
||||
// Determine which stage failed
|
||||
let errorStageIndex = -1;
|
||||
const failedStageName = errorCodeToStageMap.get(errorPayload.errorCode);
|
||||
let errorStageIndex = failedStageName ? stagesToReport.findIndex(s => s.name === failedStageName) : -1;
|
||||
|
||||
// 1. Try to map specific error codes/messages to stages
|
||||
if (errorPayload.errorCode === 'PDF_CONVERSION_FAILED' || errorPayload.errorCode === 'UNSUPPORTED_FILE_TYPE') {
|
||||
errorStageIndex = stagesToReport.findIndex(s => s.name === 'Preparing Inputs');
|
||||
} else if (errorPayload.errorCode === 'AI_VALIDATION_FAILED') {
|
||||
errorStageIndex = stagesToReport.findIndex(s => s.name === 'Extracting Data with AI');
|
||||
} else if (errorPayload.message.includes('Icon generation failed')) {
|
||||
// Fallback for generic errors not in the map. This is less robust and relies on string matching.
|
||||
// A future improvement would be to wrap these in specific FlyerProcessingError subclasses.
|
||||
if (errorStageIndex === -1 && errorPayload.message.includes('Icon generation failed')) {
|
||||
errorStageIndex = stagesToReport.findIndex(s => s.name === 'Transforming AI Data');
|
||||
} else if (errorPayload.message.includes('Database transaction failed')) {
|
||||
}
|
||||
if (errorStageIndex === -1 && errorPayload.message.includes('Database transaction failed')) {
|
||||
errorStageIndex = stagesToReport.findIndex(s => s.name === 'Saving to Database');
|
||||
}
|
||||
|
||||
@@ -254,24 +267,16 @@ export class FlyerProcessingService {
|
||||
|
||||
// Logging logic
|
||||
if (normalizedError instanceof FlyerProcessingError) {
|
||||
const logDetails: Record<string, any> = { err: normalizedError };
|
||||
// Simplify log object creation
|
||||
const logDetails: Record<string, any> = { ...errorPayload, err: normalizedError };
|
||||
|
||||
if (normalizedError instanceof AiDataValidationError) {
|
||||
logDetails.validationErrors = normalizedError.validationErrors;
|
||||
logDetails.rawData = normalizedError.rawData;
|
||||
}
|
||||
// Also include stderr for PdfConversionError in logs
|
||||
if (normalizedError instanceof PdfConversionError) {
|
||||
logDetails.stderr = normalizedError.stderr;
|
||||
}
|
||||
// Include the errorPayload details in the log, but avoid duplicating err, validationErrors, rawData
|
||||
Object.assign(logDetails, errorPayload);
|
||||
// Remove the duplicated err property if it was assigned by Object.assign
|
||||
if ('err' in logDetails && logDetails.err === normalizedError) {
|
||||
// This check prevents accidental deletion if 'err' was a legitimate property of errorPayload
|
||||
delete logDetails.err;
|
||||
}
|
||||
// Ensure the original error object is always passed as 'err' for consistency in logging
|
||||
logDetails.err = normalizedError;
|
||||
|
||||
logger.error(logDetails, `A known processing error occurred: ${normalizedError.name}`);
|
||||
} else {
|
||||
|
||||
@@ -62,6 +62,18 @@ export class AiDataValidationError extends FlyerProcessingError {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Error thrown when a transformation step fails.
|
||||
*/
|
||||
export class TransformationError extends FlyerProcessingError {
|
||||
constructor(message: string) {
|
||||
super(
|
||||
message,
|
||||
'TRANSFORMATION_FAILED',
|
||||
'There was a problem transforming the flyer data. Please check the input.',
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Error thrown when an image conversion fails (e.g., using sharp).
|
||||
*/
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// src/services/workers.server.ts
|
||||
import { Worker, Job, UnrecoverableError } from 'bullmq';
|
||||
import fsPromises from 'node:fs/promises';
|
||||
import { exec } from 'child_process';
|
||||
|
||||
164
src/tests/e2e/auth.e2e.test.ts
Normal file
164
src/tests/e2e/auth.e2e.test.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
// src/tests/e2e/auth.e2e.test.ts
|
||||
import { describe, it, expect, afterAll, beforeAll } from 'vitest';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
import { cleanupDb } from '../utils/cleanup';
|
||||
import { createAndLoginUser, TEST_PASSWORD } from '../utils/testHelpers';
|
||||
import type { UserProfile } from '../../types';
|
||||
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
|
||||
describe('Authentication E2E Flow', () => {
|
||||
let testUser: UserProfile;
|
||||
const createdUserIds: string[] = [];
|
||||
|
||||
beforeAll(async () => {
|
||||
// Create a user that can be used for login-related tests in this suite.
|
||||
const { user } = await createAndLoginUser({
|
||||
email: `e2e-login-user-${Date.now()}@example.com`,
|
||||
fullName: 'E2E Login User',
|
||||
// E2E tests use apiClient which doesn't need the `request` object.
|
||||
});
|
||||
testUser = user;
|
||||
createdUserIds.push(user.user.user_id);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (createdUserIds.length > 0) {
|
||||
await cleanupDb({ userIds: createdUserIds });
|
||||
}
|
||||
});
|
||||
|
||||
describe('Registration Flow', () => {
|
||||
it('should successfully register a new user', async () => {
|
||||
const email = `e2e-register-success-${Date.now()}@example.com`;
|
||||
const fullName = 'E2E Register User';
|
||||
|
||||
// Act
|
||||
const response = await apiClient.registerUser(email, TEST_PASSWORD, fullName);
|
||||
const data = await response.json();
|
||||
|
||||
// Assert
|
||||
expect(response.status).toBe(201);
|
||||
expect(data.message).toBe('User registered successfully!');
|
||||
expect(data.userprofile).toBeDefined();
|
||||
expect(data.userprofile.user.email).toBe(email);
|
||||
expect(data.token).toBeTypeOf('string');
|
||||
|
||||
// Add to cleanup
|
||||
createdUserIds.push(data.userprofile.user.user_id);
|
||||
});
|
||||
|
||||
it('should fail to register a user with a weak password', async () => {
|
||||
const email = `e2e-register-weakpass-${Date.now()}@example.com`;
|
||||
const weakPassword = '123';
|
||||
|
||||
// Act
|
||||
const response = await apiClient.registerUser(email, weakPassword, 'Weak Pass User');
|
||||
const errorData = await response.json();
|
||||
|
||||
// Assert
|
||||
expect(response.status).toBe(400);
|
||||
expect(errorData.errors[0].message).toContain('Password must be at least 8 characters long.');
|
||||
});
|
||||
|
||||
it('should fail to register a user with a duplicate email', async () => {
|
||||
const email = `e2e-register-duplicate-${Date.now()}@example.com`;
|
||||
|
||||
// Act 1: Register the user successfully
|
||||
const firstResponse = await apiClient.registerUser(email, TEST_PASSWORD, 'Duplicate User');
|
||||
const firstData = await firstResponse.json();
|
||||
expect(firstResponse.status).toBe(201);
|
||||
createdUserIds.push(firstData.userprofile.user.user_id); // Add for cleanup
|
||||
|
||||
// Act 2: Attempt to register the same user again
|
||||
const secondResponse = await apiClient.registerUser(email, TEST_PASSWORD, 'Duplicate User');
|
||||
const errorData = await secondResponse.json();
|
||||
|
||||
// Assert
|
||||
expect(secondResponse.status).toBe(409); // Conflict
|
||||
expect(errorData.message).toContain('A user with this email address already exists.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Login Flow', () => {
|
||||
it('should successfully log in a registered user', async () => {
|
||||
// Act: Attempt to log in with the user created in beforeAll
|
||||
const response = await apiClient.loginUser(testUser.user.email, TEST_PASSWORD, false);
|
||||
const data = await response.json();
|
||||
|
||||
// Assert
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.userprofile).toBeDefined();
|
||||
expect(data.userprofile.user.email).toBe(testUser.user.email);
|
||||
expect(data.token).toBeTypeOf('string');
|
||||
});
|
||||
|
||||
it('should fail to log in with an incorrect password', async () => {
|
||||
// Act: Attempt to log in with the wrong password
|
||||
const response = await apiClient.loginUser(testUser.user.email, 'wrong-password', false);
|
||||
const errorData = await response.json();
|
||||
|
||||
// Assert
|
||||
expect(response.status).toBe(401);
|
||||
expect(errorData.message).toBe('Incorrect email or password.');
|
||||
});
|
||||
|
||||
it('should fail to log in with a non-existent email', async () => {
|
||||
const response = await apiClient.loginUser('no-one-here@example.com', TEST_PASSWORD, false);
|
||||
const errorData = await response.json();
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(errorData.message).toBe('Incorrect email or password.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Forgot/Reset Password Flow', () => {
|
||||
it('should allow a user to reset their password and log in with the new one', async () => {
|
||||
// Arrange: Create a user to reset the password for
|
||||
const email = `e2e-reset-pass-${Date.now()}@example.com`;
|
||||
const registerResponse = await apiClient.registerUser(email, TEST_PASSWORD, 'Reset Pass User');
|
||||
const registerData = await registerResponse.json();
|
||||
expect(registerResponse.status).toBe(201);
|
||||
createdUserIds.push(registerData.userprofile.user.user_id);
|
||||
|
||||
// Act 1: Request a password reset.
|
||||
// The test environment returns the token directly in the response for E2E testing.
|
||||
const forgotResponse = await apiClient.requestPasswordReset(email);
|
||||
const forgotData = await forgotResponse.json();
|
||||
const resetToken = forgotData.token;
|
||||
|
||||
// Assert 1: Check that we received a token.
|
||||
expect(forgotResponse.status).toBe(200);
|
||||
expect(resetToken).toBeDefined();
|
||||
expect(resetToken).toBeTypeOf('string');
|
||||
|
||||
// Act 2: Use the token to set a new password.
|
||||
const newPassword = 'my-new-e2e-password-!@#$';
|
||||
const resetResponse = await apiClient.resetPassword(resetToken, newPassword);
|
||||
const resetData = await resetResponse.json();
|
||||
|
||||
// Assert 2: Check for a successful password reset message.
|
||||
expect(resetResponse.status).toBe(200);
|
||||
expect(resetData.message).toBe('Password has been reset successfully.');
|
||||
|
||||
// Act 3 & Assert 3 (Verification): Log in with the NEW password to confirm the change.
|
||||
const loginResponse = await apiClient.loginUser(email, newPassword, false);
|
||||
const loginData = await loginResponse.json();
|
||||
|
||||
expect(loginResponse.status).toBe(200);
|
||||
expect(loginData.userprofile).toBeDefined();
|
||||
expect(loginData.userprofile.user.email).toBe(email);
|
||||
});
|
||||
|
||||
it('should return a generic success message for a non-existent email to prevent enumeration', async () => {
|
||||
const nonExistentEmail = `non-existent-e2e-${Date.now()}@example.com`;
|
||||
const response = await apiClient.requestPasswordReset(nonExistentEmail);
|
||||
const data = await response.json();
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.message).toBe('If an account with that email exists, a password reset link has been sent.');
|
||||
expect(data.token).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -21,16 +21,18 @@ const request = supertest(app);
|
||||
describe('Authentication API Integration', () => {
|
||||
let testUserEmail: string;
|
||||
let testUser: UserProfile;
|
||||
const createdUserIds: string[] = [];
|
||||
|
||||
beforeAll(async () => {
|
||||
// Use a unique email for this test suite to prevent collisions with other tests.
|
||||
const email = `auth-integration-test-${Date.now()}@example.com`;
|
||||
({ user: testUser } = await createAndLoginUser({ email, fullName: 'Auth Test User', request }));
|
||||
testUserEmail = testUser.user.email;
|
||||
createdUserIds.push(testUser.user.user_id);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanupDb({ userIds: testUser ? [testUser.user.user_id] : [] });
|
||||
await cleanupDb({ userIds: createdUserIds });
|
||||
});
|
||||
|
||||
// This test migrates the logic from the old DevTestRunner.tsx component.
|
||||
@@ -83,6 +85,38 @@ describe('Authentication API Integration', () => {
|
||||
expect(errorData.message).toBe('Incorrect email or password.');
|
||||
});
|
||||
|
||||
it('should allow registration with an empty string for avatar_url and save it as null', async () => {
|
||||
// Arrange: Define user data with an empty avatar_url.
|
||||
const email = `empty-avatar-user-${Date.now()}@example.com`;
|
||||
const userData = {
|
||||
email,
|
||||
password: TEST_PASSWORD,
|
||||
full_name: 'Empty Avatar',
|
||||
avatar_url: '',
|
||||
};
|
||||
|
||||
// Act: Register the new user.
|
||||
const registerResponse = await request.post('/api/auth/register').send(userData);
|
||||
|
||||
// Assert 1: Check that the registration was successful and the returned profile is correct.
|
||||
expect(registerResponse.status).toBe(201);
|
||||
const registeredProfile = registerResponse.body.userprofile;
|
||||
const registeredToken = registerResponse.body.token;
|
||||
expect(registeredProfile.user.email).toBe(email);
|
||||
expect(registeredProfile.avatar_url).toBeNull(); // The API should return null for the avatar_url.
|
||||
|
||||
// Add the newly created user's ID to the array for cleanup in afterAll.
|
||||
createdUserIds.push(registeredProfile.user.user_id);
|
||||
|
||||
// Assert 2 (Verification): Fetch the profile using the new token to confirm the value in the DB is null.
|
||||
const profileResponse = await request
|
||||
.get('/api/users/profile')
|
||||
.set('Authorization', `Bearer ${registeredToken}`);
|
||||
|
||||
expect(profileResponse.status).toBe(200);
|
||||
expect(profileResponse.body.avatar_url).toBeNull();
|
||||
});
|
||||
|
||||
it('should successfully refresh an access token using a refresh token cookie', async () => {
|
||||
// Arrange: Log in to get a fresh, valid refresh token cookie for this specific test.
|
||||
// This ensures the test is self-contained and not affected by other tests.
|
||||
|
||||
@@ -82,6 +82,7 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
let jobStatus;
|
||||
const maxRetries = 30; // Poll for up to 90 seconds (30 * 3s)
|
||||
for (let i = 0; i < maxRetries; i++) {
|
||||
console.log(`Polling attempt ${i + 1}...`);
|
||||
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds between polls
|
||||
const statusReq = request.get(`/api/ai/jobs/${jobId}/status`);
|
||||
if (token) {
|
||||
@@ -89,6 +90,7 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
}
|
||||
const statusResponse = await statusReq;
|
||||
jobStatus = statusResponse.body;
|
||||
console.log(`Job status: ${JSON.stringify(jobStatus)}`);
|
||||
if (jobStatus.state === 'completed' || jobStatus.state === 'failed') {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ describe('Public API Routes Integration Tests', () => {
|
||||
email: userEmail,
|
||||
password: 'a-Very-Strong-Password-123!',
|
||||
fullName: 'Public Routes Test User',
|
||||
request,
|
||||
});
|
||||
testUser = createdUser;
|
||||
|
||||
|
||||
@@ -75,6 +75,32 @@ describe('User API Routes Integration Tests', () => {
|
||||
expect(refetchedProfile.full_name).toBe('Updated Test User');
|
||||
});
|
||||
|
||||
it('should allow updating the profile with an empty string for avatar_url', async () => {
|
||||
// Arrange: Define the profile updates.
|
||||
const profileUpdates = {
|
||||
full_name: 'Empty Avatar User',
|
||||
avatar_url: '',
|
||||
};
|
||||
|
||||
// Act: Call the update endpoint with the new data and the auth token.
|
||||
const response = await request
|
||||
.put('/api/users/profile')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(profileUpdates);
|
||||
const updatedProfile = response.body;
|
||||
|
||||
// Assert: Check that the returned profile reflects the changes.
|
||||
expect(response.status).toBe(200);
|
||||
expect(updatedProfile.full_name).toBe('Empty Avatar User');
|
||||
expect(updatedProfile.avatar_url).toBeNull();
|
||||
|
||||
// Also, fetch the profile again to ensure the change was persisted in the database as NULL.
|
||||
const refetchResponse = await request
|
||||
.get('/api/users/profile')
|
||||
.set('Authorization', `Bearer ${authToken}`);
|
||||
expect(refetchResponse.body.avatar_url).toBeNull();
|
||||
});
|
||||
|
||||
it('should update user preferences via PUT /api/users/profile/preferences', async () => {
|
||||
// Arrange: Define the preference updates.
|
||||
const preferenceUpdates = {
|
||||
|
||||
@@ -43,8 +43,6 @@ export const cleanupDb = async (ids: TestResourceIds) => {
|
||||
await pool.query('DELETE FROM public.user_watched_items WHERE user_id = ANY($1::uuid[])', [userIds]);
|
||||
await pool.query('DELETE FROM public.user_achievements WHERE user_id = ANY($1::uuid[])', [userIds]);
|
||||
await pool.query('DELETE FROM public.activity_log WHERE user_id = ANY($1::uuid[])', [userIds]);
|
||||
await pool.query('DELETE FROM public.user_refresh_tokens WHERE user_id = ANY($1::uuid[])', [userIds]);
|
||||
await pool.query('DELETE FROM public.password_reset_tokens WHERE user_id = ANY($1::uuid[])', [userIds]);
|
||||
}
|
||||
|
||||
// --- Stage 2: Delete parent records that other things depend on ---
|
||||
|
||||
29
src/types/ai.ts
Normal file
29
src/types/ai.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
// src/types/ai.ts
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
// Helper for consistent required string validation (handles missing/null/empty)
|
||||
// This is moved here as it's directly related to the schemas.
|
||||
export const requiredString = (message: string) =>
|
||||
z.preprocess((val) => val ?? '', z.string().min(1, message));
|
||||
|
||||
// --- Zod Schemas for AI Response Validation ---
|
||||
// These schemas define the expected structure of data returned by the AI.
|
||||
// They are used for validation and type inference across multiple services.
|
||||
|
||||
export const ExtractedFlyerItemSchema = z.object({
|
||||
item: z.string().nullable(),
|
||||
price_display: z.string().nullable(),
|
||||
price_in_cents: z.number().nullable(),
|
||||
quantity: z.string().nullable(),
|
||||
category_name: z.string().nullable(),
|
||||
master_item_id: z.number().nullish(), // .nullish() allows null or undefined
|
||||
});
|
||||
|
||||
export const AiFlyerDataSchema = z.object({
|
||||
store_name: z.string().nullable(),
|
||||
valid_from: z.string().nullable(),
|
||||
valid_to: z.string().nullable(),
|
||||
store_address: z.string().nullable(),
|
||||
items: z.array(ExtractedFlyerItemSchema),
|
||||
});
|
||||
Reference in New Issue
Block a user