stt-sdk.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
  2. import { SttSdk } from '../../src/core/stt-sdk'
  3. import { SttError } from '../../src/core/stt-error'
  4. describe('SttSdk', () => {
  5. let sdk: SttSdk
  6. beforeEach(() => {
  7. sdk = new SttSdk()
  8. })
  9. afterEach(async () => {
  10. if (sdk.isInitialized) {
  11. await sdk.destroy()
  12. }
  13. })
  14. describe('initialize', () => {
  15. it('should initialize successfully with valid config', async () => {
  16. const config = { appId: 'test-app-id', certificate: 'test-certificate' }
  17. await sdk.initialize(config)
  18. expect(sdk.isInitialized).toBe(true)
  19. expect(sdk.config).toEqual(config)
  20. })
  21. it('should throw error when appId is missing', async () => {
  22. const config = { appId: '', certificate: 'test-certificate' }
  23. await expect(sdk.initialize(config)).rejects.toThrow(SttError)
  24. await expect(sdk.initialize(config)).rejects.toThrow('App ID is required')
  25. })
  26. it('should throw error when certificate is missing', async () => {
  27. const config = { appId: 'test-app-id', certificate: '' }
  28. await expect(sdk.initialize(config)).rejects.toThrow(SttError)
  29. await expect(sdk.initialize(config)).rejects.toThrow('Certificate is required')
  30. })
  31. it('should throw error when already initialized', async () => {
  32. const config = { appId: 'test-app-id', certificate: 'test-certificate' }
  33. await sdk.initialize(config)
  34. await expect(sdk.initialize(config)).rejects.toThrow(SttError)
  35. await expect(sdk.initialize(config)).rejects.toThrow('SDK is already initialized')
  36. })
  37. })
  38. describe('createSttManager', () => {
  39. it('should create STT manager when SDK is initialized', async () => {
  40. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  41. const rtmManager = sdk.createRtmManager()
  42. const manager = sdk.createSttManager(rtmManager)
  43. expect(manager).toBeDefined()
  44. expect(sdk.sttManagerCount).toBe(1)
  45. })
  46. it('should throw error when SDK is not initialized', () => {
  47. const rtmManager = {} as any
  48. expect(() => sdk.createSttManager(rtmManager)).toThrow(SttError)
  49. expect(() => sdk.createSttManager(rtmManager)).toThrow(
  50. 'SDK must be initialized before creating managers'
  51. )
  52. })
  53. })
  54. describe('createRtmManager', () => {
  55. it('should create RTM manager when SDK is initialized', async () => {
  56. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  57. const manager = sdk.createRtmManager()
  58. expect(manager).toBeDefined()
  59. expect(sdk.rtmManagerCount).toBe(1)
  60. })
  61. it('should throw error when SDK is not initialized', () => {
  62. expect(() => sdk.createRtmManager()).toThrow(SttError)
  63. expect(() => sdk.createRtmManager()).toThrow(
  64. 'SDK must be initialized before creating managers'
  65. )
  66. })
  67. })
  68. describe('createRtcManager', () => {
  69. it('should create RTC manager when SDK is initialized', async () => {
  70. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  71. const manager = sdk.createRtcManager()
  72. expect(manager).toBeDefined()
  73. expect(sdk.rtcManagerCount).toBe(1)
  74. })
  75. it('should throw error when SDK is not initialized', () => {
  76. expect(() => sdk.createRtcManager()).toThrow(SttError)
  77. expect(() => sdk.createRtcManager()).toThrow(
  78. 'SDK must be initialized before creating managers'
  79. )
  80. })
  81. })
  82. describe('destroy', () => {
  83. it('should destroy SDK successfully', async () => {
  84. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  85. // 创建管理器以测试销毁功能
  86. const rtmManager = sdk.createRtmManager()
  87. sdk.createSttManager(rtmManager)
  88. sdk.createRtcManager()
  89. await sdk.destroy()
  90. expect(sdk.isInitialized).toBe(false)
  91. expect(sdk.config).toBeUndefined()
  92. expect(sdk.sttManagerCount).toBe(0)
  93. expect(sdk.rtmManagerCount).toBe(0)
  94. expect(sdk.rtcManagerCount).toBe(0)
  95. })
  96. it('should handle destroy when not initialized', async () => {
  97. await expect(sdk.destroy()).resolves.not.toThrow()
  98. })
  99. })
  100. describe('events', () => {
  101. it('should emit initialized event', async () => {
  102. const initializedHandler = vi.fn()
  103. sdk.on('initialized', initializedHandler)
  104. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  105. expect(initializedHandler).toHaveBeenCalledTimes(1)
  106. })
  107. it('should emit error event on initialization failure', async () => {
  108. const errorHandler = vi.fn()
  109. sdk.on('error', errorHandler)
  110. try {
  111. await sdk.initialize({ appId: '', certificate: 'test-certificate' })
  112. } catch (error) {
  113. // Expected to throw
  114. }
  115. expect(errorHandler).toHaveBeenCalledTimes(1)
  116. expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError)
  117. })
  118. it('should emit error event when certificate is missing', async () => {
  119. const errorHandler = vi.fn()
  120. sdk.on('error', errorHandler)
  121. try {
  122. await sdk.initialize({ appId: 'test-app-id', certificate: '' })
  123. } catch (error) {
  124. // Expected to throw
  125. }
  126. expect(errorHandler).toHaveBeenCalledTimes(1)
  127. expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError)
  128. })
  129. })
  130. })