Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18c1161587 | ||
| 0010396780 | |||
|
|
d4557e13fb | ||
| 3e41130c69 | |||
|
|
d9034563d6 | ||
| 5836a75157 | |||
|
|
790008ae0d | ||
|
|
b5b91eb968 | ||
| 38eb810e7a | |||
|
|
458588a6e7 | ||
| 0b4113417f |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.6.4",
|
||||
"version": "0.7.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.6.4",
|
||||
"version": "0.7.3",
|
||||
"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.4",
|
||||
"version": "0.7.3",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "concurrently \"npm:start:dev\" \"vite\"",
|
||||
|
||||
@@ -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,8 +2,9 @@
|
||||
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';
|
||||
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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