stt-manager-adapter.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
  2. import { SttManagerAdapter } from '../../src/managers/stt-manager-adapter'
  3. import { SttError } from '../../src/core/stt-error'
  4. import type { RtmManagerAdapter } from '../../src/managers/rtm-manager-adapter'
  5. describe('SttManagerAdapter', () => {
  6. let manager: SttManagerAdapter
  7. let mockRtmManager: RtmManagerAdapter
  8. const mockAppId = 'test-app-id'
  9. const mockCertificate = 'test-certificate'
  10. beforeEach(() => {
  11. // 创建模拟的 RTM 管理器
  12. mockRtmManager = {
  13. join: vi.fn().mockResolvedValue(undefined),
  14. updateSttData: vi.fn().mockResolvedValue(undefined),
  15. updateLanguages: vi.fn().mockResolvedValue(undefined),
  16. acquireLock: vi.fn().mockResolvedValue(undefined),
  17. releaseLock: vi.fn().mockResolvedValue(undefined),
  18. destroy: vi.fn().mockResolvedValue(undefined),
  19. isJoined: false,
  20. config: undefined,
  21. userId: '',
  22. channel: '',
  23. userList: [],
  24. on: vi.fn().mockReturnThis(),
  25. off: vi.fn().mockReturnThis(),
  26. emit: vi.fn().mockReturnThis(),
  27. } as any
  28. manager = new SttManagerAdapter(mockRtmManager, mockAppId, mockCertificate)
  29. })
  30. afterEach(async () => {
  31. if (manager.isInitialized) {
  32. await manager.destroy()
  33. }
  34. })
  35. describe('init', () => {
  36. it('should initialize successfully with valid config', async () => {
  37. const config = {
  38. userId: 'test-user',
  39. channel: 'test-channel',
  40. userName: 'Test User',
  41. }
  42. await manager.init(config)
  43. expect(manager.isInitialized).toBe(true)
  44. expect(manager.config).toEqual(config)
  45. expect(manager.userId).toBe('test-user')
  46. expect(manager.channel).toBe('test-channel')
  47. expect(mockRtmManager.join).toHaveBeenCalledWith({
  48. channel: 'test-channel',
  49. userId: 'test-user',
  50. userName: 'Test User',
  51. })
  52. })
  53. it('should throw error when config is invalid', async () => {
  54. const config = {
  55. userId: '',
  56. channel: '',
  57. userName: '',
  58. }
  59. await expect(manager.init(config)).rejects.toThrow(SttError)
  60. await expect(manager.init(config)).rejects.toThrow(
  61. 'Missing required configuration parameters'
  62. )
  63. })
  64. it('should throw error when appId is not provided', async () => {
  65. const managerWithoutAppId = new SttManagerAdapter(mockRtmManager, '', 'test-certificate')
  66. const config = {
  67. userId: 'test-user',
  68. channel: 'test-channel',
  69. userName: 'Test User',
  70. }
  71. await expect(managerWithoutAppId.init(config)).rejects.toThrow(SttError)
  72. await expect(managerWithoutAppId.init(config)).rejects.toThrow(
  73. 'App ID is required for STT operations'
  74. )
  75. })
  76. it('should emit initialized event', async () => {
  77. const initializedHandler = vi.fn()
  78. manager.on('initialized', initializedHandler)
  79. const config = {
  80. userId: 'test-user',
  81. channel: 'test-channel',
  82. userName: 'Test User',
  83. }
  84. await manager.init(config)
  85. expect(initializedHandler).toHaveBeenCalledTimes(1)
  86. expect(initializedHandler).toHaveBeenCalledWith({
  87. userId: 'test-user',
  88. channel: 'test-channel',
  89. })
  90. })
  91. })
  92. describe('startTranscription', () => {
  93. beforeEach(async () => {
  94. await manager.init({
  95. userId: 'test-user',
  96. channel: 'test-channel',
  97. userName: 'Test User',
  98. })
  99. })
  100. it('should start transcription successfully', async () => {
  101. const startingHandler = vi.fn()
  102. const startedHandler = vi.fn()
  103. manager.on('transcriptionStarting', startingHandler)
  104. manager.on('transcriptionStarted', startedHandler)
  105. const options = {
  106. languages: [{ source: 'en-US' }],
  107. }
  108. await manager.startTranscription(options)
  109. expect(startingHandler).toHaveBeenCalledTimes(1)
  110. expect(startingHandler).toHaveBeenCalledWith({ languages: [{ source: 'en-US' }] })
  111. expect(startedHandler).toHaveBeenCalledTimes(1)
  112. expect(startedHandler).toHaveBeenCalledWith({
  113. taskId: 'test-task-id',
  114. languages: [{ source: 'en-US' }],
  115. })
  116. // 验证 RTM 管理器被正确调用
  117. expect(mockRtmManager.acquireLock).toHaveBeenCalledTimes(1)
  118. expect(mockRtmManager.releaseLock).toHaveBeenCalledTimes(1)
  119. expect(mockRtmManager.updateLanguages).toHaveBeenCalledWith([{ source: 'en-US' }])
  120. expect(mockRtmManager.updateSttData).toHaveBeenCalledWith({
  121. status: 'start',
  122. taskId: 'test-task-id',
  123. token: 'test-token',
  124. startTime: expect.any(Number),
  125. duration: 3600000,
  126. })
  127. })
  128. it('should throw error when not initialized', async () => {
  129. const uninitializedManager = new SttManagerAdapter(mockRtmManager, mockAppId)
  130. const options = {
  131. languages: [{ source: 'en-US' }],
  132. }
  133. await expect(uninitializedManager.startTranscription(options)).rejects.toThrow(SttError)
  134. await expect(uninitializedManager.startTranscription(options)).rejects.toThrow(
  135. 'SttManager must be initialized before starting transcription'
  136. )
  137. })
  138. it('should throw error when languages are empty', async () => {
  139. const options = {
  140. languages: [],
  141. }
  142. await expect(manager.startTranscription(options)).rejects.toThrow(SttError)
  143. await expect(manager.startTranscription(options)).rejects.toThrow(
  144. 'At least one language must be provided'
  145. )
  146. })
  147. it('should handle API errors gracefully', async () => {
  148. // 模拟 API 调用失败
  149. global.fetch = vi.fn().mockResolvedValue({
  150. ok: false,
  151. status: 500,
  152. json: vi.fn().mockResolvedValue({ message: 'Internal Server Error' }),
  153. })
  154. const options = {
  155. languages: [{ source: 'en-US' }],
  156. }
  157. await expect(manager.startTranscription(options)).rejects.toThrow()
  158. // 恢复默认的 fetch 模拟
  159. global.fetch = vi.fn().mockResolvedValue({
  160. ok: true,
  161. status: 200,
  162. json: vi.fn().mockResolvedValue({
  163. tokenName: 'test-token',
  164. taskId: 'test-task-id',
  165. }),
  166. })
  167. })
  168. })
  169. describe('stopTranscription', () => {
  170. beforeEach(async () => {
  171. await manager.init({
  172. userId: 'test-user',
  173. channel: 'test-channel',
  174. userName: 'Test User',
  175. })
  176. // 先启动转录以设置 taskId 和 token
  177. await manager.startTranscription({
  178. languages: [{ source: 'en-US' }],
  179. })
  180. })
  181. it('should stop transcription successfully', async () => {
  182. const stoppingHandler = vi.fn()
  183. const stoppedHandler = vi.fn()
  184. manager.on('transcriptionStopping', stoppingHandler)
  185. manager.on('transcriptionStopped', stoppedHandler)
  186. await manager.stopTranscription()
  187. expect(stoppingHandler).toHaveBeenCalledTimes(1)
  188. expect(stoppedHandler).toHaveBeenCalledTimes(1)
  189. // 验证 RTM 管理器被正确调用
  190. expect(mockRtmManager.acquireLock).toHaveBeenCalledTimes(2) // start 和 stop 各一次
  191. expect(mockRtmManager.releaseLock).toHaveBeenCalledTimes(2)
  192. expect(mockRtmManager.updateSttData).toHaveBeenCalledWith({
  193. status: 'end',
  194. })
  195. })
  196. it('should throw error when not initialized', async () => {
  197. const uninitializedManager = new SttManagerAdapter(mockRtmManager, mockAppId, mockCertificate)
  198. await expect(uninitializedManager.stopTranscription()).rejects.toThrow(SttError)
  199. await expect(uninitializedManager.stopTranscription()).rejects.toThrow(
  200. 'SttManager must be initialized before stopping transcription'
  201. )
  202. })
  203. it('should throw error when no active task found', async () => {
  204. const managerWithoutTask = new SttManagerAdapter(mockRtmManager, mockAppId, mockCertificate)
  205. await managerWithoutTask.init({
  206. userId: 'test-user',
  207. channel: 'test-channel',
  208. userName: 'Test User',
  209. })
  210. await expect(managerWithoutTask.stopTranscription()).rejects.toThrow(SttError)
  211. await expect(managerWithoutTask.stopTranscription()).rejects.toThrow(
  212. 'No active transcription task found'
  213. )
  214. })
  215. })
  216. describe('queryTranscription', () => {
  217. beforeEach(async () => {
  218. await manager.init({
  219. userId: 'test-user',
  220. channel: 'test-channel',
  221. userName: 'Test User',
  222. })
  223. // 先启动转录以设置 taskId 和 token
  224. await manager.startTranscription({
  225. languages: [{ source: 'en-US' }],
  226. })
  227. })
  228. it('should query transcription successfully', async () => {
  229. const result = await manager.queryTranscription()
  230. expect(result).toEqual({
  231. tokenName: 'test-token',
  232. taskId: 'test-task-id',
  233. })
  234. })
  235. it('should throw error when not initialized', async () => {
  236. const uninitializedManager = new SttManagerAdapter(mockRtmManager, mockAppId)
  237. await expect(uninitializedManager.queryTranscription()).rejects.toThrow(SttError)
  238. await expect(uninitializedManager.queryTranscription()).rejects.toThrow(
  239. 'SttManager must be initialized before querying transcription'
  240. )
  241. })
  242. it('should throw error when no active task found', async () => {
  243. const managerWithoutTask = new SttManagerAdapter(mockRtmManager, mockAppId)
  244. await managerWithoutTask.init({
  245. userId: 'test-user',
  246. channel: 'test-channel',
  247. userName: 'Test User',
  248. })
  249. await expect(managerWithoutTask.queryTranscription()).rejects.toThrow(SttError)
  250. await expect(managerWithoutTask.queryTranscription()).rejects.toThrow(
  251. 'No active transcription task found'
  252. )
  253. })
  254. })
  255. describe('destroy', () => {
  256. it('should destroy manager successfully', async () => {
  257. await manager.init({
  258. userId: 'test-user',
  259. channel: 'test-channel',
  260. userName: 'Test User',
  261. })
  262. const destroyingHandler = vi.fn()
  263. const destroyedHandler = vi.fn()
  264. manager.on('destroying', destroyingHandler)
  265. manager.on('destroyed', destroyedHandler)
  266. await manager.destroy()
  267. expect(manager.isInitialized).toBe(false)
  268. expect(manager.config).toBeUndefined()
  269. expect(destroyingHandler).toHaveBeenCalledTimes(1)
  270. expect(destroyedHandler).toHaveBeenCalledTimes(1)
  271. expect(mockRtmManager.destroy).toHaveBeenCalledTimes(1)
  272. })
  273. it('should handle destroy when not initialized', async () => {
  274. await expect(manager.destroy()).resolves.not.toThrow()
  275. })
  276. })
  277. describe('extendDuration', () => {
  278. beforeEach(async () => {
  279. await manager.init({
  280. userId: 'test-user',
  281. channel: 'test-channel',
  282. userName: 'Test User',
  283. })
  284. })
  285. it('should extend duration successfully', async () => {
  286. const extendingHandler = vi.fn()
  287. const extendedHandler = vi.fn()
  288. manager.on('durationExtending', extendingHandler)
  289. manager.on('durationExtended', extendedHandler)
  290. const options = {
  291. startTime: Date.now(),
  292. duration: 7200000, // 2小时
  293. }
  294. await manager.extendDuration(options)
  295. expect(extendingHandler).toHaveBeenCalledTimes(1)
  296. expect(extendingHandler).toHaveBeenCalledWith(options)
  297. expect(extendedHandler).toHaveBeenCalledTimes(1)
  298. expect(extendedHandler).toHaveBeenCalledWith(options)
  299. expect(mockRtmManager.updateSttData).toHaveBeenCalledWith(options)
  300. })
  301. it('should throw error when not initialized', async () => {
  302. const uninitializedManager = new SttManagerAdapter(mockRtmManager, mockAppId)
  303. await expect(uninitializedManager.extendDuration({})).rejects.toThrow(SttError)
  304. await expect(uninitializedManager.extendDuration({})).rejects.toThrow(
  305. 'SttManager must be initialized before extending duration'
  306. )
  307. })
  308. })
  309. })