2
0

stt-sdk.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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('destroy', () => {
  69. it('should destroy SDK successfully', async () => {
  70. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  71. // 创建管理器以测试销毁功能
  72. const rtmManager = sdk.createRtmManager()
  73. sdk.createSttManager(rtmManager)
  74. await sdk.destroy()
  75. expect(sdk.isInitialized).toBe(false)
  76. expect(sdk.config).toBeUndefined()
  77. expect(sdk.sttManagerCount).toBe(0)
  78. expect(sdk.rtmManagerCount).toBe(0)
  79. })
  80. it('should handle destroy when not initialized', async () => {
  81. await expect(sdk.destroy()).resolves.not.toThrow()
  82. })
  83. })
  84. describe('events', () => {
  85. it('should emit initialized event', async () => {
  86. const initializedHandler = vi.fn()
  87. sdk.on('initialized', initializedHandler)
  88. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  89. expect(initializedHandler).toHaveBeenCalledTimes(1)
  90. })
  91. it('should emit error event on initialization failure', async () => {
  92. const errorHandler = vi.fn()
  93. sdk.on('error', errorHandler)
  94. try {
  95. await sdk.initialize({ appId: '', certificate: 'test-certificate' })
  96. } catch (error) {
  97. // Expected to throw
  98. }
  99. expect(errorHandler).toHaveBeenCalledTimes(1)
  100. expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError)
  101. })
  102. it('should emit error event when certificate is missing', async () => {
  103. const errorHandler = vi.fn()
  104. sdk.on('error', errorHandler)
  105. try {
  106. await sdk.initialize({ appId: 'test-app-id', certificate: '' })
  107. } catch (error) {
  108. // Expected to throw
  109. }
  110. expect(errorHandler).toHaveBeenCalledTimes(1)
  111. expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError)
  112. })
  113. })
  114. })