Explorar o código

✅ test(rtm-manager-adapter): 改进测试中RTM客户端的获取方式并增加测试稳定性

- 使用vi.mocked()替代直接类型断言获取RTM客户端实例,提高类型安全性
- 在destroy测试中添加vi.clearAllMocks()重置模拟调用次数
- 在handle empty data测试中添加mockClear()确保存储方法调用记录准确
- 为presence和storage事件处理添加类型检查,增强测试健壮性
yourname hai 4 meses
pai
achega
866ed8d21f
Modificáronse 1 ficheiros con 80 adicións e 64 borrados
  1. 80 64
      packages/stt-sdk-core/tests/managers/rtm-manager-adapter.test.ts

+ 80 - 64
packages/stt-sdk-core/tests/managers/rtm-manager-adapter.test.ts

@@ -112,7 +112,8 @@ describe('RtmManagerAdapter', () => {
       await manager.join(config)
 
       // 获取创建的 RTM 客户端实例
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
       expect(mockClient.addEventListener).toHaveBeenCalledWith('status', expect.any(Function))
       expect(mockClient.addEventListener).toHaveBeenCalledWith('presence', expect.any(Function))
       expect(mockClient.addEventListener).toHaveBeenCalledWith('storage', expect.any(Function))
@@ -150,7 +151,8 @@ describe('RtmManagerAdapter', () => {
       expect(updatedHandler).toHaveBeenCalledWith({ data })
 
       // 验证存储方法被正确调用
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
       expect(mockClient.storage.setChannelMetadata).toHaveBeenCalledWith(
         'test-channel',
         'MESSAGE',
@@ -178,10 +180,14 @@ describe('RtmManagerAdapter', () => {
     })
 
     it('should handle empty data gracefully', async () => {
+      // 重置存储方法的调用记录
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
+      mockClient.storage.setChannelMetadata.mockClear()
+
       await manager.updateSttData({})
 
       // 验证存储方法没有被调用(因为数据为空)
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
       expect(mockClient.storage.setChannelMetadata).not.toHaveBeenCalled()
     })
   })
@@ -214,7 +220,8 @@ describe('RtmManagerAdapter', () => {
       expect(updatedHandler).toHaveBeenCalledWith({ languages })
 
       // 验证 updateSttData 被正确调用
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
       expect(mockClient.storage.setChannelMetadata).toHaveBeenCalledWith(
         'test-channel',
         'MESSAGE',
@@ -241,7 +248,8 @@ describe('RtmManagerAdapter', () => {
       await manager.updateLanguages([])
 
       // 验证 updateSttData 被调用但数据为空
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
       expect(mockClient.storage.setChannelMetadata).toHaveBeenCalledWith(
         'test-channel',
         'MESSAGE',
@@ -276,7 +284,8 @@ describe('RtmManagerAdapter', () => {
       expect(acquiredHandler).toHaveBeenCalledTimes(1)
 
       // 验证锁方法被正确调用
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
       expect(mockClient.lock.acquireLock).toHaveBeenCalledWith(
         'test-channel',
         'MESSAGE',
@@ -315,7 +324,8 @@ describe('RtmManagerAdapter', () => {
       expect(releasedHandler).toHaveBeenCalledTimes(1)
 
       // 验证锁方法被正确调用
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
       expect(mockClient.lock.releaseLock).toHaveBeenCalledWith(
         'test-channel',
         'MESSAGE',
@@ -335,6 +345,9 @@ describe('RtmManagerAdapter', () => {
 
   describe('destroy', () => {
     it('should destroy manager successfully', async () => {
+      // 重置模拟调用次数
+      vi.clearAllMocks()
+
       await manager.join({
         channel: 'test-channel',
         userId: 'test-user',
@@ -355,7 +368,8 @@ describe('RtmManagerAdapter', () => {
       expect(destroyedHandler).toHaveBeenCalledTimes(1)
 
       // 验证 RTM 客户端被正确销毁
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
       expect(mockClient.logout).toHaveBeenCalledTimes(1)
     })
 
@@ -395,32 +409,33 @@ describe('RtmManagerAdapter', () => {
       manager.on('userListChanged', userListChangedHandler)
 
       // 模拟 presence 事件
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
-      const presenceHandler = mockClient.addEventListener.mock.calls.find(
-        (call: any) => call[0] === 'presence'
-      )[1]
-
-      // 模拟 SNAPSHOT 事件
-      presenceHandler({
-        channelName: 'test-channel',
-        eventType: 'SNAPSHOT',
-        snapshot: [
-          {
-            states: {
-              type: 'UserInfo',
-              userId: 'other-user',
-              userName: 'Other User',
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
+      const presenceHandler = mockClient.eventHandlers?.presence
+
+      if (presenceHandler) {
+        // 模拟 SNAPSHOT 事件
+        presenceHandler({
+          channelName: 'test-channel',
+          eventType: 'SNAPSHOT',
+          snapshot: [
+            {
+              states: {
+                type: 'UserInfo',
+                userId: 'other-user',
+                userName: 'Other User',
+              },
             },
-          },
-        ],
-      })
-
-      expect(userListChangedHandler).toHaveBeenCalledWith(
-        expect.arrayContaining([
-          { userId: 'test-user', userName: 'Test User' },
-          { userId: 'other-user', userName: 'Other User' },
-        ])
-      )
+          ],
+        })
+
+        expect(userListChangedHandler).toHaveBeenCalledWith(
+          expect.arrayContaining([
+            { userId: 'test-user', userName: 'Test User' },
+            { userId: 'other-user', userName: 'Other User' },
+          ])
+        )
+      }
     })
 
     it('should handle storage events correctly', async () => {
@@ -430,38 +445,39 @@ describe('RtmManagerAdapter', () => {
       manager.on('sttDataChanged', sttDataChangedHandler)
 
       // 模拟 storage 事件
-      const mockClient = (AgoraRTM.RTM as any).mock.results[0].value
-      const storageHandler = mockClient.addEventListener.mock.calls.find(
-        (call: any) => call[0] === 'storage'
-      )[1]
-
-      // 模拟 UPDATE 事件
-      storageHandler({
-        channelName: 'test-channel',
-        eventType: 'UPDATE',
-        data: {
-          metadata: {
-            transcribe1: { value: '"en-US"' },
-            translate1List: { value: '["zh-CN","ja-JP"]' },
-            transcribe2: { value: '"zh-CN"' },
-            translate2List: { value: '["en-US"]' },
-            status: { value: '"start"' },
-            taskId: { value: '"test-task-id"' },
+      const mockRTM = vi.mocked(AgoraRTM.RTM)
+      const mockClient = mockRTM.mock.results[0].value
+      const storageHandler = mockClient.eventHandlers?.storage
+
+      if (storageHandler) {
+        // 模拟 UPDATE 事件
+        storageHandler({
+          channelName: 'test-channel',
+          eventType: 'UPDATE',
+          data: {
+            metadata: {
+              transcribe1: { value: '"en-US"' },
+              translate1List: { value: '["zh-CN","ja-JP"]' },
+              transcribe2: { value: '"zh-CN"' },
+              translate2List: { value: '["en-US"]' },
+              status: { value: '"start"' },
+              taskId: { value: '"test-task-id"' },
+            },
           },
-        },
-      })
-
-      expect(languagesChangedHandler).toHaveBeenCalledWith({
-        transcribe1: 'en-US',
-        translate1List: ['zh-CN', 'ja-JP'],
-        transcribe2: 'zh-CN',
-        translate2List: ['en-US'],
-      })
-
-      expect(sttDataChangedHandler).toHaveBeenCalledWith({
-        status: 'start',
-        taskId: 'test-task-id',
-      })
+        })
+
+        expect(languagesChangedHandler).toHaveBeenCalledWith({
+          transcribe1: 'en-US',
+          translate1List: ['zh-CN', 'ja-JP'],
+          transcribe2: 'zh-CN',
+          translate2List: ['en-US'],
+        })
+
+        expect(sttDataChangedHandler).toHaveBeenCalledWith({
+          status: 'start',
+          taskId: 'test-task-id',
+        })
+      }
     })
   })
 })