| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import * as dotenv from 'dotenv';
- import * as fs from 'fs';
- import path from 'path';
- import { uploadMedia } from '../src/actions/media';
- import { mediaBucketName } from '../src/lib/config';
- import minioClient from '../src/lib/minio';
- import { cleanupMediaRecord } from './helpers/media-cleanup';
- dotenv.config({ path: path.resolve(process.cwd(), '.env') });
- async function runTest() {
- console.log('--- Environment Check ---');
- console.log('MINIO_ENDPOINT:', process.env.MINIO_ENDPOINT);
- console.log('-------------------------');
- if (!fs.existsSync('test_sample.mp4')) {
- console.error('Missing test_sample.mp4 in project root.');
- process.exit(1);
- }
- console.log('Starting end-to-end upload test...');
- console.log('1. Calling uploadMedia action...');
- let mediaId: string | null = null;
- let failed = false;
- try {
- const fileBuffer = fs.readFileSync('test_sample.mp4');
- const result = await uploadMedia({
- name: 'e2e-upload-test_sample.mp4',
- type: 'video/mp4',
- size: fileBuffer.byteLength,
- arrayBuffer: async () => fileBuffer.buffer.slice(
- fileBuffer.byteOffset,
- fileBuffer.byteOffset + fileBuffer.byteLength
- ),
- }, {
- enqueue: false,
- });
- mediaId = result.media.id;
- console.log('Action returned successfully.');
- console.log('Media ID:', result.media.id);
- console.log('Resource ID:', result.resource.id);
- console.log('\n2. Verifying database record...');
- if (result.media.status !== 'pending' || !result.media.storageKey) {
- console.error('Upload did not create the expected pending media record.');
- process.exit(1);
- }
- console.log('Database record verification passed.');
- console.log('\n3. Verifying MinIO file...');
- await minioClient.statObject(mediaBucketName, result.media.storageKey);
- console.log('MinIO file verification passed.');
- } catch (error: any) {
- console.error('\nTest failed.');
- console.error('Error Message:', error.message);
- if (error.code) console.error('Error Code:', error.code);
- failed = true;
- } finally {
- if (mediaId) {
- await cleanupMediaRecord(mediaId);
- }
- }
- process.exit(failed ? 1 : 0);
- }
- runTest();
|