compatibility.test.ts 7.2 KB

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