|
|
@@ -0,0 +1,238 @@
|
|
|
+import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
|
+import { SttSdk } from '../../src/core/stt-sdk'
|
|
|
+import { SttManagerAdapter } from '../../src/managers/stt-manager-adapter'
|
|
|
+import { RtmManagerAdapter } from '../../src/managers/rtm-manager-adapter'
|
|
|
+import { SttError } from '../../src/core/stt-error'
|
|
|
+
|
|
|
+describe('SDK Compatibility Test', () => {
|
|
|
+ let sdk: SttSdk
|
|
|
+ let sttManager: SttManagerAdapter
|
|
|
+ let rtmManager: RtmManagerAdapter
|
|
|
+
|
|
|
+ beforeEach(() => {
|
|
|
+ sdk = new SttSdk()
|
|
|
+ })
|
|
|
+
|
|
|
+ afterEach(async () => {
|
|
|
+ await sdk.destroy()
|
|
|
+ })
|
|
|
+
|
|
|
+ test('SDK should initialize successfully', async () => {
|
|
|
+ await sdk.initialize({
|
|
|
+ appId: 'test-app-id',
|
|
|
+ logLevel: 'info',
|
|
|
+ })
|
|
|
+
|
|
|
+ expect(sdk.isInitialized).toBe(true)
|
|
|
+ expect(sdk.config?.appId).toBe('test-app-id')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('Should create STT manager with compatible API', () => {
|
|
|
+ // 模拟SDK已初始化
|
|
|
+ sdk._initialized = true
|
|
|
+
|
|
|
+ sttManager = sdk.createSttManager()
|
|
|
+
|
|
|
+ // 验证管理器具有兼容的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', () => {
|
|
|
+ // 模拟SDK已初始化
|
|
|
+ sdk._initialized = true
|
|
|
+
|
|
|
+ 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已初始化
|
|
|
+ sdk._initialized = true
|
|
|
+
|
|
|
+ sttManager = sdk.createSttManager()
|
|
|
+
|
|
|
+ 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已初始化
|
|
|
+ sdk._initialized = true
|
|
|
+
|
|
|
+ sttManager = sdk.createSttManager()
|
|
|
+ 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已初始化
|
|
|
+ sdk._initialized = true
|
|
|
+
|
|
|
+ rtmManager = sdk.createRtmManager()
|
|
|
+
|
|
|
+ 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已初始化
|
|
|
+ sdk._initialized = true
|
|
|
+
|
|
|
+ sttManager = sdk.createSttManager()
|
|
|
+ rtmManager = sdk.createRtmManager()
|
|
|
+
|
|
|
+ await sttManager.init({
|
|
|
+ userId: 'test-user',
|
|
|
+ channel: 'test-channel',
|
|
|
+ userName: 'Test User',
|
|
|
+ })
|
|
|
+
|
|
|
+ await rtmManager.join({
|
|
|
+ channel: 'test-channel',
|
|
|
+ userId: 'test-user',
|
|
|
+ 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', () => {
|
|
|
+ // 模拟现有应用的使用模式
|
|
|
+
|
|
|
+ // 1. 创建管理器实例
|
|
|
+ sdk._initialized = true
|
|
|
+ const sttManager = sdk.createSttManager()
|
|
|
+ const rtmManager = sdk.createRtmManager()
|
|
|
+
|
|
|
+ // 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')
|
|
|
+ })
|
|
|
+})
|