Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c25b79251 | ||
| 0b0fa8294d | |||
|
|
f49f3a75fb | ||
| 8f14044ae6 | |||
|
|
55e1e425f4 | ||
| 68b16ad2e8 | |||
|
|
6a28934692 | ||
| 78c4a5fee6 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.9.41",
|
||||
"version": "0.9.45",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.9.41",
|
||||
"version": "0.9.45",
|
||||
"dependencies": {
|
||||
"@bull-board/api": "^6.14.2",
|
||||
"@bull-board/express": "^6.14.2",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "flyer-crawler",
|
||||
"private": true,
|
||||
"version": "0.9.41",
|
||||
"version": "0.9.45",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "concurrently \"npm:start:dev\" \"vite\"",
|
||||
|
||||
@@ -63,6 +63,7 @@ export class FlyerRepository {
|
||||
* @returns The newly created flyer record with its ID.
|
||||
*/
|
||||
async insertFlyer(flyerData: FlyerDbInsert, logger: Logger): Promise<Flyer> {
|
||||
console.error('[DEBUG] FlyerRepository.insertFlyer called with:', JSON.stringify(flyerData, null, 2));
|
||||
try {
|
||||
const query = `
|
||||
INSERT INTO flyers (
|
||||
|
||||
@@ -62,10 +62,13 @@ export class FlyerDataTransformer {
|
||||
baseUrl: string,
|
||||
logger: Logger,
|
||||
): { imageUrl: string; iconUrl: string } {
|
||||
console.log('[DEBUG] FlyerDataTransformer._buildUrls inputs:', { imageFileName, iconFileName, baseUrl });
|
||||
logger.debug({ imageFileName, iconFileName, baseUrl }, 'Building URLs');
|
||||
const finalBaseUrl = baseUrl || getBaseUrl(logger);
|
||||
console.log('[DEBUG] FlyerDataTransformer._buildUrls finalBaseUrl resolved to:', finalBaseUrl);
|
||||
const imageUrl = `${finalBaseUrl}/flyer-images/${imageFileName}`;
|
||||
const iconUrl = `${finalBaseUrl}/flyer-images/icons/${iconFileName}`;
|
||||
console.log('[DEBUG] FlyerDataTransformer._buildUrls constructed:', { imageUrl, iconUrl });
|
||||
logger.debug({ imageUrl, iconUrl }, 'Constructed URLs');
|
||||
return { imageUrl, iconUrl };
|
||||
}
|
||||
@@ -90,6 +93,7 @@ export class FlyerDataTransformer {
|
||||
logger: Logger,
|
||||
baseUrl: string,
|
||||
): Promise<{ flyerData: FlyerInsert; itemsForDb: FlyerItemInsert[] }> {
|
||||
console.log('[DEBUG] FlyerDataTransformer.transform called with baseUrl:', baseUrl);
|
||||
logger.info('Starting data transformation from AI output to database format.');
|
||||
|
||||
try {
|
||||
|
||||
@@ -103,6 +103,8 @@ export class FlyerProcessingService {
|
||||
// The main processed image path is already in `allFilePaths` via `createdImagePaths`.
|
||||
allFilePaths.push(path.join(iconsDir, iconFileName));
|
||||
|
||||
console.log('[DEBUG] FlyerProcessingService calling transformer with:', { originalFileName: job.data.originalFileName, imageFileName, iconFileName, checksum: job.data.checksum, baseUrl: job.data.baseUrl });
|
||||
|
||||
const { flyerData, itemsForDb } = await this.transformer.transform(
|
||||
aiResult,
|
||||
job.data.originalFileName,
|
||||
|
||||
@@ -15,6 +15,8 @@ import { cleanupFiles } from '../utils/cleanupFiles';
|
||||
import piexif from 'piexifjs';
|
||||
import exifParser from 'exif-parser';
|
||||
import sharp from 'sharp';
|
||||
// FIX: Import the singleton instance directly to spy on it
|
||||
import { aiService } from '../../services/aiService.server';
|
||||
|
||||
|
||||
/**
|
||||
@@ -25,16 +27,8 @@ const { mockExtractCoreData } = vi.hoisted(() => ({
|
||||
mockExtractCoreData: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock the AI service to prevent real API calls during integration tests.
|
||||
// This is crucial for making the tests reliable and fast. We don't want to
|
||||
// depend on the external Gemini API.
|
||||
vi.mock('../../services/aiService.server', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../../services/aiService.server')>();
|
||||
// To preserve the class instance methods of `aiService`, we must modify the
|
||||
// instance directly rather than creating a new plain object with spread syntax.
|
||||
actual.aiService.extractCoreDataFromFlyerImage = mockExtractCoreData;
|
||||
return actual;
|
||||
});
|
||||
// REMOVED: vi.mock('../../services/aiService.server', ...)
|
||||
// The previous mock was not effectively intercepting the singleton instance used by the worker.
|
||||
|
||||
// Mock the main DB service to allow for simulating transaction failures.
|
||||
// By default, it will use the real implementation.
|
||||
@@ -57,6 +51,11 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
// for the database, satisfying the 'url_check' constraint.
|
||||
// IMPORTANT: This must run BEFORE the app is imported so workers inherit the env var.
|
||||
vi.stubEnv('FRONTEND_URL', 'https://example.com');
|
||||
console.log('[TEST SETUP] FRONTEND_URL stubbed to:', process.env.FRONTEND_URL);
|
||||
|
||||
// FIX: Spy on the actual singleton instance. This ensures that when the worker
|
||||
// imports 'aiService', it gets the instance we are controlling here.
|
||||
vi.spyOn(aiService, 'extractCoreDataFromFlyerImage').mockImplementation(mockExtractCoreData);
|
||||
|
||||
const appModule = await import('../../../server');
|
||||
const app = appModule.default;
|
||||
@@ -66,13 +65,14 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
// FIX: Reset mocks before each test to ensure isolation.
|
||||
// This prevents "happy path" mocks from leaking into error handling tests and vice versa.
|
||||
beforeEach(async () => {
|
||||
console.log('[TEST SETUP] Resetting mocks before test execution');
|
||||
// 1. Reset AI Service Mock to default success state
|
||||
mockExtractCoreData.mockReset();
|
||||
mockExtractCoreData.mockResolvedValue({
|
||||
store_name: 'Mock Store',
|
||||
valid_from: null,
|
||||
valid_to: null,
|
||||
store_address: null,
|
||||
valid_from: '2025-01-01',
|
||||
valid_to: '2025-01-07',
|
||||
store_address: '123 Mock St',
|
||||
items: [
|
||||
{
|
||||
item: 'Mocked Integration Item',
|
||||
@@ -94,6 +94,7 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
|
||||
afterAll(async () => {
|
||||
vi.unstubAllEnvs(); // Clean up env stubs
|
||||
vi.restoreAllMocks(); // Restore the AI spy
|
||||
|
||||
// Use the centralized cleanup utility.
|
||||
await cleanupDb({
|
||||
@@ -110,6 +111,7 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
* It uploads a file, polls for completion, and verifies the result in the database.
|
||||
*/
|
||||
const runBackgroundProcessingTest = async (user?: UserProfile, token?: string) => {
|
||||
console.log(`[TEST START] runBackgroundProcessingTest. User: ${user?.user.email ?? 'ANONYMOUS'}`);
|
||||
// Arrange: Load a mock flyer PDF.
|
||||
const imagePath = path.resolve(__dirname, '../assets/test-flyer-image.jpg');
|
||||
const imageBuffer = await fs.readFile(imagePath);
|
||||
@@ -119,6 +121,7 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
const uniqueFileName = `test-flyer-image-${Date.now()}.jpg`;
|
||||
const mockImageFile = new File([new Uint8Array(uniqueContent)], uniqueFileName, { type: 'image/jpeg' });
|
||||
const checksum = await generateFileChecksum(mockImageFile);
|
||||
console.log('[TEST DATA] Generated checksum for test:', checksum);
|
||||
|
||||
// Track created files for cleanup
|
||||
const uploadDir = path.resolve(__dirname, '../../../flyer-images');
|
||||
@@ -128,17 +131,22 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
createdFilePaths.push(path.join(uploadDir, 'icons', iconFileName));
|
||||
|
||||
// Act 1: Upload the file to start the background job.
|
||||
const testBaseUrl = getTestBaseUrl();
|
||||
console.log('[TEST ACTION] Uploading file with baseUrl:', testBaseUrl);
|
||||
|
||||
const uploadReq = request
|
||||
.post('/api/ai/upload-and-process')
|
||||
.field('checksum', checksum)
|
||||
// Pass the baseUrl directly in the form data to ensure the worker receives it,
|
||||
// bypassing issues with vi.stubEnv in multi-threaded test environments.
|
||||
.field('baseUrl', getTestBaseUrl())
|
||||
.field('baseUrl', testBaseUrl)
|
||||
.attach('flyerFile', uniqueContent, uniqueFileName);
|
||||
if (token) {
|
||||
uploadReq.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
const uploadResponse = await uploadReq;
|
||||
console.log('[TEST RESPONSE] Upload status:', uploadResponse.status);
|
||||
console.log('[TEST RESPONSE] Upload body:', JSON.stringify(uploadResponse.body));
|
||||
const { jobId } = uploadResponse.body;
|
||||
|
||||
// Assert 1: Check that a job ID was returned.
|
||||
@@ -152,6 +160,7 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
statusReq.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
const statusResponse = await statusReq;
|
||||
console.log(`[TEST POLL] Job ${jobId} current state:`, statusResponse.body?.state);
|
||||
return statusResponse.body;
|
||||
},
|
||||
(status) => status.state === 'completed' || status.state === 'failed',
|
||||
@@ -385,6 +394,7 @@ it(
|
||||
async () => {
|
||||
// Arrange: Mock the AI service to throw an error for this specific test.
|
||||
const aiError = new Error('AI model failed to extract data.');
|
||||
// Update the spy implementation to reject
|
||||
mockExtractCoreData.mockRejectedValue(aiError);
|
||||
|
||||
// Arrange: Prepare a unique flyer file for upload.
|
||||
|
||||
@@ -22,6 +22,11 @@ const getPool = () => {
|
||||
* and then rebuilds it from the master rollup script.
|
||||
*/
|
||||
export async function setup() {
|
||||
// Ensure we are in the correct environment for these tests.
|
||||
process.env.NODE_ENV = 'test';
|
||||
// Set the FRONTEND_URL globally for any scripts or processes spawned here.
|
||||
process.env.FRONTEND_URL = process.env.FRONTEND_URL || 'https://example.com';
|
||||
|
||||
// --- START DEBUG LOGGING ---
|
||||
// Log the database connection details being used by the Vitest GLOBAL SETUP process.
|
||||
// These variables are inherited from the CI environment.
|
||||
|
||||
@@ -43,6 +43,7 @@ export async function processAndSaveImage(
|
||||
.toFile(outputPath);
|
||||
|
||||
logger.info(`Successfully processed image and saved to ${outputPath}`);
|
||||
console.log('[DEBUG] processAndSaveImage returning:', outputFileName);
|
||||
return outputFileName;
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
@@ -84,6 +85,7 @@ export async function generateFlyerIcon(
|
||||
.toFile(outputPath);
|
||||
|
||||
logger.info(`Successfully generated icon: ${outputPath}`);
|
||||
console.log('[DEBUG] generateFlyerIcon returning:', iconFileName);
|
||||
return iconFileName;
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
|
||||
Reference in New Issue
Block a user