testing routes
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 3m35s

This commit is contained in:
2025-11-28 20:19:57 -08:00
parent 79c57fd8b0
commit c0c1e2c5f1
3 changed files with 26 additions and 12 deletions

View File

@@ -6,12 +6,17 @@ import path from 'node:path';
import fs from 'node:fs/promises';
import aiRouter from './ai';
import * as aiService from '../services/aiService.server';
import * as db from '../services/db';
import { UserProfile } from '../types';
// Mock the AI service to avoid making real AI calls
vi.mock('../services/aiService.server');
const mockedAiService = aiService as Mocked<typeof aiService>;
// Mock the entire db service, as the /flyers/process route uses it.
vi.mock('../services/db');
const mockedDb = db as Mocked<typeof db>;
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: {

View File

@@ -1,7 +1,7 @@
// src/routes/auth.test.ts
import { describe, it, expect, vi, beforeEach, type Mocked } from 'vitest';
import supertest from 'supertest';
import express from 'express';
import express, { type Request, type Response, type NextFunction } from 'express';
import authRouter from './auth';
import * as bcrypt from 'bcrypt';
import passport from './passport';
@@ -150,10 +150,11 @@ describe('Auth Routes (/api/auth)', () => {
// 1. Simulate passport successfully finding a user.
const mockUser = { user_id: 'user-123', email: 'test@test.com' };
mockedAuthenticate.mockImplementation(
(strategy, options, callback) => (req, res, next) => {
// Call the route's custom callback with the mock user.
callback(null, mockUser, null);
}
(strategy: string, options: object, callback: (err: Error | null, user: object | false, info: object | null) => void) =>
(req: Request, res: Response, next: NextFunction) => {
// Call the route's custom callback with the mock user.
callback(null, mockUser, null);
}
);
// 2. Mock the database calls that happen after successful authentication.
mockedDb.saveRefreshToken.mockResolvedValue();
@@ -175,10 +176,11 @@ describe('Auth Routes (/api/auth)', () => {
it('should reject login with incorrect credentials', async () => {
// Arrange: Simulate passport failing to find a user.
mockedAuthenticate.mockImplementation(
(strategy, options, callback) => (req, res, next) => {
// Call the callback with `false` for the user and an info message.
callback(null, false, { message: 'Incorrect email or password.' });
}
(strategy: string, options: object, callback: (err: Error | null, user: object | false, info: object | null) => void) =>
(req: Request, res: Response, next: NextFunction) => {
// Call the callback with `false` for the user and an info message.
callback(null, false, { message: 'Incorrect email or password.' });
}
);
// Act
@@ -194,9 +196,10 @@ describe('Auth Routes (/api/auth)', () => {
it('should reject login for a locked account', async () => {
// Arrange: Simulate passport finding a locked account.
mockedAuthenticate.mockImplementation(
(strategy, options, callback) => (req, res, next) => {
callback(null, false, { message: 'Account is temporarily locked.' });
}
(strategy: string, options: object, callback: (err: Error | null, user: object | false, info: object | null) => void) =>
(req: Request, res: Response, next: NextFunction) => {
callback(null, false, { message: 'Account is temporarily locked.' });
}
);
const response = await supertest(app).post('/api/auth/login').send(loginCredentials);

View File

@@ -95,6 +95,7 @@ describe('Public Routes (/api)', () => {
{ flyer_id: 2, store_name: 'Store B' },
];
mockedDb.getFlyers.mockResolvedValue(mockFlyers as any);
(mockedDb.getFlyers as Mock).mockResolvedValue(mockFlyers);
// Act: Make the request
const response = await supertest(app).get('/api/flyers');
@@ -121,6 +122,7 @@ describe('Public Routes (/api)', () => {
it('should return a list of master items', async () => {
const mockItems = [{ master_grocery_item_id: 1, name: 'Milk' }];
mockedDb.getAllMasterItems.mockResolvedValue(mockItems as any);
(mockedDb.getAllMasterItems as Mock).mockResolvedValue(mockItems);
const response = await supertest(app).get('/api/master-items');
expect(response.status).toBe(200);
expect(response.body).toEqual(mockItems);
@@ -131,6 +133,7 @@ describe('Public Routes (/api)', () => {
it('should return items for a specific flyer', async () => {
const mockFlyerItems = [{ flyer_item_id: 1, item: 'Cheese' }];
mockedDb.getFlyerItems.mockResolvedValue(mockFlyerItems as any);
(mockedDb.getFlyerItems as Mock).mockResolvedValue(mockFlyerItems);
const response = await supertest(app).get('/api/flyers/123/items');
expect(response.status).toBe(200);
expect(response.body).toEqual(mockFlyerItems);
@@ -142,6 +145,7 @@ describe('Public Routes (/api)', () => {
it('should return items for multiple flyers', async () => {
const mockFlyerItems = [{ flyer_item_id: 1, item: 'Bread' }];
mockedDb.getFlyerItemsForFlyers.mockResolvedValue(mockFlyerItems as any);
(mockedDb.getFlyerItemsForFlyers as Mock).mockResolvedValue(mockFlyerItems);
const response = await supertest(app)
.post('/api/flyer-items/batch-fetch')
.send({ flyerIds: [1, 2] });
@@ -162,6 +166,7 @@ describe('Public Routes (/api)', () => {
it('should return recipes based on sale percentage', async () => {
const mockRecipes = [{ recipe_id: 1, name: 'Pasta' }];
mockedDb.getRecipesBySalePercentage.mockResolvedValue(mockRecipes as any);
(mockedDb.getRecipesBySalePercentage as Mock).mockResolvedValue(mockRecipes);
const response = await supertest(app).get('/api/recipes/by-sale-percentage?minPercentage=75');
expect(response.status).toBe(200);
expect(response.body).toEqual(mockRecipes);
@@ -217,6 +222,7 @@ describe('Public Routes (/api)', () => {
it('should return recipes for a given ingredient and tag', async () => {
const mockRecipes = [{ recipe_id: 2, name: 'Chicken Tacos' }];
mockedDb.findRecipesByIngredientAndTag.mockResolvedValue(mockRecipes as any);
(mockedDb.findRecipesByIngredientAndTag as Mock).mockResolvedValue(mockRecipes);
const response = await supertest(app).get('/api/recipes/by-ingredient-and-tag?ingredient=chicken&tag=quick');
expect(response.status).toBe(200);
expect(response.body).toEqual(mockRecipes);