import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { SttSdk } from '../../src/core/stt-sdk' import { SttError } from '../../src/core/stt-error' describe('SttSdk', () => { let sdk: SttSdk beforeEach(() => { sdk = new SttSdk() }) afterEach(async () => { if (sdk.isInitialized) { await sdk.destroy() } }) describe('initialize', () => { it('should initialize successfully with valid config', async () => { const config = { appId: 'test-app-id', certificate: 'test-certificate' } await sdk.initialize(config) expect(sdk.isInitialized).toBe(true) expect(sdk.config).toEqual(config) }) it('should throw error when appId is missing', async () => { const config = { appId: '', certificate: 'test-certificate' } await expect(sdk.initialize(config)).rejects.toThrow(SttError) await expect(sdk.initialize(config)).rejects.toThrow('App ID is required') }) it('should throw error when certificate is missing', async () => { const config = { appId: 'test-app-id', certificate: '' } await expect(sdk.initialize(config)).rejects.toThrow(SttError) await expect(sdk.initialize(config)).rejects.toThrow('Certificate is required') }) it('should throw error when already initialized', async () => { const config = { appId: 'test-app-id', certificate: 'test-certificate' } await sdk.initialize(config) await expect(sdk.initialize(config)).rejects.toThrow(SttError) await expect(sdk.initialize(config)).rejects.toThrow('SDK is already initialized') }) }) describe('createSttManager', () => { it('should create STT manager when SDK is initialized', async () => { await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' }) const manager = sdk.createSttManager() expect(manager).toBeDefined() expect(sdk.sttManagerCount).toBe(1) }) it('should throw error when SDK is not initialized', () => { expect(() => sdk.createSttManager()).toThrow(SttError) expect(() => sdk.createSttManager()).toThrow( 'SDK must be initialized before creating managers' ) }) }) describe('createRtmManager', () => { it('should create RTM manager when SDK is initialized', async () => { await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' }) const manager = sdk.createRtmManager() expect(manager).toBeDefined() expect(sdk.rtmManagerCount).toBe(1) }) it('should throw error when SDK is not initialized', () => { expect(() => sdk.createRtmManager()).toThrow(SttError) expect(() => sdk.createRtmManager()).toThrow( 'SDK must be initialized before creating managers' ) }) }) describe('destroy', () => { it('should destroy SDK successfully', async () => { await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' }) // 创建管理器以测试销毁功能 sdk.createSttManager() sdk.createRtmManager() await sdk.destroy() expect(sdk.isInitialized).toBe(false) expect(sdk.config).toBeUndefined() expect(sdk.sttManagerCount).toBe(0) expect(sdk.rtmManagerCount).toBe(0) }) it('should handle destroy when not initialized', async () => { await expect(sdk.destroy()).resolves.not.toThrow() }) }) describe('events', () => { it('should emit initialized event', async () => { const initializedHandler = vi.fn() sdk.on('initialized', initializedHandler) await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' }) expect(initializedHandler).toHaveBeenCalledTimes(1) }) it('should emit error event on initialization failure', async () => { const errorHandler = vi.fn() sdk.on('error', errorHandler) try { await sdk.initialize({ appId: '', certificate: 'test-certificate' }) } catch (error) { // Expected to throw } expect(errorHandler).toHaveBeenCalledTimes(1) expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError) }) it('should emit error event when certificate is missing', async () => { const errorHandler = vi.fn() sdk.on('error', errorHandler) try { await sdk.initialize({ appId: 'test-app-id', certificate: '' }) } catch (error) { // Expected to throw } expect(errorHandler).toHaveBeenCalledTimes(1) expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError) }) }) })