| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import { vi } from 'vitest'
- // 全局模拟配置 - 模拟真实的 Agora RTM SDK
- vi.mock('agora-rtm', () => {
- // 创建模拟的 RTM 客户端实例
- const mockClientInstance = {
- login: vi.fn().mockResolvedValue(undefined),
- logout: vi.fn().mockResolvedValue(undefined),
- subscribe: vi.fn().mockResolvedValue(undefined),
- unsubscribe: vi.fn().mockResolvedValue(undefined),
- addEventListener: vi.fn().mockImplementation(function (this: any, eventType, handler) {
- // 记录事件监听器调用
- this.eventHandlers = this.eventHandlers || {}
- this.eventHandlers[eventType] = handler
- }),
- removeEventListener: vi.fn(),
- presence: {
- setState: vi.fn().mockResolvedValue(undefined),
- whoNow: vi.fn().mockResolvedValue({ totalOccupancy: 1 }),
- },
- storage: {
- setChannelMetadata: vi.fn().mockResolvedValue(undefined),
- removeChannelMetadata: vi.fn().mockResolvedValue(undefined),
- },
- lock: {
- setLock: vi.fn().mockResolvedValue(undefined),
- acquireLock: vi.fn().mockResolvedValue(undefined),
- releaseLock: vi.fn().mockResolvedValue(undefined),
- getLock: vi.fn().mockResolvedValue({ lockDetails: [] }),
- },
- }
- const mockRTM = {
- RTM: vi.fn(() => mockClientInstance),
- }
- // 返回包含默认导出的对象
- return {
- default: mockRTM,
- ...mockRTM,
- }
- })
- // 模拟 fetch API
- const mockFetch = vi.fn()
- global.fetch = mockFetch
- // 设置默认的 fetch 响应
- mockFetch.mockImplementation((url: string) => {
- // 根据URL决定返回不同的响应
- if (url.includes('/tasks/') && url.includes('?builderToken=')) {
- // 查询转录API - 返回完整的结果
- return Promise.resolve({
- ok: true,
- status: 200,
- json: vi.fn().mockResolvedValue({
- status: 'completed',
- results: [
- {
- text: 'Hello world',
- startTime: 0,
- endTime: 1000,
- confidence: 0.95,
- },
- ],
- tokenName: 'test-token',
- taskId: 'test-task-id',
- }),
- })
- }
- // 默认响应
- return Promise.resolve({
- ok: true,
- status: 200,
- json: vi.fn().mockResolvedValue({
- tokenName: 'test-token',
- taskId: 'test-task-id',
- }),
- })
- })
- // 全局测试配置
- global.console = {
- ...console,
- debug: vi.fn(),
- log: vi.fn(),
- error: vi.fn(),
- warn: vi.fn(),
- }
|