|
| 1 | +// SPDX-FileCopyrightText: 2025 LiveKit, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +import { ReadableStream } from 'node:stream/web'; |
| 5 | +import { describe, expect, it } from 'vitest'; |
| 6 | +import { |
| 7 | + DEFAULT_TTS_TEXT_TRANSFORMS, |
| 8 | + applyTextTransforms, |
| 9 | + getAvailableTransforms, |
| 10 | +} from './transforms.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Helper to convert a string to a ReadableStream |
| 14 | + */ |
| 15 | +function stringToStream(text: string): ReadableStream<string> { |
| 16 | + return new ReadableStream({ |
| 17 | + start(controller) { |
| 18 | + controller.enqueue(text); |
| 19 | + controller.close(); |
| 20 | + }, |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Helper to read a stream to a string |
| 26 | + */ |
| 27 | +async function streamToString(stream: ReadableStream<string>): Promise<string> { |
| 28 | + const reader = stream.getReader(); |
| 29 | + let result = ''; |
| 30 | + while (true) { |
| 31 | + const { done, value } = await reader.read(); |
| 32 | + if (done) break; |
| 33 | + result += value; |
| 34 | + } |
| 35 | + return result; |
| 36 | +} |
| 37 | + |
| 38 | +describe('Text Transforms Core', () => { |
| 39 | + it('should export DEFAULT_TTS_TEXT_TRANSFORMS', () => { |
| 40 | + expect(DEFAULT_TTS_TEXT_TRANSFORMS).toBeDefined(); |
| 41 | + expect(DEFAULT_TTS_TEXT_TRANSFORMS).toEqual(['filter_markdown', 'filter_emoji']); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should list available transforms for English', () => { |
| 45 | + const transforms = getAvailableTransforms('en'); |
| 46 | + expect(transforms.has('filter_markdown')).toBe(true); |
| 47 | + expect(transforms.has('filter_emoji')).toBe(true); |
| 48 | + expect(transforms.has('format_numbers')).toBe(true); |
| 49 | + expect(transforms.has('format_dollar_amounts')).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should list available transforms for German', () => { |
| 53 | + const transforms = getAvailableTransforms('de'); |
| 54 | + expect(transforms.has('filter_markdown')).toBe(true); |
| 55 | + expect(transforms.has('filter_emoji')).toBe(true); |
| 56 | + expect(transforms.has('format_numbers_de')).toBe(true); |
| 57 | + expect(transforms.has('format_euro_amounts')).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw error for invalid transform name', async () => { |
| 61 | + const stream = stringToStream('test'); |
| 62 | + await expect( |
| 63 | + applyTextTransforms(stream, ['invalid_transform' as any], { language: 'en' }), |
| 64 | + ).rejects.toThrow('Invalid transform'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should apply custom transform function', async () => { |
| 68 | + const customTransform = (text: ReadableStream<string>) => { |
| 69 | + return new ReadableStream({ |
| 70 | + async start(controller) { |
| 71 | + const reader = text.getReader(); |
| 72 | + while (true) { |
| 73 | + const { done, value } = await reader.read(); |
| 74 | + if (done) { |
| 75 | + controller.close(); |
| 76 | + break; |
| 77 | + } |
| 78 | + controller.enqueue(value.toUpperCase()); |
| 79 | + } |
| 80 | + }, |
| 81 | + }); |
| 82 | + }; |
| 83 | + |
| 84 | + const stream = stringToStream('hello world'); |
| 85 | + const result = await applyTextTransforms(stream, [customTransform]); |
| 86 | + const output = await streamToString(result); |
| 87 | + expect(output).toBe('HELLO WORLD'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should apply multiple transforms in sequence', async () => { |
| 91 | + const stream = stringToStream('**Price: $5** 🎉'); |
| 92 | + const result = await applyTextTransforms( |
| 93 | + stream, |
| 94 | + ['filter_markdown', 'filter_emoji', 'format_dollar_amounts'], |
| 95 | + { language: 'en' }, |
| 96 | + ); |
| 97 | + const output = await streamToString(result); |
| 98 | + expect(output).toContain('Price:'); |
| 99 | + expect(output).toContain('five dollars'); |
| 100 | + expect(output).not.toContain('**'); |
| 101 | + expect(output).not.toContain('🎉'); |
| 102 | + }); |
| 103 | +}); |
0 commit comments