Files
MyTube/backend/src/__tests__/controllers/scanController.test.ts

58 lines
1.7 KiB
TypeScript

import { exec } from 'child_process';
import { Request, Response } from 'express';
import fs from 'fs-extra';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { scanFiles } from '../../controllers/scanController';
import * as storageService from '../../services/storageService';
vi.mock('../../services/storageService');
vi.mock('fs-extra');
vi.mock('child_process');
describe('ScanController', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let json: any;
let status: any;
beforeEach(() => {
vi.clearAllMocks();
json = vi.fn();
status = vi.fn().mockReturnValue({ json });
req = {};
res = {
json,
status,
};
});
describe('scanFiles', () => {
it('should scan files and add new videos', async () => {
(storageService.getVideos as any).mockReturnValue([]);
(fs.existsSync as any).mockReturnValue(true);
(fs.readdirSync as any).mockReturnValue(['video.mp4']);
(fs.statSync as any).mockReturnValue({
isDirectory: () => false,
birthtime: new Date(),
});
(exec as any).mockImplementation((_cmd: string, cb: (error: Error | null) => void) => cb(null));
await scanFiles(req as Request, res as Response);
expect(storageService.saveVideo).toHaveBeenCalled();
expect(status).toHaveBeenCalledWith(200);
expect(json).toHaveBeenCalledWith(expect.objectContaining({ addedCount: 1 }));
});
it('should handle errors', async () => {
(storageService.getVideos as any).mockImplementation(() => {
throw new Error('Error');
});
await scanFiles(req as Request, res as Response);
expect(status).toHaveBeenCalledWith(500);
});
});
});