tests: convert audio test cases

This commit is contained in:
Chesterkxng 2025-12-10 17:01:26 +01:00
parent ff27f8debe
commit 46d4e1ce1f

View File

@ -1,4 +1,6 @@
import { expect, describe, it, vi, beforeEach } from 'vitest'; import { expect, describe, it, vi, beforeEach } from 'vitest';
import { convertAudio } from './service';
import { InitialValuesType } from './types';
// Mock FFmpeg since it doesn't support Node.js in tests // Mock FFmpeg since it doesn't support Node.js in tests
vi.mock('@ffmpeg/ffmpeg', () => ({ vi.mock('@ffmpeg/ffmpeg', () => ({
@ -16,8 +18,6 @@ vi.mock('@ffmpeg/util', () => ({
fetchFile: vi.fn().mockResolvedValue(new Uint8Array([10, 20, 30, 40, 50])) fetchFile: vi.fn().mockResolvedValue(new Uint8Array([10, 20, 30, 40, 50]))
})); }));
import { convertAudio } from './service';
describe('convertAudio', () => { describe('convertAudio', () => {
let mockInputFile: File; let mockInputFile: File;
@ -29,17 +29,17 @@ describe('convertAudio', () => {
}); });
it('should convert to MP3 format correctly', async () => { it('should convert to MP3 format correctly', async () => {
const outputFormat = 'mp3' as const; const options: InitialValuesType = { outputFormat: 'mp3' };
const result = await convertAudio(mockInputFile, outputFormat); const result = await convertAudio(mockInputFile, options);
expect(result).toBeInstanceOf(File); expect(result).toBeInstanceOf(File);
expect(result.name).toBe('input.mp3'); // base name + outputFormat extension expect(result.name).toBe('input.mp3');
expect(result.type).toBe('audio/mpeg'); expect(result.type).toBe('audio/mpeg');
}); });
it('should convert to AAC format correctly', async () => { it('should convert to AAC format correctly', async () => {
const outputFormat = 'aac' as const; const options: InitialValuesType = { outputFormat: 'aac' };
const result = await convertAudio(mockInputFile, outputFormat); const result = await convertAudio(mockInputFile, options);
expect(result).toBeInstanceOf(File); expect(result).toBeInstanceOf(File);
expect(result.name).toBe('input.aac'); expect(result.name).toBe('input.aac');
@ -47,18 +47,49 @@ describe('convertAudio', () => {
}); });
it('should convert to WAV format correctly', async () => { it('should convert to WAV format correctly', async () => {
const outputFormat = 'wav' as const; const options: InitialValuesType = { outputFormat: 'wav' };
const result = await convertAudio(mockInputFile, outputFormat); const result = await convertAudio(mockInputFile, options);
expect(result).toBeInstanceOf(File); expect(result).toBeInstanceOf(File);
expect(result.name).toBe('input.wav'); expect(result.name).toBe('input.wav');
expect(result.type).toBe('audio/wav'); expect(result.type).toBe('audio/wav');
}); });
it('should throw error for unsupported formats', async () => { it('should convert to FLAC format correctly', async () => {
// @ts-expect-error - intentionally passing unsupported format const options: InitialValuesType = { outputFormat: 'flac' };
await expect(convertAudio(mockInputFile, 'flac')).rejects.toThrow( const result = await convertAudio(mockInputFile, options);
'Unsupported output format'
); expect(result).toBeInstanceOf(File);
expect(result.name).toBe('input.flac');
expect(result.type).toBe('audio/flac');
});
it('should convert to OGG format correctly', async () => {
const options: InitialValuesType = { outputFormat: 'ogg' };
const result = await convertAudio(mockInputFile, options);
expect(result).toBeInstanceOf(File);
expect(result.name).toBe('input.ogg');
expect(result.type).toBe('audio/ogg');
});
it('should return original file if input format matches output format', async () => {
const options: InitialValuesType = { outputFormat: 'aac' };
const result = await convertAudio(mockInputFile, options);
expect(result).toBe(mockInputFile); // Same instance
expect(result.name).toBe('input.aac');
});
it('should handle files without extensions', async () => {
const fileNoExt = new File([new Uint8Array([1, 2, 3])], 'audiofile', {
type: 'audio/aac'
});
const options: InitialValuesType = { outputFormat: 'mp3' };
const result = await convertAudio(fileNoExt, options);
expect(result).toBeInstanceOf(File);
expect(result.name).toBe('audiofile.mp3');
expect(result.type).toBe('audio/mpeg');
}); });
}); });