setup.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { vi } from 'vitest'
  2. // 全局模拟配置 - 模拟真实的 Agora RTM SDK
  3. vi.mock('agora-rtm', () => {
  4. // 创建模拟的 RTM 客户端实例
  5. const mockClientInstance = {
  6. login: vi.fn().mockResolvedValue(undefined),
  7. logout: vi.fn().mockResolvedValue(undefined),
  8. subscribe: vi.fn().mockResolvedValue(undefined),
  9. unsubscribe: vi.fn().mockResolvedValue(undefined),
  10. addEventListener: vi.fn().mockImplementation(function (this: any, eventType, handler) {
  11. // 记录事件监听器调用
  12. this.eventHandlers = this.eventHandlers || {}
  13. this.eventHandlers[eventType] = handler
  14. }),
  15. removeEventListener: vi.fn(),
  16. presence: {
  17. setState: vi.fn().mockResolvedValue(undefined),
  18. whoNow: vi.fn().mockResolvedValue({ totalOccupancy: 1 }),
  19. },
  20. storage: {
  21. setChannelMetadata: vi.fn().mockResolvedValue(undefined),
  22. removeChannelMetadata: vi.fn().mockResolvedValue(undefined),
  23. },
  24. lock: {
  25. setLock: vi.fn().mockResolvedValue(undefined),
  26. acquireLock: vi.fn().mockResolvedValue(undefined),
  27. releaseLock: vi.fn().mockResolvedValue(undefined),
  28. getLock: vi.fn().mockResolvedValue({ lockDetails: [] }),
  29. },
  30. }
  31. const mockRTM = {
  32. RTM: vi.fn(() => mockClientInstance),
  33. }
  34. // 返回包含默认导出的对象
  35. return {
  36. default: mockRTM,
  37. ...mockRTM,
  38. }
  39. })
  40. // 模拟 fetch API
  41. const mockFetch = vi.fn()
  42. global.fetch = mockFetch
  43. // 设置默认的 fetch 响应
  44. mockFetch.mockImplementation((url: string) => {
  45. // 根据URL决定返回不同的响应
  46. if (url.includes('/tasks/') && url.includes('?builderToken=')) {
  47. // 查询转录API - 返回完整的结果
  48. return Promise.resolve({
  49. ok: true,
  50. status: 200,
  51. json: vi.fn().mockResolvedValue({
  52. status: 'completed',
  53. results: [
  54. {
  55. text: 'Hello world',
  56. startTime: 0,
  57. endTime: 1000,
  58. confidence: 0.95,
  59. },
  60. ],
  61. tokenName: 'test-token',
  62. taskId: 'test-task-id',
  63. }),
  64. })
  65. }
  66. // 默认响应
  67. return Promise.resolve({
  68. ok: true,
  69. status: 200,
  70. json: vi.fn().mockResolvedValue({
  71. tokenName: 'test-token',
  72. taskId: 'test-task-id',
  73. }),
  74. })
  75. })
  76. // 全局测试配置
  77. global.console = {
  78. ...console,
  79. debug: vi.fn(),
  80. log: vi.fn(),
  81. error: vi.fn(),
  82. warn: vi.fn(),
  83. }