stt-manager-adapter.test.ts 12 KB

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