lootsa tests fixes
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 4m35s

This commit is contained in:
2025-12-05 15:08:59 -08:00
parent 64afc5853d
commit 5cf1e610b3
14 changed files with 810 additions and 300 deletions

View File

@@ -1,5 +1,6 @@
// src/services/db/admin.db.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mockPoolInstance } from '../../tests/setup/tests-setup-unit';
import {
getSuggestedCorrections,
approveCorrection,
@@ -16,20 +17,8 @@ import {
updateRecipeStatus,
updateReceiptStatus,
} from './admin.db';
import { getPool } from './connection.db';
import type { SuggestedCorrection } from '../../types';
// Define test-local mock functions. These will be used to control the mock's behavior.
const mockQuery = vi.fn();
// Mock the entire connection module.
vi.mock('./connection', () => ({
// The mock factory for getPool returns an object that uses our test-local mockQuery.
getPool: () => ({
query: mockQuery,
}),
}));
// Mock the logger to prevent console output during tests
vi.mock('../logger', () => ({
logger: {
@@ -42,8 +31,7 @@ vi.mock('../logger', () => ({
describe('Admin DB Service', () => {
beforeEach(() => {
// FIX: Reset mocks
mockQuery.mockReset();
// Reset the global mock's call history before each test.
vi.clearAllMocks();
});
@@ -52,30 +40,30 @@ describe('Admin DB Service', () => {
const mockCorrections: SuggestedCorrection[] = [
{ suggested_correction_id: 1, flyer_item_id: 101, user_id: 'user-1', correction_type: 'WRONG_PRICE', suggested_value: '250', status: 'pending', created_at: new Date().toISOString() },
];
mockQuery.mockResolvedValue({ rows: mockCorrections });
mockPoolInstance.query.mockResolvedValue({ rows: mockCorrections });
const result = await getSuggestedCorrections();
expect(getPool().query).toHaveBeenCalledWith(expect.stringContaining("FROM public.suggested_corrections sc"));
expect(mockPoolInstance.query).toHaveBeenCalledWith(expect.stringContaining("FROM public.suggested_corrections sc"));
expect(result).toEqual(mockCorrections);
});
});
describe('approveCorrection', () => {
it('should call the approve_correction database function', async () => {
mockQuery.mockResolvedValue({ rows: [] }); // Mock the function call
mockPoolInstance.query.mockResolvedValue({ rows: [] }); // Mock the function call
await approveCorrection(123);
expect(getPool().query).toHaveBeenCalledWith('SELECT public.approve_correction($1)', [123]);
expect(mockPoolInstance.query).toHaveBeenCalledWith('SELECT public.approve_correction($1)', [123]);
});
});
describe('rejectCorrection', () => {
it('should update the correction status to rejected', async () => {
mockQuery.mockResolvedValue({ rowCount: 1 });
mockPoolInstance.query.mockResolvedValue({ rowCount: 1 });
await rejectCorrection(123);
expect(getPool().query).toHaveBeenCalledWith(
expect(mockPoolInstance.query).toHaveBeenCalledWith(
expect.stringContaining("UPDATE public.suggested_corrections SET status = 'rejected'"),
[123]
);
@@ -85,11 +73,11 @@ describe('Admin DB Service', () => {
describe('updateSuggestedCorrection', () => {
it('should update the suggested value and return the updated correction', async () => {
const mockCorrection: SuggestedCorrection = { suggested_correction_id: 1, flyer_item_id: 101, user_id: 'user-1', correction_type: 'WRONG_PRICE', suggested_value: '300', status: 'pending', created_at: new Date().toISOString() };
mockQuery.mockResolvedValue({ rows: [mockCorrection], rowCount: 1 });
mockPoolInstance.query.mockResolvedValue({ rows: [mockCorrection], rowCount: 1 });
const result = await updateSuggestedCorrection(1, '300');
expect(getPool().query).toHaveBeenCalledWith(
expect(mockPoolInstance.query).toHaveBeenCalledWith(
expect.stringContaining("UPDATE public.suggested_corrections SET suggested_value = $1"),
['300', 1]
);
@@ -100,7 +88,7 @@ describe('Admin DB Service', () => {
describe('getApplicationStats', () => {
it('should execute 5 parallel count queries and return the aggregated stats', async () => {
// Mock responses for each of the 5 parallel queries
mockQuery
mockPoolInstance.query
.mockResolvedValueOnce({ rows: [{ count: '10' }] }) // flyerCount
.mockResolvedValueOnce({ rows: [{ count: '20' }] }) // userCount
.mockResolvedValueOnce({ rows: [{ count: '300' }] }) // flyerItemCount
@@ -109,7 +97,7 @@ describe('Admin DB Service', () => {
const stats = await getApplicationStats();
expect(getPool().query).toHaveBeenCalledTimes(5);
expect(mockPoolInstance.query).toHaveBeenCalledTimes(5);
expect(stats).toEqual({
flyerCount: 10,
userCount: 20,
@@ -123,22 +111,22 @@ describe('Admin DB Service', () => {
describe('getDailyStatsForLast30Days', () => {
it('should execute the correct query to get daily stats', async () => {
const mockStats = [{ date: '2023-01-01', new_users: 5, new_flyers: 2 }];
mockQuery.mockResolvedValue({ rows: mockStats });
mockPoolInstance.query.mockResolvedValue({ rows: mockStats });
const result = await getDailyStatsForLast30Days();
expect(getPool().query).toHaveBeenCalledWith(expect.stringContaining("WITH date_series AS"));
expect(mockPoolInstance.query).toHaveBeenCalledWith(expect.stringContaining("WITH date_series AS"));
expect(result).toEqual(mockStats);
});
});
describe('logActivity', () => {
it('should insert a new activity log entry', async () => {
mockQuery.mockResolvedValue({ rows: [] });
mockPoolInstance.query.mockResolvedValue({ rows: [] });
const logData = { userId: 'user-123', action: 'test_action', displayText: 'Test activity' };
await logActivity(logData);
expect(getPool().query).toHaveBeenCalledWith(
expect(mockPoolInstance.query).toHaveBeenCalledWith(
expect.stringContaining("INSERT INTO public.activity_log"),
[logData.userId, logData.action, logData.displayText, null, null]
);
@@ -147,47 +135,47 @@ describe('Admin DB Service', () => {
describe('getMostFrequentSaleItems', () => {
it('should call the correct database function', async () => {
mockQuery.mockResolvedValue({ rows: [] });
mockPoolInstance.query.mockResolvedValue({ rows: [] });
await getMostFrequentSaleItems(30, 10);
expect(getPool().query).toHaveBeenCalledWith(expect.stringContaining('FROM public.flyer_items fi'), [30, 10]);
expect(mockPoolInstance.query).toHaveBeenCalledWith(expect.stringContaining('FROM public.flyer_items fi'), [30, 10]);
});
});
describe('updateRecipeCommentStatus', () => {
it('should update the comment status and return the updated comment', async () => {
const mockComment = { comment_id: 1, status: 'hidden' };
mockQuery.mockResolvedValue({ rows: [mockComment], rowCount: 1 });
mockPoolInstance.query.mockResolvedValue({ rows: [mockComment], rowCount: 1 });
const result = await updateRecipeCommentStatus(1, 'hidden');
expect(getPool().query).toHaveBeenCalledWith(expect.stringContaining('UPDATE public.recipe_comments'), ['hidden', 1]);
expect(mockPoolInstance.query).toHaveBeenCalledWith(expect.stringContaining('UPDATE public.recipe_comments'), ['hidden', 1]);
expect(result).toEqual(mockComment);
});
});
describe('getUnmatchedFlyerItems', () => {
it('should execute the correct query to get unmatched items', async () => {
mockQuery.mockResolvedValue({ rows: [] });
mockPoolInstance.query.mockResolvedValue({ rows: [] });
await getUnmatchedFlyerItems();
expect(getPool().query).toHaveBeenCalledWith(expect.stringContaining('FROM public.unmatched_flyer_items ufi'));
expect(mockPoolInstance.query).toHaveBeenCalledWith(expect.stringContaining('FROM public.unmatched_flyer_items ufi'));
});
});
describe('updateRecipeStatus', () => {
it('should update the recipe status and return the updated recipe', async () => {
const mockRecipe = { recipe_id: 1, status: 'public' };
mockQuery.mockResolvedValue({ rows: [mockRecipe], rowCount: 1 });
mockPoolInstance.query.mockResolvedValue({ rows: [mockRecipe], rowCount: 1 });
const result = await updateRecipeStatus(1, 'public');
expect(getPool().query).toHaveBeenCalledWith(expect.stringContaining('UPDATE public.recipes'), ['public', 1]);
expect(mockPoolInstance.query).toHaveBeenCalledWith(expect.stringContaining('UPDATE public.recipes'), ['public', 1]);
expect(result).toEqual(mockRecipe);
});
});
describe('incrementFailedLoginAttempts', () => {
it('should execute an UPDATE query to increment failed attempts', async () => {
mockQuery.mockResolvedValue({ rows: [] });
mockPoolInstance.query.mockResolvedValue({ rows: [] });
await incrementFailedLoginAttempts('user-123');
// Fix: Use regex to match query with variable whitespace
expect(getPool().query).toHaveBeenCalledWith(
expect(mockPoolInstance.query).toHaveBeenCalledWith(
expect.stringMatching(/UPDATE\s+public\.users\s+SET\s+failed_login_attempts\s*=\s*failed_login_attempts\s*\+\s*1/),
['user-123']
);
@@ -196,18 +184,18 @@ describe('Admin DB Service', () => {
describe('updateBrandLogo', () => {
it('should execute an UPDATE query for the brand logo', async () => {
mockQuery.mockResolvedValue({ rows: [] });
mockPoolInstance.query.mockResolvedValue({ rows: [] });
await updateBrandLogo(1, '/logo.png');
expect(getPool().query).toHaveBeenCalledWith('UPDATE public.brands SET logo_url = $1 WHERE brand_id = $2', ['/logo.png', 1]);
expect(mockPoolInstance.query).toHaveBeenCalledWith('UPDATE public.brands SET logo_url = $1 WHERE brand_id = $2', ['/logo.png', 1]);
});
});
describe('updateReceiptStatus', () => {
it('should update the receipt status and return the updated receipt', async () => {
const mockReceipt = { receipt_id: 1, status: 'completed' };
mockQuery.mockResolvedValue({ rows: [mockReceipt], rowCount: 1 });
mockPoolInstance.query.mockResolvedValue({ rows: [mockReceipt], rowCount: 1 });
const result = await updateReceiptStatus(1, 'completed');
expect(getPool().query).toHaveBeenCalledWith(expect.stringContaining('UPDATE public.receipts'), ['completed', 1]);
expect(mockPoolInstance.query).toHaveBeenCalledWith(expect.stringContaining('UPDATE public.receipts'), ['completed', 1]);
expect(result).toEqual(mockReceipt);
});
});