UNPKG

@volley/recognition-client-sdk

Version:

Recognition Service TypeScript/Node.js Client SDK

266 lines (237 loc) 7.59 kB
/** * Unit tests for ConfigBuilder */ import { ConfigBuilder } from './config-builder.js'; import { RecognitionProvider, RecognitionContextTypeV1 } from '@recog/shared-types'; describe('ConfigBuilder', () => { it('should build empty config', () => { const config = new ConfigBuilder().build(); expect(config).toBeDefined(); }); it('should set url', () => { const config = new ConfigBuilder() .url('ws://localhost:3101/ws/v1/recognize') .build(); expect(config.url).toBe('ws://localhost:3101/ws/v1/recognize'); }); it('should set asrRequestConfig', () => { const asrConfig = { provider: RecognitionProvider.DEEPGRAM, model: 'nova-2-general', language: 'en-US', sampleRate: 16000, encoding: 'linear16' }; const config = new ConfigBuilder() .asrRequestConfig(asrConfig) .build(); expect(config.asrRequestConfig).toEqual(asrConfig); }); it('should set gameContext', () => { const gameContext = { type: RecognitionContextTypeV1.GAME_CONTEXT as const, gameId: 'test-game', gamePhase: 'test-phase' }; const config = new ConfigBuilder() .gameContext(gameContext) .build(); expect(config.gameContext).toEqual(gameContext); }); it('should set audioUtteranceId', () => { const config = new ConfigBuilder() .audioUtteranceId('test-utterance-id') .build(); expect(config.audioUtteranceId).toBe('test-utterance-id'); }); it('should set callbackUrls', () => { const callbackUrls = [ { url: 'https://example.com/callback', event: 'transcript' as const } ]; const config = new ConfigBuilder() .callbackUrls(callbackUrls) .build(); expect(config.callbackUrls).toEqual(callbackUrls); }); it('should set userId', () => { const config = new ConfigBuilder() .userId('user-123') .build(); expect(config.userId).toBe('user-123'); }); it('should set gameSessionId', () => { const config = new ConfigBuilder() .gameSessionId('session-456') .build(); expect(config.gameSessionId).toBe('session-456'); }); it('should set deviceId', () => { const config = new ConfigBuilder() .deviceId('device-789') .build(); expect(config.deviceId).toBe('device-789'); }); it('should set accountId', () => { const config = new ConfigBuilder() .accountId('account-abc') .build(); expect(config.accountId).toBe('account-abc'); }); it('should set questionAnswerId', () => { const config = new ConfigBuilder() .questionAnswerId('qa-xyz') .build(); expect(config.questionAnswerId).toBe('qa-xyz'); }); it('should set platform', () => { const config = new ConfigBuilder() .platform('ios') .build(); expect(config.platform).toBe('ios'); }); it('should set onTranscript callback', () => { const callback = jest.fn(); const config = new ConfigBuilder() .onTranscript(callback) .build(); expect(config.onTranscript).toBe(callback); }); it('should set onMetadata callback', () => { const callback = jest.fn(); const config = new ConfigBuilder() .onMetadata(callback) .build(); expect(config.onMetadata).toBe(callback); }); it('should set onError callback', () => { const callback = jest.fn(); const config = new ConfigBuilder() .onError(callback) .build(); expect(config.onError).toBe(callback); }); it('should set onConnected callback', () => { const callback = jest.fn(); const config = new ConfigBuilder() .onConnected(callback) .build(); expect(config.onConnected).toBe(callback); }); it('should set onDisconnected callback', () => { const callback = jest.fn(); const config = new ConfigBuilder() .onDisconnected(callback) .build(); expect(config.onDisconnected).toBe(callback); }); it('should set highWaterMark', () => { const config = new ConfigBuilder() .highWaterMark(1000) .build(); expect(config.highWaterMark).toBe(1000); }); it('should set lowWaterMark', () => { const config = new ConfigBuilder() .lowWaterMark(500) .build(); expect(config.lowWaterMark).toBe(500); }); it('should set maxBufferDurationSec', () => { const config = new ConfigBuilder() .maxBufferDurationSec(10) .build(); expect(config.maxBufferDurationSec).toBe(10); }); it('should set chunksPerSecond', () => { const config = new ConfigBuilder() .chunksPerSecond(50) .build(); expect(config.chunksPerSecond).toBe(50); }); it('should set logger', () => { const logger = jest.fn(); const config = new ConfigBuilder() .logger(logger) .build(); expect(config.logger).toBe(logger); }); it('should support method chaining', () => { const config = new ConfigBuilder() .url('ws://localhost:3101/ws/v1/recognize') .audioUtteranceId('test-id') .userId('user-123') .gameSessionId('session-456') .deviceId('device-789') .platform('ios') .highWaterMark(1000) .lowWaterMark(500) .build(); expect(config.url).toBe('ws://localhost:3101/ws/v1/recognize'); expect(config.audioUtteranceId).toBe('test-id'); expect(config.userId).toBe('user-123'); expect(config.gameSessionId).toBe('session-456'); expect(config.deviceId).toBe('device-789'); expect(config.platform).toBe('ios'); expect(config.highWaterMark).toBe(1000); expect(config.lowWaterMark).toBe(500); }); it('should build complete configuration', () => { const onTranscript = jest.fn(); const onError = jest.fn(); const onConnected = jest.fn(); const onDisconnected = jest.fn(); const logger = jest.fn(); const config = new ConfigBuilder() .url('ws://localhost:3101/ws/v1/recognize') .asrRequestConfig({ provider: RecognitionProvider.DEEPGRAM, model: 'nova-2-general', language: 'en-US', sampleRate: 16000, encoding: 'linear16' }) .gameContext({ type: RecognitionContextTypeV1.GAME_CONTEXT as const, gameId: 'test-game', gamePhase: 'test-phase' }) .audioUtteranceId('test-utterance-id') .userId('user-123') .gameSessionId('session-456') .deviceId('device-789') .accountId('account-abc') .questionAnswerId('qa-xyz') .platform('ios') .onTranscript(onTranscript) .onError(onError) .onConnected(onConnected) .onDisconnected(onDisconnected) .highWaterMark(1000) .lowWaterMark(500) .maxBufferDurationSec(10) .chunksPerSecond(50) .logger(logger) .build(); expect(config).toMatchObject({ url: 'ws://localhost:3101/ws/v1/recognize', audioUtteranceId: 'test-utterance-id', userId: 'user-123', gameSessionId: 'session-456', deviceId: 'device-789', accountId: 'account-abc', questionAnswerId: 'qa-xyz', platform: 'ios', highWaterMark: 1000, lowWaterMark: 500, maxBufferDurationSec: 10, chunksPerSecond: 50 }); expect(config.asrRequestConfig?.provider).toBe(RecognitionProvider.DEEPGRAM); expect(config.gameContext?.gameId).toBe('test-game'); expect(config.onTranscript).toBe(onTranscript); expect(config.onError).toBe(onError); expect(config.onConnected).toBe(onConnected); expect(config.onDisconnected).toBe(onDisconnected); expect(config.logger).toBe(logger); }); });