Files
frontend-miniapp/tests/unit/AudioPlayInfoRepository.spec.ts
lyf 2edb6bd9f2
Some checks failed
CI / verify (push) Has been cancelled
适配H5讲解统一详情接口
2026-08-05 14:52:36 +08:00

82 lines
2.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { DefaultAudioPlayInfoRepository } from '@/repositories/AudioPlayInfoRepository'
const detailResponse = {
code: 0,
data: {
id: '865546647764037632',
version: 'standard',
name: '测试讲解点',
imageStatus: 'MISSING',
textVariants: [{ version: 'standard', languageCode: 'zh-CN', text: '测试正文' }],
audioTracks: [{
version: 'standard', languageCode: 'zh-CN', gender: 'female',
playUrl: '/museum-assets/audio/zh-female.mp3'
}],
hasText: true,
hasAudio: true
}
}
describe('DefaultAudioPlayInfoRepository unified stop detail', () => {
afterEach(() => vi.unstubAllGlobals())
it('requests the versioned endpoint and caches each detail version independently', async () => {
const requestUrls: string[] = []
vi.stubGlobal('uni', {
request: ({ url, success }: { url: string, success: (response: unknown) => void }) => {
requestUrls.push(url)
success({
statusCode: 200,
data: {
...detailResponse,
data: {
...detailResponse.data,
version: String(url).includes('version=extended') ? 'extended' : 'standard'
}
}
})
}
})
const repository = new DefaultAudioPlayInfoRepository()
const first = await repository.getStopDetail('865546647764037632')
const second = await repository.getStopDetail('865546647764037632')
const extended = await repository.getStopDetail('865546647764037632', { version: 'extended' })
expect(first.id).toBe('865546647764037632')
expect(second).toBe(first)
expect(extended).not.toBe(first)
expect(extended.version).toBe('extended')
expect(requestUrls).toEqual([
'/app-api/gis/guide/stops/865546647764037632?version=standard',
'/app-api/gis/guide/stops/865546647764037632?version=extended'
])
expect(first.audioTracks[0]?.channelCode).toBe('standard.zh-CN.female')
})
it('bypasses a completed entry only when refresh is explicitly requested', async () => {
const request = vi.fn(({ success }: { success: (response: unknown) => void }) => {
success({ statusCode: 200, data: detailResponse })
})
vi.stubGlobal('uni', { request })
const repository = new DefaultAudioPlayInfoRepository()
await repository.getStopDetail('stop-1')
await repository.getStopDetail('stop-1', { refresh: true })
expect(request).toHaveBeenCalledTimes(2)
})
it('surfaces a business missing-detail response as a detail error', async () => {
vi.stubGlobal('uni', {
request: ({ success }: { success: (response: unknown) => void }) => {
success({ statusCode: 200, data: { code: 1020005000, msg: '导览讲解点不存在' } })
}
})
const repository = new DefaultAudioPlayInfoRepository()
await expect(repository.getStopDetail('not-found')).rejects.toThrow('导览讲解点不存在')
})
})