| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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)
- })
- })
- })
|