compatibility.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest'
  2. import { SttSdk } from '../../src/core/stt-sdk'
  3. import { SttManagerAdapter } from '../../src/managers/stt-manager-adapter'
  4. import { RtmManagerAdapter } from '../../src/managers/rtm-manager-adapter'
  5. import { SttError } from '../../src/core/stt-error'
  6. describe('SDK Compatibility Test', () => {
  7. let sdk: SttSdk
  8. let sttManager: SttManagerAdapter
  9. let rtmManager: RtmManagerAdapter
  10. beforeEach(() => {
  11. sdk = new SttSdk()
  12. })
  13. afterEach(async () => {
  14. await sdk.destroy()
  15. })
  16. test('SDK should initialize successfully', async () => {
  17. await sdk.initialize({
  18. appId: 'test-app-id',
  19. logLevel: 'info',
  20. })
  21. expect(sdk.isInitialized).toBe(true)
  22. expect(sdk.config?.appId).toBe('test-app-id')
  23. })
  24. test('Should create STT manager with compatible API', () => {
  25. // 模拟SDK已初始化
  26. sdk._initialized = true
  27. sttManager = sdk.createSttManager()
  28. // 验证管理器具有兼容的API
  29. expect(sttManager).toHaveProperty('init')
  30. expect(sttManager).toHaveProperty('startTranscription')
  31. expect(sttManager).toHaveProperty('stopTranscription')
  32. expect(sttManager).toHaveProperty('queryTranscription')
  33. expect(sttManager).toHaveProperty('updateTranscription')
  34. expect(sttManager).toHaveProperty('extendDuration')
  35. expect(sttManager).toHaveProperty('destroy')
  36. expect(sttManager).toHaveProperty('isInitialized')
  37. expect(sttManager).toHaveProperty('config')
  38. expect(sttManager).toHaveProperty('userId')
  39. expect(sttManager).toHaveProperty('channel')
  40. })
  41. test('Should create RTM manager with compatible API', () => {
  42. // 模拟SDK已初始化
  43. sdk._initialized = true
  44. rtmManager = sdk.createRtmManager()
  45. // 验证管理器具有兼容的API
  46. expect(rtmManager).toHaveProperty('join')
  47. expect(rtmManager).toHaveProperty('updateSttData')
  48. expect(rtmManager).toHaveProperty('updateLanguages')
  49. expect(rtmManager).toHaveProperty('acquireLock')
  50. expect(rtmManager).toHaveProperty('releaseLock')
  51. expect(rtmManager).toHaveProperty('destroy')
  52. expect(rtmManager).toHaveProperty('isJoined')
  53. expect(rtmManager).toHaveProperty('config')
  54. expect(rtmManager).toHaveProperty('userId')
  55. expect(rtmManager).toHaveProperty('channel')
  56. expect(rtmManager).toHaveProperty('userList')
  57. })
  58. test('STT manager should handle initialization correctly', async () => {
  59. // 模拟SDK已初始化
  60. sdk._initialized = true
  61. sttManager = sdk.createSttManager()
  62. await sttManager.init({
  63. userId: 'test-user',
  64. channel: 'test-channel',
  65. userName: 'Test User',
  66. })
  67. expect(sttManager.isInitialized).toBe(true)
  68. expect(sttManager.userId).toBe('test-user')
  69. expect(sttManager.channel).toBe('test-channel')
  70. })
  71. test('STT manager should handle transcription lifecycle', async () => {
  72. // 模拟SDK已初始化
  73. sdk._initialized = true
  74. sttManager = sdk.createSttManager()
  75. await sttManager.init({
  76. userId: 'test-user',
  77. channel: 'test-channel',
  78. userName: 'Test User',
  79. })
  80. // 开始转录
  81. await sttManager.startTranscription({
  82. languages: [{ source: 'en', target: ['zh'] }],
  83. })
  84. // 查询转录结果
  85. const result = await sttManager.queryTranscription()
  86. expect(result).toHaveProperty('status')
  87. expect(result).toHaveProperty('results')
  88. // 更新转录
  89. await sttManager.updateTranscription({
  90. data: { newData: 'test' },
  91. updateMaskList: ['newData'],
  92. })
  93. // 停止转录
  94. await sttManager.stopTranscription()
  95. // 延长持续时间
  96. await sttManager.extendDuration({
  97. startTime: Date.now(),
  98. duration: 60000,
  99. })
  100. })
  101. test('RTM manager should handle channel operations', async () => {
  102. // 模拟SDK已初始化
  103. sdk._initialized = true
  104. rtmManager = sdk.createRtmManager()
  105. await rtmManager.join({
  106. channel: 'test-channel',
  107. userId: 'test-user',
  108. userName: 'Test User',
  109. })
  110. expect(rtmManager.isJoined).toBe(true)
  111. expect(rtmManager.userId).toBe('test-user')
  112. expect(rtmManager.channel).toBe('test-channel')
  113. // 更新STT数据
  114. await rtmManager.updateSttData({
  115. status: 'start',
  116. taskId: 'test-task-id',
  117. token: 'test-token',
  118. })
  119. // 更新语言设置
  120. await rtmManager.updateLanguages([{ source: 'en', target: ['zh'] }])
  121. // 锁操作
  122. await rtmManager.acquireLock()
  123. await rtmManager.releaseLock()
  124. })
  125. test('Should handle error events properly', async () => {
  126. const errorHandler = vi.fn()
  127. sdk.on('error', errorHandler)
  128. // 模拟错误
  129. const testError = new SttError('NETWORK_ERROR', 'Network error occurred')
  130. sdk.emit('error', testError)
  131. expect(errorHandler).toHaveBeenCalledWith(testError)
  132. })
  133. test('Should provide event emitter functionality', () => {
  134. const testHandler = vi.fn()
  135. // 测试事件监听
  136. sdk.on('initialized', testHandler)
  137. sdk.emit('initialized')
  138. expect(testHandler).toHaveBeenCalled()
  139. // 测试事件移除
  140. sdk.off('initialized', testHandler)
  141. sdk.emit('initialized')
  142. // 应该只被调用一次
  143. expect(testHandler).toHaveBeenCalledTimes(1)
  144. })
  145. test('Should handle manager destruction correctly', async () => {
  146. // 模拟SDK已初始化
  147. sdk._initialized = true
  148. sttManager = sdk.createSttManager()
  149. rtmManager = sdk.createRtmManager()
  150. await sttManager.init({
  151. userId: 'test-user',
  152. channel: 'test-channel',
  153. userName: 'Test User',
  154. })
  155. await rtmManager.join({
  156. channel: 'test-channel',
  157. userId: 'test-user',
  158. userName: 'Test User',
  159. })
  160. // 销毁管理器
  161. await sttManager.destroy()
  162. await rtmManager.destroy()
  163. expect(sttManager.isInitialized).toBe(false)
  164. expect(rtmManager.isJoined).toBe(false)
  165. })
  166. test('Should maintain backward compatibility with existing API patterns', () => {
  167. // 模拟现有应用的使用模式
  168. // 1. 创建管理器实例
  169. sdk._initialized = true
  170. const sttManager = sdk.createSttManager()
  171. const rtmManager = sdk.createRtmManager()
  172. // 2. 验证属性访问模式(如 window.sttManager.property)
  173. expect(typeof sttManager.userId).toBe('string')
  174. expect(typeof sttManager.channel).toBe('string')
  175. expect(typeof sttManager.isInitialized).toBe('boolean')
  176. expect(typeof rtmManager.userId).toBe('string')
  177. expect(typeof rtmManager.channel).toBe('string')
  178. expect(typeof rtmManager.isJoined).toBe('boolean')
  179. // 3. 验证方法调用模式
  180. expect(typeof sttManager.init).toBe('function')
  181. expect(typeof sttManager.startTranscription).toBe('function')
  182. expect(typeof sttManager.stopTranscription).toBe('function')
  183. expect(typeof sttManager.queryTranscription).toBe('function')
  184. expect(typeof rtmManager.join).toBe('function')
  185. expect(typeof rtmManager.updateSttData).toBe('function')
  186. expect(typeof rtmManager.updateLanguages).toBe('function')
  187. expect(typeof rtmManager.acquireLock).toBe('function')
  188. })
  189. })