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

This commit is contained in:
2025-11-28 19:15:28 -08:00
parent d680c15e68
commit 84c6902bd8
3 changed files with 19 additions and 13 deletions

View File

@@ -64,14 +64,14 @@ describe('Public Routes (/api)', () => {
describe('GET /health/storage', () => {
it('should return 200 OK if storage is writable', async () => {
mockedFs.access.mockResolvedValue(undefined);
(mockedFs.access as Mocked<any>).mockResolvedValue(undefined);
const response = await supertest(app).get('/api/health/storage');
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
});
it('should return 500 if storage is not accessible', async () => {
mockedFs.access.mockRejectedValue(new Error('Permission denied'));
(mockedFs.access as Mocked<any>).mockRejectedValue(new Error('Permission denied'));
const response = await supertest(app).get('/api/health/storage');
expect(response.status).toBe(500);
expect(response.body.success).toBe(false);

View File

@@ -1,15 +1,19 @@
// src/routes/system.test.ts
import { describe, it, expect, vi, beforeEach, type Mocked } from 'vitest';
import { describe, it, expect, vi, beforeEach, type Mocked, type Mock } from 'vitest';
import supertest from 'supertest';
import express from 'express';
import type { ExecException } from 'child_process';
import systemRouter from './system';
import { exec } from 'child_process';
// Mock the child_process module to control the behavior of `exec`
vi.mock('child_process');
// Define a mock function at the module level. This will be our controlled version of `exec`.
const mockedExec = vi.fn();
// Mock the child_process module. We explicitly define its exports.
vi.mock('child_process', () => ({
exec: mockedExec,
}));
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: {
info: vi.fn(),
@@ -25,9 +29,6 @@ app.use(express.json());
app.use('/api/system', systemRouter);
describe('System Routes (/api/system)', () => {
// Cast the mocked exec to vi.Mock to access mock methods like mockImplementation
const mockedExec = exec as Mocked<typeof exec>;
beforeEach(() => {
vi.clearAllMocks();
});
@@ -42,7 +43,7 @@ describe('System Routes (/api/system)', () => {
└───────────┴───────────┘
`;
// The `exec` callback receives (error, stdout, stderr). For success, error is null.
(mockedExec as any).mockImplementation((
mockedExec.mockImplementation((
command: string,
callback: (error: ExecException | null, stdout: string, stderr: string) => void
) => {
@@ -63,7 +64,7 @@ describe('System Routes (/api/system)', () => {
const pm2StoppedOutput = `
│ status │ stopped │
`;
(mockedExec as any).mockImplementation((
mockedExec.mockImplementation((
command: string,
callback: (error: ExecException | null, stdout: string, stderr: string) => void
) => {
@@ -82,7 +83,7 @@ describe('System Routes (/api/system)', () => {
it('should return success: false when pm2 process does not exist', async () => {
// Arrange: Simulate the error and stdout when a process is not found.
const processNotFoundOutput = "[PM2][ERROR] Process or Namespace flyer-crawler-api doesn't exist";
(mockedExec as any).mockImplementation((
mockedExec.mockImplementation((
command: string,
callback: (error: ExecException | null, stdout: string, stderr: string) => void
) => {
@@ -100,7 +101,7 @@ describe('System Routes (/api/system)', () => {
it('should return 500 on a generic exec error', async () => {
// Arrange: Simulate a generic failure of the `exec` command.
(mockedExec as any).mockImplementation((
mockedExec.mockImplementation((
command: string,
callback: (error: ExecException | null, stdout: string, stderr: string) => void
) => {

View File

@@ -47,6 +47,11 @@ app.use('/api/users', userRouter);
describe('User Routes (/api/users)', () => {
beforeEach(() => {
vi.clearAllMocks();
// Reset the authenticate mock to its default "unauthorized" state before each test
mockedAuthenticate.mockImplementation(
(strategy: string, options: object) => (req: Request, res: Response, next: NextFunction) => {
res.status(401).json({ message: 'Unauthorized' });
});
});
describe('when user is not authenticated', () => {