Files
project-fileshare/apps/backend/test/video-processing.e2e-spec.ts

98 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-10-29 13:21:30 +06:00
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import * as request from 'supertest';
import * as cookieParser from 'cookie-parser';
import * as fs from 'fs';
import * as path from 'path';
import { AppModule } from '../src/app.module';
import { VideoProcessorService } from '../src/modules/video/video-processor.service';
describe('Video Processing & HLS Transcoding (E2E Integration Test)', () => {
let app: INestApplication;
let videoProcessor: VideoProcessorService;
let userToken: string;
let uploadedVideoId: string;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.use(cookieParser());
app.setGlobalPrefix('api/v1');
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
await app.init();
videoProcessor = app.get<VideoProcessorService>(VideoProcessorService);
// Login as User A
const loginRes = await request(app.getHttpServer())
.post('/api/v1/auth/login')
.send({ identifier: 'usera@fileshare.local', password: 'UserA@Secure2026!' })
.expect(200);
userToken = loginRes.body.accessToken;
});
afterAll(async () => {
await app.close();
});
it('uploads an MP4 video, probes metadata, extracts thumbnail and generates HLS playlist', async () => {
const videoFilePath = path.resolve(__dirname, '../../../test_video.mp4');
expect(fs.existsSync(videoFilePath)).toBe(true);
const videoBuffer = fs.readFileSync(videoFilePath);
// 1. Upload video
const uploadRes = await request(app.getHttpServer())
.post('/api/v1/files/direct-upload')
.set('Authorization', `Bearer ${userToken}`)
.attach('file', videoBuffer, 'sample_demo_clip.mp4')
.expect(201);
expect(uploadRes.body.id).toBeDefined();
expect(uploadRes.body.fileCategory).toBe('VIDEO');
uploadedVideoId = uploadRes.body.id;
// 2. Run video processing directly to verify FFmpeg and HLS generation
await videoProcessor.processVideo(uploadedVideoId);
// 3. Verify file record is updated with metadata, thumbnail and HLS key
const fileRes = await request(app.getHttpServer())
.get(`/api/v1/files/${uploadedVideoId}`)
.set('Authorization', `Bearer ${userToken}`)
.expect(200);
expect(fileRes.body.status).toBe('READY');
expect(fileRes.body.durationSeconds).toBeGreaterThan(0);
expect(fileRes.body.width).toBe(640);
expect(fileRes.body.height).toBe(360);
expect(fileRes.body.videoCodec).toBe('h264');
expect(fileRes.body.hasThumbnail).toBe(true);
expect(fileRes.body.hasHlsStream).toBe(true);
// 4. Verify thumbnail can be fetched
await request(app.getHttpServer())
.get(`/api/v1/files/${uploadedVideoId}/thumbnail`)
.set('Authorization', `Bearer ${userToken}`)
.expect(200);
// 5. Verify HLS master playlist can be fetched
const hlsRes = await request(app.getHttpServer())
.get(`/api/v1/files/${uploadedVideoId}/stream/master.m3u8`)
.set('Authorization', `Bearer ${userToken}`)
.expect(200);
expect(hlsRes.text).toContain('#EXTM3U');
expect(hlsRes.text).toContain('segment_');
// 6. Verify HLS media chunk segment can be fetched
await request(app.getHttpServer())
.get(`/api/v1/files/${uploadedVideoId}/stream/segment_000.ts`)
.set('Authorization', `Bearer ${userToken}`)
.expect(200);
}, 45000);
});