import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest' import { SttSdk } from '../../src/core/stt-sdk' import { SttError } from '../../src/core/stt-error' describe('SDK Compatibility Test', () => { let sdk: SttSdk beforeEach(() => { sdk = new SttSdk() }) afterEach(async () => { await sdk.destroy() }) test('SDK should initialize successfully', async () => { await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) expect(sdk.isInitialized).toBe(true) expect(sdk.config?.appId).toBe('test-app-id') expect(sdk.config?.certificate).toBe('test-certificate') }) test('Should create STT manager with compatible API', async () => { // 先初始化SDK await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) const rtmManager = sdk.createRtmManager() const sttManager = sdk.createSttManager(rtmManager) // 验证管理器具有兼容的API expect(sttManager).toHaveProperty('init') expect(sttManager).toHaveProperty('startTranscription') expect(sttManager).toHaveProperty('stopTranscription') expect(sttManager).toHaveProperty('queryTranscription') expect(sttManager).toHaveProperty('updateTranscription') expect(sttManager).toHaveProperty('extendDuration') expect(sttManager).toHaveProperty('destroy') expect(sttManager).toHaveProperty('isInitialized') expect(sttManager).toHaveProperty('config') expect(sttManager).toHaveProperty('userId') expect(sttManager).toHaveProperty('channel') }) test('Should create RTM manager with compatible API', async () => { // 先初始化SDK await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) const rtmManager = sdk.createRtmManager() // 验证管理器具有兼容的API expect(rtmManager).toHaveProperty('join') expect(rtmManager).toHaveProperty('updateSttData') expect(rtmManager).toHaveProperty('updateLanguages') expect(rtmManager).toHaveProperty('acquireLock') expect(rtmManager).toHaveProperty('releaseLock') expect(rtmManager).toHaveProperty('destroy') expect(rtmManager).toHaveProperty('isJoined') expect(rtmManager).toHaveProperty('config') expect(rtmManager).toHaveProperty('userId') expect(rtmManager).toHaveProperty('channel') expect(rtmManager).toHaveProperty('userList') }) test('STT manager should handle initialization correctly', async () => { // 先初始化SDK await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) const rtmManager = sdk.createRtmManager() const sttManager = sdk.createSttManager(rtmManager) await sttManager.init({ userId: 'test-user', channel: 'test-channel', userName: 'Test User', }) expect(sttManager.isInitialized).toBe(true) expect(sttManager.userId).toBe('test-user') expect(sttManager.channel).toBe('test-channel') }) test('STT manager should handle transcription lifecycle', async () => { // 先初始化SDK await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) const rtmManager = sdk.createRtmManager() const sttManager = sdk.createSttManager(rtmManager) await sttManager.init({ userId: 'test-user', channel: 'test-channel', userName: 'Test User', }) // 开始转录 await sttManager.startTranscription({ languages: [{ source: 'en', target: ['zh'] }], }) // 查询转录结果 const result = await sttManager.queryTranscription() expect(result).toHaveProperty('status') expect(result).toHaveProperty('results') // 更新转录 await sttManager.updateTranscription({ data: { newData: 'test' }, updateMaskList: ['newData'], }) // 停止转录 await sttManager.stopTranscription() // 延长持续时间 await sttManager.extendDuration({ startTime: Date.now(), duration: 60000, }) }) test('RTM manager should handle channel operations', async () => { // 先初始化SDK await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) const rtmManager = sdk.createRtmManager() // RTM管理器需要先加入频道 await rtmManager.join({ channel: 'test-channel', userId: 'test-user', userName: 'Test User', }) expect(rtmManager.isJoined).toBe(true) expect(rtmManager.userId).toBe('test-user') expect(rtmManager.channel).toBe('test-channel') // 更新STT数据 await rtmManager.updateSttData({ status: 'start', taskId: 'test-task-id', token: 'test-token', }) // 更新语言设置 await rtmManager.updateLanguages([{ source: 'en', target: ['zh'] }]) // 锁操作 await rtmManager.acquireLock() await rtmManager.releaseLock() }) test('Should handle error events properly', async () => { const errorHandler = vi.fn() sdk.on('error', errorHandler) // 模拟错误 const testError = new SttError('NETWORK_ERROR', 'Network error occurred') sdk.emit('error', testError) expect(errorHandler).toHaveBeenCalledWith(testError) }) test('Should provide event emitter functionality', () => { const testHandler = vi.fn() // 测试事件监听 sdk.on('initialized', testHandler) sdk.emit('initialized') expect(testHandler).toHaveBeenCalled() // 测试事件移除 sdk.off('initialized', testHandler) sdk.emit('initialized') // 应该只被调用一次 expect(testHandler).toHaveBeenCalledTimes(1) }) test('Should handle manager destruction correctly', async () => { // 先初始化SDK await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) const rtmManager = sdk.createRtmManager() const sttManager = sdk.createSttManager(rtmManager) await sttManager.init({ userId: 'test-user', channel: 'test-channel', userName: 'Test User', }) // 销毁管理器 await sttManager.destroy() await rtmManager.destroy() expect(sttManager.isInitialized).toBe(false) expect(rtmManager.isJoined).toBe(false) }) test('Should maintain backward compatibility with existing API patterns', async () => { // 模拟现有应用的使用模式 // 1. 创建管理器实例 await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate', logLevel: 'info', }) const rtmManager = sdk.createRtmManager() const sttManager = sdk.createSttManager(rtmManager) // 2. 验证属性访问模式(如 window.sttManager.property) expect(typeof sttManager.userId).toBe('string') expect(typeof sttManager.channel).toBe('string') expect(typeof sttManager.isInitialized).toBe('boolean') expect(typeof rtmManager.userId).toBe('string') expect(typeof rtmManager.channel).toBe('string') expect(typeof rtmManager.isJoined).toBe('boolean') // 3. 验证方法调用模式 expect(typeof sttManager.init).toBe('function') expect(typeof sttManager.startTranscription).toBe('function') expect(typeof sttManager.stopTranscription).toBe('function') expect(typeof sttManager.queryTranscription).toBe('function') expect(typeof rtmManager.join).toBe('function') expect(typeof rtmManager.updateSttData).toBe('function') expect(typeof rtmManager.updateLanguages).toBe('function') expect(typeof rtmManager.acquireLock).toBe('function') }) })