diff --git a/src/long_audio.js b/src/long_audio.js index b3a36f26..83e486ba 100644 --- a/src/long_audio.js +++ b/src/long_audio.js @@ -456,6 +456,7 @@ async function runAutoSentenceWindowing({ * @param {number} [opts.timeOffset=0] * @returns {Promise<{text: string, chunks?: Array<{ text: string, timestamp: [number, number] }>, words?: Array<{ text: string, start_time: number, end_time: number, confidence?: number }>}>} */ +export { validateAudio }; export async function transcribeLongAudioWithChunks(model, audio, sampleRate = 16000, opts = {}) { validateAudio(audio); if (typeof sampleRate !== 'number' || !Number.isFinite(sampleRate) || sampleRate <= 0) { diff --git a/tests/long_audio_chunking.test.mjs b/tests/long_audio_chunking.test.mjs index 21653e86..6beb6502 100644 --- a/tests/long_audio_chunking.test.mjs +++ b/tests/long_audio_chunking.test.mjs @@ -1,6 +1,6 @@ import { describe, it, expect, vi } from 'vitest'; import { ParakeetModel } from '../src/parakeet.js'; -import { transcribeLongAudioWithChunks } from '../src/long_audio.js'; +import { transcribeLongAudioWithChunks, validateAudio } from '../src/long_audio.js'; describe('long-audio chunking helpers', () => { it('returns a simple text result when timestamps are not requested', async () => { @@ -504,3 +504,36 @@ describe('long-audio chunking helpers', () => { ]); }); }); + +describe('validateAudio', () => { + it('throws TypeError for invalid types', () => { + const invalidInputs = [ + [], + [1, 2, 3], + new Uint8Array([1, 2, 3]), + 'string', + null, + undefined, + {}, + ]; + for (const input of invalidInputs) { + expect(() => validateAudio(input)).toThrow(TypeError); + } + }); + + it('throws Error if audio contains non-finite values', () => { + expect(() => validateAudio(Float32Array.from([1, NaN, 3]))).toThrow(/finite audio samples/); + expect(() => validateAudio(Float64Array.from([1, Infinity, 3]))).toThrow(/finite audio samples/); + expect(() => validateAudio(Float32Array.from([-Infinity]))).toThrow(/finite audio samples/); + }); + + it('passes for empty typed arrays', () => { + expect(() => validateAudio(new Float32Array(0))).not.toThrow(); + expect(() => validateAudio(new Float64Array(0))).not.toThrow(); + }); + + it('passes for valid finite typed arrays', () => { + expect(() => validateAudio(Float32Array.from([1.5, -2.5, 0]))).not.toThrow(); + expect(() => validateAudio(Float64Array.from([1.5, -2.5, 0]))).not.toThrow(); + }); +});