2
0

stt-sdk.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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' }
  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: '' }
  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 already initialized', async () => {
  27. const config = { appId: 'test-app-id' }
  28. await sdk.initialize(config)
  29. await expect(sdk.initialize(config)).rejects.toThrow(SttError)
  30. await expect(sdk.initialize(config)).rejects.toThrow('SDK is already initialized')
  31. })
  32. })
  33. describe('createSttManager', () => {
  34. it('should create STT manager when SDK is initialized', async () => {
  35. await sdk.initialize({ appId: 'test-app-id' })
  36. const manager = sdk.createSttManager()
  37. expect(manager).toBeDefined()
  38. expect(sdk.sttManagerCount).toBe(1)
  39. })
  40. it('should throw error when SDK is not initialized', () => {
  41. expect(() => sdk.createSttManager()).toThrow(SttError)
  42. expect(() => sdk.createSttManager()).toThrow(
  43. 'SDK must be initialized before creating managers'
  44. )
  45. })
  46. })
  47. describe('createRtmManager', () => {
  48. it('should create RTM manager when SDK is initialized', async () => {
  49. await sdk.initialize({ appId: 'test-app-id' })
  50. const manager = sdk.createRtmManager()
  51. expect(manager).toBeDefined()
  52. expect(sdk.rtmManagerCount).toBe(1)
  53. })
  54. it('should throw error when SDK is not initialized', () => {
  55. expect(() => sdk.createRtmManager()).toThrow(SttError)
  56. expect(() => sdk.createRtmManager()).toThrow(
  57. 'SDK must be initialized before creating managers'
  58. )
  59. })
  60. })
  61. describe('destroy', () => {
  62. it('should destroy SDK successfully', async () => {
  63. await sdk.initialize({ appId: 'test-app-id' })
  64. // 创建管理器以测试销毁功能
  65. sdk.createSttManager()
  66. sdk.createRtmManager()
  67. await sdk.destroy()
  68. expect(sdk.isInitialized).toBe(false)
  69. expect(sdk.config).toBeUndefined()
  70. expect(sdk.sttManagerCount).toBe(0)
  71. expect(sdk.rtmManagerCount).toBe(0)
  72. })
  73. it('should handle destroy when not initialized', async () => {
  74. await expect(sdk.destroy()).resolves.not.toThrow()
  75. })
  76. })
  77. describe('events', () => {
  78. it('should emit initialized event', async () => {
  79. const initializedHandler = vi.fn()
  80. sdk.on('initialized', initializedHandler)
  81. await sdk.initialize({ appId: 'test-app-id' })
  82. expect(initializedHandler).toHaveBeenCalledTimes(1)
  83. })
  84. it('should emit error event on initialization failure', async () => {
  85. const errorHandler = vi.fn()
  86. sdk.on('error', errorHandler)
  87. try {
  88. await sdk.initialize({ appId: '' })
  89. } catch (error) {
  90. // Expected to throw
  91. }
  92. expect(errorHandler).toHaveBeenCalledTimes(1)
  93. expect(errorHandler.mock.calls[0][0]).toBeInstanceOf(SttError)
  94. })
  95. })
  96. })