stt-sdk.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 manager = sdk.createSttManager()
  42. expect(manager).toBeDefined()
  43. expect(sdk.sttManagerCount).toBe(1)
  44. })
  45. it('should throw error when SDK is not initialized', () => {
  46. expect(() => sdk.createSttManager()).toThrow(SttError)
  47. expect(() => sdk.createSttManager()).toThrow(
  48. 'SDK must be initialized before creating managers'
  49. )
  50. })
  51. })
  52. describe('createRtmManager', () => {
  53. it('should create RTM manager when SDK is initialized', async () => {
  54. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  55. const manager = sdk.createRtmManager()
  56. expect(manager).toBeDefined()
  57. expect(sdk.rtmManagerCount).toBe(1)
  58. })
  59. it('should throw error when SDK is not initialized', () => {
  60. expect(() => sdk.createRtmManager()).toThrow(SttError)
  61. expect(() => sdk.createRtmManager()).toThrow(
  62. 'SDK must be initialized before creating managers'
  63. )
  64. })
  65. })
  66. describe('destroy', () => {
  67. it('should destroy SDK successfully', async () => {
  68. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  69. // 创建管理器以测试销毁功能
  70. sdk.createSttManager()
  71. sdk.createRtmManager()
  72. await sdk.destroy()
  73. expect(sdk.isInitialized).toBe(false)
  74. expect(sdk.config).toBeUndefined()
  75. expect(sdk.sttManagerCount).toBe(0)
  76. expect(sdk.rtmManagerCount).toBe(0)
  77. })
  78. it('should handle destroy when not initialized', async () => {
  79. await expect(sdk.destroy()).resolves.not.toThrow()
  80. })
  81. })
  82. describe('events', () => {
  83. it('should emit initialized event', async () => {
  84. const initializedHandler = vi.fn()
  85. sdk.on('initialized', initializedHandler)
  86. await sdk.initialize({ appId: 'test-app-id', certificate: 'test-certificate' })
  87. expect(initializedHandler).toHaveBeenCalledTimes(1)
  88. })
  89. it('should emit error event on initialization failure', async () => {
  90. const errorHandler = vi.fn()
  91. sdk.on('error', errorHandler)
  92. try {
  93. await sdk.initialize({ appId: '', certificate: 'test-certificate' })
  94. } catch (error) {
  95. // Expected to throw
  96. }
  97. expect(errorHandler).toHaveBeenCalledTimes(1)
  98. expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError)
  99. })
  100. it('should emit error event when certificate is missing', async () => {
  101. const errorHandler = vi.fn()
  102. sdk.on('error', errorHandler)
  103. try {
  104. await sdk.initialize({ appId: 'test-app-id', certificate: '' })
  105. } catch (error) {
  106. // Expected to throw
  107. }
  108. expect(errorHandler).toHaveBeenCalledTimes(1)
  109. expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError)
  110. })
  111. })
  112. })