|
|
@@ -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',
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
})
|
|
|
})
|