Compare commits

...

6 Commits

Author SHA1 Message Date
Gitea Actions
7a52bf499e ci: Bump version to 0.2.31 [skip ci] 2025-12-30 06:58:25 +05:00
2489ec8d2d fix unit tests
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 12m3s
2025-12-29 17:57:40 -08:00
Gitea Actions
4a4f349805 ci: Bump version to 0.2.30 [skip ci] 2025-12-30 06:19:25 +05:00
517a268307 fix unit tests
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 12m5s
2025-12-29 17:18:52 -08:00
Gitea Actions
a94b2a97b1 ci: Bump version to 0.2.29 [skip ci] 2025-12-30 05:41:58 +05:00
542cdfbb82 fix unit tests
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 12m18s
2025-12-29 16:41:32 -08:00
9 changed files with 190 additions and 154 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "flyer-crawler",
"version": "0.2.28",
"version": "0.2.31",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "flyer-crawler",
"version": "0.2.28",
"version": "0.2.31",
"dependencies": {
"@bull-board/api": "^6.14.2",
"@bull-board/express": "^6.14.2",

View File

@@ -1,7 +1,7 @@
{
"name": "flyer-crawler",
"private": true,
"version": "0.2.28",
"version": "0.2.31",
"type": "module",
"scripts": {
"dev": "concurrently \"npm:start:dev\" \"vite\"",

View File

@@ -12,11 +12,10 @@ vi.mock('util', async (importOriginal) => {
const actual = await importOriginal<typeof import('util')>();
return {
...actual,
default: actual,
promisify: (fn: Function) => {
return (...args: any[]) => {
return new Promise((resolve, reject) => {
fn(...args, (err: Error | null, stdout: unknown, stderr: unknown) => {
fn(...args, (err: Error | null, stdout: string, stderr: string) => {
if (err) {
// Attach stdout/stderr to the error object to mimic child_process.exec behavior
Object.assign(err, { stdout, stderr });
@@ -31,18 +30,15 @@ vi.mock('util', async (importOriginal) => {
};
});
// FIX: Use the simple factory pattern for child_process to avoid default export issues
vi.mock('child_process', () => {
const mockExec = vi.fn((command, callback) => {
if (typeof callback === 'function') {
callback(null, 'PM2 OK', '');
}
return { unref: () => {} };
});
// The `importOriginal` pattern is the robust way to mock built-in Node modules.
// It preserves the module's original structure, preventing "No default export" errors
// that can occur with simple factory mocks when using ESM-based test runners like Vitest.
vi.mock('child_process', async (importOriginal) => {
const actual = await importOriginal<typeof import('child_process')>();
return {
default: { exec: mockExec },
exec: mockExec,
...actual,
// We provide a basic mock function that will be implemented in each test.
exec: vi.fn(),
};
});

View File

@@ -127,12 +127,21 @@ describe('AnalyticsService', () => {
throw new Error('Processing failed');
}); // "Successfully generated..."
const promise = service.processWeeklyReportJob(job);
// Wrap the async operation that is expected to reject in a function.
// This prevents an "unhandled rejection" error by ensuring the `expect.rejects`
// is actively waiting for the promise to reject when the timers are advanced.
const testFunction = async () => {
const promise = service.processWeeklyReportJob(job);
// Advance timers to trigger the part of the code that throws.
await vi.advanceTimersByTimeAsync(30000);
// Await the promise to allow the rejection to be caught by `expect.rejects`.
await promise;
};
await vi.advanceTimersByTimeAsync(30000);
await expect(promise).rejects.toThrow('Processing failed');
// Now, assert that the entire operation rejects as expected.
await expect(testFunction()).rejects.toThrow('Processing failed');
// Verify the side effect (error logging) after the rejection is confirmed.
expect(mockLoggerInstance.error).toHaveBeenCalledWith(
expect.objectContaining({
err: expect.any(Error),

View File

@@ -1,70 +1,16 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
// Set environment variables before importing the service
process.env.JWT_SECRET = 'test-secret';
process.env.FRONTEND_URL = 'http://localhost:3000';
import { authService } from './authService';
import * as bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import crypto from 'crypto';
import { userRepo, adminRepo } from './db/index.db';
import { UniqueConstraintError } from './db/errors.db';
import { logger } from './logger.server';
import { sendPasswordResetEmail } from './emailService.server';
import type { UserProfile } from '../types';
// Mock dependencies
vi.mock('bcrypt');
vi.mock('jsonwebtoken');
vi.mock('crypto', () => ({
default: {
randomBytes: vi.fn().mockReturnValue({
toString: vi.fn().mockReturnValue('mocked-random-string'),
}),
},
}));
vi.mock('./db/index.db', () => ({
userRepo: {
createUser: vi.fn(),
saveRefreshToken: vi.fn(),
findUserByEmail: vi.fn(),
createPasswordResetToken: vi.fn(),
getValidResetTokens: vi.fn(),
updateUserPassword: vi.fn(),
deleteResetToken: vi.fn(),
findUserByRefreshToken: vi.fn(),
findUserProfileById: vi.fn(),
deleteRefreshToken: vi.fn(),
},
adminRepo: {
logActivity: vi.fn(),
},
}));
vi.mock('./logger.server', () => ({
logger: {
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
},
}));
vi.mock('./emailService.server', () => ({
sendPasswordResetEmail: vi.fn(),
}));
vi.mock('./db/connection.db', () => ({
getPool: vi.fn(),
}));
vi.mock('../utils/authUtils', () => ({
validatePasswordStrength: vi.fn(),
}));
describe('AuthService', () => {
let authService: typeof import('./authService').authService;
let bcrypt: typeof import('bcrypt');
let jwt: typeof import('jsonwebtoken');
let userRepo: typeof import('./db/index.db').userRepo;
let adminRepo: typeof import('./db/index.db').adminRepo;
let logger: typeof import('./logger.server').logger;
let sendPasswordResetEmail: typeof import('./emailService.server').sendPasswordResetEmail;
let UniqueConstraintError: typeof import('./db/errors.db').UniqueConstraintError;
const reqLog = {}; // Mock request logger object
const mockUser = {
user_id: 'user-123',
@@ -76,8 +22,63 @@ describe('AuthService', () => {
role: 'user',
} as unknown as UserProfile;
beforeEach(() => {
beforeEach(async () => {
vi.clearAllMocks();
vi.resetModules();
// Set environment variables before any modules are imported
process.env.JWT_SECRET = 'test-secret';
process.env.FRONTEND_URL = 'http://localhost:3000';
// Mock all dependencies before dynamically importing the service
vi.mock('bcrypt');
vi.mock('jsonwebtoken', () => ({
sign: vi.fn(),
verify: vi.fn(),
}));
vi.mock('crypto', () => ({
default: {
randomBytes: vi.fn().mockReturnValue({
toString: vi.fn().mockReturnValue('mocked-random-string'),
}),
},
}));
vi.mock('./db/index.db', () => ({
userRepo: {
createUser: vi.fn(),
saveRefreshToken: vi.fn(),
findUserByEmail: vi.fn(),
createPasswordResetToken: vi.fn(),
getValidResetTokens: vi.fn(),
updateUserPassword: vi.fn(),
deleteResetToken: vi.fn(),
findUserByRefreshToken: vi.fn(),
findUserProfileById: vi.fn(),
deleteRefreshToken: vi.fn(),
},
adminRepo: {
logActivity: vi.fn(),
},
}));
vi.mock('./logger.server', () => ({
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() },
}));
vi.mock('./emailService.server', () => ({
sendPasswordResetEmail: vi.fn(),
}));
vi.mock('./db/connection.db', () => ({ getPool: vi.fn() }));
vi.mock('../utils/authUtils', () => ({ validatePasswordStrength: vi.fn() }));
// Dynamically import modules to get the mocked versions and the service instance
authService = (await import('./authService')).authService;
bcrypt = await import('bcrypt');
jwt = await import('jsonwebtoken');
const dbModule = await import('./db/index.db');
userRepo = dbModule.userRepo;
adminRepo = dbModule.adminRepo;
logger = (await import('./logger.server')).logger;
sendPasswordResetEmail = (await import('./emailService.server')).sendPasswordResetEmail;
UniqueConstraintError = (await import('./db/errors.db')).UniqueConstraintError;
});
describe('registerUser', () => {
@@ -227,7 +228,7 @@ describe('AuthService', () => {
});
it('should log warning and return undefined for non-existent user', async () => {
vi.mocked(userRepo.findUserByEmail).mockResolvedValue(null);
vi.mocked(userRepo.findUserByEmail).mockResolvedValue(undefined);
const result = await authService.resetPassword('unknown@example.com', reqLog);
@@ -295,7 +296,7 @@ describe('AuthService', () => {
});
it('should return null if token not found', async () => {
vi.mocked(userRepo.findUserByRefreshToken).mockResolvedValue(null);
vi.mocked(userRepo.findUserByRefreshToken).mockResolvedValue(undefined);
const result = await authService.getUserByRefreshToken('invalid-token', reqLog);
@@ -330,7 +331,7 @@ describe('AuthService', () => {
});
it('should return null if user not found', async () => {
vi.mocked(userRepo.findUserByRefreshToken).mockResolvedValue(null);
vi.mocked(userRepo.findUserByRefreshToken).mockResolvedValue(undefined);
const result = await authService.refreshAccessToken('invalid-token', reqLog);
expect(result).toBeNull();
});

View File

@@ -409,24 +409,18 @@ describe('FlyerProcessingService', () => {
mockFileHandler.prepareImageInputs.mockRejectedValue(fileTypeError);
const { logger } = await import('./logger.server');
const reportErrorSpy = vi.spyOn(service as any, '_reportErrorAndThrow');
await expect(service.processJob(job)).rejects.toThrow(UnsupportedFileTypeError);
expect(job.updateProgress).toHaveBeenCalledWith({
errorCode: 'UNSUPPORTED_FILE_TYPE',
message: 'Unsupported file type: .txt. Supported types are PDF, JPG, PNG, WEBP, HEIC, HEIF, GIF, TIFF, SVG, BMP.',
stages: [
{ name: 'Preparing Inputs', status: 'failed', critical: true, detail: 'Unsupported file type: .txt. Supported types are PDF, JPG, PNG, WEBP, HEIC, HEIF, GIF, TIFF, SVG, BMP.' },
{ name: 'Extracting Data with AI', status: 'skipped', critical: true, detail: 'Communicating with AI model...' },
{ name: 'Transforming AI Data', status: 'skipped', critical: true },
{ name: 'Saving to Database', status: 'skipped', critical: true },
],
});
expect(reportErrorSpy).toHaveBeenCalledWith(fileTypeError, job, expect.any(Object), expect.any(Array));
expect(mockCleanupQueue.add).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalledWith(
'Job failed. Temporary files will NOT be cleaned up to allow for manual inspection.',
);
});
it('should throw an error and not enqueue cleanup if icon generation fails', async () => {
it('should delegate to _reportErrorAndThrow if icon generation fails', async () => {
const job = createMockJob({});
const { logger } = await import('./logger.server');
const iconError = new Error('Icon generation failed.');
@@ -435,18 +429,11 @@ describe('FlyerProcessingService', () => {
// bubbling up from the icon generation step.
vi.spyOn(FlyerDataTransformer.prototype, 'transform').mockRejectedValue(iconError);
const reportErrorSpy = vi.spyOn(service as any, '_reportErrorAndThrow');
await expect(service.processJob(job)).rejects.toThrow('Icon generation failed.');
expect(job.updateProgress).toHaveBeenCalledWith({
errorCode: 'UNKNOWN_ERROR',
message: 'Icon generation failed.',
stages: [
{ name: 'Preparing Inputs', status: 'completed', critical: true, detail: '1 page(s) ready for AI.' },
{ name: 'Extracting Data with AI', status: 'completed', critical: true, detail: 'Communicating with AI model...' },
{ name: 'Transforming AI Data', status: 'failed', critical: true, detail: 'Icon generation failed.' },
{ name: 'Saving to Database', status: 'skipped', critical: true },
],
}); // This was a duplicate, fixed.
expect(reportErrorSpy).toHaveBeenCalledWith(iconError, job, expect.any(Object), expect.any(Array));
expect(mockCleanupQueue.add).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalledWith(
'Job failed. Temporary files will NOT be cleaned up to allow for manual inspection.',
@@ -454,8 +441,58 @@ describe('FlyerProcessingService', () => {
});
});
describe('_reportErrorAndThrow (private method)', () => {
it('should update progress and throw UnrecoverableError for quota messages', async () => {
describe('_reportErrorAndThrow (Error Reporting Logic)', () => {
it('should update progress with a generic error and re-throw', async () => {
const { logger } = await import('./logger.server');
const job = createMockJob({});
const genericError = new Error('A standard failure');
const initialStages = [
{ name: 'Stage 1', status: 'completed', critical: true, detail: 'Done' },
{ name: 'Stage 2', status: 'in-progress', critical: true, detail: 'Working...' },
{ name: 'Stage 3', status: 'pending', critical: true, detail: 'Waiting...' },
];
const privateMethod = (service as any)._reportErrorAndThrow;
await expect(privateMethod(genericError, job, logger, initialStages)).rejects.toThrow(genericError);
expect(job.updateProgress).toHaveBeenCalledWith({
errorCode: 'UNKNOWN_ERROR',
message: 'A standard failure',
stages: [
{ name: 'Stage 1', status: 'completed', critical: true, detail: 'Done' },
{ name: 'Stage 2', status: 'failed', critical: true, detail: 'A standard failure' },
{ name: 'Stage 3', status: 'skipped', critical: true },
],
});
});
it('should use toErrorPayload for FlyerProcessingError instances', async () => {
const { logger } = await import('./logger.server');
const job = createMockJob({});
const validationError = new AiDataValidationError(
'Validation failed',
{ foo: 'bar' },
{ raw: 'data' },
);
const initialStages = [
{ name: 'Extracting Data with AI', status: 'in-progress', critical: true, detail: '...' },
];
const privateMethod = (service as any)._reportErrorAndThrow;
await expect(privateMethod(validationError, job, logger, initialStages)).rejects.toThrow(validationError);
expect(job.updateProgress).toHaveBeenCalledWith({
errorCode: 'AI_VALIDATION_FAILED',
message: "The AI couldn't read the flyer's format. Please try a clearer image or a different flyer.",
validationErrors: { foo: 'bar' },
rawData: { raw: 'data' },
stages: [
{ name: 'Extracting Data with AI', status: 'failed', critical: true, detail: "The AI couldn't read the flyer's format. Please try a clearer image or a different flyer." },
],
});
});
it('should throw UnrecoverableError for quota messages', async () => {
const { logger } = await import('./logger.server');
const job = createMockJob({});
const quotaError = new Error('RESOURCE_EXHAUSTED');
@@ -472,53 +509,35 @@ describe('FlyerProcessingService', () => {
});
});
it('should use toErrorPayload for FlyerProcessingError instances', async () => {
const { logger } = await import('./logger.server');
const job = createMockJob({});
const validationError = new AiDataValidationError(
'Validation failed',
{ foo: 'bar' },
{ raw: 'data' },
);
const privateMethod = (service as any)._reportErrorAndThrow;
await expect(privateMethod(validationError, job, logger, [])).rejects.toThrow(
validationError,
);
// The payload should now come from the error's `toErrorPayload` method
expect(job.updateProgress).toHaveBeenCalledWith({
errorCode: 'AI_VALIDATION_FAILED',
message:
"The AI couldn't read the flyer's format. Please try a clearer image or a different flyer.",
validationErrors: { foo: 'bar' },
rawData: { raw: 'data' },
stages: [],
});
});
it('should update progress and re-throw standard errors', async () => {
const { logger } = await import('./logger.server');
const job = createMockJob({});
const genericError = new Error('A standard failure');
const privateMethod = (service as any)._reportErrorAndThrow;
await expect(privateMethod(genericError, job, logger, [])).rejects.toThrow(genericError);
expect(job.updateProgress).toHaveBeenCalledWith({
errorCode: 'UNKNOWN_ERROR',
message: 'A standard failure', // This was a duplicate, fixed.
stages: [],
});
});
it('should wrap and throw non-Error objects', async () => {
const { logger } = await import('./logger.server');
const job = createMockJob({});
const nonError = 'just a string error';
const privateMethod = (service as any)._reportErrorAndThrow;
await expect(privateMethod(nonError, job, logger, [])).rejects.toThrow('just a string error');
await expect(privateMethod(nonError, job, logger, [])).rejects.toThrow(
'just a string error',
);
});
it('should correctly identify the failed stage based on error code', async () => {
const { logger } = await import('./logger.server');
const job = createMockJob({});
const pdfError = new PdfConversionError('PDF failed');
const initialStages = [
{ name: 'Preparing Inputs', status: 'in-progress', critical: true, detail: '...' },
{ name: 'Extracting Data with AI', status: 'pending', critical: true, detail: '...' },
];
const privateMethod = (service as any)._reportErrorAndThrow;
await expect(privateMethod(pdfError, job, logger, initialStages)).rejects.toThrow(pdfError);
expect(job.updateProgress).toHaveBeenCalledWith(expect.objectContaining({
stages: [
{ name: 'Preparing Inputs', status: 'failed', critical: true, detail: expect.any(String) },
{ name: 'Extracting Data with AI', status: 'skipped', critical: true },
],
}));
});
});

View File

@@ -242,7 +242,10 @@ export class FlyerProcessingService {
// Mark subsequent critical stages as skipped
for (let i = errorStageIndex + 1; i < stagesToReport.length; i++) {
if (stagesToReport[i].critical) {
stagesToReport[i] = { ...stagesToReport[i], status: 'skipped' };
// When a stage is skipped, we don't need its previous 'detail' property.
// This creates a clean 'skipped' state object by removing `detail` and keeping the rest.
const { detail, ...restOfStage } = stagesToReport[i];
stagesToReport[i] = { ...restOfStage, status: 'skipped' };
}
}
}

View File

@@ -1,3 +1,4 @@
// src/services/systemService.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { exec, type ExecException } from 'child_process';
import { logger } from './logger.server';
@@ -35,10 +36,15 @@ vi.mock('util', async (importOriginal) => {
});
// Mock child_process
// Node.js built-in modules like 'child_process' are CommonJS modules.
// When mocked in an ESM context (like Vitest), they might sometimes
// be interpreted as having a default export if not explicitly handled.
// By providing `__esModule: true` and explicitly defining `exec`,
// we ensure Vitest correctly resolves the named import.
vi.mock('child_process', () => {
const mockExec = vi.fn();
return {
exec: mockExec,
__esModule: true, // Explicitly mark as an ES module
exec: vi.fn(),
};
});

View File

@@ -4,6 +4,8 @@ import { promisify } from 'util';
import { logger } from './logger.server';
const execAsync = promisify(exec);
logger.debug({ typeOfExec: typeof exec, isExecFunction: typeof exec === 'function' }, 'SystemService: Initializing execAsync');
logger.debug({ typeOfPromisify: typeof promisify, isPromisifyFunction: typeof promisify === 'function' }, 'SystemService: Initializing execAsync');
class SystemService {
async getPm2Status(): Promise<{ success: boolean; message: string }> {