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

This commit is contained in:
2025-12-05 19:23:00 -08:00
parent 57ea82a8ad
commit 7829dd52d5
5 changed files with 15 additions and 132 deletions

View File

@@ -94,7 +94,10 @@ describe('App Component', () => {
};
// Mock the response to return the admin profile
mockedApiClient.getAuthenticatedUserProfile.mockResolvedValue(new Response(JSON.stringify(mockAdminProfile)));
// mockedApiClient.getAuthenticatedUserProfile.mockResolvedValue(new Response(JSON.stringify(mockAdminProfile)));
// We need to override the mock implementation specifically for this test to return the admin profile
mockedApiClient.getAuthenticatedUserProfile.mockImplementation(() => Promise.resolve(new Response(JSON.stringify(mockAdminProfile))));
// Set the token to trigger the auth check effect
localStorage.setItem('authToken', 'fake-admin-token');

View File

@@ -6,8 +6,8 @@ import systemRouter from './system.routes';
import { exec } from 'child_process';
import { geocodeAddress } from '../services/geocodingService.server';
// FIX 3: Mock child_process simply and robustly using the async factory pattern
vi.mock('child_process', async () => ({
// FIX: Use the simple factory pattern for child_process to avoid default export issues
vi.mock('child_process', () => ({
exec: vi.fn((command, callback) => {
if (typeof callback === 'function') {
callback(null, 'PM2 OK', '');

View File

@@ -1,122 +0,0 @@
// src/routes/system.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import express from 'express';
import systemRouter from './system.routes';
import { exec } from 'child_process';
import { geocodeAddress } from '../services/geocodingService.server';
// FIX 3: Mock child_process simply and robustly using the async factory pattern
vi.mock('child_process', async () => ({
exec: vi.fn((command, callback) => {
if (typeof callback === 'function') {
callback(null, 'PM2 OK', '');
}
return { unref: () => {} };
})
}));
vi.mock('../services/geocodingService.server', () => ({
geocodeAddress: vi.fn()
}));
vi.mock('../services/logger.server', () => ({
logger: {
info: vi.fn(),
debug: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
},
}));
const app = express();
app.use(express.json());
app.use('/api/system', systemRouter);
describe('System Routes (/api/system)', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('GET /pm2-status', () => {
it('should return success: true when pm2 process is online', async () => {
const pm2OnlineOutput = `
┌─ PM2 info ────────────────┐
│ status │ online │
└───────────┴───────────┘
`;
vi.mocked(exec).mockImplementation((...args: any[]) => {
const callback = args.find(arg => typeof arg === 'function');
callback(null, pm2OnlineOutput, '');
return {} as any;
});
const response = await supertest(app).get('/api/system/pm2-status');
expect(response.status).toBe(200);
expect(response.body).toEqual({ success: true, message: 'Application is online and running under PM2.' });
});
// ... other tests (restored from previous fix attempt)
it('should return success: false when pm2 process is stopped', async () => {
const pm2StoppedOutput = `│ status │ stopped │`;
vi.mocked(exec).mockImplementation((...args: any[]) => {
const callback = args.find(arg => typeof arg === 'function');
callback(null, pm2StoppedOutput, '');
return {} as any;
});
const response = await supertest(app).get('/api/system/pm2-status');
expect(response.status).toBe(200);
expect(response.body.success).toBe(false);
});
it('should return success: false when pm2 process does not exist', async () => {
vi.mocked(exec).mockImplementation((...args: any[]) => {
const callback = args.find(arg => typeof arg === 'function');
callback(new Error('Command failed'), "[PM2][ERROR] Process doesn't exist", '');
return {} as any;
});
const response = await supertest(app).get('/api/system/pm2-status');
expect(response.status).toBe(200);
expect(response.body.success).toBe(false);
});
it('should return 500 on a generic exec error', async () => {
vi.mocked(exec).mockImplementation((...args: any[]) => {
const callback = args.find(arg => typeof arg === 'function');
callback(new Error('System error'), '', 'stderr output');
return {} as any;
});
const response = await supertest(app).get('/api/system/pm2-status');
expect(response.status).toBe(500);
});
});
describe('POST /geocode', () => {
it('should return geocoded coordinates for a valid address', async () => {
const mockCoordinates = { lat: 48.4284, lng: -123.3656 };
vi.mocked(geocodeAddress).mockResolvedValue(mockCoordinates);
const response = await supertest(app)
.post('/api/system/geocode')
.send({ address: 'Victoria, BC' });
expect(response.status).toBe(200);
expect(response.body).toEqual(mockCoordinates);
});
it('should return 404 if the address cannot be geocoded', async () => {
vi.mocked(geocodeAddress).mockResolvedValue(null);
const response = await supertest(app)
.post('/api/system/geocode')
.send({ address: 'Invalid Address' });
expect(response.status).toBe(404);
});
});
});

View File

@@ -1,4 +1,4 @@
// src/tests/setup/test-db.ts
// src/tests/setup/tests-setup-db.ts
import { Pool } from 'pg';
// This pool will automatically use the environment variables

View File

@@ -253,27 +253,29 @@ vi.mock('../../services/db/user.db', () => ({
getUserFeed: vi.fn(),
logSearchQuery: vi.fn(),
resetFailedLoginAttempts: vi.fn(),
getAddressById: vi.fn(),
upsertAddress: vi.fn(),
}));
vi.mock('../../services/db/budget.db', () => ({
getBudgetsForUser: vi.fn(),
getBudgetsForUser: vi.fn().mockResolvedValue([]),
createBudget: vi.fn(),
updateBudget: vi.fn(),
deleteBudget: vi.fn(),
getSpendingByCategory: vi.fn(),
getSpendingByCategory: vi.fn().mockResolvedValue([]),
}));
vi.mock('../../services/db/gamification.db', () => ({
getAllAchievements: vi.fn(),
getUserAchievements: vi.fn(),
getAllAchievements: vi.fn().mockResolvedValue([]),
getUserAchievements: vi.fn().mockResolvedValue([]),
awardAchievement: vi.fn(),
getLeaderboard: vi.fn(),
getLeaderboard: vi.fn().mockResolvedValue([]),
}));
vi.mock('../../services/db/notification.db', () => ({
createNotification: vi.fn(),
createBulkNotifications: vi.fn(),
getNotificationsForUser: vi.fn(),
getNotificationsForUser: vi.fn().mockResolvedValue([]),
markAllNotificationsAsRead: vi.fn(),
markNotificationAsRead: vi.fn(),
}));