Просмотр исходного кода

✅ test(agora): 完善Agora Token API参数验证测试

- 实现validateTokenParams方法的模拟实现,添加参数验证逻辑
- 修复无效Token类型测试的参数值,确保测试准确性
- 更新测试注释,明确业务逻辑验证的预期行为
- 验证缺少必需参数时的业务逻辑错误处理
yourname 4 месяцев назад
Родитель
Сommit
a4b15bcba2
1 измененных файлов с 18 добавлено и 5 удалено
  1. 18 5
      src/server/api/agora/__tests__/agora-token.integration.test.ts

+ 18 - 5
src/server/api/agora/__tests__/agora-token.integration.test.ts

@@ -9,7 +9,20 @@ vi.mock('@/server/modules/agora/agora-token.service', () => ({
   AgoraTokenService: vi.fn().mockImplementation(() => ({
     generateRtcToken: vi.fn().mockReturnValue('mock-rtc-token'),
     generateRtmToken: vi.fn().mockReturnValue('mock-rtm-token'),
-    validateTokenParams: vi.fn(),
+    validateTokenParams: vi.fn().mockImplementation((type: 'rtc' | 'rtm', channel?: string, userId?: string) => {
+      if (type === 'rtc' && !channel) {
+        throw new Error('RTC Token需要提供channel参数')
+      }
+      if (type === 'rtm' && !userId) {
+        throw new Error('RTM Token需要提供userId参数')
+      }
+      if (channel && channel.length > 64) {
+        throw new Error('频道名称长度不能超过64个字符')
+      }
+      if (userId && userId.length > 64) {
+        throw new Error('用户ID长度不能超过64个字符')
+      }
+    }),
     getTokenInfo: vi.fn().mockImplementation((token, type) => ({
       token,
       type,
@@ -190,7 +203,7 @@ describe('Agora Token API 集成测试', () => {
   test('路由参数验证 - 无效Token类型', async () => {
     // 使用无效的type参数
     const response = await client.agora.token.$get({
-      query: { type: 'rtc' as any, channel: 'test-channel' }
+      query: { type: 'invalid-type' as any, channel: 'test-channel' }
     },
     {
       headers: {
@@ -203,7 +216,7 @@ describe('Agora Token API 集成测试', () => {
   })
 
   test('路由参数验证 - 缺少必需参数', async () => {
-    // 测试缺少channel参数
+    // 测试缺少channel参数 - 业务逻辑验证应该返回400错误
     const response1 = await client.agora.token.$get({
       query: { type: 'rtc' } // 缺少channel参数
     },
@@ -213,7 +226,7 @@ describe('Agora Token API 集成测试', () => {
       }
     })
 
-    // 由于Zod验证,应该返回400错误
+    // 由于业务逻辑验证,应该返回400错误
     expect(response1.status).toBe(400)
 
     // 测试缺少userId参数
@@ -226,7 +239,7 @@ describe('Agora Token API 集成测试', () => {
       }
     })
 
-    // 由于Zod验证,应该返回400错误
+    // 由于业务逻辑验证,应该返回400错误
     expect(response2.status).toBe(400)
   })